Programming Style and Naming Conventions

If you use a good naming convention in your source code then this can increase the readability tremendously.
In this tutorial we look at programming style and naming conventions which will increase the readability of the source code.

General Naming Conventions

To be consistent you should always try to use the same set of naming conventions throughout the code.
Naming conventions are usually things such as:

  • Capitalization of variables
  • Class or function name.
  • Prefixes.
  • Indication that a class field is private.
  • Etc.

Some common naming conventions for functions, classes or objects are c-style naming and c-style naming with underscores. An example of c-style naming of a function name can be “AutoEject.” You see, each single word starts with a capital. An example of c-style naming with underscores is “Auto_Eject.”

Prefix

Another way to make it easier to read is by using prefixes for certain data types. These prefixes helps you to understand what data type a certain variable has. For instance if you have a pointer “A” you can add the prefix “ptr_” (of pointer) so you get “ptr_A”. Now whenever you see a variable beginning with a prefix ptr_ you know that it is a pointer.

Another place where you can use a prefix is for global or static variables. The prefix then will help you to make a distinct difference between a local, global or static variable. Another plus is that it helps to prevent naming collisions between a local and a global variable.

Another common convention is using a prefix on the private fields and methods of a class. The prefix that is normally used is an underscore. For example _private_variable.

Abbreviations

Abbreviations are very useful and can help to speed up the reading of the source code tremendously. Some common abbreviations that are frequently used are: “ptr” for pointer or “itr” for iterator. Also names like i, j and k are usually used as counter variables.

(Personally we don’t use i for counters, because in some fonts it looks like a 1.)

But be careful where you use abbreviations, because sometimes it can also have a negative effect on the readability.

In Conclusion

As you have seen, naming convention, prefixes and abbreviations can help to make your source more readable.
But a little warning; it’s a fine line between better or worse to read, so use them with care and use them consistently throughout the entire code of the project.

This entry was posted in General Programming. You can follow any responses to this entry through the RSS 2.0 feed. Both comments and pings are currently closed. Tweet This! Tweet This! or use to share this post with others.

There are currently 2 responses to “Programming Style and Naming Conventions”

Why not let us know what you think by adding your own comment!

  1. LayZee on November 10th, 2012:

    “… sometimes it can also have a negative effect on the usability.”

    I think you mean readability, right? 🙂

  2. admin on November 10th, 2012:

    @layzee: you are right, changed it in the text.