Good Code is a love letter to the next developer who will maintain it

Please scroll moderately to fully experience the animations

We often romanticize the notion of programming, presenting it as an abstract form of art, a science, or even a form of magic. The truth, however, is much more practical and grounded.

Code, in its essence, is communication.

At the start of my book, Learning JavaScript Design Patterns, I say "good code is like a love letter to the next developer who will maintain it". It is an intimate correspondence, from one developer to another, spanning time and space.

LanguageOf Love

A love letter is personal, sincere, and considerate. It's a poetic testament of feelings, often meticulously crafted, with the intent to convey emotions accurately. Good code shares these characteristics. It's personal, as it mirrors the logic and approach of the coder.

Good code is sincere and unadorned with unnecessary complexity. It's considerate, mindful of the next developer who will decipher it.

And above all, it's meticulously crafted to solve a problem with utmost efficiency.

Patterns andPrinciples

Just like we have grammar rules and linguistic structures to frame our words and feelings into comprehensible sentences, we have design patterns and principles to shape our code.

Patterns don't just make code scalable, maintainable, and efficient, but also readable and understandable. They provide a shared vocabulary for developers, enabling them to express intricate software designs with universally recognized structures.

Good code, therefore, leverages these patterns strategically, just like a seasoned poet would use poetic devices to create resonance. It does not apply patterns just for the sake of it, but because they add value to the solution, they make the code more comprehensible, and they ensure the longevity of the codebase.

SOLID, DRY, KISS, and YAGNI are not merely principles but are cornerstones of crafting good code. They guide a developer to make wise decisions, balance between under and over-engineering, and ultimately, write a 'love letter' that the receiver cherishes.

CodeEtiquette

Good code makes sure the intent of the coder is not lost in translation.

It also adheres to established best practices, much like a love letter would follow certain social etiquettes. Proper naming conventions, modularization, and thorough commenting are all part of this.

They are not just rules to be followed, but they are the norms that define how considerate the code (or coder) is towards the next developer.

RefactoringTargets

In the world of love letters, one doesn't always get it right on the first draft.

There's a beauty in revisiting and refining your words, ensuring they convey the right message. Similarly, in coding, refactoring is an art.

It’s about revisiting existing code and improving its structure, readability, and performance without changing its behavior. Good developers understand that code, like a love letter, is a living document, evolving and improving over time.

They embrace refactoring as a vital part of the software development lifecycle, ensuring their ‘love letter’ remains relevant and resonant.

Beautyremains

In the end, coding is an act of creation, not unlike writing a poem or painting a picture. The beauty of our creations, however, is not judged solely by the elegance of our algorithms or the efficiency of our code, but by the joy and ease with which others can build upon our work.

As developers, our task is not just to solve today's problems but also to ensure we do not become tomorrow's problem.

Good code, therefore, is not just a love letter, but it is our lasting legacy to those who follow us.

JavaScriptDesignPatterns

In 2023, I wrote "Learning JavaScript Design Patterns: The Second Edition". The book focuses on how to write beautiful, structured, and maintainable JavaScript and React code by applying modern (pragmatic) design patterns to the language. You also learn about performance and rendering patterns such as the different kinds of server-side rendering and islands architecture. The First Edition of the book was read over 5 million times online over a decade and I hope the second physical edition is an interesting follow-up.

APragmaticPerspective

Design patterns, as conceptualized in the seminal "Design Patterns: Elements of Reusable Object-Oriented Software" by the "Gang of Four" (GoF), are generalized, reusable solutions to common problems encountered in software design. They provide a vocabulary for developers to discuss, critique, and improve software architecture.

The Evolution of Language Features

One argument against traditional design patterns is the evolution of programming languages. For instance, the adoption of higher-order functions in JavaScript and React may reduce the need for complex strategies to manage behavior variation, a role traditionally filled by patterns like Strategy or Command. Even in languages with advanced features, the need for structured ways to address common architectural problems persists.

Functional Programming and New Patterns

Functional programming has indeed introduced new patterns and best practices. Concepts like immutability, pure functions, and higher-order functions are now integral to modern software design. These concepts can coexist with, and sometimes enhance, traditional design patterns.

Pragmatism in Applying Design Patterns

The key to effective use of design patterns lies in pragmatism. Developers should avoid over-engineering and the temptation to fit a problem into a predefined pattern. Instead, focus on the specific problem at hand and consider if a pattern genuinely adds clarity and value to the solution. The goal should not be to use patterns for their own sake, but to write clearer, more maintainable code.

Trade-offs

Avoid dogma

While design patterns offer undeniable advantages, it's crucial to avoid their dogmatic application. Over-engineering complex solutions for simple problems can lead to unnecessary bloat and hinder performance. The key lies in pragmatism. Approach design patterns as a toolbox, not a rulebook. Choose the right tool for the job, considering the project's specific needs, complexity, and resource constraints.

Design Patterns: Cons

One could argue that design patterns have become obsolete due to the following reasons:

Design Patterns: Pros

Despite the criticisms, design patterns can still hold significant value:

Move Forward with Nuance

Rather than adopting a binary stance, a more nuanced approach to design patterns is necessary:

Writing Code for Two Audiences: Machine and Human

For generations, software engineers have toiled with the dual task of crafting code that both the machine and another human could understand. This delicate balancing act between precision and clarity has been at the heart of good software design. Today, with the rise of AI-powered tools like code generation and large language models (LLMs), the landscape is shifting. While these tools promise increased productivity and efficiency, they also raise questions about the future of software design principles like design patterns.

The Human Element: A Legacy of Readability

The core tenets of good code have always prioritized human understanding. Techniques like descriptive variable names, proper indentation, and clear comments aim to make code more legible and maintainable for future developers. This focus on human readability ensures smoother collaboration, faster debugging, and easier code reuse.

While AI tools can generate code quickly and efficiently, they often prioritize functionality over readability. This can lead to code that is difficult to understand and maintain, ultimately negating the initial gain in productivity.

The Role of Design Patterns in the Age of AI

Design patterns have long been considered cornerstones of good software design. They offer proven solutions to recurring problems, promoting code reuse and maintainability. However, with AI-powered tools capable of suggesting or even generating code that implements these patterns, their relevance might appear diminished.

This, however, is a misinterpretation. AI tools, while capable of generating code based on design patterns, cannot replace the human understanding and decision-making required to apply these patterns effectively. More importantly, AI cannot yet grasp the nuances of context, intent, and future development needs that are crucial for choosing the right pattern and adapting it to specific situations.

The Future: Collaboration, not Replacement

The rise of AI in software development is not a cause for alarm, but rather an opportunity for evolution. Instead of replacing the human developer, AI tools should be viewed as collaborators, capable of automating tedious tasks and freeing up time for higher-level thinking.

Instead of obsessing over whether AI will render design patterns obsolete, we should focus on how AI can enhance their implementation and application. For example, AI can help us discover new patterns, tailor existing ones to specific contexts, and generate code that adheres to specific design principles.

This collaboration between human and machine holds immense potential for improving the quality, efficiency, and maintainability of software. The future belongs to developers who can leverage AI tools while remaining grounded in the principles of good design, human readability, and a deep understanding of software architecture.

AI will augment how we write good code

While AI tools are revolutionizing the way we write code, they should not be seen as a replacement for the human element in software development. The ability to write code that is both readable and efficient remains a critical skill for developers. By embracing AI as a collaborator and focusing on understanding the nuances of software design, we can create code that is both powerful and enduring.

This future is not about surrendering to AI, but rather about embracing a new era of human-machine collaboration, where developers can focus on creativity, innovation, and problem-solving, leaving the tedious tasks to their AI partners. Together, humans and AI have the potential to unlock a new chapter of software development, one that is more efficient, more productive, and more human-centered than ever before.

Code with clarity, let simplicity reign,
Where each line has a purpose, nothing in vain.
In great simplicity, true genius is found,
Where functionality and readability abound.

PracticalElegance

Build with foresight, for maintainability is key,
A codebase that's robust, yet easy to oversee.
Let each design pattern be a thoughtful choice,
Giving structure to chaos, a clear, unified voice.

Balance in Design, Function over Form, Coding not just for today, But for storms yet unborn.

Embrace refactoring as a path to improvement,
Where old code gains new life, in continuous movement.
It's not just about adding, but also about pruning,
For a cleaner, more efficient, future-proof code tuning.

Good CodeIs ALove Letter

So code with purpose, let pragmatism guide,
In each decision, let wisdom and insight coincide.
For in the realm of code, true artistry lies,
Not in complexity, but in smart, effective design.

Buy the book

The Design Patterns Book

Learning JavaScript Design Patterns Cover

Learning JavaScript Design Patterns: A JavaScript and React Developer's Guide 2nd Edition

★★★★★

"A clear and straightforward must-read book to understand how to use the latest features of JavaScript and React to develop modern and sustainable web applications."

- Antonio Perrone

★★★★★

"This is a must-read for frontend engineers!"

- Erik Alonso Ahumada Sarmiento

★★★★★

"Must read for all experience levels!"

- Rizvi Iqubal

Praise for the Book

"Addy's book is a pragmatic approach to fundamental concepts that are useful across myriad uses. JavaScript Design Patterns are intrinsic to developers applying systems thinking to their own work. Any developer reading this book will see incredible returns on the time investment reading it."

sarah

Sarah Drasner, Director of Engineering at Google leading Core Web Dev

"Addy Osmani strikes again, and he has delivered a much-appreciated update to his already classic book on design patterns in JavaScript."

stoyan

Stoyan Stefanov, WebPageTest.org engineer and author of JavaScript Patterns

"This week, I started reading Learning JavaScript Design Patterns by Addy Osmani... As a part of honing your craft, I advocate that engineers should know, study, and practice design patterns... Osmani starts Chapter 1 with a statement that resonated deeply with me: 'Good code is like a love letter to the next developer who will maintain it.'"

alex

Alex Dennis, Senior Software Engineer at Amazon

About the Book

Author Addy Osmani shows you how to apply modern design patterns to JavaScript and React—including modules, mixins, observers, and mediators. You'll learn about performance and rendering patterns such as server-side rendering and Islands architecture. You'll also learn how architectural patterns like MVC, MVP, and MVVM are useful from the perspective of a modern web application developer.

This book explores:

  • Architectural patterns for structuring your components and apps
  • More than 20 design patterns in JavaScript and React, applicable for developers at any level
  • Different pattern categories including creational, structural, and behavioral
  • Essential performance patterns including dynamic imports and code-splitting
  • Rendering patterns such as server-side rendering, hydration, Islands architecture, and more

Additionally, you'll explore modern JavaScript syntax like JavaScript modules, React patterns like Hooks, higher-order components (HOCs), and more, to stay ahead in the ever-evolving world of web development.

Photos from fans of the book

 

From the Preface

The world of JavaScript has come a long way since I wrote the first edition of Learning JavaScript Design Patterns over 10 years ago. At that time, I was working on large-scale web applications and found that the lack of structure and organization in JavaScript code made it difficult to maintain and scale those applications.

Fast forward to today, and the web development landscape has changed dramatically. JavaScript has become one of the most popular programming languages in the world and is used for everything from simple scripts to complex web applications. The JavaScript language has evolved to include modules, promises, and async/await, which has heavily influenced how we architect applications. The way developers write components, such as with React, has also significantly impacted how they think about maintainability. This has resulted in the need for modern patterns that take these new changes into account.

With the rise of modern libraries and frameworks like React, Vue, and Angular, developers are now building applications that are more complex than ever before. I recognized the need for an updated version of Learning JavaScript Design Patterns to reflect the changes in JavaScript and web application development.

In this second edition of Learning JavaScript Design Patterns, I aim to help developers apply modern design patterns to their JavaScript code and React applications. The book covers more than 20 design patterns essential for building maintainable and scalable applications. The book is not just about design patterns but also about rendering and performance patterns, which are critical to the success of modern web applications.

The first edition of this book focused on classical design patterns, such as the Module pattern, the Observer pattern, and the Mediator pattern. These patterns are still important and relevant today, but the web development world has evolved significantly in the past decade, and new patterns have emerged. This new edition covers these new patterns, such as promises, async/await, and the newer variations of the Module pattern. We also cover architectural patterns such as MVC, MVP, and MVVM and discuss where modern frameworks fit with these architectural patterns.

Today’s developers are exposed to many library-specific or framework-specific design patterns. React’s matured ecosystem and utilization of newer JS primitives provide an excellent launchpad to talk about best practices and patterns in the framework or library context. In addition to classic design patterns, this book covers modern React patterns, such as Hooks, Higher-Order Components, and Render Props. These patterns are specific to React and essential for building modern web applications using this popular framework.

This book is not just about patterns; it’s also about best practices. We cover topics such as code organization, performance, and rendering, which are crucial for building high-quality web applications. You will learn about dynamic imports, code-splitting, server-side rendering, hydration, and Islands architecture, all of which are essential for building fast and responsive web applications.

By the end of this book, you will have a deep understanding of design patterns and how to apply them to your JavaScript code and React applications. You will also know which patterns are relevant to the modern web and which are not. This book is not just a reference for patterns; it’s also a guide to building high-quality web applications. You will learn how to structure your code for maximum maintainability and scalability and how to optimize your code for performance.

"I have already looked through this book and it includes great content. Thank's, Amazon, for delivering it!"

★★★★★

- Nazar Kovtun

"I've read so many good things about the first edition by Addy Osmani so I know the second one can only be better."

★★★★★

- Andrea M

"Loved the first one and this one is just as great 💪🏾"

★★★★★

Jose

"Great holiday read on JS and React fundamentals."

★★★★★

"Finally, I have the book Learning JavaScript Design Patterns, by Addy Osmani. I want to prepare a Rendering Patterns talk, and I love to see that there is a chapter talking about it."

★★★★★

Joan Leon

"Very glad to have in my hands the second edition of 'Learning JavaScript Design Patterns' by the great Addy"

★★★★★

Andrés Valle

About Addy Osmani

Addy Osmani is an Engineering Leader working on Google Chrome. He leads Chrome's Developer Experience teams, helping to keep the web fast and delightful to build. Addy has authored several open-source projects as well as a number of books including Learning Patterns, Learning JavaScript Design Patterns, and Image Optimization. His personal blog is addyosmani.com.