What Are The Best Software Engineering Principles?
Tags: career, programming, oop, devtips
Published: 2019-01-20 10:48:48

---
title: What Are The Best Software Engineering Principles?
published: true
description: Have you ever thought about the basic rules of hygiene and safety in software engineering? 
tags: career, programming, oop, devtips
cover_image: https://thepracticaldev.s3.amazonaws.com/i/e3itrg2y69xjh0v2r1rs.jpg

---

Software Development Principles — it is a number of specific rules and recommendations, which engineers need to be followed during program implementation if they want to write beautiful, understandable and maintainable code. There is no magic wand, by which you can transform a mix of variables, classes, and functions into the ideal code, but there are some tips and hints, which can help the engineer to determine if he doing things right. 

Let's look at this base recommendations. Some of the principles below are Python-specific, but most are not.

## Measure twice and cut once

I think this is the most important principle of all. If you learn just one principle from this post - it should be this. We, ~~developers/devops/architects/managers~~ people struggle from lack of attention, from stupid mistakes and typos, from personal problems, from bad mood and cold coffee. That all is irrelevant - you need to solve a problem. For me as an engineer, this principle means **choosing the right problem to solve, choosing the right approach to the problem, choosing the right tools to solve the problem, confidence in built solution**.  

But this confidence cannot go from the code itself, it should go from [all kind of tests](https://luminousmen.com/post/ode-to-unit-tests), from [running them often](https://luminousmen.com/post/continuous-Integration-continuous-delivery), from [building the right solution for the problem](https://luminousmen.com/post/basic-architecture-post) and so on. This is Engineering as it is. I think I myself is not ready to describe it in the right words.

## Don’t Repeat Yourself ([DRY](https://en.wikipedia.org/wiki/Don%27t_repeat_yourself))

It's a pretty simple, but very useful principle, which says that repeating the same code in different places is a bad idea. This is primarily due to the need to further maintain and modify the code. If some code snippet duplicates in several places within the program then there is a high probability of two disastrous situations:

1. Making even small corrections into source code, you need to change the same code in several places. It will require additional time, effort and attention([sometimes that is not easy](https://en.wikipedia.org/wiki/The_Magical_Number_Seven,_Plus_or_Minus_Two))
2. From the first item follows the second. You or another developer from your team may accidentally miss one of the fixes and face subsequent errors in the application. That errors may be frustrating because you heard that such bug was already been fixed.

In this regard, there is a recommendation - if any code is found in the listing more than two times, then it should be placed in a separate method. This is a general recommendation, in fact, you need to think about creating a separate method, even if you meet the repetition a second time.

<ins class="adsbygoogle"
     style="display:block; text-align:center;"
     data-ad-layout="in-article"
     data-ad-format="fluid"
     data-ad-client="ca-pub-4665632381152577"
     data-ad-slot="7970039881"></ins>

## Occam’s Razor

It is a very common idea which came into programming from philosophy. The principle got its name from english monk William of Ockham. This [principle says](https://en.wikipedia.org/wiki/Occam%27s_razor): «Entities are not to be multiplied without necessity». In engineering, this principle is interpreted as follows - no need to create extra entities without the need for them. So, you always need to think first about the benefits of adding one more method/class/tool/process/etc. After all, if you add one more method/class/tool/process/etc and don't get any benefits but increasing complexity then what's the point?

## Keep It Simple Stupid ([KISS](https://en.wikipedia.org/wiki/KISS_principle))

![KISS](https://thepracticaldev.s3.amazonaws.com/i/1p7haqe0wki9x51irqno.jpg)

It is a very similar principle with the one above but it has a slightly different meaning. This principle says that code should be simple, without any complicated structures if possible, otherwise it will overcomplicate debugging and maintenance of the code. In addition, it will be more difficult for another programmer to understand the code logic, which in turn will also require additional time and effort. Therefore, always try to use simple constructions as possible which solves the problem without numerous branching, deep nesting, overengineered class structures. By doing this you will simplify life for yourself and for your colleagues because complexity breeds bugs. Remember what Pieter Hintjens said: "Simplicity is always better than functionality."

## You Aren’t Gonna Need It ([YAGNI](https://en.wikipedia.org/wiki/You_aren%27t_gonna_need_it))

A problem which a lot of programmers suffer. The desire to implement at once all necessary (and sometimes even unnecessary) functionality from the very beginning of the development process. That is when the developer from the very beginning adds all possible methods to the class and implements them, while in the future it may even never use them. Therefore, according to this recommendation, implement only what you need in the first place, and in the future, if necessary, increase the functionality. So you will save efforts, time and nerves on debugging code [that is not really needed](https://en.wikipedia.org/wiki/Worse_is_better).

## Big Design Up Front

Before starting to develop functionality it is necessary to think first about [application architecture](https://luminousmen.com/post/basic-architecture-post) and design the entire information system up to fairly small details, and only then proceed to implement according to a previously prepared plan. [The principle](https://en.wikipedia.org/wiki/Big_Design_Up_Front) has the right to exist, but recently there is quite a lot of his criticism. This is primarily due to the obsolescence of the plan during design and development. In this connection, you still have to make subsequent changes. But it also has irrefutable advantages, with the proper design you can significantly reduce the cost of further debugging and fixing bugs. In addition, such information systems are usually more concise and architecturally correct.

## Avoid Premature Optimization

>"Premature optimization is the root of all evil (or at least most of it) in programming" - Donald Knuth

Optimization is a very right and necessary process that allows you to speed up the work of the program, as well as reduce the consumption of system resources. But everything has its time. If optimization is carried out in the early stages of development, then **it can do more harm than good**. This is primarily due to the fact that the development of optimized code requires more time and effort of the developer and it may be more complex. In this case, quite often it is necessary to first verify the correctness of the chosen development approach. Therefore, at first, it is more profitable to use simple, but not the most optimal approach. And in the future, assessing how much this approach slows down the work of the application, move to a faster or less resource-intensive algorithm. In addition, for the time while you initially implement the most optimal algorithm, the requirements may change and the code will go to the garbage. So there is no need to spend time on premature optimization.

## Principle Of Least Astonishment

This principle means that your code should be intuitive and obvious, and not surprise another developer during code review. For example, if the method called "make cookies", but as a result, you get a potato than this code is bad (obviously).

<ins class="adsbygoogle"
     style="display:block; text-align:center;"
     data-ad-layout="in-article"
     data-ad-format="fluid"
     data-ad-client="ca-pub-4665632381152577"
     data-ad-slot="7970039881"></ins>

## S.O.L.I.D.

![SOLID](https://thepracticaldev.s3.amazonaws.com/i/agulsg1y21onynqekm7b.jpg)


“[SOLID](https://en.wikipedia.org/wiki/SOLID)” is actually a group of object-oriented design principles. Each letter in “SOLID” represents one of the principles, which are:

* [Single responsibility](https://en.wikipedia.org/wiki/Single_responsibility_principle) states that every module or class should have responsibility for a single part of the functionality provided by the software and that responsibility should be entirely encapsulated by the class;
* [Open-closed](https://en.wikipedia.org/wiki/Open%E2%80%93closed_principle) states that software entities (classes, modules, functions, etc.) should be open for extension, but closed for modification;
* [Liskov substitution](https://en.wikipedia.org/wiki/Liskov_substitution_principle) states that the inherited class should complement, not replace, the behavior of the base class;
* [Interface segregation](https://en.wikipedia.org/wiki/Interface_segregation_principle) states that no client should be forced to depend on methods it does not use;
* [Dependency inversion](https://en.wikipedia.org/wiki/Dependency_inversion_principle) says that programmer should work at the interface level and not at the implementation level.

When applied together, these principles help a developer create code that is easy to maintain and extend over time.

## Law of Demeter

The main idea of [this principle](https://en.wikipedia.org/wiki/Law_of_Demeter) is to divide the areas of responsibilities between classes and encapsulate the logic inside the class, method or structure. From this principle we can highlight several recommendations:

1. Classes or entities should be independent
2. Need to try to reduce the number of connections between different classes(aka [coupling](https://en.wikipedia.org/wiki/Coupling_(computer_programming)))
3. Related classes should be in the same module/package/directory (aka [cohesion](https://en.wikipedia.org/wiki/Cohesion_(computer_science)))

By following this principle, the application becomes more flexible, understandable and maintainable.

## Conclusion

**Fellow developers let’s be engineers!** Let’s think about design and build robust and well-implemented systems, rather than growing organic monsters. Listed principles are highly correlated and connected in there essence. Of course, I didn't create them, but a small reminder does not hurt, at least my memory is definitely not perfect.

------

**Thank you for reading!**

Any questions? Leave your comment below to start fantastic discussions!

Check out [my blog](https://luminousmen.com/) or come to say hi 👋 on [Twitter](https://twitter.com/luminousmen) or subscribe to [my telegram channel](https://t.me/iamluminousmen).
Plan your best!