Questions tagged [dry]
DRY is short for "Don’t Repeat Yourself". This paradigm advocates to avoid code and data redundancy.
146
questions
111
votes
12
answers
16k
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 ...
85
votes
15
answers
11k
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 ...
75
votes
6
answers
13k
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. ...
50
votes
9
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 ...
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 ...
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 ...
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 ...
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 ...
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 ...
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>...
20
votes
6
answers
13k
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 ...
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 = [...
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 ...
19
votes
4
answers
7k
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 ...
17
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
...
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....
15
votes
3
answers
707
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 ...
15
votes
2
answers
3k
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, ...
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 ...
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 ...
12
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).
...
12
votes
5
answers
3k
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 ...
12
votes
3
answers
2k
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 ...
11
votes
5
answers
759
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, ...
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 ...
10
votes
12
answers
4k
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 ...
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 ...
10
votes
6
answers
2k
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 ...
10
votes
3
answers
22k
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 ...
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?
9
votes
3
answers
36k
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
...
8
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 ...
8
votes
3
answers
368
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 ...
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, ...
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 ...
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 ...
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 ...
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 ...
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/...
6
votes
6
answers
657
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){
...
6
votes
6
answers
1k
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 ...
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 ...
6
votes
3
answers
598
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 ...
5
votes
3
answers
4k
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 ...
5
votes
3
answers
378
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(...
5
votes
2
answers
308
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'...
5
votes
5
answers
641
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 ...
5
votes
3
answers
453
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 ...
4
votes
5
answers
667
views
Is copy and paste the head of for-loop (e.g.:for(let i=0;i<something.length;i++)) violating DRY principle?
For example, in my project, I often found some head of for-loop appears many times, eg:
for(let i=0;i<SharedData.students.length;i++){
SharedData.students[i].something=.....
}
if(isReset){
...
4
votes
4
answers
3k
views
Is wrapping third-party API calls a design smell?
Five methods within my API call the same third-party method. In trying to abide by DRY, does it make sense to wrap this call in a private method?