Skip to content

Latest commit

 

History

History
36 lines (25 loc) · 4.38 KB

File metadata and controls

36 lines (25 loc) · 4.38 KB

Clean Code

Clean code is code that is easy to read, easy to understand, and easy to modify. It follows a set of standards and conventions that make it easy for developers to work with, and it is an important aspect of software development.

There are several principles that can help you write clean code. One of the most important principles is the DRY principle, which stands for "Don't Repeat Yourself." This principle states that you should avoid duplication in your code, and instead find ways to reuse existing code whenever possible.

Another set of principles that can help you write clean code is the SOLID principles. These principles include:

  • Single Responsibility Principle: A class should have only one reason to change.
  • Open/Closed Principle: Software entities (classes, modules, functions, etc.) should be open for extension but closed for modification.
  • Liskov Substitution Principle: Subtypes must be substitutable for their base types.
  • Interface Segregation Principle: Clients should not be forced to depend on interfaces they do not use.
  • Dependency Inversion Principle: High-level modules should not depend on low-level modules. Both should depend on abstractions.

Other principles that can help you write clean code include the KISS principle, which stands for "Keep It Simple, Stupid," and the YAGNI principle, which stands for "You Aren't Gonna Need It." These principles encourage you to avoid unnecessary complexity and to focus on implementing only the features that are immediately necessary.

Other principles of clean code are:

  • Keep it simple: Avoid unnecessary complexity in your code. Keep your code as simple and straightforward as possible.
  • Keep it readable: Write code that is easy to read and understand. Use clear and descriptive names for variables, functions, and other elements, and use comments to provide additional context where needed.
  • Keep it well-structured: Organize your code into logical units, and use clear and consistent indentation and formatting to make it easy to navigate.
  • Keep it maintainable: Write code that is easy to modify and extend. Avoid duplication, and use modular design patterns to make it easier to change one part of the code without affecting the rest.
  • Keep it tested: Write automated tests to ensure that your code is correct and working as expected. This will help you catch bugs early on and make it easier to make changes to your code with confidence.

Finally, the Law of Demeter states that an object should only communicate with its immediate neighbors, and should not have knowledge of the inner workings of other objects. This helps to keep your code well-structured and easy to understand.

In the book "Clean Code: A Handbook of Agile Software Craftsmanship.", Robert C. Martin, also known as "Uncle Bob" describes a number of principles and practices for writing clean, maintainable code. Here are some of the key guides that Uncle Bob recommends for applying clean code:

  • Use meaningful names: Choose descriptive, meaningful names for variables, functions, and other code elements. Avoid using abbreviations or acronyms unless they are well-known and widely used.
  • Write short functions: Keep functions short and focused on a single responsibility. If a function becomes too long or complex, consider refactoring it into smaller, simpler functions.
  • Use clear and concise code: Avoid unnecessary complexity and write code that is easy to read and understand. Use whitespace and formatting to improve readability and make the code easier to navigate.
  • Write tests: Write automated tests for your code to ensure that it is correct and maintainable. This will make it easier to detect and fix bugs, and to make changes to the code without introducing new problems.
  • Follow design patterns: Use established design patterns to solve common problems and structure your code in a way that is easy to understand and work with.
  • Refactor code regularly: Regularly review and refactor your code to ensure that it is clean, maintainable, and easy to understand. This may involve breaking up large functions into smaller ones, extracting common code into reusable functions, or reorganizing code to improve readability.

By following these principles, you can help ensure that your code is clean, maintainable, and easy to understand. This will make it easier for you and other developers to work with your code, and it will help reduce the risk of bugs and other issues.