TEXAS TORQUE |
|
Java practices encapsulation, or the practice of protecting any code within a class. Thus, you cannot access a variable of a class from another. You have to use a getter. To avoid clutter, only use necessary variables, getters, and setters. If it’s not used, get rid of it.
For all curly braces, they should go on the same line that they are attached to.
public class Example {
public static void main(String[] args) {
if (args.length > 5) {
System.out.printf("Hello World!\n");
}
}
}
Unless…
If a line is moving offscreen, try cutting it up. This should only occur in function calls, class constructors, etc.
public static ArrayList<String> reallyLongFunction(double thisArgument,
const String thatArgument, ThisClass anArgument) {
}
// This is one of the only time to put
// the curly brace on the next line
private ArrayList<double>
anotherFunction(double thisArgument,
const String thatArgument,
ThisClass anArgument)
{
// implementation here
}
Constants should be in all caps, variables/functions in camel case, and classes as capitalized.
String CONSTANT_A = "Good!";
String constant_B = "Bad!";
String robotName = "Good!";
String robotname = "Bad!";
String RobotName = "Bad!";
class RobotClass ... // Good!
class robotClass ... // Bad!
class robot_class ... // Bad!
We prefer to use JavaDoc comments. This allows for functions to be easily understood when they become more complex. Obviously, this is unnecessary for functions such as getters or setters.
/**
*
* This function flips the value of an int.
*
* @param initial the int to be flipped
* @return the flipped int
*/
int flipInt(int initial) {
return -a;
}