dry

DRY unique objects in Django

I want to ensure an object is unique, and to throw an error when a user tries to save it (e.g. via the admin) if not? By unique, I mean that some of the object's attributes might hold the same values as those of other objects, but they can't ALL be identical to another object's values. If I'm not mistaken, I can do this like so: class ...

DRYing Views in Rails (number_to_currency)

I have code similar to: number_to_currency(line_item.price, :unit => "£") littering my views in various models. Since my application deals only in GBP (£), should I not move this into each of my models so that line_item.price returns the string as it should be (i.e. number_to_currency(line_item.price, :unit => "£") and line_item.price ...

Simplest code to sort arrays in ruby?

I usually do something like array.sort{|a,b| a.something <=> b.something} How should I DRY this up? ...

Is the set of SOLID principles missing an extra 'D'?

Although not a pure OOD principle - should DRY also be included when thinking about SOLID principles? If not - why not? ...

Best way to reuse a dialog in WPF?

I have a WPF dialog with a couple user controls and some other standard controls. We have many uses for this dialog with specific tweaks. Right now this dialog manages all of the logic to tweak itself. I can't help but think there's gotta be a better way. I wish I could do something like this: Original dialog <dialog> <Control1> ...

DRY form partial for create and update

I have a _form.html.erb form partial which helps to DRY up my code but I need the form to have different labels depending on if I am creating a new user or updating an existing user. Here is my form partial. I don't need to show the eula checkbox during update and I also need to replace the "Create my account" submit button text to some...

Asp.net - Refactor action

Hi! I have this "problem" that i have code like this in many of my controller actions: var users = new List<SelectListItem>(); foreach(var user in userRepository.GetAll()) { var item = new SelectListItem { Text = user.FriendlyName, Value = user.UserId.ToString() }; if (User.Identity.Name == user.UserName) item.Selected = true; u...

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? ...

"inheriting" ASP.NET MVC sites from a common template app? (multi-tenancy)

We're building about 10 ASP.NET MVC sites which have a common set of features (and corresponding URLs, Routes, Controllers, Actions, and Views). The sites will also all share a base set of domain objects (e.g. users, companies) and base attributes on those objects (e.g. name, address, etc.). But each site will also be highly customize...

Refactoring controller code for DRYness

Hi, I have numerous statements like these in both my create and update action of my controller: @company.set_preference(:api_username, params[:company]['api_username']) if params[:company]['api_username'] @company.set_preference(:api_password, params[:company]['api_password']) if params[:company]['api_password'] I'm wondering if how ...

Python optparse defaults vs function defaults

I'm writing a python script which I would like to be able to both call from the command line and import as a library function. Ideally the command line options and the function should use the same set of default values. What is the best way to allow me to reuse a single set of defaults in both places? Here's the current code with duplic...

Django - use reverse url mapping in settings

A few of the options in the django settings file are urls, for example LOGIN_URL and LOGIN_REDIRECT_URL. Is it possible to avoid hardcoding these urls, and instead use reverse url mapping? At the moment this is really the only place where I find myself writing the same urls in multiple places. ...

When to stop DRYing up the code?

So DRYing up code is supposed to be good thing right? There was a situation in one of the projects I was working on where there were certain models/entities that were more-or-less the same except the context in which they were being used. That is, Every such entity had a title, descriptions, tags, a user_id etc and some other attributes....

D.R.Y vs "avoid macros"

I am creating my own implementation of XUL in C++ using the Windows API. The fact that the elements are constructed by the XML parser requires that they have identical interfaces, so that we don't need to write custom code for each element constructor. The result is that most of my elements look like this: class Button : public Element ...

How to specify a parameter as part of every web service call?

Currently, each web service for our application has a user parameter that is added for every method. For example: @WebService public interface FooWebService { @WebMethod public Foo getFoo(@WebParam(name="alwaysHere",header=true,partName="alwaysHere") String user, @WebParam(name="fooId") Long fooId); @WebMethod public Result d...

Jquery - (re)wiring dynamically generated elements

Often times I have elements hooked to added functionality, like: $('.myfav').autocomplete(); $('.myfav').datepicker(); $('.myfav').click(somefunction); But when more instances of this class are generated dynamically through some code, the new $('.myfav') are dead and need rewiring, so I do this: $("#somelink").click(function(){ ...

Excel: How to do DRY formatting?

I have some conditional formatting styles. I don't want to keep creating new rules for new ranges; I would rather follow DRY by declaring it once and referencing it elsewhere. I'm having difficulty doing this. The conditional formatting rule works fine when it's just one range. =Travel!$C$5:$P$8 However, I then try to add another ran...

How to DRY this snippet of Ruby code?

This is bothering me. It doesn't look too DRY. What would be a better implementation? As an aside, how come this ActiveRecord finder doesn't throw an exception when record is not found, but .find does? def current_account return @account if @account unless current_subdomain.blank? @account = Account.find_by_host(current...

Java: DRY out these two functions

These two functions have basically the same code. How can I factor it out? public static String[] removeSuffix(String gA, String gB) { String[] results = new String[2]; results[0] = gA; results[1] = gB; if (gA.equals("") || gB.equals("")) { return results; } if (gA.charAt(gA.length() - 1) == gB.cha...

How do I prevent the repetition of business logic?

OK. So here's my simplified scenario. We have a system which handles orders for a number of clients. We want staff users to be able to view all orders and we want client user to only be able to view orders which relate to them. When attempting to view a particular record we make use of the following function in our OrderSecurity class: ...