Using clean code principles for better code

Variables

Always define variables just before using them in the program code. This approach offers several advantages:

  • The program code and the matching variable definition fit on one screen page and can be read together.
  • The variables' scope is clearly defined. Writing all of the variables at the beginning of a program is by far one of the worst ideas, because it would make all the variables global.
  • Variables and objects take up memory and should therefore only be defined when they are actually needed (something that many developers no longer consider).

In the end, unclean code needs more system resources. This results in users needing more hardware, which in turn requires more power and pollutes the environment. Saving physical resources is obviously easier than saving virtual ones.

Methods

When it comes to using methods or functions, you can do a number of things to improve your code. Always choose meaningful names for the transfer parameters and the return values. If the IDE generates these names automatically, make sure you rename them to reflect the actual function.

Methods should only do one task at a time, but they should do it completely. Try to get into the habit of always using the same order for the methods in a class (e.g., the constructor first, then the public methods, and then the private methods).

Methods and functions will ideally contain no more than seven statements. If a method becomes longer, think seriously about breaking it down into several smaller ones. It is also useful to remove complex logical expressions from the if command and move them to a private method.

Methods operating on private variables should either set a value or return one (get or set), but not both. Do not use flags to change the way methods work. Instead, write two methods rather than including special versions of a method.

Methods should not have too many parameters. If you end up with four or more parameters, you need to consider whether it would make more sense to split up the method. Another possible solution is the use of parameter objects. Methods should not modify the transfered parameters or objects if possible. It is cleaner to create new objects and return them.

More Tips

You can employ side effects when programming code so that one command performs several tasks at the same time. This practical approach makes the code very short and compact. However, this approach can pose risks, because not every developer can easily recognize these side effects in the code. Try to write simple and understandable programs rather than "clever" ones.

Another option, the To approach, is a top-down process where you break down a task into increasingly smaller sub-steps. In the end, the individual steps consist of simple, short methods. You can also start by first writing comments to describe what you want to happen, and then implement the methods themselves. In the To approach, a method's results are passed to the next method.

A class corresponds to an entity and must describe the entity completely. Try to create as few classes as possible. When doing this, keep classes as small as possible, and handle only one task per class where practical. Coupling of classes needs to be as loose as possible. This makes it much easier to replace a single class.

All good programmers are proud when they create a working loop for the first time. Unfortunately, this leads to the habit of using Magic Numbers, which are numbers that appear in the code without any comments and control some loop or specify a limit value in a condition. These numbers have no real meaning for the reader, but the code does what it is supposed to do in a magical way. You should replace these numbers with constants using mnemonic names, which will result in more readable code.

When you start developing the source code, don't optimize for speed first. Instead, work on making the code readable. If it turns out that program execution is too slow, you can always optimize the offending code passages.

And always be aware that you may be prone to forgetfulness yourself. The rules and tips mentioned here are intended to generally build source code in a way that makes it easier to understand. It's not only others that will benefit from this, but almost certainly your future self as well. Nothing is more annoying than not understanding your own code after a few months.

Buy this article as PDF

Express-Checkout as PDF
Price $2.95
(incl. VAT)

Buy Linux Magazine

SINGLE ISSUES
 
SUBSCRIPTIONS
 
TABLET & SMARTPHONE APPS
Get it on Google Play

US / Canada

Get it on Google Play

UK / Australia

Related content

  • PHP XHP Extension

    The PHP XHP extension lets you use HTML and XML tags directly in the PHP code.

  • JavaScript Alternatives

    JavaScript is the stuff of which many interactive web clients is made, but it comes with a fair amount of historical ballast. The creators of four alternative scripting languages seek to ditch the ballast.

  • Kotlin Programming Language

    Kotlin, a small island in the Gulf of Finland, is also the name of a new programming language that aims to become a modern alternative to Java.

  • Rails Framework

    Most web libraries make 90 percent of the job simple and the rest impossible. Rails,an open source framework programmed in Ruby,is flexible enough to succeed with that remaining 10 percent.

  • GPIO on Linux Devices

    The general purpose input/output interface is not just for small-board computers anymore: You can use GPIO on your Linux desktop or laptop, too, through the USB port.

comments powered by Disqus
Subscribe to our Linux Newsletters
Find Linux and Open Source Jobs
Subscribe to our ADMIN Newsletters

Support Our Work

Linux Magazine content is made possible with support from readers like you. Please consider contributing when you’ve found an article to be beneficial.

Learn More

News