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.

2016 questions
196
votes
12 answers

What's the recommended way to extend AngularJS controllers?

I have three controllers that are quite similar. I want to have a controller which these three extend and share its functions.
vladexologija
  • 6,637
  • 4
  • 27
  • 28
135
votes
14 answers

How to repeat a "block" in a django template

I want to use the same {% block %} twice in the same django template. I want this block to appear more than once in my base template: # base.html {% block title %}My Cool Website{% endblock %}
David Arcos
  • 5,691
  • 5
  • 28
  • 37
132
votes
18 answers

Why is "copy and paste" of code dangerous?

Sometimes, my boss will complain to us: Why do we need such a long time to implement a feature? Actually, the feature has been implemented in another application before, you just need to copy and paste codes from there. The cost should be…
Yigang Wu
  • 3,486
  • 5
  • 34
  • 54
121
votes
11 answers

Is duplicated code more tolerable in unit tests?

I ruined several unit tests some time ago when I went through and refactored them to make them more DRY--the intent of each test was no longer clear. It seems there is a trade-off between tests' readability and maintainability. If I leave…
Daryl Spitzer
  • 121,723
  • 75
  • 151
  • 166
91
votes
12 answers

Java error: Implicit super constructor is undefined for default constructor

I have a some simple Java code that looks similar to this in its structure: abstract public class BaseClass { String someString; public BaseClass(String someString) { this.someString = someString; } abstract public String…
Joel
  • 10,593
  • 17
  • 58
  • 72
66
votes
5 answers

Can I inherit constructors?

I know it's not possible to inherit constructors in C#, but there's probably a way to do what I want to do. I have a base class that is inherited by many other classes, and it has an Init method that does some initializing taking 1 parameter. All…
Alex
  • 30,475
  • 22
  • 84
  • 129
57
votes
6 answers

How to avoid code duplication implementing const and non-const iterators?

I'm implementing a custom container with an STL-like interface. I have to provide a regular iterator and a const iterator. Most of the code for the two versions of the iterators is identical . How can I avoid this duplication? For example, my…
Adrian McCarthy
  • 41,073
  • 12
  • 108
  • 157
54
votes
6 answers

How to elegantly symbolize_keys for a 'nested' hash

Consider the following code: hash1 = {"one" => 1, "two" => 2, "three" => 3} hash2 = hash1.reduce({}){ |h, (k,v)| h.merge(k => hash1) } hash3 = hash2.reduce({}){ |h, (k,v)| h.merge(k => hash2) } hash4 = hash3.reduce({}){ |h, (k,v)| h.merge(k…
SHS
  • 7,261
  • 3
  • 15
  • 28
54
votes
7 answers

Where to put partials shared by the whole application in Rails?

Where would I go about placing partial files shared by more than one model? I have a page called crop.html.erb that is used for one model - Photo. Now I would like to use it for another model called User as well. I could copy and paste the code but…
Yuval Karmi
  • 24,847
  • 38
  • 116
  • 172
54
votes
4 answers

How to make a char string from a C macro's value?

For example, how to avoid writing the 'func_name' twice? #ifndef TEST_FUN # define TEST_FUN func_name # define TEST_FUN_NAME "func_name" #endif I'd like to follow the Single Point of Truth rule. Version of C preprocessor: $ cpp --version cpp…
jfs
  • 346,887
  • 152
  • 868
  • 1,518
53
votes
10 answers

Calling Django `reverse` in client-side Javascript

I'm using Django on Appengine. I'm using the django reverse() function everywhere, keeping everything as DRY as possible. However, I'm having trouble applying this to my client-side javascript. There is a JS class that loads some data depending on a…
noio
  • 5,435
  • 7
  • 37
  • 61
53
votes
6 answers

DRY Ruby Initialization with Hash Argument

I find myself using hash arguments to constructors quite a bit, especially when writing DSLs for configuration or other bits of API that the end user will be exposed to. What I end up doing is something like the following: class Example …
Kyle E. Mitchell
  • 683
  • 1
  • 6
  • 6
53
votes
13 answers

How much duplicated code do you tolerate?

In a recent code review I spotted a few lines of duplicated logic in a class (less than 15 lines). When I suggested that the author refactor the code, he argued that the code is simpler to understand that way. After reading the code again, I have to…
Sylvain
  • 18,671
  • 22
  • 89
  • 141
52
votes
3 answers

sed command in dry run

How it is possible to make a dry run with sed? I have this command: find ./ -type f | xargs sed -i 's/string1/string2/g' But before I really substitute in all the files, i want to check what it WOULD substitute. Copying the whole directory…
dmeu
  • 2,885
  • 4
  • 22
  • 36
50
votes
2 answers

How to reduce code duplication when dealing with recursive sum types

I am currently working on a simple interpreter for a programming language and I have a data type like this: data Expr = Variable String | Number Int | Add [Expr] | Sub Expr Expr And I have many functions that do simple things like: --…
1
2 3
99 100