DEV Community

Sandip Mavani
Sandip Mavani

Posted on

Basic principles that will make your programming life in 2019.

I have seen code that only the programmer who writes it understands how it works. However, when one asks the programmer to explain what the code does, he or she would only say “Only God knows that”.

That is why I would like to discuss some basic (but very powerful) principles that will make your life (and most of your collaborators’ lives) easier.

1. DRY – Don’t Repeat Yourself

You must try to maintain the behavior of the functionality of the system in a single piece of code.
On the other hand, when the DRY principle is not followed, this is known as WET solutions, which stands for either Write Everything Twice or We Enjoy Typing.
DRY programming is very useful, especially in big applications where the code is constantly maintained, changed and extended by a lot of programmers.

A common way to DRY up code is to wrap our duplicated logic with a function and replace all places it appears with function calls.

Example (with code repetition):

//flow A

let username = getUserName();
let email = getEmail();
let user = { username, email };
client.post(user).then(/*do stuff*/);
Enter fullscreen mode Exit fullscreen mode

//flow B
let username = getUserName();
let email = getEmail();
let user = { username, email };
client.get(user).then(/*do stuff*/);
Enter fullscreen mode Exit fullscreen mode

DRY Example:

function getUser(){
    return {
        user:getUserName();
        email:getEmail();
    }
}
//flow A
client.post(getUser()).then(/*do stuff*/ );
//flow B
client.get(getUser()).then(/*do stuff*/);

Enter fullscreen mode Exit fullscreen mode

2. KISS – Keep It Simple Stupid

The simpler your code is, the simpler it will be to maintain it in the future. This will be greatly appreciated by anyone else that needs to examine your code in the future.

Keeping it simple surprisingly hard. Usually when someone tries to over-engineer a solution to a problem. For example, an Architect suggests creating a Microservice framework for a simple website.

3. YAGNI – You Aren’t Gonna Need It

Sometimes, as developers, we try to think way ahead, into the future of the project, coding some extra features “just in case we need them” or thinking“we will eventually need them”. Just one word: Wrong! You didn’t need it, you don’t need it and in most of the cases… “You Aren’t Gonna Need It”.

A step brother of KISS. Part of keeping it simple is not adding modules, frameworks and dependencies we don’t actually need.

Top comments (3)

Collapse
 
kungtotte profile image
Thomas Landin

A really important part of the DRY principle is that you don't know that you need to make it DRY before it's already WET.

Until you've written both the A and B examples you don't know that you need a function. In simple examples like this you'll probably figure it out as you start writing B, but if you started out with the DRY example and only needed it once then you did that work for nothing.

Collapse
 
quantumsheep profile image
Nathanael Demacon

The YAGNI really makes me laugh, that's exactly what I do and that's exactly what happen 😂

We, developers, have issues to NOT make extra helpers functions or variables that are useless!

Collapse
 
patricktingen profile image
Patrick Tingen

Important to know that DRY is more about knowledge than code. Check this comment for a link to a /very/ interesting article