---
title: "Software Engineering Principles Every Developer Should Know"
description: "The software engineering principles every developer should know: DRY, KISS, and YAGNI. What each one asks of you, and Python examples of the same code before and after applying them."
author: "Mohammad Abu Mattar"
canonical: https://mkabumattar.com/blog/post/software-engineering-principles-every-developer-should-know
---

# Software Engineering Principles Every Developer Should Know

Some software engineering principles hold up no matter what stack you're using. They guide you toward maintainable, efficient code. Here's a look at why every developer should know them.

## What is the DRY principle, and why is it important?

**DRY (Don't Repeat Yourself)** is about writing a piece of logic once and reusing it.

- Avoid code duplication: repeating the same code in multiple places increases the risk of errors and makes maintenance harder.
- Modularize code: break functionality into reusable modules or functions, which cuts duplication and keeps behaviour consistent.

Here's a common example in Python that doesn't adhere to the DRY principle:

```python title="without_dry_principle.py"
def create_user_profile(user_id, name, email):
    profile = {
        "id": user_id,
        "name": name,
        "email": email,
        "welcome_message": f"Welcome {name}! Your email is {email}."
    }
    print(f"Creating profile for {name} with email {email}")
    return profile

def send_welcome_email(name, email):
    message = f"Hello {name}, welcome to our platform! Please verify your email: {email}."
    print(f"Sending email to {email}: {message}")
```

The above code repeats the process of constructing welcome messages. Let's refactor it to adhere to the DRY principle:

```python title="with_dry_principle.py"
def format_welcome_message(name, email):
    return f"Hello {name}, welcome to our platform! Please verify your email: {email}."

def create_user_profile(user_id, name, email):
    profile = {
        "id": user_id,
        "name": name,
        "email": email,
        "welcome_message": format_welcome_message(name, email)
    }
    print(f"Creating profile for {name} with email {email}")
    return profile

def send_welcome_email(name, email):
    message = format_welcome_message(name, email)
    print(f"Sending email to {email}: {message}")
```

By creating a single function to format welcome messages, we eliminate redundancy and improve maintainability.

## How does the KISS principle improve software development?

**KISS (Keep It Simple, Stupid)** advocates for simplicity in design and implementation.

- Clarity and readability: simple code is easier to understand, debug, and maintain.
- Reduce complexity: avoid over-engineering by choosing straightforward solutions over unnecessarily complex ones.

Consider the following Python code snippet for logging user activities:

```python title="complex_user_logging.py"

def log_user_activity(user_id, activity):
    logging.basicConfig(level=logging.DEBUG, format='%(asctime)s %(message)s')
    logger = logging.getLogger()
    log_message = f"User {user_id} performed {activity}."
    if activity == 'login':
        logger.debug(log_message)
    elif activity == 'logout':
        logger.debug(log_message)
    elif activity == 'error':
        logger.error(log_message)
    else:
        logger.info(log_message)
```

The above code is more complex than necessary. Let's simplify it:

```python title="simple_user_logging.py"

logging.basicConfig(level=logging.DEBUG, format='%(asctime)s %(message)s')
logger = logging.getLogger()

def log_user_activity(user_id, activity):
    log_message = f"User {user_id} performed {activity}."
    logger.log(logging.DEBUG if activity in ['login', 'logout'] else logging.INFO, log_message)
```

By using a more straightforward approach, we keep the same behaviour and the code is easier to read.

## What does YAGNI mean in software development?

**YAGNI (You Aren't Gonna Need It)** encourages developers to avoid adding functionality prematurely.

- Focus on requirements: implement only the features that are currently needed, not the speculative ones.
- Avoid over-engineering: when you build only what is needed, there is less complexity and less room for bugs.

Consider the following Python code snippet for handling user permissions:

```python title="over_engineered_permissions.py"
def get_user_permissions(user_role, has_admin_rights, is_super_user, is_active):
    if not is_active:
        return "No permissions"
    if is_super_user:
        return "All permissions"
    if has_admin_rights:
        return "Admin permissions"
    if user_role == "editor":
        return "Edit permissions"
    if user_role == "viewer":
        return "View permissions"
    return "No permissions"
```

This code over-engineers the permissions logic. Let's simplify it by focusing on essential functionality:

```python title="simple_permissions.py"
def get_user_permissions(user_role):
    permissions = {
        "super_user": "All permissions",
        "admin": "Admin permissions",
        "editor": "Edit permissions",
        "viewer": "View permissions"
    }
    return permissions.get(user_role, "No permissions")
```

By adhering to the YAGNI principle, we eliminate unnecessary complexity and focus on core requirements.

## Conclusion

Understanding and applying principles like DRY, KISS, and YAGNI makes a real difference in code quality and maintainability. They push you toward code reuse, simplicity, and building only what you actually need.

## References

1.  "Don't repeat yourself." Wikipedia, [https://en.wikipedia.org/wiki/Don%27t_repeat_yourself](https://en.wikipedia.org/wiki/Don%27t_repeat_yourself)
2.  "KISS principle." Wikipedia, [https://en.wikipedia.org/wiki/KISS_principle](https://en.wikipedia.org/wiki/KISS_principle)
3.  "You ain't gonna need it (YAGNI)." Wikipedia, [https://en.wikipedia.org/wiki/You_aren%27t_gonna_need_it](https://en.wikipedia.org/wiki/You_aren%27t_gonna_need_it)
4.  Fowler, Martin. "Yagni." MartinFowler.com, [https://martinfowler.com/bliki/Yagni.html](https://martinfowler.com/bliki/Yagni.html)
5.  "SOLID Principles for C# Developers" - Atree (While C#-focused, SOLID principles are related and often discussed alongside DRY, KISS, YAGNI.), [https://www.atree.com.au/insights/solid-principles-for-c-developers/](https://www.atree.com.au/insights/solid-principles-for-c-developers/)
6.  "Refactoring Guru: Code Smells." (Discusses issues often solved by applying these principles.), [https://refactoring.guru/smells](https://refactoring.guru/smells)
