Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: (Spelling) "Manual Run" from "Manual Runinnn" under Checkstyle


Info

OpenMRS tries to follow a common coding style in order to make our code base robust: more readable and more reliable (if the same code is created by three different developers, it should look and read very similar). Our style conventions are designed to promote consistency and good coding practices, while reducing common errors. If you feel that our coding styles could be improved (by changing, adding, or removing something), please bring your comments to the Developers Mailing List.

...

We use checkstyle for static code analysis. checkstyle defines rules http://checkstyle.sourceforge.net/checks.html which when violated in the code will result in errors or warnings. The checkstyle rules we want our code to adhere to are defined in the openmrs-core repostitory's checkstyle.xml

Manual

...

Run

You can run checkstyle locally using the maven checkstyle plugin with the following command

...

PMD is used to check for issues like unused, unnecessary or error prone code. The PMD rules we want our code to adhere to are defined in the openmrs-core repostitory's ruleset.xml

Manual Run

Install PMD locally and try

...

FindBugs is another tool we use to check the code for issues. The FindBugs rules we want our code to adhere to are defined in the openmrs-core repository's findbugs-include.xml

Manual Run

At the moment you cannot check the code against the FindBugs rules locally using maven as with checkstyle. We are working on it (smile)

...

For concatenation when logging, see How To Log.

Use of StringUtils

  • Do not do this: "if (s == null || s.equals("")) ...". Instead do "if (StringUtils.isNotEmpty(s) ..."
  • We have both the apache commons StringUtils and springframework StringUtils library. If possible, use the apache commons StringUtils method. However, if the spring StringUtils is already imported, use that one.

...

  • Use String Builder (or StringBuffer)  rather than the  + Operator when concatenating String                                                                                                                                                                                               use this

    Code Block
    languagehtml/xml
    StringBuilder x = new StringBuilder("a");
    x.append("b");

                 instead of     

    Code Block
    languagehtml/xml
    String s = "a"; 
     s = s.concat("b."); 

                                                                                                                                                                                                                                                                                                                                                           This saves memory space by avoiding creating new unnecesary objects hence reducing on extra pressure put on the GC.   see above concatenating Strings 

  • Avoid BigInteger and BigDecimal                                                                                                                                                                                                                                       BigInteger and BigDecimal require much more memory than a simple long or double and slow down all calculations dramatically. 

  • Use primitives where possible Rather than Wrapper classes                                                                                                                                                                                                                            Another quick and easy way to avoid any overhead and improve the performance of your application is to use primitive types instead of their wrapper classes. So, it’s better to use an int  instead of an Integer, or a double instead of a Double. That allows your JVM to store the value in the stack instead of the heap to reduce memory consumption and overall handle it more efficiently.    

                                                             That allows your JVM to store the value in the stack instead of the heap to reduce memory consumption and overall handle it more efficiently.
  • Make use of Use Apache Commons String Uitils for String operations as much as possible                                                                                                                  This library contains more efficient string operations functionalities which may greatly optimize Application Performance

  • Cache expensive resources                                                                                                                                                                                                                                Caching is a popular solution to avoid the repeated execution of expensive or frequently used code snippets.   The general idea is simple: Reusing such resources is cheaper than creating a new one over and over again. A typical example is caching database connections in a pool. The creation of a new connection takes time, which you can avoid if you reuse an existing connection.  see Caching in openmrs

Openmrs API Level

  • Avoid Fetching all resources in a single method call eg avoid calls like 

    Code Block
    languagehtml/xml
    getAllPatients();

     If a database has more than 10k patients, this will probably cause an Out of Memory Error

...