5 Clean Code Practices for Every Software Project | by Shalitha Suranga | Jul, 2021

5 Clean Code Practices for Every Software Project | by Shalitha Suranga | Jul, 2021

Ideas to improve the quality of your frontend, backend, CLI, desktop, or mobile app codebase

Laptop with code on screen
Photo by Sean Lim on Unsplash

Programmers usually work with various sorts of software codebases, and they often add, edit, and remove code lines. Programming is a collaborative approach most of the time. A particular software development team’s highly experienced programmers known as architects typically create the initial foundation of the project. In other words, they prepare the design documents of the conceptual software system. Besides, they define coding standards and architectural standards. After that, other developers start developing each component with the help of the architecture design documents.

Almost all real-world software systems need rapid-feature implementations. Therefore, programmers always try to make their codebases clean and well-organized, then other programmers can easily implement new features. Further, new programmers can start working with the particular software system quickly.

Therefore, we should always think about maintainability factors before applying any code modification to the codebase. I worked with many closed source software projects and open source software projects. I always try to write clean code with the following practices. You may rethink your coding practices after checking these.

Any programming language has several predefined keywords that we cannot use in custom namings. For example, The C programming language has 32 special keywords such as if, for, return, etc. Software libraries and frameworks follow different naming conventions. For example, The C programming language’s standard library has functions such asprintf, sprintf, etc. Programmers have the freedom to choose any name they wish for source code elements except the specific programming language’s keywords and library APIs.

Programmers should always try to select meaningful and self-explanatory naming for source code elements. It’s not only about variable names. We need to choose better names for classes, namespaces, custom data types, functions, and even file names. If there is no clear naming for these source code elements, other programmers may have to analyze the entire module to apply a straightforward patch.

Apart from the programming language’s default naming convention, it is always great to maintain our own naming convention to enhance the readability of the codebase. For example, I typically use a verb as a prefix for every boolean value. For example, hasExtraParameters implies that it holds a boolean value.

Even though we use self-explanatory names for source code elements, our source codes may contain complex and unclear parts. Also, we may include temporary solutions into our codebases. Sometimes, we implement somewhat complex algorithms or calculations. Adding comments is indeed a great way to give hints about complex processes. Your code comments will save other programmers time when they are applying changes to your code.

Writing comments is a great way to give hints about limitations, problems, or temporary work too. If you implemented a workaround due to a limitation in a framework or library, it is always helpful to add a code comment — because another programmer may spend time again on what you already researched.

For example, check the following comment that I found from the Electron framework’s codebase on GitHub:

A helpful code comment in Electron’s source. A screenshot by the author.

However, excessive comments may add unwanted complexity to your source codes. That’s why many programmers tend to skip writing Javadoc.

When code statements are repeated in multiple areas, programmers often move those statements into a function or class for enhanced reusability. For example, if more than one feature tries to download a file from the internet, you may create another class or function to handle file downloading-related workflow. Improving the reusability of code brings many benefits to the entire project: the unwanted complexity will be reduced, programmers can implement new features with minimum modifications, and your codebase’s core becomes more stable.

Therefore, it’s great to think about reusability before implementing any new feature. Some programmers may argue that early refactoring processes waste the time that you may allocate for feature implementations. However, early refactoring will help you to prevent complete, time-consuming rewrites.

Whereas there should be a limit for reusability enhancements and source code decomposition. Overusing reusability principles often make overengineered codebases that every programmer is afraid to touch. Overengineering also adds unwanted complexity to your source codes. Therefore, we need to avoid repetition by keeping overengineering side-effects in our minds.

Programmers typically tend to use different practices when it comes to code formatting. Some programmers let code editor plugins autoformat their code. Meanwhile, some programmers love to do code formatting manually. Code formatting refers to your coding style. In other words, it refers to how you handle the programming language’s syntax — and how you structure your source code with spaces and line feeds. A code formatting rule set defines what coding style that you need to follow.

A well-defined code style guide brings many perks. It will improve the entire source code’s readability and quality. Moreover, code reviewers will be able to review new contributors’ code without any hassle. See how several line feeds and spaces increase the readability of the following code.

Without line feeds and spaces:

After adding line feeds and spaces:

Moreover, if you add some blank lines between continuous statements to group the logic conceptually, other programmers can easily understand the section that they need to modify or study.

The project structure of a medium or large project plays a crucial role in clean code. The project structure refers to the directory and file tree of your codebase. Almost all code editors and IDEs render a tree view from your project’s directory structure. Therefore, programmers often use the tree view to navigate into different modules and components. A well-organized project structure helps programmers to simplify any complex software project which has thousands of source files.

When we decompose large source files into smaller ones, we need to use more statements to import required source files. Sorting or grouping import statements is a great way to reduce the complexity of a particular source file’s header. Clear grouping and sorting are possible only if you have a well-organized project structure.

Check how the Flutter Engine project isolates platform-specific code into different directories for a better project structure.

Flutter engine isolates platform-specific codes as shown above. A screenshot by the author.

Programmers need to maintain clean code practices for every software project, and every programming language and framework motivates programmers to use well-defined naming conventions or practices. Big tech companies maintain their own code styling guides for their code contributors to ensure the quality of their codebases.

Two different parties will indeed read your source code: the compiler and programmers who work on your project. Impress the compiler with optimized error-free code, and impress programmers with clean code.

Source link

Leave a reply

Please enter your comment!
Please enter your name here