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
25
votes
5 answers

Is there any well-known method for DRYing JSON

Consider this JSON response: [{ Name: 'Saeed', Age: 31 }, { Name: 'Maysam', Age: 32 }, { Name: 'Mehdi', Age: 27 }] This works fine for small amount of data, but when you want to serve larger amounts of data (say many…
Saeed Neamati
  • 35,341
  • 41
  • 136
  • 188
22
votes
5 answers

How to reuse a large query without repeating it?

If I have two queries, which I will call horrible_query_1 and ugly_query_2, and I want to perform the following two minus operations on them: (horrible_query_1) minus (ugly_query_2) (ugly_query_2) minus (horrible_query_1) Or maybe I have a…
Buttons840
  • 9,239
  • 15
  • 58
  • 85
21
votes
4 answers

Reducing code duplication between operator= and the copy constructor

I have a class that requires a non-default copy constructor and assignment operator (it contains lists of pointers). Is there any general way to reduce the code duplication between the copy constructor and the assignment operator?
Dan Hook
  • 6,769
  • 7
  • 35
  • 52
21
votes
7 answers

Does it exist: Repeated Code Finder?

In the near future, I will be inheriting a somewhat large project. I've been making some small updates to it recently, and noticed that parts of it could use some refactoring, since there are methods that perform the same operation with a small…
Dan McClain
  • 11,780
  • 9
  • 47
  • 67
20
votes
14 answers

How can I simplify repetitive if-elif statements in my grading system function?

The goal is to build a program to convert scores from a '0 to 1' system to an 'F to A' system: If score >= 0.9 would print 'A' If score >= 0.8 would print 'B' 0.7, C 0.6, D And any value below that point, print F This is the way to build it and it…
20
votes
2 answers

DDD Invariants Business Rules and Validation

I am looking for advice on where to add validation rules for domain entities, and best practices for implementation. I did search and did not find what i was looking for, or i missed it. I would like to know what the recommended way is for…
19
votes
5 answers

AngularJS controllers, design pattern for a DRY code

I have created a full example for the purpose of describing this issue. My actual application is even bigger than the presented demo and there are more services and directives operated by every controller. This leads to even more code repetition. I…
Atais
  • 10,857
  • 6
  • 71
  • 111
18
votes
5 answers

How do I DRY up my CouchDB views?

What can I do to share code among views in CouchDB? Example 1 -- utility methods Jesse Hallett has some good utility methods, including function dot(attr) { return function(obj) { return obj[attr]; } } Array.prototype.map = function(func)…
James A. Rosen
  • 64,193
  • 61
  • 179
  • 261
18
votes
9 answers

Do setup/teardown hurt test maintainability?

This seemed to spark a bit of conversation on another question and I thought it worthy to spin into its own question. The DRY principle seems to be our weapon-of-choice for fighting maintenance problems, but what about the maintenance of test code? …
cwash
  • 4,185
  • 5
  • 43
  • 53
17
votes
5 answers

When KISS and DRY collide

I'm an obsessive follower of the DRY and KISS principles but last week I had a case where both seem to contradict each other: For an application I was doing, I had to implement a loop for times which does the following: iterate over the elements of…
helpermethod
  • 59,493
  • 71
  • 188
  • 276
17
votes
1 answer

How to use GraphQL fragment on multiple types

I have a Gatsby project with very similar GraphQL queries for two different types of content: regular pages and wiki articles. Page by slug export const query = graphql` query($slug: String!) { page: contentfulPage(slug: {eq: $slug}) { …
Janosh
  • 3,392
  • 2
  • 27
  • 35
16
votes
2 answers

DRYing rails view: partial vs helper

I need an advice on best practices for DRYing view code. I have three classes (NewsItem, RssItem and BlogItem) in my app, that use separate views, but have similar parts in them. One of the parts is such: <% if current_user %>
<%=…
sandrew
  • 3,109
  • 5
  • 19
  • 29
16
votes
3 answers

How to customize to_json response in Rails 3

I am using respond_with and everything is hooked up right to get data correctly. I want to customize the returned json, xml and foobar formats in a DRY way, but I cannot figure out how to do so using the limited :only and :include. These are great…
coneybeare
  • 33,113
  • 21
  • 131
  • 183
15
votes
3 answers

Best way to require several modules in NodeJS

I don't much like the standard way to require modules, which goes something like this: connect = require 'connect' express = require 'express' redis = require 'redis' sys = require 'sys' coffee = require 'coffee-script' fs = require 'fs' It's not…
mahemoff
  • 44,526
  • 36
  • 160
  • 222
15
votes
1 answer

How can I create custom route helpers for usage in routes.rb

I have a few reoccurring patterns in my routes.rb and I would like to make it DRY by creating a method that creates those routes for me. An example of what I want to accomplish can be seen in the Devise gem where you can use the following…
DanneManne
  • 21,107
  • 5
  • 57
  • 58