What are the principles DRY, KISS and YAGNI?

Asked

Viewed 8,297 times

30

These three principles (DRY, KISS and YAGNI) are widely cited in web.

What are they? Who created them? How and where can they be applied?

  • 3

    The link of this answer is the best in all answers. Reading simplifications can cause totally wrong understanding of DRY. The same goes for the other principles. So I don’t even know if asking 3 different things is the best strategy. It would be if I were asking the relationship between them. It is a pity that the most important question was not answered directly: How they can be applied? The rest find it easy on the internet. I’m a little short of time now and I didn’t find a nice way, if you find I answer this part.

  • 1

    I answered about DRY here: http://answall.com/q/120931/101

3 answers

28


DRY - Don’t Repeat Yourself

If you use too many repetitions, you don’t have full knowledge of the subject, namely you’re stalling. Thinking about just not repeating the code doesn’t mean you are fully using the DRY, we have to work on logic too.

describe "Person#full_name" do
it "concats the first and last names" do
first_name = "John"
last_name = "Doe"
person = Person.new(:first_name => first_name, :last_name => last_name)
person.full_name.should eq "#{first_name} #{last_name}"
end
end

Above is the use of DRY philosophy and below no.

describe "Person#full_name" do
it "concats the first and last names" do
person = Person.new(:first_name => "John", :last_name => "Doe")
person.full_name.should eq "John Doe"
end
end

But second David Chelimsky (http://www.infoq.com/br/news/2012/07/DRY-acoplamento-duplicacao), following this religiously may perhaps impair the readability of the code.

YAGNI - You Ain’t Gonna Need It

This is the philosophy of You won’t need it, many times I put functions that at the time were not needed in my system, so what started to happen? People calling and asking." What is this for?". In addition to the rework of having to meet people and remove or disable the view this can bring an extra processing without need. I’ve heard a lot of people say: "What is really needed?" Here’s a tip below:

"If it’s cheap to make now and cheap later, leave it for later. If it’s cheap now, but it’ll cost a lot later, do it now."(http://seiti.eti.br/blog/2010/kiss-yagni-e-dry)

KISS - Keep It Simple, Stupid

Here where I work I have a colleague who has many years of algorithms, he developed MUMPS, His nickname is Alligator, I’ll tell him to get in the stackoverflow, I don’t know if he’ll want to, but okay. He always taught me that we should do the simple, interesting thing is to see from the client’s side, because I worked a period of my life as a support. The customer does not know what is behind a beautiful screen, he simply wants to see everything working and why complicate and invent the wheel when we can do the simple in record time and with good logic. The Alligator always talks about doing the simple, but always makes a crazy code.

Surely there will be people who disapprove of these philosophies, but my main tip is:

Anything too much, too bad.

12

These are some basic rules to make your job easier, especially when it comes to maintenance.

The meaning of each is:

  • DRY: Don’t Repeat Yourself. Do not repeat yourself. Suppose the effort to fix a bug, for a normal programmer, is X. For a programmer who copies and pastes the same code into several different parts of the program, the effort is X times the amount of copies of the same snippet.

Some people say that programming is similar to composing music. This is no excuse for putting a chorus in your code.

The two codes below, in Javascript, do the same thing:

// código escrito por um moleque, a mando de satã
var result = 2;
var temp = result;
for (var i = 0; i < 5; i++) {
    result *= temp;
}
if (result == 0) result = 1; // tratando exponenciação por zero

and

// código que não reinventa a roda, fácil de ler e que resolve o problema
Math.pow(2, 5);

In which you prefer to maintain?

  • YAGNI: You Aren’t Gonna Need It. Literally, "you won’t need this". It’s a way of saying that if a code does nothing, give it a use or delete it from the repository. I like to apply also to comments that are not useful. It means that you shouldn’t bother coding something until you really need it. If you’re coding something because stifle which you will need further, you may find that you really didn’t need it, and then the time you spent with the component was lost. (Thanks to Casali and Omni for the correction!)
  • 5

    "Kids In Satan’s Service"?!! Where did you get this? rsrs The second definition ("Keep It Simple, Stupid") is the most commonly used [in this area].

  • 1

    @mgibsonbr I also did not know this. I think it has nothing to do with programming.

  • 2

    This "Kids in Satan’s Service" is something eheh. I don’t agree with your definition of YAGNI. It doesn’t mean that if something doesn’t do anything, eliminate it, but yeah, don’t invent it has

  • 3

    Kids In Satan’s Service is a prank on an urban legend related to one of the best bands in the world. Sense of humor, developers!

  • 1

    @Renan don’t listen to their records backwards huh!

9

DRY

Don’t Repeat Yourself: It is very popular in the community and for those who develop with Ruby / Rails, it assumes the fundamental principle that one should NEVER repeat something that is already created. There enters the name (Do not repeat yourself!). That is to say, define a method, property in one place and reuse it, in any other!!. Look at the example that the wiki on the Rails page gives:

For example, instead of having a People table and a Person class, with a property, a "reader" (getter) method and a "modifier" (Setter) for each field in the table, one has only the database. The required properties and methods are "injected" into the class through Ruby language features.

KISS

I do not know the definition very well, but from what I saw it is the following. As the name itself (acronym) says you must keep it simple, Keep it simple, stupid!, (Even some authors use without the comma or with, ended up not having a standard for it! ), in short, any complexity "must" be avoided, that is, it does not have to be complex if it can be simple!

YAGNI

This principle says that you should only implement something, WHEN YOU WILL ACTUALLY USE IT, not when you think you will use it later!

The names are very suggestive and the principles are very simple, but that in the day-to-day, who manages to implement them in the right way, has a code agile, simple, effective and beautiful!! Here are the fonts (wikiiii!) for you to go into more detail on each of them!

sources:

  1. Wikipédia dry
  2. Wikipédia Kiss
  3. Wikipédia yagni

Browser other questions tagged

You are not signed in. Login or sign up in order to post.