Friday 18, Apr 2025
We are moved to new domain
Click -> www.ehowtonow.com
Saturday, 14 September 2013

Java Best Practice – Programming Practices


Providing Access to Instance and Class Variables
Don’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 struct instead of a
class (if Java supported struct), then it’s appropriate to make the class’s instance variables public.

Referring to Class Variables and Methods
Avoid using an object to access a class (static) variable or method. Use a class name instead.
For example:
classMethod(); //OK
AClass.classMethod(); //OK

anObject.classMethod(); //AVOID!

Constants
Numerical constants (literals) should not be coded directly, except for -1, 0, and 1, which can
appear in a for loop as counter values.

Variable Assignments 
Avoid assigning several variables to the same value in a single statement. It is hard to read.
Example:
fooBar.fChar = barFoo.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)
...
}
should be written as
if ((c++ = d++) != 0) {
...
}
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!
should be written as
a = b + c;
d = a + r;
Miscellaneous Practices

1. Parentheses
It 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.
if (a == b && c == d) // AVOID!
if ((a == b) && (c == d)) // USE

2. Returning Values Try to make the structure of your program match the intent. Example:
if (booleanExpression) {
return true;
} else {
return false;
}
should instead be written as
return booleanExpression;
Similarly,
if (condition) {
return x;
}
return y;
should be written as
return (condition ? x : y);
3. Expressions before ‘?’ in the Conditional Operator
If an expression containing a binary operator appears before the ? in the ternary ?: operator, it should be parenthesized. Example:
(x >= 0) ? x : -x; 

4. Special Comments
Use XXX in a comment to flag something that is bogus but works. Use FIXME to flag something that is bogus and broken.

Shop and help us

Flipkart Offer Snapdeal offer Amazon.in offer Amazon.com offer

Related Posts:

  • Java Best Practice - DeclarationsNumber Per Lineint level; // indentation levelint size; // size of tableis prepared overint level, size;Do not put different type on the same line. Example:int foo, fooarray[]; //WRONGNote: The example above use one space be… Read More
  • Java Best Practice – White SpacesBlank LinesBlank lines improved readability by setting off sections of code that are locally related.Two blank lines should always be used in the following circumstances:Between sections of source file Between class and inter… Read More
  • Java Best Practice - StatementsSimple StatementsEach line should contains one statement.Examplearg1++; //CORRECTarg2++; //CORRECTarg1++;arg2- -; //AVOIDCompound StatementsCompound statemen… Read More
  • Java Best Practice – Naming ConventionsNaming conventions make programs more understandable by making them easier to read. They can also give information about the function of the identifier—for example, whether it’s a constant, package, or class—which can … Read More
  • Java Best Practice - File OrganizationThe Java file consist of section that should be separated by blank lines and optional comments identifying each sections. Java Source file longer than 2000 lines are cumbersome and should be avoided. Java program must be prop… Read More
  • Blogger Comments
  • Facebook Comments
  • Disqus Comments

0 comments:

Post a Comment

Item Reviewed: Java Best Practice – Programming Practices Rating: 5 Reviewed By: eHowToNow