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.
« Previous 1 2 3 Next »
Buy this article as PDF
(incl. VAT)
Buy Linux Magazine
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.
News
-
Linux Servers Targeted by Akira Ransomware
A group of bad actors who have already extorted $42 million have their sights set on the Linux platform.
-
TUXEDO Computers Unveils Linux Laptop Featuring AMD Ryzen CPU
This latest release is the first laptop to include the new CPU from Ryzen and Linux preinstalled.
-
XZ Gets the All-Clear
The back door xz vulnerability has been officially reverted for Fedora 40 and versions 38 and 39 were never affected.
-
Canonical Collaborates with Qualcomm on New Venture
This new joint effort is geared toward bringing Ubuntu and Ubuntu Core to Qualcomm-powered devices.
-
Kodi 21.0 Open-Source Entertainment Hub Released
After a year of development, the award-winning Kodi cross-platform, media center software is now available with many new additions and improvements.
-
Linux Usage Increases in Two Key Areas
If market share is your thing, you'll be happy to know that Linux is on the rise in two areas that, if they keep climbing, could have serious meaning for Linux's future.
-
Vulnerability Discovered in xz Libraries
An urgent alert for Fedora 40 has been posted and users should pay attention.
-
Canonical Bumps LTS Support to 12 years
If you're worried that your Ubuntu LTS release won't be supported long enough to last, Canonical has a surprise for you in the form of 12 years of security coverage.
-
Fedora 40 Beta Released Soon
With the official release of Fedora 40 coming in April, it's almost time to download the beta and see what's new.
-
New Pentesting Distribution to Compete with Kali Linux
SnoopGod is now available for your testing needs