Questions tagged [dry]

DRY is short for "Don’t Repeat Yourself". This paradigm advocates to avoid code and data redundancy.

147 questions
113
votes
12 answers

Best practices for sharing tiny snippets of code across projects

I always try to follow the DRY principle strictly at work; every time I've repeated code out of laziness it bites back later when I need to maintain that code in two places. But often I write small methods (maybe 10 - 15 lines of code) that need to…
George Powell
  • 1,386
  • 2
  • 11
  • 11
85
votes
15 answers

Why is DRY important?

Quite simple, why would I want to write code that works for all cases and scalable data when all I need to do is repeat the same process a few times with a few minor tweaks? I'm unlikely to need to edit this again any time soon. It looks like a lot…
Incognito
  • 3,458
  • 2
  • 25
  • 38
77
votes
6 answers

What should I consider when the DRY and KISS principles are incompatible?

The DRY principle sometimes forces the programmers to write complex, hard-to-maintain functions/classes. Code like this has a tendency to become more complex and harder to maintain over time. Violating the KISS principle. For example, when multiple…
user158443
  • 799
  • 1
  • 6
  • 5
50
votes
9 answers

How should I test "Glue Functions" without testing that "the code I wrote is the code I wrote"?

I usually write my code in a test driven style. I write tests as specifications and then my code. It's great and useful. I always try to ignore implementation when testing and only test behaviour. I don't care how it gets done, just that it got…
Derek C.
  • 573
  • 4
  • 7
38
votes
3 answers

Is "composition over inheritance" violating "dry principle"?

For example, consider I have a class for other classes to extend: public class LoginPage { public String userId; public String session; public boolean checkSessionValid() { } } and some subclasses: public class HomePage extends…
ocomfd
  • 5,652
  • 8
  • 29
  • 37
35
votes
1 answer

DRY unrelated, but nearly identical, code

I have some code that is nearly identical, but uses absolutely different types, with no inheritance between them, on the main variable. Specifically, I am writing an analyzer with Roslyn for C# and VB.NET, with the following…
user113093
25
votes
9 answers

Adding complexity to remove duplicate code

I have several classes that all inherit from a generic base class. The base class contains a collection of several objects of type T. Each child class needs to be able to calculate interpolated values from the collection of objects, but since the…
Phil
  • 3,660
  • 26
  • 29
24
votes
11 answers

The Don't Repeat Yourself (DRY) principle in documentation

Dave Thomas, the author of the Don't Repeat Yourself principle said: DRY says that every piece of system knowledge should have one authoritative, unambiguous representation. Every piece of knowledge in the development of something should have a…
iwis
  • 349
  • 2
  • 9
24
votes
5 answers

Many small classes vs. logical (but) intricate inheritance

I'm wondering what is better in terms of good OOP desing, clean code, flexibility and avoiding code smells in the future. Image situation, where you have a lot of very similar objects you need to represent as classes. These classes are without any…
user969153
  • 349
  • 2
  • 5
23
votes
8 answers

How to implement DRY principle when using 'using' keyword?

Consider these methods: public List GetAllEmployees() { using (Entities entities = new Entities()) { return entities.Employees.ToList(); } } public List GetAllJobs() { using (Entities entities = new…
Saeed Neamati
  • 18,142
  • 23
  • 87
  • 125
20
votes
6 answers

Validation of the input parameter in caller: code duplication?

Where is the best place to validate input parameters of function: in caller or in function itself? As I would like to improve my coding style, I try to find the best practices or some rules for this issue. When and what is better. In my previous…
srnka
  • 345
  • 1
  • 2
  • 10
19
votes
6 answers

The importance of duplicate code removal

I tried to explain to a coworker the gravity of having duplicate code in a project, on this piece of code: + (void)createIapInParse:(SKPaymentTransaction *)transaction { Reachability *reach = [Reachability…
Iulian Onofrei
  • 303
  • 1
  • 12
19
votes
4 answers

Managing client-side and server-side validations in one place

I'm 100% on board with the case that one should definitely use both client-side and server-side data validations. However, in the frameworks and environments I've worked in, the approaches I've seen have never been DRY. Most of the time there's no…
asfallows
  • 2,331
  • 19
  • 18
19
votes
3 answers

Does decoupling trump DRY in REST?

I am building a REST API to expose most of functionality of an existing Java API. Both APIs are for internal use within my organization; I do not have to design for external use. I have influence over both APIs but am implementing the REST one. …
Will
  • 712
  • 5
  • 12
17
votes
1 answer

Reasoning to wait until third time in the Rule of Three?

I just came across the article "Rule of Three" in wikipedia Rule of three is a code refactoring rule of thumb to decide when a replicated piece of code should be replaced by a new procedure. It states that the code can be copied once, but that…
Louis Rhys
  • 6,042
  • 11
  • 42
  • 59
1
2 3
9 10