-
Notifications
You must be signed in to change notification settings - Fork 515
AuthMe Coding Styleguide
- Use indentation of four spaces (not tabs)
- Always wrap structures like
if
,while
with braces (neverif (isTrue) doThis();
) - Fields should not be public – use getters and setters where appropriate
- Max line width: 120 characters
As usual in Java:
- Class names should be uppercase and in CamelCase
- Names of methods, fields and variables in lowercase and camelCase
Do not "swallow" exceptions unless an exception is part of an expected scenario that is handled otherwise. Elsewhere you can log the exception with the ConsoleLogger
easily:
try {
Files.write(configFile, settings);
} catch (IOException e) {
ConsoleLogger.logException("Could not write to config file:", e);
}
This will output something like: [WARN] Could not write to config file: [IOException] File is read-only
to the console and the log file. Logging exceptions tremendously helps debugging and helps detecting problems as they occur in the future.
Try to create "bite-sized" elements: a method should do exactly one thing, and a class should solve one main problem. If too many things are done in one method too much brain power gets lost on understanding at what point you are in the method, and you lose a higher level overview that would allow you to find improvements or flaws in the logic.
Program for an audience – let others benefit from the effort you've spent in analyzing a problem by writing comments and JavaDoc for code that is not obvious:
// Schedule the task to run later so it doesn't interfere with the inventory process
scheduler.scheduleSyncTaskLater(new LoginTask());
Similarly, create issues in the issue tracker for larger problems or improvements instead of just writing a TODO comment. TODO comments are guaranteed to get forgotten if they are not actively tended to.
Use the third person ("Gets the player's name") or the imperative mood ("Get the player's name"); it should be consistent within a class. JavaDoc should briefly explain what the method does and provide additional information to the developer.
Put an empty line between the description and the first @param
.
Example:
/**
* Adds a permission node required to execute this command.
*
* @param permissionNode the permission node to add
* @return true on success, false on failure
*/
public boolean addPermissionNode(String permissionNode)