Coding Standards & Style Rules for Java & C# 2

Coding Style

Hi there,

Today, I want to share with you some of the coding standards, also known as style rules. It’s important to have good coding habits because your code is read multiple times by many people, including you, and everyone should be able to follow it without any problems or need to improve it.

Some software engineers say that writing good consistent code is an art and code that is written without the coding standards is as bad as code with bugs.

Coding Style can be easily implemented, even by people who just started programming because it doesn’t require any extraordinary programming skills. However, it makes the program look professional and easier to read.

Before I start, I just want to mention that each language has its own unique style, and you should follow the appropriate one.

In this article, my main focus is on Java and C#.  If you program in Python, please see this site.

 

Naming Convention

There are two main types of casing:

  • Pascal Casing – each word starts with a capital letter, for example, HelloWord
  • Camel Casing – the first word is lowercase and the rest begins with a capital letter, for example, helloWord

For class names

  • C#: use Pascal Casing, for example, PrimaryNumbers, Secretary,
  • Java: use Pascal Casing, for example, PrimaryNumbers, Secretary,

For function names

  •   C#: Pascal Casing, for example, GetIncome(), SetYear()
  •   Java: Camel Casing, for instance, getIncome(), setYear()

For local variable and parameters names

  • C#: use Camel Casing, such as employeeAge
  • Java: use Camel Casing, such as employeeAge

For constant variables

  • C#:  use Pascal Casing, such as YearlyBonus2017
  • Java: use upper case with underscores, such as YEARLY_BONUS_2017

Use descriptive names

Your code should be self-explanatory. This means that you shouldn’t add additional comments when declaring variables – the name of the variable should be descriptive.

The following example shows how you shouldn’t name your variables.

This method is not the best option because later in the code you will be only using bonus1 and bonus2, and those names don’t say that they hold data for the secretary yearly or monthly bonus.  

Instead, do this:

Comments

Your code should be easily understood without comments, so use descriptive names for variables, functions, etc. Add comments for blocks of code where something is not clear and obvious. Also, you should comment what your functions or classes do.

Spaces

Use spaces to separate arguments in a function, for example:

Also, remember not to add too many white spaces. The following example shows how not to do it.

Don’t use spaces when accessing an array element:

The recommended style is:

Use one space around binary operators and assignments

Instead of

 

Final Thought

Just to sum up, there is some flexibility in formatting code, but you should follow your organization standards, for example, if everyone uses Camel Casing you shouldn’t use Pascal Casing or other notations because this creates chaos in the documentation and makes the code analysis harder. Consistency is the key.

And of course, pay attention when declaring variables or writing method names, so you won’t have any typos in the code.

 

I hope you found this useful.  

What else would you add to the list?