Java Programming Style Guidelines

post
By kioko on February 17th In Programming
java-oracle-juuchini

I have been programming  in Java for a while now. I have learnt a few tips along the way from reading peoples code and of course the internet. Join me and i will share what i have learnt.

I have had a “discussion” with a couple of Developers on using Naming Conventions.  A friend of mine told me, “As long as my code runs the rest does not matter”.

Different programmers can have different styles and approaches to the way they program. The whole idea of using Naming Conventions is to make your code more readable to you and other programmers. Whether you are working in a team or not, Code Readability is important because you will spend less time trying to figure what the code does, leaving more time to debug or modify it. Team members are more likely to share and re-use code when the style and commenting conventions among team members are the same.

A common naming convention in programming is CamelCase. This is a naming convention which a name is formed of multiple words that are joined together as a single word with the first letter of each of the multiple words capitalised so that each word that makes up the name can easily be read. In programming the first letter is a lowercase. e.g emailAddress, homeAddress.

General guidelines to apply when choosing Identifier Names in your programs:

  • Avoid using Abbreviations. Use names like firstName, emailAddress, middleInitial rather than fName, mInit.
  • Avoid names that only differ in case. e.g  person and Person.

By using meaningful names, you go a long ways towards writing self-documenting code. That is, code that is understandable on its own without requiring accompanying comments. Example.

 

Variable Naming Conventions

Use meaningful names that describe what the variable is being used for. Avoid generic names

Constant Naming Conventions

When naming constants, use ALL_UPPER_CASE for your named constants, separating words with the underscore character. For example,

Method Naming Conventions

Methods names should be in mixed case. Use verbs to describe what the method does.

Last but not least, always remember to use Comments in your code. Comments provide readers with the information helpful to understanding your program.

There are three basic types of comments:

  • File header comments: Provide identification information about the program and author.
  • Single-line comments: Provide overviews or summaries of chunks of code.
  • Trailing comments: Provide information for one line of code.

 

Remember, you should write your code with the aim of making it understandable to others. Others will need to read and understand your code and one of the major keys to understanding is through the use of meaningful identifier names.

Now go and make your code Readable if.

Leave a reply

Recent Posts

Recent Comments

Archives

Categories