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)