steffen9349_man_working_on_computer_not_clean_frustruated_caric_a1ab5046-04f1-46a1-9473-29701ad700ed (Custom).png

Code comments have long been regarded as a necessary evil in the world of software development. They serve as a way to explain complex code or provide context for future developers (or even our future selves). However, I believe that if you have the need to add code comments, there may be something fundamentally wrong with the code itself. In this blog post, we'll explore why I consider code comments to be a code smell and how writing clean and self-explanatory code can eliminate the need for excessive commenting.

The Problem with Code Comments

Code comments, when used sparingly and effectively, can be helpful. They can provide insights into the intent behind certain lines of code or clarify complex algorithms. But more often than not, they become a crutch for poorly written or convoluted code. Here are a few reasons why I think code comments should be minimized:

1. Code Should Be Self-Explanatory

Good code should read like well-written prose - it should be clear and easy to understand without the need for additional explanations. When we write clean and self-explanatory code, it becomes easier for other developers (and ourselves) to follow along and make modifications if needed.

2. Comments Can Be Misleading or Outdated

One major issue with code comments is that they are not always maintained alongside the actual codebase. As the code evolves over time, comments may become outdated or even misleading. This can lead to confusion and introduce bugs when developers rely on outdated information.

3. Comments Can Hide Bad Code

Sometimes, we use comments as a way to justify or excuse poorly written or overly complex code. Instead of taking the time to refactor and improve the structure of our code, we slap on a comment to explain what's going on. This creates technical debt and makes it harder for others (and ourselves) to maintain and understand the codebase.

The Solution: Writing Clean and Readable Code

So, if code comments are not the answer, what is? The solution lies in writing clean and readable code that tells a story. Here are a few tips to help you achieve this:

1. Use Meaningful Names

One of the most important aspects of writing clean code is using meaningful and descriptive names for variables, functions, and classes. By choosing names that clearly convey their purpose or functionality, you can significantly reduce the need for comments.

2. Break Down Complex Logic

Complex logic can quickly become hard to follow, even with comments. Instead of relying on comments to explain convoluted code, focus on breaking down complex logic into smaller, more manageable pieces (ie. methods, classes). Each piece should have a clear purpose and be easy to understand without additional explanations.

3. Embrace Design Patterns and Best Practices

Design patterns and best practices exist for a reason - they help us write cleaner and more maintainable code. By following established patterns and best practices in your codebase, you can make it easier for other developers (and yourself) to understand the code without relying on comments.

4. Write Self-Documenting Code

Self-documenting code is code that is so clear and well-structured that it doesn't need additional comments to explain what it does. By structuring your code in a way that follows established conventions and standards, you can make it easier for others to understand the logic behind it.

5. Classes should do one thing only, and do it well

If a class needs comments it could be a code smell that this class is doing too much or is doing it in a inappropriate way. In the world of programming and software development, it is widely accepted that classes should adhere to a fundamental principle: they should have a single responsibility and excel at performing that task.  By following this principle, code becomes more maintainable, reusable, and easier to understand. When a class is focused on one specific aspect or functionality, it becomes less prone to bugs and complications. Moreover, this approach promotes modularity and allows for more flexible designs. By ensuring that classes do one thing only and do it well, developers can create robust systems that are scalable and efficient.

Conclusion

While there may be some cases where code comments are necessary or helpful, I believe that they should be used very sparingly. Relying too heavily on comments can indicate underlying issues with the code itself - it may be poorly structured or lack clarity. By focusing on writing clean and self-explanatory code, we can eliminate the need for excessive commenting and create a more maintainable and understandable codebase.

So, the next time you find yourself writing a comment, take a step back and ask yourself if there's a way to improve the code itself. Remember, code should be like a well-written story - it should tell a clear and concise tale without the need for additional explanations.

Billede

Oliver Sturm had an interesting talk on day 3 of SDD Conference. He talked about the event sourcing architecture, that can be used for highly performing and error resilient communication in a software system (applicable to both SOA and Microservice applications).

Event sourcing

Event sourcing is an architectural pattern that records all changes to an application's state as a sequence of events. These events are then stored in an event store - usually a database that is optimized for storing and querying events.

Event store

When an event is created, it is assigned a unique identifier and a timestamp. The event also includes information about the entity that was changed, the type of change that was made, and the old and new values of the entity.

The event store is typically append-only, which means that events can only be added to the end of the log. This makes it very efficient to query the event store, as only the most recent events need to be scanned.

A normal pattern is to have an endpoint that receives all requests coming in to the system and saving them in the event store. This is a database that holds all the requests that comes in to the system, so that they can be inspected at a later time if neccesary. The event store will send a message to the event bus, when the request has been saved. Normally the data fra the request is not changed in anyway ie. the data saved to the event store database is exactly the same, that is sent to the event bus.

Read model

A read model is a representation of the current state of an application that is derived from the event stream coming from the event bus (ie. the messages that was forwarded from the event store service). The read model is used to provide a consistent view of the application state to clients, even if the application has been updated multiple times since the client last requested data.

A read model will usually be responsible for a subset of the data. This could be customers, the stock in the warehouse or something completely else. This means that there will be multiple read model, each responsible for handling queries regarding a subset of the data. The read model is populated by replaying the event stream - but only the events that relate to the entities that is relevant for that read model (eg. changes to customers). This means that the event store is read from start to finish, and each event is used to update the read model.

Using VAR for failure recovery 

The event stream provides the possiblity to replay everything that happend from a certain point in time (or from the beginning if needed). This means that there's always the option to use the event messages as VAR in football, where we can replay everything that has happend over and over again (and debug it if we need to). 

An error happend due to a bug in the code, can be fixed and when the fix has been deployed, the event stream can be replayed and the data can be verified. If everything looks to be in order, we can drop the old read model database, create a new database and replay the event stream to repopulate the new database with the correct data.

Event sourcing framework for C#

The best event sourcing framework for you will depend on your specific needs and requirements. Here's a few of the most popular frameworks for C#

Axon Framework

Axon Framework is a complete event sourcing framework that provides a number of features, including event sourcing, command bus, query bus, and event store. If you are looking for a complete framework with a lot of features, then Axon Framework is a good option. 

EventStore.NET

EventStore.NET is an event store that can be used with any event sourcing framework. It provides a number of features, including append-only storage, event compaction, and event replication. If you are looking for an event store that can be used with any event sourcing framework, then EventStore.NET is a good option.

SimpleCQRS

SimpleCQRS is a lightweight event sourcing framework that is easy to use. It provides a number of features, including command bus, query bus, and event store. If you are looking for a lightweight framework that is easy to use, then SimpleCQRS is a good option.