If we always try to leave things better than we found, the world would be a better place. That concept is applicable to the process of software development as well. Software changes gradually and the cost of additional rework caused by choosing an easy solution instead of using a better approach, which is known as technical debt, increases over time. “Always leave the campground cleaner than you found it.” The boy scouts have this rule which advices us to clean up any mess which is found while camping, regardless of whom that might have made it. This improves the environment for the next group of campers.
What if we followed that simple rule in our code?
Regardless of the amount of knowledge and experience, every programmer might end up adding technical debt, due to the resource constraints. Whenever a bad smell of a code is found, while solving a bug or adding a new feature, it should be removed as soon as possible, irrespective of how small it is. Software development is a process that focuses not only in achieving the main goal by implementing the software to meet customer requirements but also in maintaining the quality by constantly taking care of the code behind the software, as well.
What is ‘Code Quality’?
Software Quality Assurance (SQA) is a process which ensures that a software complies with standardized quality specifications. In the context of SQA, ‘Quality Coding’ has become a major concern as well as one of the trending topics in which the industry spends most of its resources. ‘Quality Code’ is always considered as an essential property of successful software. If the code does not reach the defined level of quality, it may lead to financial matters or time management issues due to maintenance and modifications. Efficiency, reliability, robustness, portability, maintainability and readability are considered as the mainly defined characteristics of a ‘Quality (Clean) Code’. The efficiency of software is defined related to performance and speed. Since no one would prefer to use software that takes an unacceptable amount of time to perform, efficiency plays a major role in evaluating software quality. Efficiency of software can be improved by writing reusable code and removing redundant code. To improve efficiency, it is important to use appropriate data types and functions and reduce resource consumption. The reliability of software is the ability to perform the operations consistently without any interruptions or failures.
A software is said to be unreliable if it functions differently although the same input is given within the same environment. Proper error and exception handling, frequent code reviews and thorough testing are the main factors which improve the reliability of software. Robustness is the ability of software to cope with erroneous input. End users would not expect strange messages when they accidentally or unknowingly do something wrong while using the software. Although a typical software can contain some unidentified defects or bugs, the errors and exceptions have to be handled properly. The software has to be tested on both usual and unusual inputs. The error messages should contain the error in clear and understandable form to the end user, with directions to overcome the issue. Portability of a software is defined as the ability of the source code to be run on different platforms and various environments. It would be a waste of time, effort and resources if the developers have to re-write the code when it is transferred from one platform to another. This feature has to be planned at the very beginning of the software development. Maintainability is the ability to fix, update and modify the system for the correction of errors, performance improvements or extensions of client requirements with minimum effort without affecting other related functionalities. In order to achieve maintainability, it is important that the source code is easy to understand, easy to locate and easy to do changes without affecting the existing features and functionalities.
Proper indentation, formatting, proper naming convention for variables, methods and classes, technical documentation and appropriate comments within the code itself can be considered as the best practices in maintaining a software source code. Readability is defined as the ability of the reader to clearly and quickly understand what is written. Clean code or the quality code is always easy to understand and readable by anyone regardless the reader is the author of the code or a completely new programmer. If the source code is not clear and not easily understandable, more time and effort would be wasted on trying to figure out the purpose of code blocks as well as on locating a particular code block that performs a specific task. Proper naming conventions, comments and indentation improves the readability of source code. Readability is defined as the ability of the reader to clearly and quickly understand what is written. Clean code or the quality code is always easy to understand and readable by anyone regardless the reader is the author of the code or a completely new programmer. If the source code is not clear and not easily understandable, more time and effort would be wasted on trying to figure out the purpose of code blocks as well as on locating a particular code block that performs a specific task. Proper naming conventions, comments and indentation improves the readability of source code.
Why should we strive for ‘Clean Code’?
Writing clean code is similar to good habits that come with time, practice and experience. Programmers tend to come up with various excuses for not following the clean code principles; time constraints, not knowing how to write clean code, believing that it is QA’s task to clean up the code, the software functions regardless of the cleanliness of the code and etc. Even though a code with too many bad smells does not become problematic for its author to understand, the other developers may find it very difficult to handle it afterward. Getting into the clean code mindset offers many important.
• Better use of time
The programmer, themself, is the very first to get the benefit of clean code. If the project is extended to months or years, there is a higher possibility that they could forget each and everything in the code. If the code is clean and self-explanatory, understanding and making changes would not make any mess.
• Easier adaption for new programmers
A new programmer can easily understand the code and start work, even without any documentation if the code is clean. It saves time for both the
trainer and the trainee.
• Fast and easy debugging
Bugs are unavoidable regardless the code is clean or dirty. However, a clean code helps to locate the issue and debug more efficiently. Even the other programmers will not find it difficult to intervene in the debugging process.
• More efficient maintenance
As the project expands, it requires new features and changes to existing features. Clean code makes maintenance relatively fast and easy.
The software developers should feel confident to share their work with other developers, as well as with the client. If the written code is clean and up to the standards, the self-satisfaction and confidence become higher, where there is no fear of breakdowns or efficiency issues.
No one is perfect..!
A programmer, as an author, should be able to identify his audience properly. The clean code concept mainly focuses on humans as readers of the code, not the computers. Spending time on learning the theories, principles and methodologies along with the programming languages is not enough to be a quality coder. Software quality can be basically defined as the degree of conformance of functional and non-functional requirements. Coding is an intellectually challenging and stressful activity which requires a level of concentration and focus when comparing to most of the other disciplines. It is because coding requires to consider and handle many competing factors at once. Most importantly, the programmer should build the code into a working software product. The first step of coding is, ensuring that the problem is well defined and the ways and means of solving the particular problem is clearly understood. While remaining consistent with the language, platform and architecture, the solution has to be flexible and manageable.
Most of the time, the customer may not clearly define the problems they need to get solved by using the expected software platform. It is the programmer’s task to derive the untold requirements of the customer by analyzing the defined problems. Negotiating with the customer to ensure that the real needs are met and they are up to the required standards, is the next most challenging task for a programmer. The programmers have to be familiar with best practices of software engineering to deliver quality output. Developing quality code is required to be done from the very beginning of software development and the programmers should practice the quality code concept in order to produce a quality software product. Programming or coding is an ongoing process that will get improved based on the degree of experience. As a good programmer, it is important to have an interest in keeping up with the latest trends and technologies. Developers should have an eager to take opportunities, be innovative and find new ways of making their work more efficient and effective. To ask the right questions at the right time and to understand the concepts correctly and quickly, programmers have to get equipped with good communication skills. Developing the software by coding is one part of a programmer’s tasks.
They are expected to identify the defects of the developed software and derive the root of those defects quickly and effectively. Starting to write code as clean as possible from the very first line, so that later it can be improved easily with the experience gained through the time, is the best way to become a Clean Coder. According to ICTA (Information and Communication Technology Agency of Sri Lanka), “an average employee takes around 3 years to practice quality coding”. It is quite a long time in an industry that is developing and improving at a higher speed. It is very important to make sure that the ICT professionals are aware of the quality coding concept along with the theoretical and practical knowledge in programming, in order to reduce this adapting time as much as possible.
Exposition Magazine Issue 15
Department of Industrial Management
University of Kelaniya