Clean Code Overview
Code that is easy to understand is clean code. It can be read and enhanced by a developer other than its original author, which makes it more changeable, extensible and maintainable. The cost of testing and maintenance can also be significantly reduced with efficient coding.
Read more about it here.
Object Oriented Concepts
Object-Oriented Programming (OOPs) Concept helps in reducing the code complexity and allows the reusability of code. OOPs is a programming paradigm that brings together data and methods in a single entity called an object.
Read more about it here.
Naming Conventions
We will be naming lots of variables, classes, and methods in a codebase. According to Robert Cecil Martin also known as Uncle Bob in Clean Code, we should name a variable with utmost care and the name should convey the intent of its creation. The “intention-revealing” names will save you more time in the long run, save everyone's time, and prevents a snowball of bad things from happening to the precious code. The names we choose should answer the big questions:
- Why does it exist?
- What does it do?
- How is it used?
Read more about proper naming conventions here.
Magic Numbers
A magic number is a number in the code that seems arbitrary and has no context or meaning. One of the most important aspects of the quality of code is how it conveys intention. Magic numbers hide intention so they should be avoided. Magic numbers also make our code brittle and harder to change.
The recommended approach is to create a constant to represent the value. This will improve the readability of the code and make it easier to modify in the future.
Read more about it here.
Writing Good Functions/Methods
Functions are one of the most important parts of writing code. They make the code reusable, and easy to read and maintain. Functions make the code organized.
A good function should have the following properties:
- Should be small
- Should do just one thing
- Should have fewer arguments
- Should not have side-effects
One should be able to look at the function name and understand what that function does.
Read more about it here.
Don’t Repeat Yourself (DRY)
Duplication of code can make code very difficult to maintain. Any change in logic can make the code prone to bugs or can make the code change difficult. This can be fixed by doing code reuse (DRY Principle).
The DRY principle is stated as "Every piece of knowledge must have a single, unambiguous, authoritative representation within a system". The way to achieve DRY is by creating functions and classes to make sure that any logic should be written in only one place.
Read more about it here.
Single Responsibility Principle
The Single Responsibility Principle states that there should never be more than one reason for a class to change. In other words, every class should have only one responsibility. If there are two reasons to change a class then it has more than one responsibility and should be split. Having multiple reasons to change means that the module has high coupling.
Read more about it here.
References
Video Links
- https://www.youtube.com/watch?v=SPlS4kW0UbE&ab_channel=CodingTech
- https://www.youtube.com/watch?v=D4auWwMsEnY&t=96s&ab_channel=CodingTech
- https://www.youtube.com/watch?v=OVf0xP4BLq0&ab_channel=SkyBetting%26GamingTechnology
- https://www.youtube.com/watch?v=5DFUH0zCn3Y&ab_channel=WolfSound
- https://www.youtube.com/watch?v=JnFh2NoAM4s&ab_channel=WebDevSimplified
- https://www.youtube.com/watch?v=2lD8fjedeis&ab_channel=Rebeloper-RebelDeveloper
Clean Code principles
- https://gist.github.com/wojteklu/73c6914cc446146b8b533c0988cf8d29
- https://betterprogramming.pub/12-conventions-for-writing-clean-code-e16c51e3939a
- https://workat.tech/machine-coding/tutorial/introduction-clean-code-software-design-principles-nwu4qqc63e09
- https://x-team.com/blog/principles-clean-code/
- https://www.baeldung.com/java-clean-code
- https://www.freecodecamp.org/news/clean-coding-for-beginners/
- https://garywoodfine.com/what-is-clean-code/
- https://www.codebyamir.com/blog/software-anti-patterns-magic-numbers
- https://workat.tech/machine-coding/tutorial/design-good-functions-classes-clean-code-86h68awn9c7q
- Clean Code: A Handbook of Agile Software Craftsmanship by Robert C. Martin (Uncle Bob).
- https://thecoderoad.blog/2020/03/29/clean-code-naming-conventions/
Geektrust Clean Code Videos