Software Design Principles

Software design refers to the way code is organized in a program, and the algorithms (procedures) used. Good software design is important if you want your code to run as speedily and efficiently as possible. It also allows your code to be easily maintained and scaled in the future.

Software design may not be the most exciting aspect of coding, and it kind of gets in the way. But if you’re serious about becoming a coder, software design principles like KISS and DRY aren’t things you want to neglect.

Here are 4 important software design principles. At the very least, you should be aware of them when you’re writing your code.

Don’t Repeat Yourself – DRY

Don’t Repeat Yourself is the principle that any code in your program should only be written once, and never duplicated. The DRY principle is one of the oldest and best-known software design principles, and employing it makes your code much easier to edit and scale. Here’s an example of the DRY principle in action.

vegetables = ['asparagus', 'broccoli', 'cabbage']

print vegetables[0], 'is a vegetable'
print vegetables[2], 'is a vegetable'

Giving the output:

asparagus is a vegetable
cabbage is a vegetable

That code is repetitive, and can be refactored (re-written while maintaining the same functionality) to this:

vegetables = ['asparagus', 'broccoli', 'cabbage']

def print_veg(index):
    print vegetables[index], 'is a vegetable'


Giving the same output:

asparagus is a vegetable
cabbage is a vegetable

The refactored version actually uses more code than the original, but it removes all the duplication. In fact, the only things that are repeated in that version of the program are the variable names vegetables and print_veg.

Why is this important? In the original version, if you wanted to change the text ‘is a vegetable’ that gets printed after the name of the vegetable, you’d have two instances to change. In the refactored version, there’s only one instance to change.

In essence, employing the DRY principle makes your code more scalable. Imagine you had hundreds of vegetable names running through your program. In which version of the program would it be easier to change the text? The refactored version, of course. Because you’d only have to rewrite the text once instead of copying and pasting it hundreds of times.

Separation of Concerns

Separation of Concerns is when you separate your program into modules that each deal with one particular focus, or concern. A concern could be a number of different things:

  • an action or process
  • a data structure
  • a property of something
  • and so on.

HTML, CSS and JavaScript are good examples of the separation of concerns, because each language deals with one particular concern. HTML deals with page structure, CSS with page styling and JavaScript with page behavior.

Here’s a more specific example of the separation of concerns in action.

def print_veg():
    print '3 Vegetables:'
    print 'asparagus', 'broccoli', 'cabbage'


Giving the output:

3 Vegetables:
asparagus broccoli cabbage

In the above program, there’s one function that handles all the work. It defines the list of vegetables, the number of vegetables and prints them out.

You’d need to manually change the count if you wanted to add a fourth vegetable, and you’d need to write the whole function again (thereby breaking the DRY rule) if you wanted to add a list of fruits. Refactor the code to this:

vegetables = ['asparagus', 'broccoli', 'cabbage']

def count_list(list):
    return len(list)

def print_list(list):
    print count_list(list), 'Items:'
    for item in list:
        print item,


Giving the similar output:

3 Items:
asparagus broccoli cabbage

Again, there’s more code but it’s worth it. The list of vegetables, the number of vegetables and the function to print them all out are now all separated and work independently of one another.

You could easily add a fourth vegetable to the list and the count would automatically update. Or, you could add a list of fruits and have the same count_list and print_list functions work with it, seamlessly.

As you can see, employing the separation of concerns principle makes your code cleaner and more scalable.

Don’t Make Me Think

This principle was originally intended for web design, being the title of a book by Steve Krug. The idea is that a web interface should be dead simple for anyone to use and navigate, and it shouldn’t require any ‘thinking’ on the user’s part.

The Don’t Make Me Think principle has been adapted for coding. Your code should be written in a way that is logical and understandable, if anyone were to read it. If it requires too much thinking, then it should probably be simplified.

The ternary operator is a classic example of this. Designed to be a ‘shortcut’ to an if statement, it gets abused by some coders in an effort to write less code. Here’s the example:

return 1 + 1 == 2 ? 'Hello' : undefined;

What? 😯 Let’s write that as an if statement instead.

if (1 + 1 == 2) {
    return 'Hello';

Oh, that makes more sense. Once again, this principle shows that less lines of code is not necessarily better. If it sacrifices readability, then it’s not worth it.

Keep it Simple

Lastly, the classic KISS principle (keep it simple stupid) is no less relevant in coding than it is anywhere else. Keeping your code simple and eliminating unnecessary complexity has many benefits:

  • less chance of bugs
  • smaller file size
  • easier to debug, modify, write and read!

Don’t underestimate the value in code readability. Another thing you can do to make your code more readable is to add comments, something which is covered in detail on the next page.

Recommended Training – Treehouse

TreehouseAlthough this site recommends various training services, our top recommendation is Treehouse.

Treehouse is an online training service that teaches web design, web development and app development with videos, quizzes and interactive coding exercises.

Treehouse's mission is to bring technology education to those who can't get it, and is committed to helping its students find jobs. If you're looking to turn coding into your career, you should consider Treehouse.

Read our full review of Treehouse…

Disclosure of Material Connection: Some of the links in the post above are “affiliate links.” This means if you click on the link and purchase the item, I will receive an affiliate commission. Regardless, I only recommend products or services I use personally and believe will add value to my readers.