Skip to main content
The 2024 Developer Survey results are live! See the results

Questions tagged [dry]

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

Filter by
Sorted by
Tagged with
115 votes
12 answers
17k views

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 ...
George Powell's user avatar
85 votes
15 answers
12k views

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 ...
Incognito's user avatar
  • 3,478
79 votes
6 answers
14k views

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. ...
user158443's user avatar
53 votes
10 answers
7k views

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 ...
Derek C.'s user avatar
  • 617
38 votes
3 answers
11k views

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 ...
ocomfd's user avatar
  • 5,712
35 votes
1 answer
5k views

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 ...
user avatar
25 votes
9 answers
6k views

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 ...
Phil's user avatar
  • 3,660
24 votes
5 answers
1k views

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 ...
user969153's user avatar
24 votes
11 answers
7k views

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 ...
iwis's user avatar
  • 349
23 votes
8 answers
7k views

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

Consider these methods: public List<Employee> GetAllEmployees() { using (Entities entities = new Entities()) { return entities.Employees.ToList(); } } public List<Job>...
Saeed Neamati's user avatar
20 votes
6 answers
14k views

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 ...
srnka's user avatar
  • 355
19 votes
6 answers
2k views

The importance of duplicate code removal [duplicate]

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 = [...
Iulian Onofrei's user avatar
19 votes
3 answers
2k views

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 ...
Will's user avatar
  • 712
19 votes
4 answers
8k views

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 ...
asfallows's user avatar
  • 2,331
18 votes
1 answer
3k views

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 ...
Louis Rhys's user avatar
  • 6,142
16 votes
3 answers
873 views

Code coupling introduce by DRY and OOD

I'm looking for guidance about DRY vs Code coupling. I do not like to duplicate my code and I also do not like code coupling between unrelated modules. So I refactor duplicate code if I find ...
user2549686's user avatar
15 votes
5 answers
1k views

Is it possible to apply DRY without increasing coupling?

Assume we have a software module A that implements a function F. Another module B implements the same function as F'. There are a number of ways to get rid of the duplicate code: Let A use F' from B....
Frank Puffer's user avatar
  • 6,439
15 votes
2 answers
4k views

Const C++ DRY Strategies

For avoiding non-trivial C++ const related duplication, are there cases where const_cast would work but a private const function returning non-const wouldn't? In Scott Meyers' Effective C++ item 3, ...
JDiMatteo's user avatar
  • 375
14 votes
3 answers
3k views

Should I choose repeated code in unit test or test logic? Can I avoid both?

When writing unit tests, I feel that there is a trade-off between code repetition and test logic. Example of my current (likely flawed) approach: To test this function (overly simple function for ...
Jasper Braun's user avatar
14 votes
3 answers
3k views

Doesn't single-assert unit testing break the DRY principle?

Whenever I write unit tests I have always tried to have a single assert per test to make debugging easier when tests fail. However as I follow this rule I feel like I am constantly copying the same ...
Korey Hinton's user avatar
  • 2,676
13 votes
5 answers
2k views

For an ORM supporting data validation, should constraints be enforced in the database as well?

I have always applied constraints at the database level in addition to my (ActiveRecord) models. But I've been wondering if this is really required? A little background I recently had to unit test a ...
n a's user avatar
  • 235
12 votes
5 answers
4k views

Too much abstraction making code hard to extend

I'm facing problems with what I feel is too much abstraction in the code base (or at least dealing with it). Most methods in the code base have been abstracted to take in the highest parent A in the ...
YamizGers's user avatar
  • 137
11 votes
7 answers
2k views

How to sell DRY architecture [duplicate]

I'm sure most are familiar with the phrase DRY in the software world - Don't Repeat Yourself. This is a fundamental principle of good software development. Here is a question (background first). ...
Catchops's user avatar
  • 2,529
11 votes
5 answers
772 views

Is an architecture description document a violation of the DRY Principle?

The DRY Principle (Don't Repeat Yourself) states that "every piece of knowledge must have a single, unambiguous, authoritative representation within a system." Most of the time this refers to code, ...
Michael's user avatar
  • 2,122
11 votes
3 answers
3k views

I have to compromise: DRY, or Command-Query-Separation?

I was recently refactoring a method that was both a command and a query method. After separating it into a one command method and one query method, I found that there are now multiple places in the ...
Kris Welsh's user avatar
10 votes
12 answers
5k views

Testing vs Don't Repeat Yourself (DRY)

Why is repeating yourself by writing tests so highly encouraged? It seems that tests basically express the same thing as the code, and hence is a duplicate (in concept, not implementation) of the ...
John Tseng's user avatar
10 votes
6 answers
3k views

Interpretation of DRY principle

Right now I'm struggling with this concept of DRY (Don't Repeat Yourself) in my coding. I'm creating this function in which I'm fearing it's becoming too complex but I'm trying to follow the DRY ...
Albinoswordfish's user avatar
10 votes
7 answers
2k views

Violation of the DRY Principle

I am sure there's a name for this anti-pattern somewhere; however I am not familiar enough with the anti-pattern literature to know it. Consider the following scenario: or0 is a member function in a ...
Onorio Catenacci's user avatar
10 votes
3 answers
38k views

Unit testing functions calling other tested functions [duplicate]

I have three functions: ValidateUsername(), which determines if a string is a valid username according to some rules SetUsername() which sets a string as a user's Username if it passes validation ...
io-quois's user avatar
  • 111
10 votes
3 answers
23k views

How to remove duplicate code (in general)?

In an OO language (e.g. but not limited to Java) how do you fix duplicate code depending on the scope of it's occurrence? I would start with (for example) in the same class (scope) perform the ...
Peter Kofler's user avatar
10 votes
2 answers
2k views

DRY way to write Javadoc on overload methods

I want to write Javadoc in DRY way. But the oracle document about Javadoc says write same thing again in overload method comment. Can't I avoid repetition?
Sanghyun Lee's user avatar
9 votes
4 answers
1k views

Is coding and unit testing violating the DRY principle

The dry principle states: "Every piece of knowledge must have a single, unambiguous, authoritative representation within a system." However when writing tests for code you are describing the ...
refro's user avatar
  • 1,396
8 votes
3 answers
371 views

Is there a drawback in defining multiple small DRY classes, instead of bigger more repetitive classes? [duplicate]

I read some articles and it seems that using multiple classes in CSS is encouraged. I am curious to find out if there is a maximum number of classes an element can have before this multiple class ...
Ionut's user avatar
  • 537
8 votes
2 answers
2k views

Practices for domain models in Javascript (with frameworks)

This is a question I've to-and-fro'd with for a while, and searched for and found nothing on: what're the accepted practices surrounding duplicating domain models in Javascript for a web application, ...
Andy Hunt's user avatar
  • 6,036
7 votes
4 answers
2k views

Why aren't OOP design patterns included in the standard libraries?

I have a question similar to this other question Why aren't design patterns added to the languages constructs? Why isn't there java.util.Singleton and then we inherit it? The boilerplate code ...
Niklas Rosencrantz's user avatar
7 votes
4 answers
1k views

Don't Repeat Yourself Vs Single Point Of Failure in authentication

In my work we've been tasked with implementing two factor authentication across a number of our web applications which currently share a credentials database (asp.net forms authentication) but have ...
Slappywag's user avatar
  • 189
7 votes
2 answers
2k views

How can I make code that is both DRY and fast where intermediate values in a calculation may or may not be needed?

How can I make DRY (lacks repetitive patterns) code that also avoids inefficiencies from using intermediate values in a calculation that might not need to be used? Here is an example: In this code, I ...
CPlus's user avatar
  • 1,189
7 votes
4 answers
3k views

DRY, string, and unit testing

I have a recurring question when writing unit tests for code that involves constant string values. Let's take an example of a method/function that does some processing and returns a string containing ...
Rodrigue's user avatar
  • 194
7 votes
4 answers
2k views

Can I make my constructors less repetitive?

I'm extending a class with 10 different constructors. The new subclass, SpecialImage, is used like this: SpecialImage specialImage = new SpecialImage(..); // Leverage the Rotate() method of ...
kdbanman's user avatar
  • 1,457
7 votes
2 answers
3k views

Ruby on Rails and DRY

I've started to learn a little ROR and everthing I read says that ROR espouses the DRY principle and they seem to imply that this is a big thing that makes ROR different from other languages/...
Joshua Slocum's user avatar
6 votes
6 answers
673 views

Is duplicating files to avoid programming branching a good or anti pattern?

for example, suppose I need to play different sounds according to "grade": file list: fairSound.mp3 goodSound.mp3 excellentSound.mp3 code: showResult(grade){ if(grade==0 || grade==1){ ...
ocomfd's user avatar
  • 5,712
6 votes
6 answers
2k views

DRY principle often makes my code more complicated and/or more difficult to understand [duplicate]

This often happens in my projects. Sometime I have this part of code that is very similar to this other part, yet a few lines makes it complicated to keep the code clean and without duplication. Here ...
Aulaulz's user avatar
  • 289
6 votes
3 answers
480 views

How to handle repetitive mallocs/frees in a DRY way in C

So I have a function which is like a constructor for my structure: MyStructure* CreateMyStructure(...) { MyStructure *my_structure; double *data; int *colind, *rowptrs; data = malloc(...
cbalos's user avatar
  • 171
6 votes
3 answers
3k views

Is it poor practice to have a class implement an abstract class without adding any new fields/members/functionality?

I am writing a wrapper for a REST API and I have run into something I have never had to ask myself before. This API is for E-Commerce transactions, it has SALE and RETURN endpoints (and some other ...
maccettura's user avatar
6 votes
3 answers
611 views

Eliminating Dependencies vs Eliminating Redundancy

Upon my assignment to a project, I discovered that many message classes were received and then kept intact and passed around inside the receiving application. When these messages changed, code ...
user24449's user avatar
  • 232
5 votes
3 answers
5k views

How is DRY principle ( applied at class level ) related to SRP?

In other words, is DRY (don't repeat yourself) applied at a class level a subset of SRP (single responsibilty principle)? What I mean is that while SRP states that each class should have only a ...
bckpwrld's user avatar
  • 439
5 votes
2 answers
349 views

I keep bouncing from "god function" to "tiny SRP functions" how do I break this cycle?

This question is important for me in growing in my technical abilities. I find I swing from end-to-end, like a pendulum, in writing code that is simultaneously DRY yet readable & efficient. And I'...
james's user avatar
  • 161
5 votes
5 answers
648 views

How do you keep SOA DRY?

In our organization, we've shifted to a more "service oriented architecture". To give an example, let's assume we need to retrieve a "Quote" object. This quote has a shipper, a consignee, phone ...
TaylorOtwell's user avatar
  • 2,667
5 votes
3 answers
558 views

How to apply DRY to files shared by repositories?

I've got a few files which are used in several of my repos: functions.sh, shell library to for example print a colored warning/error message or the documentation of a script file. Makefile; a ...
l0b0's user avatar
  • 11.5k
5 votes
1 answer
1k views

Implementing both Sync and Async clients with DRY

I'm developing a client library. I'd like to provide both Sync and Async interfaces to endpoints on a server. They would be rather easy to implement as completely separate entities, but I would like ...
Felix's user avatar
  • 377