Questions tagged [dry]

Don't Repeat Yourself, a software development philosophy which aims at reducing redundancy and code repetition. Questions regarding how to refactor code are better suited on codereview.stackexchange.com

DRY, Don't Repeat Yourself, is a software engineering principle aimed at reducing redundancy and repetition of information of all kinds.

Note that questions regarding how to refactor code are better suited on CodeReview.

2196 questions
15
votes
4 answers

How can I remove duplicated code between classes?

I have 2 class: RecursiveFibonacci and MemorizedRecursiveFibonacci. This is what I have so far. RecursiveFibonacci Class public class SimpleRecursiveFibonacci { public BigInteger fibonacci(int n) { if(n < 2) { return BigInteger.ONE; …
Minh Ngo
  • 289
  • 2
  • 9
15
votes
4 answers

In Specflow can I run one test as a step of another?

TL;DR; How can I create a specflow test that calls another test as its first step? Given I already have one specflow test And I want to run another test that goes deeper than the first test Then I create a second test that runs the first test as…
JK.
  • 21,477
  • 35
  • 135
  • 214
15
votes
2 answers

RAII wrapper for OpenGL objects

I want to write a simple RAII wrapper for OpenGL objects (textures, frame buffers, etc.) I have noticed, that all glGen* and glDelete* functions share the same signature, so my first attempt was like this: typedef void (__stdcall…
n0rd
  • 11,850
  • 5
  • 35
  • 56
14
votes
4 answers

Is there an automatic tool to find the DRY-ness of your code base?

I am a strong advocate of the DRY principle: Every piece of knowledge must have a single, unambiguous, authoritative representation within a system. Are there any tools that can test a code base for the amount of DRYness and both quantify and…
Scott Stafford
  • 43,764
  • 28
  • 129
  • 177
14
votes
3 answers

NextJS, _app, SSG, getInitialProps vs getStaticProps and how am I supposed to stick to DRY?

I know that the topic isn't new and I found (and read) a few discussions. What I couldn't find is an answer to my still remaining question. How do others work around the problem of not being able to use getStaticProps in _app.js ? getInitialProps is…
SMEETT
  • 166
  • 1
  • 9
14
votes
7 answers

Kotlin - Overwrite Obj Props With Modified Obj Props if Not Null

TL;DR: How do I make this less redundant (any approach that works helps)? if (personModification.firstName != null) {person.firstName = personModification.firstName} if (personModification.lastName != null) {person.lastName =…
VSO
  • 11,546
  • 25
  • 99
  • 187
14
votes
2 answers

Refactoring a library to be async, how can I avoid repeating myself?

I have a method like so: public void Encrypt(IFile file) { if (file == null) throw new ArgumentNullException(nameof(file)); string tempFilename = GetFilename(file); using (var stream = new…
14
votes
4 answers

Declare and initialize pointer concisely (i. e. pointer to int)

Given pointers to char, one can do the following: char *s = "data"; As far as I understand, a pointer variable is declared here, memory is allocated for both variable and data, the latter is filled with data\0 and the variable in question is set to…
user3849273
  • 221
  • 1
  • 2
  • 7
14
votes
5 answers

How to "DRY up" C# attributes in Models and ViewModels?

This question was inspired by my struggles with ASP.NET MVC, but I think it applies to other situations as well. Let's say I have an ORM-generated Model and two ViewModels (one for a "details" view and one for an "edit" view): Model public class…
devuxer
  • 41,681
  • 47
  • 180
  • 292
14
votes
2 answers

rails - DRY respond_to with repeated actions

In one of my rails controller, I must respond to several types of formats, so I use the typical respond_to chain: respond_to do |format| format.html { ... } format.mobile { ... } format.jpg { ... } format.xml { ... } format.js { ...…
kikito
  • 51,734
  • 32
  • 149
  • 189
14
votes
1 answer

Should mapping value be declared in a constant or as an enum?

I see this scattered throughout code base: @RequestMapping(value = "myValue") I would prefer to use something like this: @RequestMapping(value = Constants.myValue) It seems to break DRY using the actual String value within @RequestMapping…
blue-sky
  • 51,962
  • 152
  • 427
  • 752
13
votes
1 answer

How to extend or override BeginForm to include a AntiForgeryToken field

I was reading this article (http://weblogs.asp.net/dixin/archive/2010/05/22/anti-forgery-request-recipes-for-asp-net-mvc-and-ajax.aspx) about how to prevent CSRF attacks. It seems like the solution is to create a tag inside each form. <%:…
zgirod
  • 4,189
  • 3
  • 28
  • 36
13
votes
3 answers

Using Marshmallow without repeating myself

According to the official Marshmallow docs, it's recommended to declare a Schema and then have a separate class that receives loaded data, like this: class UserSchema(Schema): name = fields.Str() email = fields.Email() created_at =…
Teyras
  • 1,262
  • 11
  • 22
13
votes
4 answers

Python, I'm repeating myself a lot when it comes to for loops and there must be a better way

Lets say I have three lists and I need to iterate through them and do some stuff to the contents. The three lists are streaks_0, streaks_1, and streaks_2. For each list, I need to use different values specific to each list. For example,…
Ari Madian
  • 357
  • 1
  • 4
  • 13
13
votes
5 answers

DRY between Production and Test Code Constants

I normally try to avoid duplication and adhere to the DRY principle. However, I'm wondering about a case like this: public class Feature { final static String FEATURE_LABEL = "blah"; public void doSomething() { ... } ... } public class…
Paul Croarkin
  • 14,496
  • 14
  • 79
  • 118