0% found this document useful (0 votes)
12 views2 pages

Best Practice Java

The document outlines 30 best practices for Java programming aimed at improving code quality and maintainability. Key recommendations include validating parameters early, using enums instead of strings, and preferring composition over inheritance. Additionally, it emphasizes the importance of clear naming conventions, avoiding null pointer exceptions, and writing tests alongside code development.

Uploaded by

Oussama Ouaddi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views2 pages

Best Practice Java

The document outlines 30 best practices for Java programming aimed at improving code quality and maintainability. Key recommendations include validating parameters early, using enums instead of strings, and preferring composition over inheritance. Additionally, it emphasizes the importance of clear naming conventions, avoiding null pointer exceptions, and writing tests alongside code development.

Uploaded by

Oussama Ouaddi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

https://www.linkedin.

com/pulse/30-best-practices-java-programming-robert-mitchell/

/30-best-practices-java-programming

1. Validate Parameters Passed Into A Method as Soon as Possible:

=>If a parameter is passed into a method then perform the necessary checks on
it as soon as possible. The earlier you check it, the less likely errors
and exceptions are to occur. This includes checking if objects are null,
empty, false, the correct type, etc .

2.Surround Operators With Spaces:

3. Avoid Using the Word �return� in a Void Method When Possible:

4. Program to an Interface, Not an Implementation:


=>promotes loose-coupling
=>It means that your code doesn�t care which specific class you use, just that it
implements certain behavior.

5. Prefer Enums to Strings When Dealing With a Known Number of Possible Values:

6. Utility Classes Should Be Composed of Static Methods and a Private Constructor:

=>An example of a utility method would be a method that accepts two integers and
returns the larger integer of the two

7. Prefer Composition Over Inheritance:


=>loose coupling,
=>makes the code more modular. �HAS-A� is better than �IS-A�.
=>Benefit of using composition in java is that we can control the visibility of
other object to client classes and reuse only what we need.
=>Also if there is any change in the other class implementation, for example
getSalary returning String, we need to change Person class
to accommodate it but client classes doesn�t need to change

8.Avoid Static Import Statements

9. Avoid Autoboxing / Unboxing When Possible:

=Excessive usage can hurt performance as objects are converted from a primitive
type
to the corresponding wrapper type or vice versa. This can also lead to Null
Pointer Exceptions if you aren�t careful
=>L'autoboxing permet de transformer automatiquement une variable de type
primitif en un objet du type du wrapper correspondant

10. When You Need to Do Lots of String Concatenation, Prefer StringBuilder to


String or StringBuffer

11. Use Curly Braces on Single-Line If or Else Statements:

13. Avoid Writing Methods Longer Than One Screen Long:

14. Values Used Commonly Throughout the Code Base Should Be Defined In a Constants
File:

15. Avoid Null Pointer Exceptions When Using .equals():


=>Notice below in the call to .equals(), if null was passed in for input and it
had instead
been input.equals(CONSTANT), then a NullPointerException would have been thrown.
You can�t call a method on a null object but it�s perfectly fine
for null to be passed into the .equals() method. In this case, the output would
simply be false and no exceptions would be thrown

16. Use Method Names and Field Names That Are Descriptive:

17. Use In-Line Comments Sparingly and Javadoc Comments Always:

18. When Comparing Strings, Use .equals() instead of the == operator:

=>Using .equals() will check to see if they contain the same value. Using == will
check to see if they are the same object.

19. When Doing a Null Comparison, Put the Word �null� on the Right Side of the ==
Operator:

20. Use Blank Lines to Separate Code Into Logical and Readable Blocks:

21.Be Careful When Creating a Method Signature:

=>Try to pass in as few parameters as possible into a method. Try to avoid a


method signature where several
of the parameters are the same type and appear one after the other like this

22. When Possible, Reuse Existing Classes Rather Than Reinventing a Solution:

23. Prefer For-Each Loops to Traditional For Loops:

24. Remove Unused Import Statements From Your Classes:

25. In A POJO, Use Private Fields With Setters & Getters Rather Than Allowing
Direct Access to Fields:

26. Think About JUnit Tests While You Are Writing Your Code:

27. Use the Ternary Operator Sensibly:

28. Avoid Creating Objects Inside a Loop:

29. When Creating an Interface, Be Careful Not to Include More Abstract Methods
Than Necessary:

30. When You Refactor Code, Communicate the Changes to Other Potential Users of
That Code:

----------
code smells ou mauvaises odeurs sont des mauvaises pratiques de conception
logicielle qui conduisent � l�apparition de d�fauts.

You might also like