sentenz / convention

General articles, conventions, and guides.
https://sentenz.github.io/convention/
Apache License 2.0
4 stars 2 forks source link

Create an article about `Four Rules of Simple Design` of `Software Design Principles` #363

Open sentenz opened 3 days ago

sentenz commented 3 days ago

Four Rules of Simple Design

The Four Rules of Simple Design articulated by Kent Beck to guide developers toward creating efficient and maintainable code.

The principles are integral to practices such as Extreme Programming (XP) and Test-Driven Development (TDD), where they provide a framework for creating software that is both functional and adaptable to change.

By implementing these principles, the resulting code is robust (passes tests), comprehensible (reveals intention), streamlined (no duplication), and efficient (fewest elements).

Category

The rules are sequenced by priority, for instance, ensuring that the software passes all tests takes precedence over eliminating duplication. By diligently applying these principles, developers can craft systems that are not only functional but also resilient and straightforward to maintain.

Passes the Tests

Foremost, the software must satisfy all specified requirements and successfully pass all automated tests. This ensures the system functions as intended and provides a safety net for future modifications.

  1. Examples and Explanations
def add_numbers(a, b):
    return a + b

# Test case
def test_add_numbers():
    assert add_numbers(2, 3) == 5
    assert add_numbers(-1, 1) == 0
    assert add_numbers(0, 0) == 0

test_add_numbers()
print("All tests passed.")

Reveals Intention

The code should clearly express its purpose, facilitating comprehension and ease of maintenance. Utilizing meaningful names and straightforward logic enhances readability, enabling developers to grasp the system's functionality without extensive interpretation.

  1. Examples and Explanations
# Ambiguous
def calc(x, y):
    return x * y

# Clear and revealing intention
def calculate_area_of_rectangle(length, width):
    return length * width

No Duplication

Eliminating redundant code is crucial, as duplication can lead to inconsistencies and increased maintenance efforts. By adhering to the Don't Repeat Yourself (DRY) principle, developers ensure that each piece of knowledge within the system has a single, unambiguous representation.

  1. Examples and Explanations
# With duplication
def calculate_circle_area(radius):
    return 3.14 * radius * radius

def calculate_sphere_volume(radius):
    return (4/3) * 3.14 * radius * radius * radius

# Without duplication
import math

def calculate_circle_area(radius):
    return math.pi * radius**2

def calculate_sphere_volume(radius):
    return (4/3) * math.pi * radius**3

Fewest Elements

The design should incorporate the minimal number of components necessary to meet the system's requirements. This minimalist approach reduces complexity, making the codebase more understandable and adaptable to change.

  1. Examples and Explanations
# Complex and unnecessary elements
def process_data(data):
    cleaned_data = []
    for item in data:
        if isinstance(item, int):
            cleaned_data.append(item * 2)
        elif isinstance(item, float):
            cleaned_data.append(item / 2)
    return cleaned_data

# Simplified version
def process_data(data):
    return [item * 2 if isinstance(item, int) else item / 2 for item in data if isinstance(item, (int, float))]

References