Clean Code

Writing clean code with Metalama

Clean Code, as defined by Robert C. Martin, emphasizes the importance of writing code that is easy to understand, maintain, and modify.

There has been much discussion around the importance of writing clean code, a skill every developer should strive to acquire.

It's crucial to understand that clean code matters most at scale, when the application reaches thousands of lines of code.

Clean code tools

There are several categories of tools:

  • General code analysis tools detect violations of general principles, industry standards, and best practices, but they cannot easily be customized to your specific situation.

  • Architecture verification tools, such as Metalama Architecture Verification, allow you to create your own rules.

  • Code generation tools, including aspect-oriented frameworks, allow you to generate redundant code on the fly during compilation, keeping your source code clean and concise.

Your clean code pipeline will need all of these tools.

How can Metalama help?

According to Martin, clean code possesses several characteristics. Let's see how Metalama can help improve each of them.

Criteria

How Metalama can help

Complementary tools

Readability

Code should be well-organized, and names should clearly indicate their purpose.

  • Code is often cluttered by operational requirements like error handling, data binding, and caching. With Metalama, you can encapsulate these requirements as aspects and apply them to your code using custom attributes or fabrics. This helps you remove this clutter from the source code, making its essence more apparent.

  • Using Metalama Architecture Verification, you can enforce naming conventions, for instance, naming classes and members according to their role in the design pattern.

Code style and formatting.

Code cleanup.

Simplicity

Code should be as simple as possible, avoiding unnecessary complexity.

  • Metalama can seem like a source of complexity in the beginning, but when used wisely (not as a hacking tool) and at scale (dozens of uses of each aspect), it reduces complexity by achieving a better separation of concerns between business logic and non-technical requirements.

  • Metalama Architecture Verification or custom validation aspects can detect when code does not adhere to the desired architecture or pattern guidelines.

Efficiency

Code should be efficient in performance and resource usage.

  • Thanks to its unique C#-to-C# template technology, the code generated by Metalama is as fast as hand-written code.

  • Metalama allows easy implementation of performance-improving features such as caching or memoization, or it can instrument the code and collect performance information.

Profilers.

Benchmark and performance tests.

Testability

Code should be easy to test, meaning it's modular and allows for independent component testing.

  • Operational features such as logging or error handling can be difficult to test effectively as their implementation is replicated in several classes, each of which needs to be tested separately. However, by utilizing Metalama, these features can be implemented in a centralized location, known as an "aspect." Aspects can then be thoroughly tested using Metalama's unique testing framework.

Refactorability

The code should be written in a way that allows easy modification or refactoring without breaking functionality.

  • Using aspects can reduce the need for refactoring because features implemented with aspects can be changed or added without refactoring. When a pattern needs change, the only thing to modify is the aspect, not the hundreds of files using the aspect.

  • You can use Architecture Verification to raise warnings for code that requires refactoring. For instance, if you no longer want a component to be used except from a given namespace, you can report a warning for any undesired use.

  • Metalama aspects can be programmed to suggest custom refactorings. They are offered to developers as other C# refactorings.

Lack of Duplication

Avoid repetition of code (DRY Principle).

  • Metalama can further reduce repetitive code that cannot be efficiently abstracted by OOP by encapsulating cross-cutting concerns into aspects.

Code duplication detection tools.

Expressiveness

The code should clearly express the intentions of the developer.

  • By removing boilerplate, the business meaning of the source code is more apparent.

  • Instead of using code comments, you can express your design intent using architecture validation custom attributes or fabrics. Metalama will report a warning whenever this rule is violated.

Error Handling

Code should anticipate and properly handle potential errors.

Minimal Dependencies

Code should have minimal dependencies on other pieces of code.

  • Dependencies on technical services such as the logger or cache will be moved to aspects. Your source code will be free of these dependencies. During the build, Metalama will pull them into your code, making them runtime dependencies, but not source-code ones.

Standards Compliance

Code should adhere to industry standards and best practices.

  • Beyond industry-wide standards, Metalama Architecture Validation can help you validate the code against rules and conventions that are specific to your project.

Generic code analysis tools.

```