[Editor's Note] Programming is a way of communication, excellent code is like a love letter, individualized, sincere, thoughtful, constructed using design patterns and principles, following best practices and emphasizing testing, expressing sympathy and respect for readers , is a

[Editor's note] Programming is a way of communication, excellent code is like a love letter, individualized, sincere, well thought out, constructed using design patterns and principles, following best practices and emphasizing testing, expressing our respect for readers Empathy and respect are the lasting legacy left by developers to subsequent developers.

Original text link: https://addyosmani.com/blog/good-code/

author | Daniel Chale translator | Mingming Ruyue editor | Xia Meng producer | CSDN (ID: CSDNnews)

We sometimes have programming There is an overly idealized understanding, and it is regarded as an abstract art, science, and even endowed with mysterious magic. The reality, however, is more pragmatic. Fundamentally, code is a means of communicating with . In my book "Learning JavaScript Design Patterns", I have 4 quotes from html: "High-quality code is like a love letter to subsequent developers." This is an affectionate communication across time and space, from a developer from the developer to another developer.

Code: The Language of Love

A love letter is personal, sincere, thoughtful, expressing emotion poetically, often conveying it accurately through well-chosen words and phrases. Good code also has these characteristics. It is individual because the code reflects the programmer's logical thinking and problem-solving approach. Good code needs to be sincere and avoid unnecessary complexity. It requires careful consideration, considering the next developer who interprets it. Most importantly, good code is carefully designed with the goal of solving problems in the most efficient way possible.

Design Patterns and Principles

Just as we have grammar rules and sentence structures to structure language so that emotions can be understood, we also have design patterns and principles to structure code. These patterns make the code scalable, maintainable, efficient, and easy to read and understand. Design patterns provide a shared vocabulary for developers to express complex software designs in a recognized structure.

Thus, good code uses these patterns strategically, just as a seasoned poet uses poetic techniques to create resonance. Use these patterns not just for usage, but because they enhance the value of the solution, make the code easier to understand, and ensure the durability of the code base.

SOLID (Single Responsibility Principle, Opening and Closing Principle, Liskov Substitution Principle, Interface Segregation Principle and Lai Inversion Principle English initials), DRY (Don't Repeat Yourself, emphasizing avoiding repeated logic or Information), KISS (Keep It Simple, Stupid. Emphasize that when designing and implementing software, you should try to keep it simple and straightforward and avoid over-complications), and YAGNI (You Ain't Gonna Need It. The core idea is that when writing code, don't Implement features you don't need now, and don't add complexity to features you may need in the future) are not only principles, developers also regard them as the cornerstone of writing good code. They lead developers to make informed decisions, find a balance between over-engineering and over-simplification, and ultimately, create a "love letter to code" that will be cherished by recipients.

Best Practices

Good code follows established best practices, like social etiquette when writing a love letter. Proper naming conventions, a modular design, and thorough comments are all part of it. These are not just rules that need to be followed, but also standards for measuring whether programmers are responsible to subsequent developers in the code. They exist to ensure that the original intention of the programmer is not lost in the process of understanding and passing the code.

Testing is important

Just as authors proofread their work, developers should test their code. Rigorous testing and Test Driven Development (TDD) are great hallmarks of well-written code. Testing verifies the performance of the code under various circumstances, revealing potential flaws and blind spots.A powerful testing framework is often a strong guarantee of code quality.

Empathy and respect

The most important thing is that the core of a love letter lies in empathy and respect for readers, and the same is true for excellent code. Writing code that others can read, understand, and maintain is a form of professional respect. This shows that programmers understand that their work is part of a larger, ongoing effort, that software is an evolving entity, and that many people will influence its fate over time.

Conclusion

Finally, programming is a creative activity that has something in common with writing poetry or drawing a picture. However, the beauty of our creations is not determined solely by the elegance of our algorithms or the efficiency of our code, but by the enjoyment and ease with which others can build upon our results. As developers, our job is not only to solve the problems of the present, but also to ensure that our work does not become a problem for the future.

Therefore, the good code we write is not only a love letter, but also a precious legacy we leave to subsequent developers.

Do you agree that good code is like a love letter? Have you ever seen an elegant code like a love poem? What do you think good code should look like?