PROGRAMMING PRACTICES10 - Programming Practices10.1 Providing Access to Instance and Class VariablesDon't make any instance or class variable public without good reason. Often, instance variables don't need to be explicitly set or gotten-often that happens as a side effect of method calls.
One example of appropriate public instance variables is the case where the class is essentially a data structure,
with no behavior. In other words, if you would have used a See Class variable access methods for additional information. 10.2 Referring to Class Variables and MethodsAvoid using an object to access a class (static) variable or method. Use a class name instead. For example: classMethod(); //OK AClass.classMethod(); //OK an_object.classMethod(); //AVOID! 10.3 Constants
Numerical constants (literals) should not be coded directly, except for -1, 0, and 1, which can appear in a 10.4 Variable AssignmentsAvoid assigning several variables to the same value in a single statement. It is hard to read. Example: foo_bar.f_char = bar_foo.lchar = 'c'; // AVOID! Do not use the assignment operator in a place where it can be easily confused with the equality operator. Example: if (c++ = d++) // AVOID! (Java disallows) { ... } Do not use embedded assignments in an attempt to improve run-time performance. This is the job of the compiler. Example: d = (a = b + c) + r; // AVOID! a = b+c; d = a+r; 10.5 Miscellaneous Practices10.5.1 ParenthesesIt is generally a good idea to use parentheses liberally in expressions involving mixed operators to avoid operator precedence problems. Even if the operator precedence seems clear to you, it might not be to others-you shouldn't assume that other programmers know precedence as well as you do. 10.5.2 Returning ValuesTry to make the structure of your program match the intent. Example: if (boolean_expression) { return true; } else { return false; } return boolean_expression; if (condition) { return x; } return y; if (condition) { return x; } else { return y; } 10.5.3 Expressions before `?' in the Conditional Operator
If an expression containing a binary operator appears before the (x>=0) ? x : -x; 10.5.4 Special Comments
Use
|