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
11
votes
3 answers

Validate a value in property

So I heard that validating a value in a property like this: //dummy example, let's assume that I want my value without dots public string MyProp { set { if(value.Contains('.')) throw new ArgumentException("Must not…
ramires.cabral
  • 910
  • 4
  • 13
  • 28
10
votes
2 answers

Best Practices for using partials in Rails

In keeping with the DRY-principle I try to use partials as soon as I am repeating a particular pattern more than once or twice. As a result, some of my views consist of ten or more different partials. I am worried that this might have a negative…
sdfx
  • 21,653
  • 4
  • 26
  • 34
10
votes
2 answers

Is there any good way to DRY up scope / predicate logic duplication?

For example, consider the following code (in a model): scope :popular, where("views >= 250 OR (views >= 10 AND avg_rating >= 4.75)") def popular? views >= 250 or views >= 10 && avg_rating >= 4.75 end First condition is SQL, second one is ruby,…
Alexis
  • 4,317
  • 1
  • 25
  • 34
10
votes
1 answer

DRY user-input validation (clientside, serverside) using JSON-schema

As part of a extensive test-case, I'm building an ajax-based CMS-like application which provides CRUD-functionality on various documenttypes, e.g: articles, tags, etc. on the server and client-side I'm considering to use JSON-schema (…
Geert-Jan
  • 18,623
  • 16
  • 75
  • 137
10
votes
3 answers

How to avoid this kind of repetition

I have code similar to this: #include class A{ public: std::string &get(){ return s; } const std::string &get() const{ return s; } std::string &get_def(std::string &def){ return ! s.empty() ? s…
Nick
  • 9,962
  • 4
  • 42
  • 80
10
votes
11 answers

How to generate a non-const method from a const method?

While striving for const-correctness, I often find myself writing code such as this class Bar; class Foo { public: const Bar* bar() const { /* code that gets a Bar somewhere */ } Bar* bar() { return const_cast< Bar* >( static_cast<…
Tobias
  • 6,388
  • 4
  • 39
  • 64
10
votes
3 answers

How can I avoid repeating myself when creating a C++ enum and a dependent data structure?

Possible Duplicate: Enum to string : return the enum integer value if invalid / not found In brief, the (working) definition code that I have is something like this: enum Gadget { First, Second, }; const char* gadget_debug_names[] = { …
GrandOpener
  • 1,943
  • 1
  • 17
  • 25
10
votes
2 answers

How to balance DRY principle with minimizing dependencies?

I'm having a problem with the DRY principle (Don't Repeat Yourself) and minimizing dependencies that revolves around Rete rules engines. Rules engines in large IT organizations tend to be Enterprise (note the capital "E" - that's serious business). …
duffymo
  • 305,152
  • 44
  • 369
  • 561
10
votes
5 answers

Access Level to certain class must be public error in PHP

I created this class _errors = $message; } …
user962206
  • 15,637
  • 61
  • 177
  • 270
9
votes
3 answers

Duplicate code detection: Tools you can use

I am looking out for a software that identifies duplicate/redundant Javascript code. I found one such tool named CloneDR, but don't know how good it is . I was looking out for similar open source tools . Please guide .
Aazim
  • 129
  • 1
  • 6
9
votes
3 answers

Rails: Making ''show' view and 'edit' view match

Using Rails 2.3.5, Ruby 1.8.7. Is there any plugin that will make it easier to make my "show" and "edit" and "new" pages have the same look and feel? I'd love to be able to do the following type of thing. Right now, if I add a field to my model, I…
Thomas Andrews
  • 1,577
  • 1
  • 13
  • 31
9
votes
5 answers

How to stay DRY when using both Javascript and ERB templates (Rails)

I'm building a Rails app that uses Pusher to use web sockets to push updates to directly to the client. In javascript: channel.bind('tweet-create', function(tweet){ //when a tweet is created, execute the following code: $('#timeline').append("
user94154
  • 16,176
  • 20
  • 77
  • 116
9
votes
2 answers

setattr and getattr with methods

I have a boiler platey class that delegates some actions to a reference class. It looks like this: class MyClass(): def __init__(self, someClass): self.refClass = someClass def action1(self): …
zapatilla
  • 1,711
  • 3
  • 22
  • 38
9
votes
3 answers

Redundant code in getters and setters

I have a class that needs to call a method NotifyPropertyChanged when any of its properties are changed. What I've seen in examples is something like: private string property1_; public string Property1 { get { return property1_; } set {…
jcai
  • 3,448
  • 3
  • 21
  • 36
9
votes
3 answers

DRY up CSS - multiple parents to one child

I have several hierarchies separately listed out below where first selector is the parent div, second is the image item within the div. But could I combine these somehow? .outdoors .how-to-image { cursor: pointer; } .snowsports .how-to-image { …
james
  • 3,989
  • 8
  • 47
  • 102