Clean Code

Clean Code

While wondering about what I should write for my first ever tech blog, I figured why not go to the basics and apply the same principle here. So here I am talking about Clean Code and summarizing my learnings from Clean Code by Uncle Bob

So what is clean code?

Any code which is easy to understand, debug and maintain is clean code. The only valid measurement of code quality is number of wtfs/minute. As per 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 code messy by making unprinciples optimization. Clean code does one thing well."

Let's look at some of the guidelines for writing clean code.

Meaningful Names

Functions

Comments and Formatting

Objects, Data Structures and Classes

We don't want to expose the details of our data. Rather we want to express our data in abstract terms. Objects hide their data behind abstractions and expose functions that operate on that data. Keep your classes small in terms of responsibilities. Single responsibility principle states that a class or module should have one, and only one reason to change. Have many, but smaller classes.

Error handling, Unit tests, Et. Al

Always use exceptions rather than error codes. Try to write tests that force exceptions and then add behaviours to your handler to satisfy your tests. You many not write production code until you have written a failing unit test. Test code is just as important as production code. And always have single concept per test. Your tests should be fast, independent, repeatble and self-validating.

Classes

This is in no way an exhaustive list, but I hope this will act as a good starting point on how to write clean code. Always remember to keep things simple, complexity kills - it sucks the life out of developers, it makes products difficult to plan,build and test. A design must produce a system that acts as intended. Code should clearly express the intent of it's author. A design is simple if it follows the below rules:

Happy Coding!!