12 Rules To Follow To Create Quality Code

Author of Code Quality: The Open Source Perspective, Diomidis Spinellis, has come up with 15 most important points which you need to remember for writing codes which are sparkling. If you follow them then you will be able to write professional codes which have longer shelf life. These codes will be loved by your colleagues too. You can write poor codes in a lot of ways. But if you\’re serious about developing quality programs, then you just need to follow 12 simple rules. You may not become a master programmer but you can improve your quality with these rules, for sure.

Rule 1: Follow the Style Guide:
Every programming language has a style guide to teach you where to put spaces, how to name stuff, how to comment and all the do\’s and dont\’s. You need to read the guide carefully, learn the basics, apply the rules and your programs are destined to get better very soon.

Sometimes style guides are customised to meet specific requirements. These guides are well documented and these guides also have editor settings to help you apply a programming style. You can also custom tools adhering to any particular style.

Rule 2: Create Descriptive Names:
In the past, programmers used to contract the names of their variables and routines in order to save time. But in modern day coding this practice is no more entertained. You have to use long descriptive names to help yourself as well as your colleagues to understand what the code does. More importantly, think long and hard before you name something. There are some easy naming rules. Class and type names should be nouns while methods names should contain a verb.

Rule 3: Comment and Document:
You have to start every routine you write (function or method) with a comment outlining what the routine does. It includes parameters as well as possible errors and exceptions. The role of each file and class should be summarised in a comment and also the contents of each class field along with the major steps of complex code. You can write the comments as you develop the code. Ensure that your code as a whole comes with a guide explaining what it does.

Rule 4: Don\’t Repeat Yourself:
Codes should never copy-pasted. The common parts should be abstracted into a routine or class and then use it with appropriate parameters. Avoid duplicate instances of similar data or code and keep a definitive version in one place. You should involve yourself in creation of API reference guides from comments, automatic detection of unit tests through an annotation or a naming convention, generation of both PDF and HTML documentation from a single markup source and derivation of object classes.

Rule 5: Check for Errors and Respond to Them:
The routines you create can come up with an error indication. Face it. A disk can fill up any time, your configuration file cannot be always there and your application will not always run with the required permissions. Good error-handling is a tough job but it makes the code longer and less readable. But if you ignore errors, then it will have a negative impact on your coding practices.

Rule 6: Split Your Code into Short, Focused Units:
A reasonably-sized screen window should get compatible with every method, function or logical code block. An exception can be made for simple repetitive code sequences. Within a routine, you can divide long code sequences into blocks as per their functionalities. Moreover, each class, module, file, or process should concern one single thing. You need to split it accordingly, as per your requirements.

Rule 7: Use Framework APIs and Third-Party Libraries:
You should know the basic functionalities of an API in your programming framework including what\’s commonly available through mature, widely adopted third-party libraries. Your system\’s package manager supports some libraries which are often good to use. Use that code only.

Rule 8: Don\’t Overdesign:
You should keep your design updated with your current needs. You don\’t need to create parameterized classes, factory methods and arcane interfaces to solve problems which are still not there. On the other hand, when the code\’s structure looks inappropriate for your needs, you need to redesign in a more appropriate way.

Rule 9: Be Consistent:
Consistency is very essential in quality coding which means you need to do similar things in similar way. If you\’re developing a routine which has same functions with that of an existing routine, you should use a similar name, the same parameter order, and a comparable structure for the code body. The same rule applies to classes too. Your code should adopt the conventions of the framework in which you\’re programming.

Rule 10: Avoid Security Pitfalls:
It\’s risky if your code becomes the target of malicious attacks. These attacks not generate from the Internet only; the attack vector could be data fed into your application too. Depending on your programming language and application domain, you should be concerned about buffer overflows, cross-site scripting, SQL injection, and similar issues. Learn about the frequent problems to avoid them in your code. It\’s not that myuch difficult.

Rule 11: Use Efficient Data Structures and Algorithms:
Simple code can be maintained better than an equivalent code. You can combine maintainability with efficiency by utilising the data structures and algorithms as provided by your programming framework. You can use maps, sets, vectors and the algorithms to make your code clearer, more scalable, faster and memory-frugal.

Rule 12: Put Everything Under Version Control:
Your system\’s code, documentation, tool sources, build scripts, test data – everything should be under version control. Git and GitHub make this task cheap and hassle-free while other powerful tools and services are also available. You should be able to build and test your program on a properly configured system.

If you make these 12 rules part of your everyday programming practices, you\’ll be able to create code which is easier to read and can be run correctly. To gain further knowledge about best practices in coding read the book Code Quality: The Open Source Perspective.

Reposted from http://www.efytimes.com/e1/fullnews.asp?edid=148009

Scroll to Top
Scroll to Top