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
199
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,857
  • 4
  • 29
  • 28
145
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
  • 143,156
  • 76
  • 154
  • 173
142
votes
15 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,957
  • 5
  • 30
  • 39
138
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,596
  • 5
  • 40
  • 54
97
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
  • 11,431
  • 17
  • 62
  • 72
88
votes
6 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
  • 34,581
  • 26
  • 91
  • 135
68
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,651
  • 3
  • 18
  • 28
66
votes
4 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
  • 3,842
  • 5
  • 27
  • 43
61
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
  • 45,555
  • 16
  • 123
  • 175
59
votes
3 answers

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

For example, how can I 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…
jfs
  • 399,953
  • 195
  • 994
  • 1,670
56
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
  • 26,277
  • 39
  • 124
  • 175
55
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
  • 703
  • 1
  • 6
  • 6
54
votes
4 answers

How to specify version in only one place when using pyproject.toml?

My package version is defined in two places: __version__ = 1.2.3 in mypackage/__init__.py version = "1.2.3" in pyproject.toml (I am using Poetry) I have to update both whenever I bump the version which is annoying and not DRY. Is there a way to…
Haterind
  • 1,095
  • 1
  • 8
  • 16
54
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,744
  • 7
  • 44
  • 61
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
  • 19,099
  • 23
  • 96
  • 145
1
2 3
99 100