Java Best Practices for Layout, Comments and Naming Conventions

java best practicesWhy you should care about best practices

Best practices in coding are similar to grammar and spelling in language. You can use bad grammar in a sentence but bad grammar or spelling mistakes mean that your reader may not understand what you are saying. Bad grammar also affects how easily the document is read. Best practices in programming are the guidelines that are generally recommended so that the code programmers write conforms to certain industry standards. These practices are created and expanded as programmers have gained experience with the language, and although these rules and standards are “informal”, most programmers realize that they are essential for ensuring code that is high quality, practical and efficient.

Code is read far more often than you would think and writing clear, correct code using the style guidelines considered part of the best practices for a programming language, will make your code more readable, and therefore more accessible, to other programmers like your teammates or successors.

Here are some of the top java best practices for coding:

1.       Be consistent

2.       Apply the DRY principle

3.       Ensure effective code Layout

4.       Comment, comment, comment.

5.       Follow good naming conventions

To learn to program in Java online using step by step Java tutorials, why not sign up for the Java Programming for Beginners course from udemy today?

1. Be consistent

Whether you are a lone programmer or part of a programming team, it’s good to remember that your code will be read by someone in the future. If you are working on existing code, then its good practice to take note of the style used in the existing code. Try to apply and stick to the conventions of the existing code, unless those conventions are completely wrong in terms of general good practice.

If you write code and write it using consistent styles and use the style of existing code, the code will be easier to read, and thus easier to work with and understand. Ensuring consistency of style also improves the predictability of code and saves time when other programmers need to access, maintain or expand the code. Style guidelines ensure that others can focus on the code’s functions and performance rather than how you have written it – in other words programmers can focus on what you are trying to say rather than how you are trying to say it. For a definitive guide to programming in Java, why not sign up for the core Java Fundamentals I and II course from udemy today.

2. Apply the “DRY” principle

The whole point of programming is to reduce repetitive tasks to small snippets of code that can handle the repetition quickly, easily and efficiently. The DRY principle is an acronym for “Don’t Repeat Yourself”. Do not use the same snippets of code repetitively within your applications. Create methods, objects or classes to avoid continuous repetition.

3. Ensure effective code layout

When you are creating code blocks, always use the tab key to indent code blocks. Proper indentation makes the code simpler to follow and more logical to read. Do not use the spacebar to create the same indentation. Try to use one tab for each new indentation level. Avoid using white spaces after your code and avoid creating blank lines within the code. Try to ensure that you do not exceed common line lengths. Line lengths should not exceed 160 characters per line and there should be no need to wrap lines. Write short, effective, efficient statements. Split your expressions and use local variables in your code and never join lines with multiple statements. When working with complex arrays, list each new array element on a new line, end each element with a comma and use a double indent for each new element to ensure ease of readability and manipulation. Avoid deep nesting within your code blocks to ensure that your code is easy to read and follow. You should use a blank line between the logical sections of your code to improve readability and logical progression of the code. You should also include a blank line between the declaration of a local variable and the first statement in your code. Group, or keep related, lines of code together in order to create logical “paragraphs” within your code to improve the way it reads logically.

4. Comment, comment, comment

Comments should be written in clear, concise English. They should be written keeping other programmers in mind. Comments should explain the code but not necessarily the programming language itself. Write comments that make the intent of the code clear as opposed to comments that explain the way the code functions. Good comments should explain the code and its intent, not repeat the code itself.

Use comments to explain the context of the code and how it fits into the application. They should not try to teach a programmer to program but rather “discuss” or elucidate the reason for the code. Always keep comments up to date when you are changing or updating the code. Comments that contradict a set of code are worse than no comments at all, so ensure that comments make sense and are kept up to date. Comments should be written in proper English. Spelling mistakes, incorrect grammar and sloppy language are an indication of sloppy programming and coding and the normal rules of English grammar apply. Take your Java programming skills to the next level with the Advanced Java Programming course today.

5.  Follow good naming conventions

The first rule of naming conventions is to be consistent throughout the code. When naming your variables and code elements, use names that are sensible and logical. Avoid using single characters unless they specify an index variable. When naming class variables and methods, you should choose names that make their content and functionality as clear as possible.  The names should illuminate why the method or class is there, what it does and how it is going to be used. You should avoid names that are ambiguous or names that have special meaning to you but may not be clear to others. Distinguish names using lowerCamelCase notation like myVariableList. Conclusion At the end of the day, best practices were developed over a long period of time because they made code easier for programmers to read and understand. A lot of the naming conventions, layout standards, and comment requirements are common sense. Programmers should write their code with other programmers in mind and ensure that their code would make sense to others. To learn Javascript best practices best practices using Key web development approach, why not sign up for the Modern Javascript Develop and Design course from udemy now?