What does Clean Code mean?

“Any fool can write code that a computer can understand. Good programmers write code that humans can understand.” — Martin Fowler.

Many software projects suffer from an overwhelming jump in costs. As the project survives, it becomes more and more expensive to implement new features in the existing code base.

  • Due to its size, rewriting is not an alternative.
  • The software thus makes more and more difficulties in the areas of correctness and changeability.
  • Since very limited tests available, errors remain undetected when changes are made, and the correctness of the software deteriorates.
  • Since the structures are very complicated, methods and classes are too long and responsibilities are mixed up and many other things are still in trouble, the changeability is also not given.
  • Code that nobody understands is difficult to change.

So in short, bad code is difficult to understand, more complex than it should be, not easy to test, and it makes other developers approach with frustration. While it might take longer to write clean code in the short term, it’s beyond established that writing clean code will save everyone time, effort, and ultimately money.

Clean Code is a term from software development and addresses the clear, comprehensible, traceable, logical and disciplined implementation of code. The goal is to produce software efficiently and effectively, and to design the code so that it is easily

  • readable,
  • changeable,
  • expandable
  • and maintainable.

In today’s software development practice it is not uncommon for working code not necessarily to be “clean”. This quickly leads to errors during adaptations or problems with extensions, for example when a small change leads to a cascade of subsequent changes. The maintenance and further development of software is unnecessarily complicated.

Moreover, in real world scenarios it is highly likely that you will be working as a team of programmers to build any specific application; therefore, it is important to keep your code clean and understandable because you are not the only one working on them. If you write clean code, then you are helping your future self and your co-workers.

And finally, clean code adds to the value of the project, as it ensures that developers as well as non-programmers (generally) are able to understand what the code is actually doing with the help of the flow and structure of the program.

When we talk about clean code, we talk about a reader-focused development style that produces software that’s easy to write, read, and maintain. And because people need to understand the code we write, we can say that the code we write is not intended only for the computer but also for humans.

Programming is the art of telling another human what one wants the computer to do.
— Donald Knuth

Clean Code Principles

Clean code doesn’t rely on language-specific rules. Instead, it relies on language-agnostic principles agreed upon by the developer community. Here are some to list;

A design principle originating from the U.S. Navy that goes back to 1960 already. It states that most systems should be kept as simple as possible (but not simpler, as Einstein would have said). Unnecessary complexity should be avoided. The question to ask when you’re writing code is “can this be written in a simpler way?” Don’t get fancy if you don’t have to, and don’t over-complicate problems (a common issue among software developers). By keeping it simple you can produce higher quality code, solve problems faster, work better in developer groups and have a more flexible code base, among other things.

As a beginner, even if you are writing simple code having an ‘if else’ statement, start by realizing the code on a piece of paper. The algorithm and the whole compiler process will look more meaningful once you understand the idea behind the code. Even for experts, the best way to solve a complex problem or formulate an algorithm to solve any complex problem is by breaking it into sub-parts and then try to formulate a solution for each. Once you start internalizing the code and solving problems you will quickly build up your confidence.

Adding comments to your code can be invaluable — they can quickly show what a complex function is doing, or maybe even explain why certain things need to happen in a certain order. As people grow and gain experience as coders, they tend to forget one of the most basic tips that they received in their early days of programming — leave comments. Adding comments to your code is a tip that spans every programming language. It makes updating, debugging, analysis and other post-programming activities easier and more efficient. Further, if you are working as a team, having comments in the code makes it convenient for the other members to understand your idea of the code. On the other hand, they do have a downside, though, because too much commenting can have the opposite effect and actually make for messier code.

Closely related to KISS and the minimalist design philosophy. It states that every piece of knowledge (code, in this case) must have a single, unambiguous, authoritative representation within a system (codebase). Violations of DRY are referred to as WET: We Enjoy Typing, Write Everything Twice, Waste Everyone’s Time.

A developer should not add functionality unless deemed necessary. YAGNI is part of the Extreme Programming (XP) methodology, which wants to improve software quality and increase responsiveness to customer requirements. YAGNI should be used in conjunction with continuous refactoring, unit testing, and integration.

Imagine you go to a supermarket and there is no consistency over how the items are placed in the area. Some dairy products are at the clothing sections, others are at the cosmetics area, and bread products are placed with the vegetables. Indentation in the code is much like the arrangement that you need in a supermarket or any other place in the real world. When your code is indented, it becomes more readable and easier to find what you’re looking for.

Using whitespace can be incredibly powerful and normally has absolutely no downside. Sometimes in languages like JavaScript where the file size of the source code itself is important, you might want your files to be small, and that whitespace can add a few extra kilobytes. When you can, keep all your whitespace during development so the code is readable. Then, use one of the many smart little programs that go through code and chop out all the whitespace just before you upload it.

https://devhumor.com/user/tecHindustan

This is one tip that keeps popping up in every single article about the correct way of working on any programming language, and still people tend to forget or neglect it. Having a proper naming convention is extremely important in a code as the doors for future edits and updating is always wide open. Having irrelevant or contradicting names to your pages, variables, functions or arrays will only create troubles for you in the future. Therefore, name elements on the basis of what they are and make it a habit to maintain a convention throughout your code.

It’s not because a machine can read your code that another human can. Particularly when working with multiple people on a project, always favor readability over conciseness. There’s no point in having concise code if people don’t understand it.

There are many ways to make your code more readable. Two examples are placing common numbers into well-named constants (e.g. const CACHE_TIME = 200;) and creating long names instead of shorter ones (e.g. userHasFormAccess over canAccess, which doesn't tell as much).

Not an acronym, sadly. It’s a principle where you design your types over what they do instead of over what they are. It’s explained in more detail in this video. One of the ways to implement this principle is with the Object.assign() method in ES6.

Composition is favored over inheritance by many developers, because inheritance forces you to build a taxonomy of objects early on in a project, making your code inflexible for changes later on.

This is arguably the overarching principle of all clean code principles. If you decide to do something a certain way, stick to it throughout the entire project. If you have no choice but to move away from your original choice, explain why in the comments.

Clean coding is a reader-focused development style that produces software that’s easy to write, read and maintain. Often, you may be tempted to consider your work complete when the application operates as expected. But we’re not merely writing code for computer consumption. Clean code is about recognizing that your audience isn’t just a computer, it’s real-live humans!

Clean coding is not a skill that can be acquired overnight. It is a habit that needs to be developed by keeping the above mentioned principles in mind and applying them whenever you write code.

A Full-stack developer | Project Manager | SOF Veteran | Tech Enthusiast who is trying to be a human among people