+ 6

What's the DRY and WET principles

How can a code be DRY or WET (examples please )

23rd Dec 2019, 1:07 PM
Pattern
Pattern - avatar
11 Answers
+ 10
D.R.Y.= Do not Repeat Yourself W.E.T.= Write Every Time And rest is beautifully explained by HonFu
23rd Dec 2019, 3:04 PM
Arsenic
Arsenic - avatar
+ 7
It's specific to the task you're doing, so we could give a million examples. The main idea is to not write something twice when you can also just write it once. It's easier to read and - depending on how you do it - might perform better. Instead of print(1) print(2) ... print(10) just write for i in range(1, 11): print(i) Generally, I'd add, that you should write efficiently and not waste statements you don't need. So instead of inp = input() a = int(inp) b = 2*a print(b) just write print(int(input() * 2)
23rd Dec 2019, 1:22 PM
HonFu
HonFu - avatar
+ 5
DRY and WET should be implemented side by side and are not complete opposites. For examle it really doesnt make sense to write a new function for a routine that is only performed once or twice. But if it significantly improves readability (such as when this routine is very long) and makes it easier to update your program then it really helps to make new functions.
23rd Dec 2019, 10:02 PM
Oluwaseun Familusi
Oluwaseun Familusi - avatar
+ 2
Probably DRY is usually taught as an ideal to beginners because they're more likely to 'Write Everything Twice'. They first have to learn to be efficient in general, and later, they learn to recognize the situation where it's simpler or quicker or more performant to just write something a second time.
24th Dec 2019, 12:47 AM
HonFu
HonFu - avatar
+ 2
JoeSponge, I can empathize with that: When you enter the recursion, eternally googling stuff, and end up knowing less instead of more, that's not so pretty. So rest assured: This is nothing you have to fully understand down to the tiniest nuts and bolts, you can let go of it. Just remember the essence, when coding (which is sort of obvious): Try not to babble.
31st Dec 2019, 7:31 PM
HonFu
HonFu - avatar
+ 1
This was an interesting title, but the content was too abstract for me. I now know what WET and DRY "mean"* - besides looking like interestingly related TLAs**... but I don't know what they MEAN. "DRY" - from the name, gives a hint to what it means, but it doesn't do it justice. "Don't Repeat Yourself" What does that mean? "Don't repeat yourself." But what does that mean? It means, "don't repeat yourself". Can you give me an example? "..." You didn't SAY anything. "I also didn't repeat myself." WET - Write Every Time* (?) Hunh? I'm going to _guess_ that it means "to the greatest extent possible, make sure that every statement actually DOES SOMETHING meaningful..." Meaning, in my mind, instead of working towards the goal, a = 'Starting at $72' b = a.split(' ') c = b[-1] d = c.strip('a-zA-Z!@#$%^&*_:;') e = int(d) go for the gusto - skip the middle men - grab the end result as early as possible: f = int('Starting at $72'.split(' ')[-1].strip('a-zA-Z!@#$%^&*_:;')) If that's what WET means***, then it's useful for Code Golf, but not a good practice for someone who's beginning to learn a language. That requires too many context shifts for me to grok it in a single pass. What is saved by "nifty" is lost by the time it requires to scan, understand, break it out to try the individual parts and troubleshoot it if it's wrong. Lastly, **** * in trying to figure this out, I had to leave SL and hit up my good friend DuckDuckGo... and he told me I was wrong. WET, to some, might mean Write Every Time, but it also may be "Write Everything Twice", "We Enjoy Typing", "Waste Everyone's Time". ** TLA - Three Letter Acronym, a recursive definition where the acronym that the name refers to is, in itself, what the definition refers to. The reference to "TLA" implies that people are relying too much on acronyms. *** I found defs to DRY, pro and con. I found refs to WET, but no good definitions or examples, just the implication that WET is bad. **** I was wrong. Waterlogged Mummies.
31st Dec 2019, 5:01 PM
JoeSponge
JoeSponge - avatar
+ 1
JoeSponge, up there, I gave a few very simple and unabstract examples for the difference between DRY and WET, right at the top. Have you read them? I get the impression you're overthinking this! It's a really simple concept: If you can write a piece of code once instead of twice or thrice, you normally would do it. That's just common sense. Just like you don't go to the supermarket for one item, several times a day - but instead you go only once and buy all of it. Martin, then, pointed out, that a program sometimes runs more quickly if you *do* write out stuff several times. That's a technical detail a beginner doesn't have to think about. And that's already all there is to it. Some funny dude invented the abbreviations DRY and WET, so that a beginner or whoever can better grasp or remember the concept.
31st Dec 2019, 6:32 PM
HonFu
HonFu - avatar
+ 1
This code is DRY . It is simple and is readable and understandable for i in range(1,int(input()) + 1) : print(i,"\n") This code on the other hand achieves the same purpose in an ugly way nums = [] counter = 0 while counter <= i : nums.insert(counter , counter) counter = counter + 1 counter_2 = 0 while counter_2 >= len(nums): print(print(nums[counter_2]),"\n") counter_2 = counter_2 + 1 see how overcomplicated it is
1st Jan 2020, 3:37 PM
Oluwaseun Familusi
Oluwaseun Familusi - avatar
0
HonFu , in answer to your question, Yes, I did! In answer to your statement, Yes, I AM! I can over-think readily enough. The problem is, as I'm on the inside, I can't tell when it's happening. I like things that are clearly explained, as that tends to prevent my excursion into the grey unknown. :-) Ambiguity is my nemesis.
31st Dec 2019, 7:26 PM
JoeSponge
JoeSponge - avatar
0
HonFu , Sigh. First you comfort me, then your scold me. :) Yes: 420 tabs open in Firefox; concrete knowledge is a fcn inversely proportional to the set of Ff(tabs). Yes: Babble. I can only try.
31st Dec 2019, 7:36 PM
JoeSponge
JoeSponge - avatar