Graduate Program KB

Chapter 1 - Clean Code


What is Clean Code?

Clean code is essentially code that the author has taken the time to care for and look after. It is simple, easy to read, efficient, lacks any duplicates, and does one thing well.
This is the general idea of what clean code is.
It is not just enough to know what clean code is and what bad/messy code is. Many people can differentiate between the two.
The goal of this book is to not only be able to see and know the difference, but to be able to change bad code into good code or just simply write good code.
Doing is different to knowing.

Quotes

Bjarne Stroustrup

I like my code to be elegant and efficient. > The logic should be straightforward to make it hard for bugs to hide, > the dependencies minimal to ease maintenance, > error handling complete according to an articulated strategy, > and performance close to optimal so as not to tempt people > to make the code messy with unprincipled optimizations. > Clean code does one thing well.

I really liked this quote as I believe it encaptures everything that the other quotes talk about, other than caring. That one is next.

Michael Feathers

I could list all of the qualities that I notice in clean code, > but there is one overarching quality that leads to all of them. > Clean code always looks like it was written by someone who cares. > There is nothing obvious that you can do to make it better. > All of those things were thought about by the code's author, > and if you try to imagine improvements, you're led back to where > you are, sitting in appreciation of the code someone left for > you - code left by someone who cares deeply about the craft.

This is the missing piece to the first quote. These 2 together encompasses everything that I believe to be clean code (from reading the first chapter).

The Idea and Premise of Why it's Important

The main reasons for clean code being so important in the workplace is due to how much it impedes development efficiency and enjoyability. Working through a messy code base is miserable and slows down efficiency massively.
The only real way to solve this and improve efficiency (if it's a current issue) is to to do a large remodel which takes up a lot of time and resources.
The ideal way to solve this long term is to teach developers early on clean code practices and to encourage a fix as you go mentality.
To expand on that last point, once you have finished what you are working on, check the surrounding code to see if there are any easy fixes you can make to maintain the code. Everyone makes mistakes, even if consciously trying to write clean code. These little changes could be as simple as a better variable or function name, removing an accidental duplicate, or removing an unnecessary function.
Fixing these little things as we go will maintain the code and prevent it from rotting.


Return