Power of Clean Code: Unleashing the Developer’s Superpower

Chamila Ambahera
6 min readSep 29, 2023

--

Any fool can write code that a computer can understand. Good programmers write code that humans can understand.

Quote by Martin Fowler

You might think why another article about clean code when there are 100 articles and books about it?

Sadly you are wrong. Here I’m helping you clean the existing code smells and how to prevent adding new code smells to keep the code clean.

If you want to learn about Clean Code, then I recommend you to read “Clean Code” book by Robert C. Martin

https://www.amazon.com/Clean-Code-Handbook-Software-Craftsmanship/dp/0132350882

Before we dig into the main purpose of the document we need to understand what is the clean code and its benefits in summary.

Clean code is code that is not only functional but also easy to read, understand, and maintain. It adheres to best practices, follows a consistent style, and has minimal complexity. Clean code reflects the developer’s commitment to craftsmanship and a deep understanding of the software’s inner workings. It is code that a developer can be proud of, and that others can effortlessly pick up and work on.

Check how dirty code looks like

class DirtyCode{
public static void main(String[] args){int a= 1;int b=2;int result=0;
for(int i=1;i<=b;i++){result = a + i;}
if(result>=3){System.out.println("Result is greater than or equal to 3");} else {System.out.println("Result is less than 3");}
}}

Is this easy to read and understand?

The Power

  1. Enhanced Readability: Clean code is like a well-written novel. It’s a pleasure to read and comprehend. When code is clean, anyone, from the original developer to a newcomer, can quickly grasp its purpose and functionality. This clarity fosters efficient collaboration within the development team.
  2. Reduced Bugs and Defects: Clean code reduces the chances of introducing bugs. When code is clear and follows best practices, developers are less likely to make mistakes. This, in turn, reduces the time spent debugging and testing, leading to more reliable software.
  3. Maintainability: Software doesn’t exist in isolation; it evolves. Clean code is much easier to maintain and extend. When changes are needed, clean code allows developers to make alterations without the fear of causing unintended side effects or breaking the system.
  4. Improved Collaboration: Clean code is a universal language. It eliminates ambiguity and misunderstandings. When team members can rely on clean code, collaboration becomes more efficient. Code reviews are faster and more productive, and knowledge transfer among team members is seamless.
  5. Efficiency and Productivity: Clean code streamlines the development process. It reduces cognitive load, making it easier for developers to work on the codebase. Developers spend less time deciphering code and more time writing new features or addressing user needs.
  6. Ease of Onboarding: In any development team, there’s a constant flow of new members. Clean code simplifies the onboarding process, enabling new developers to become productive more quickly. They can focus on learning the domain and business logic, rather than wrestling with cryptic code.
  7. Cost Savings: The benefits of clean code extend to the bottom line. It saves time and resources that would otherwise be spent on maintenance, debugging, and addressing issues stemming from unclear code. Over the long term, this can lead to significant cost savings.

Unleashing the Superpower

So how do we unleash this superpower?

Fortunately, there are several tools available to help developers write clean and maintainable code. These tools assist in code analysis, code formatting, identifying issues, and promoting coding best practices. Here are some popular clean code tools in various categories:

Code Analysis Tools:

  1. SonarQube: SonarQube is a comprehensive code quality and security analysis tool. It identifies code smells, bugs, security vulnerabilities, and more. It supports a wide range of programming languages and integrates with various CI/CD pipelines.
  2. ESLint: ESLint is a popular JavaScript and TypeScript linter that identifies and enforces coding standards and best practices for these languages. It can be extended with custom rules to suit your project’s specific needs.
  3. RuboCop: RuboCop is a Ruby static code analyzer and formatter that enforces the community Ruby-style guide. It helps keep your Ruby code clean and consistent.
  4. Checkmarx: Checkmarx is a static application security testing (SAST) tool that identifies security vulnerabilities and code quality issues in a variety of programming languages.

This is the output of Code analysis tools look like

This example is from a Selenium code scan in SonarCloud (https://sonarcloud.io/)

Code Formatting Tools:

  1. Prettier: Prettier is an opinionated code formatter that automatically formats code to follow a consistent style. It supports multiple languages, including JavaScript, TypeScript, HTML, CSS, and more.
  2. Black: Black is a code formatter for Python that enforces a consistent style and formatting. It’s known for its “one true way” approach to Python code formatting.
  3. gofmt: Gofmt is the official code formatter for the Go programming language. It automatically formats Go code to a specific style.

Version Control and Collaboration Tools:

  1. Git and Git Linters: Git is a widely used version control system. You can use Git hooks and linting tools like pre-commit to enforce coding standards and conduct checks before code is committed.
  2. Code Review Tools (e.g., GitHub, GitLab, Bitbucket): Many version control platforms offer built-in code review features. These platforms allow team members to collaborate on code reviews and provide feedback on code quality.
  3. IDE integration plugins like SonarList: There are many plugins available for different quality purposes. Most of the plugins support main IDEs like Eclipse, IntelliJ etc

Example for SonarLint code analysis tool integration with
IntelliJ IDEA. Before you commit the message it will throw a warning to review the code.

Continuous Integration/Continuous Deployment (CI/CD) Tools:

  1. Jenkins: Jenkins is a popular CI/CD tool that can be configured to run code analysis and checks, including those for clean code, as part of the CI/CD pipeline.
  2. Travis CI: Travis CI is a cloud-based CI/CD service that integrates with various code analysis and linter tools.

Issue identification frameworks :

  1. SpotBugs: Static code analysis tool used to identify common software bugs and code quality issues in Java applications. It is the successor to FindBugs, offering enhanced features and support. SpotBugs analyzes Java bytecode rather than source code, making it suitable for detecting issues in compiled Java applications.
  2. Checkstyle: This popular static code analysis tool helps developers and teams maintain coding standards and best practices in their Java codebase. It enforces a set of rules and guidelines to ensure that code quality is consistent and aligns with established coding conventions. Checkstyle scans Java source code to identify violations of coding standards, style issues, and potential problems.

These tools help developers and teams maintain clean code by identifying issues, enforcing coding standards, and automating code formatting. The choice of tools may depend on the programming languages used in your project and the specific requirements of your development process. Integrate these tools into your workflow to achieve consistent and clean code, improve code quality, and enhance maintainability.

When you clean the code it will be looks like this.

public class CleanCodeExample {
public static void main(String[] args) {
int a = 1;
int b = 2;

int result = a + b;

if (result >= 3) {
System.out.println("Result is greater than or equal to 3");
} else {
System.out.println("Result is less than 3");
}
}
}

What I have done to clean the code.

  • Proper indentation and spacing are used for readability.
  • Variable names are more descriptive, making it clear what they represent.
  • The unnecessary for loop is removed as it served no purpose.
  • The conditional statements are simplified and follow standard Java conventions.
  • Comments are not required as the code is now self-explanatory.

Conclusion

Clean code is not a luxury but a necessity in software development. It’s the developer’s superpower that enables teams to build high-quality software efficiently. By investing in clean code, we not only improve the software but also our own productivity and job satisfaction. In the fast-paced world of technology, where change is the only constant, clean code provides stability and empowers us to create exceptional software.

The power of clean code is within your reach, waiting to be harnessed. So, the next time you’re coding, remember that writing clean, readable, and maintainable code is not just a best practice; it’s your superpower as a developer.

--

--

Chamila Ambahera

Principle Automation Engineer | Arctic Code Vault Contributor | Trained Over 500 engineers