Skip to main content
Share Your Experience: Take the 2024 Developer Survey

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
-1 votes
2 answers
108 views

How to handle complex logic, avoiding recalculation for performance reasons?

let's say we're building an Ecommerce Marketplace. We have Sellers and each Seller has Products. We want to display a list of available Products across all Sellers to Buyers. But, we only need to ...
Staskij's user avatar
  • 21
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,187
0 votes
3 answers
129 views

How to avoid duplicating Create and Update UIs in CRUD apps

For most apps I've built which deal with CRUD operations, I end up two very similar UI pages: one for the creation of the object, one for updating it. An example would be StackExchange's UI for ...
Simon Farshid's user avatar
1 vote
1 answer
2k views

How to handle duplicate validation logic with CQRS pattern

I am implementing a CQRS pattern in ASP.NET Core with MediatR, and I am wondering what the general consensus is for handling duplicate validation logic for queries/commands which operate on the same ...
Mark Lisoway's user avatar
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
2 votes
5 answers
467 views

Do db calls in constructors lead to more DRY code?

It recently came to my attention that its best practice to avoid database calls in constructors. I feel like this means you end up repeating unnecessary code, thus the code is less DRY? For example, ...
pooteeweet's user avatar
1 vote
3 answers
316 views

How to split out shared authorization logic across spring microservices

Currently working on a project where we have multiple services that all need to consume the same authorization service when their endpoints are hit. Right now we have the authorization boilerplate ...
Astrum's user avatar
  • 143
-4 votes
1 answer
75 views

How to use DRY methods with OS commands with Python and classes

My goal is to learn more about OOP patterns and use DRY principles. I am trying this for wrapping an os command that interacts with a database using classes: This works fine: import subprocess class ...
james6125'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
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
2 votes
1 answer
244 views

Selective method inheritance

I have a lot of classes that are just a CRUD interface for microservices. They only have a param for the endpoint and some of the methods get_list / get_item / create / update / delete / activate / ...
xpy's user avatar
  • 129
3 votes
1 answer
390 views

DRY principle vs decoupling (business from GUI)

Say I have a business model called Vehicle. Vehicle has many fields but to keep it simple say it looks like: public class Vehicle { String ownerName; String brand; FuelType fuelType; }...
George Z.'s user avatar
  • 705
4 votes
5 answers
228 views

Approaches for comment duplication

For code, we know approaches like DRY and we tend to extract common functionality. What approaches are recommended for comments? Perhaps it's a really open question, so I'm going to go with my ...
Gonzalo.-'s user avatar
  • 161
-4 votes
1 answer
375 views

Best way to structure reusable code using Node.JS, EJS, and front end JS?

I'm more or less learning the MEAN stack (have yet to start on Angular, so currently using straight vanilla JS for front-end) and part of what I'm building for my portfolio is a drag-and-drop form ...
R Greenstreet's user avatar
0 votes
2 answers
196 views

DRY Violation for Logical Code Organization and Readability

I have a block of code that branches into 2 pathways, let's call them the "simple" and "complex" branch, based on user input. Either the simple or complex logic has 4 steps, let's call them A, B, C ...
Dragonsheep's user avatar
0 votes
0 answers
548 views

DRY(Don't repeat yourself) Principle and BFFs (Backend-for-Frontend)

We are about to plan our server architecture and we want to use the BFF strategy with node.js servers to serve multiple front-end apps. However we also want to be able to scale easily (e.g. a new ...
telion's user avatar
  • 61
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
0 votes
2 answers
263 views

Reduce DRY for different classes which have the same data model

I have 2 "user" classes which are essentially the same except one is immutable without setters and one is mutable with setters. I have a separate factory for creating each object. ImmutableUserDto - ...
James's user avatar
  • 243
0 votes
2 answers
364 views

Modify Command object inside decorator of CommandHandler(CQRS) or any other practice for avoiding duplication

Currently, we have multiple commands which share two same properties (BookName and ShopId). While handling those commands those properties must be validated with the help of some service. Let's say ...
Farhad Jabiyev's user avatar
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
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
-1 votes
1 answer
158 views

How to generalize this method for its duplicate/common logic?

I've a method that apparently has quite some duplicate logic, I'd like to generalize it so that their duplicate logic could be shared and reused, also make my code more extensible for future additions....
Fisher Coder's user avatar
3 votes
4 answers
670 views

Should one-line functions be avoided in jQuery/JavaScript?

So, I had a coworker complain about how I made the following code: ... var foo = getKendoDropdown(window.foo); var bar = getKendoDropdown(window.foo); var sna = getKendoDropdown(window.sna); var fu = ...
Sarov's user avatar
  • 403
0 votes
3 answers
531 views

How to write DRY Unit Tests when order of method calls matters

Suppose I'm writing a calculator engine with a class as follows... public class ArithmeticExpression { ArithmeticExpression Add(double operand) { /* ... */ } ArithmeticExpression Subtract(...
mdx's user avatar
  • 203
3 votes
1 answer
320 views

How to make updates to data and return list of updates without breaking Command query separation or DRY?

I have a use case to make updates to a slightly complex JSON object and then return the list of changes made (not the updated data). The process of finding the list of changes is extremely similar to ...
Shreyas P Babu's user avatar
1 vote
2 answers
437 views

Best practice when creating classes that are solely in charge of the creation of other objects i.e. factories?

I often find myself creating classes that I suffix with 'Factory'. These classes tend to be solely in charge of the creation of a specific class or set of related classes. However I've always felt ...
Jake12342134's user avatar
4 votes
4 answers
2k views

Is it bad practice to repeat logic being tested in unit tests?

Often I find myself writing a unit test for code and repeating some or a lot of the logic in the unit test to make the unit test DRY. For example, consider the following piece of code: function ...
Adam Thompson's user avatar
1 vote
1 answer
196 views

What is the best way to handle the scenario in C++ where there are two methods that are identical, except one is const?

I am currently working on a program in which I encountered an issue (not for the first time) where I have two acessor methods for a data structure. The methods are identical, except one is const and ...
john01dav's user avatar
  • 889
-1 votes
1 answer
254 views

Keeping steps in sync of long running process and creating common layer for code repetition

I have 1 long running process wrapped inside a method and it is for 2 different types like below: Type1 Type2 Code: public interface IBaseType { MyResult LongRunningProcess(int jobId,...
I Love Stackoverflow's user avatar
3 votes
3 answers
5k views

Are there any well known alternatives to the SOLID principles for OO programming? [closed]

I´m looking for some kind of better compilation of principles which takes the old basic concepts (DRY, KISS, etc...) and applies them to OOP related concepts like abstract clasess, interfaces etc... ...
jacmkno's user avatar
  • 163
2 votes
2 answers
1k views

How to avoid code duplication in two similar projects?

We have a website with a lot of functionality, and we now start another one, which should have a lot of common code with the first one. What would be the best approach to avoid code duplication? ...
Oleg's user avatar
  • 123
4 votes
5 answers
733 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){ ...
ocomfd's user avatar
  • 5,712
0 votes
1 answer
2k views

How to remove code duplication in Web API versioning

I'm working with ASP.NET core web API project, where I have 8 controllers, multiple DTOs being used for request and response and mappers to map models(also used in the web app) with DTOs. Recently, ...
Div's user avatar
  • 167
1 vote
2 answers
203 views

DRY or not to DRY in SDK

I'm making contribution to an opensource project by one of the leading hypervisor providers and I have noticed that there is awfully lot of repetition. Below is what I mean by it, imagine that they ...
MMT's user avatar
  • 199
6 votes
6 answers
671 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
-1 votes
1 answer
92 views

My action creators and reducers all mostly look the same - is this a code smell?

Most of my redux actions are 'fetch some data from the database, here's the authentication token', and they for the most part look the same, like this: action creator export const fetchDashboard = ...
dwjohnston's user avatar
  • 2,563
0 votes
1 answer
195 views

ReactJS - proper way to handle a responsive design approach?

I've always used CSS and media queries when it comes to responsive design. I've been developing in ReactJS for a while, but I feel my approach to responsive design is sloppy. I find myself using a mix ...
sme's user avatar
  • 101
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,429
6 votes
3 answers
462 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
2 votes
1 answer
480 views

Is 'Keep it looks similar to sample code' a valid reason to prefer WET over DRY principle?

For example, for oauth, I may need to copy and paste oauth and oauth callback code like it (assume Client is the framework I use, I don't use real framework like google because I want to simplify the ...
ocomfd's user avatar
  • 5,712
15 votes
3 answers
850 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
0 votes
1 answer
356 views

Is creating near duplicate partial views solely for populating html elements with Ajax returns violating DRY?

Maybe anti-pattern is a better word for this question. I do not know, but I mean a violation of DRY. Id like to reuse a partial view, and I can, but that partial view has other things in it I don't ...
johnny's user avatar
  • 3,679
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
2 votes
4 answers
3k views

Put conditional logic inside method for DRY, or keep it outside for readability?

Take the following example which loads an interstitial ad every 10 times user does XYZ in the app, under certain conditions. It is called in multiple places in the code base: public class AdHandler { ...
Chris Cirefice's user avatar
1 vote
2 answers
343 views

Test on each Authorization endpoints?

I'm writing an API server. The framework we're using is Rails + Pundit + graphql-ruby, but I guess this question is framework-independent. We have authentication + authorization implemented on the ...
songyy's user avatar
  • 119
3 votes
1 answer
138 views

Organizing Ad-Hoc Tooling in a Muti-Repo Structure

Imagine you have the following scenario. Your organization deploys some app core to its mission. For example if you're Stack Exchange, this might be the customer facing web server. Let's also say you ...
user79126's user avatar
  • 147
3 votes
5 answers
601 views

Does my code violate DRY principle?

Suppose that I have two services Person Service and Company Service and I want to maintain links between them for example a Person is linked to Company because he works there or he owns the company ...
Saad's user avatar
  • 49
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
-1 votes
4 answers
435 views

How to insert code in a method in a derived class [duplicate]

I have a base class with a method called Update: Start Update Code block 1 (An If statement) Code block 2 (Setting a variable based on the If result) Code block 3 (A switch which is ...
Krowi's user avatar
  • 157
1 vote
0 answers
115 views

Can we break CQS to get some DRY?

In our codebase we have a lot of controllers with a very similar structure: @my_model.update_owner(something) if @my_model.update_attributes(params) return render_object(@my_model) else ...
Jacob's user avatar
  • 321