KISS, DRY, Law of Demeter - why do we need design heuristics?

By Alexandra Leshanu

Creating maintainable, scalable, and efficient code is always a challenge for software engineers. To address this challenge, they often rely on design heuristics, a set of guiding principles that help to implement code architecture and design. These heuristics are not strict rules or algorithms, but rather general guidelines based on experience and best practices. Heuristics don't guarantee a perfect solution in every situation, they offer valuable insights and principles that developers can apply to make design decisions.

The focus of my essay is to review design heuristics KISS (Keep It Simple, Stupid), DRY (Don't Repeat Yourself), and the Law of Demeter and to highlight the aspects that, in my opinion, are present while using them and which seem to me to be the most important.

In the ever-evolving software development area, the Keep It Simple, Stupid (KISS) principle stands out as a fundamental design heuristic, which contains all other design principles. This principle states that there is no value in a solution being “clever” – there is value in it being easily understandable for all. It sounds simple, but following the statement improves plenty of software developing aspects, which are provided below.

Complexity Management

Complexity is the primary challenge in software development, so the KISS principle helps manage and reduce the complexities associated with coding, making the software more understandable and maintainable in general.

Better Readability and Code Understanding

The readability of code is crucial in collaborative development environments. According to Robert C. Martin’s “Clean Code”, code is read far more often than it is written. KISS principle helps to provide clear, concise, and easily comprehensible code, promoting better collaboration among team members and reducing errors.

Maintenance and Debugging

KISS plays a pivotal role in easing the burden of maintenance and debugging. Every line of code is a potential bug. Simple, well-structured code, as advocated by KISS, minimizes the surface area for bugs and facilitates quicker identification and resolution during maintenance.

Agile & Adaptability to Change

In a dynamic development environment where requirements always change, adaptability is key. The simplicity supported by KISS aligns with the agile principle.

Efficiency and Performance

The KISS principle prevents unnecessary complexity that can affect system performance. In “Clean Code”, Robert C. Martin argues that a clean and simple design often leads to better performance. Following KISS, developers can create software that not only functions correctly but also does so in an optimized way.

In software development, the Don't Repeat Yourself (DRY) principle stands as a design heuristic which is promoting the elimination of redundancy in code. DRY urges developers to write code once and only once, promoting code reusability and maintainability. If the code is not duplicated, then making corrections in just one place is enough to change the logic. Following the DRY principle improves many represented below software developing aspects.

Code Reusability

At the core of the DRY principle is that reuse is a fundamental aspect of building maintainable and efficient software. Writing the same logic multiple times not only increases the chance of errors, but also the ability to update code consistently. Following the DRY principle always leads to the decomposition of complex algorithms into simple functions.

Maintainability

The maintenance phase of software development is where DRY truly shines, because the true cost of software is in its maintenance. By adhering to DRY, developers minimize the effort required to make changes or updates and reduce the risk of introducing new bugs.Testing software code in general also becomes easier.

Readability

In my opinion, DRY contributes to improved code readability. When functionality is repeated, it becomes harder to understand the purpose and behavior of the code.

Following the Don't Repeat Yourself (DRY) principle in large projects is not as easy as it may seem at first glance. Developers are required to carefully plan the architecture, and an architect or team leader is required to have a vision of the system as a whole and a clear statement of tasks to developers, which might be a disadvantage for some teams. This approach also overlaps with the principle of sole responsibility of the five SOLID principles formulated by Robert Martin.

The Law of Demeter, often referred to as the “Principle of Least Knowledge,” is another foundational design heuristic in software development. This principle advocates for loose relations (loose coupling) between objects, emphasizing that a module should have limited knowledge about the internals of other modules. From the side of building the program code, it recommends that objects should avoid accessing the internal data and methods of other objects. In other words, an object should not “reach through” its collaborators to access their collaborators’ data, methods, or collaborators. It helps to achieve some advantages, which are represented below.

Maintainability

The Law of Demeter contributes significantly to the maintainability of software systems, because developers minimize the impact of modifications, making it easier to maintain and update individual modules without affecting the entire system.

Better Code Understandanding

By adhering to the Law of Demeter, developers create code and the entire system is more understandable and readable, as the interactions between objects are explicitly defined and limited.

Collaboration

Law of Demeter helps to create clear interfaces between components, which is useful for teams and companies to work more independently. This fosters collaboration by allowing teams to focus on their assigned modules without needing an in-depth understanding of the entire system.

Modularity and Encapsulation

At the heart of the Law of Demeter is the promotion of modularity and encapsulation. By limiting the interactions between modules, the principle supports the creation of self-contained, independent components, which is well-known good practice.

In conclusion, analysis shows that all the design heuristics: KISS, DRY, and the Law of Demeter, stands as a powerful base for guiding software developers toward the creation of reliable maintainable, and adaptable systems. KISS promotes simplicity to combat complexity, DRY advocates for efficiency through code reuse, and the Law of Demeter fosters modularity and resilience. Collectively, these design heuristics form a holistic approach to software development.

1) Clean Code by Robert C. Martin (2008)

2) KISS — принцип проектирования, содержащий все остальные принципы проектирования, Habr. https://habr.com/ru/articles/249639/

3) Don't repeat yourself, Wikipedia. https://en.wikipedia.org/wiki/Don%27t_repeat_yourself

4) Принцип программирования DRY — don’t repeat yourself / не повторяйте себя. https://web-creator.ru/articles/dry

5) Law of Demeter, Wikipedia. https://en.wikipedia.org/wiki/Law_of_Demeter