DEV Community

Cover image for Crafting Clarity: A Journey into the World of Clean Code Code Needs Care!đŸ€Ż
Rowan Ibrahem
Rowan Ibrahem

Posted on

Crafting Clarity: A Journey into the World of Clean Code Code Needs Care!đŸ€Ż

Introduction âœđŸ»
Do you spend countless hours fixing bugs in your code? Have you wondered why it takes so long?

Everyone can write code that computers understand. But, not everyone can write code that humans can easily read.

As software developers, understanding how to write clean code is essential. Let’s dive into the world of Clean Code.

Image description``

Table of Contents
What is the clean code?
What causes bad code?
Why is clean code important?
Comparison Between Bad Code and Clean Code.
How Can We Determine if Our Code is Clean?
Principles of Clean Code.
Conclusion.

What is the clean code ?đŸ€”
Writing clean code is a fundamental skill for every software engineer. Clean code is to write code that is well-organized, easy to read, understand, and maintain. Writing clean code allows others to read it without wasting time to understand it. Robert Cecil Martin made it popular. He has also gained popularity as Uncle Bob. He wrote “Clean Code: A Handbook of Agile Software Craftsmanship” in 2008.

In another sense, if three people have different experience levels, they can quickly understand the same code. It’s a clean code.

What causes bad code?
Consider this scenario: There’s a new project, and your manager gives you a tight deadline. You start coding, promising yourself to enhance it later. But you deliver the product quickly because of pressure from your manager and the customer. But, it’s poorly written. You will never return to improve it.

Oh, there is Leblanc’s law says:

“Later equals never.”

That means if you said you’d improve this later, you wouldn’t return to it.

Why is clean code important?
Let’s revisit a story from the “Clean Code” book to understand the importance of clean code. A company wanted to release a product quickly, resulting in poorly written code. When they tried to add more features, they found it almost impossible to make changes. The changes would break the existing functions and they lost all their money.

Clean code ensures that companies can add features quickly and without errors.

When we need to add more features or when someone contributes to complete with you, it will take a long time to read and understand it to fix old bugs and make new features.

When writing code implementation is simple, the difficulty is reading and debugging. That takes a long time.

Keeping code clean will give you the most value from your software. The right clean code tool can help you get there. Clean code makes debugging more efficient and unit testing easier.

Image description

A comparison between clean and badly written code.
The compiler will compile every code, clean or not. But not every code will be readable. Let’s take a look:

Can you see this?

You don’t need to understand. Let’s see first:

Bad code

Image description

Clean version

Image description

Even without understanding the code, do you see the difference? It’s formatting the code that makes the code easy to read and comfortable to the eye.

Let’s see another example, its login page contains an image and text field.

Image description

This is the same page but make every part as a small function.

Image description

Here, we make functions with clear names. They make the code shorter. If we want to fix any part, we can go right to it.

You will never know the importance of clean code. Even though you edit your code, you will say: “What a horrible thing I wrote.â€đŸ€Ż

How Can We Determine if Our Code is Clean?
Writing clean code is an art and a way of thinking. It’s like a craft that comes from knowledge and practice. Knowledge comes from knowing principles, patterns, and practice. So, you know this and use it in your code.

There is a Boy Scout rule that says:

“Leave the campground cleaner than you found it.”

That means we need to always check our code and enhance it. Every time we read it we will find that we can make it more cleanly.

In the book “Art of Clean Code,” they provide some questions we should ask ourselves to help us improve our code.

  • Can you consolidate files and modules to reduce interdependencies?
  • Can you divide large, complicated files into simpler ones?
  • Can you generalize code into libraries to simplify the main application?
  • Can you use existing libraries to reduce the amount of code?
  • Can you use caching to avoid recomputing results?
  • Are there more suitable algorithms for your tasks?
  • Can you remove premature optimizations that don’t improve performance?
  • Would another programming language be more suitable for your problem?

Principles of Clean Code.
Like a beautiful painting, clean code needs well-chosen colors and composition. It follows several principles. We will discuss each in later articles.

  • Meaningful and descriptive names.
  • Proper Indentation.
  • Short functions that do one thing.
  • The DRY (Don’t Repeat Yourself) Principle.
  • Avoid code duplication.
  • Establish code writing standards.

These principles are just a starting point; there are many more to explore and apply.

Conclusion :
“Coders spend the vast majority of their time reading old code to write new code. If the old code is easy to read, this will speed up the process considerably.”

If we write messy code, it will reduce productivity, even if we can make it productive and enjoyable while reading it. Clean code makes it easier to reduce errors and add features without being scared to have errors in another function.

By following this, you will become a professional developer, and always remember there are no limitations to making your code clean; you can make it better every time.

Image description

That’s it! Start now and begin writing clean code! Connect with me on LinkedIn and GitHub for more articles and insights. If you have questions, contact me.

https://linktr.ee/rowan_ibrahim

Remember, “Later Equals Never.” Start writing clean code today! 😉

Top comments (0)