dry

How to reuse a large query without repeating it?

让人想犯罪 __ 提交于 2019-11-30 11:56:12
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 terribly_large_and_useful_query , and the result set it produces I want to use as part of several future queries. How can I avoid copying and pasting the same queries in multiple places? How can I "not repeat myself," and follow DRY principles. Is this possible in SQL? I'm using Oracle SQL. Portable SQL solutions are preferable, but if I have to use an

DRYing rails view: partial vs helper [duplicate]

烈酒焚心 提交于 2019-11-30 11:14:34
This question already has an answer here: When to use Helpers instead of Partials 6 answers 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 %> <footer> <%= marks_bar(@item) %> <%= favorite_button(@item, "blog_item") || delete_from_favorite_button(@item, "blog_item") %> <%= share_button(@item) %> <% if current_user.is_mine?(@item) %> <div><%= link_to "Edit", edit_user_blog_item_path(current_user, @item) %></div> <% end %>

Localized project with several targets with localized app names

馋奶兔 提交于 2019-11-30 11:09:00
I have recently merged together 5 of my stand-alone projects into one project to have a common code base. So now I have one project with 5 targets instead. Each target has the same fileset, except for some files which differ for each target (such as Default.png, and icons files amongst others). My application is translated to 7 languages with English as default. The other languages are: Swedish, Dutch, German, French, Polish and Spanish. Now I would also like to translate the name of the application depending the language used. For instance, for my Swedish aviation app, the app should be

DRY arithmetic expression evaluation in Prolog

你说的曾经没有我的故事 提交于 2019-11-30 09:36:10
问题 I wanted to write evaluating predicate in Prolog for arithmetics and I found this: eval(A+B,CV):-eval(A,AV),eval(B,BV),CV is AV+BV. eval(A-B,CV):-eval(A,AV),eval(B,BV),CV is AV-BV. eval(A*B,CV):-eval(A,AV),eval(B,BV),CV is AV*BV. eval(Num,Num):-number(Num). Which is great but not very DRY. I've also found this: :- op(100,fy,neg), op(200,yfx,and), op(300,yfx,or). positive(Formula) :- atom(Formula). positive(Formula) :- Formula =.. [_,Left,Right], positive(Left), positive(Right). ?- positive((p

How to customize to_json response in Rails 3

帅比萌擦擦* 提交于 2019-11-30 08:37:57
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 when the data is simple, but with complex finds, they fall short of what I want. Lets say I have a post which has_many images def show @post = Post.find params[:id] respond_with(@post) end I want to include the images with the response so I could do this: def show @post = Post.find params[:id] respond_with(@post, :include => :images) end but I dont

Reducing code duplication between operator= and the copy constructor

强颜欢笑 提交于 2019-11-30 06:46:13
问题 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? 回答1: There's no "general way" for writing custom copy constructors and assignment operators that works in all cases. But there's an idiom called "copy-&-swap": class myclass { ... public: myclass(myclass const&); void swap(myclass & with); myclass& operator=(myclass

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

限于喜欢 提交于 2019-11-30 02:38:23
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 that's not very DRY, so I figured I would move it into a partial. Since it's shared between two models - where would I place that partial ? Thanks! The Rails convention is to put shared partials in /app/views/shared . Update Layout inheritance is now in the guides under layout and rendering Template inheritance works similarly. Rails 3.1 and

Rails 3 - Restricting formats for action in resource routes

拜拜、爱过 提交于 2019-11-29 21:20:16
I have a resource defined in my routes. resources :categories And I have the following in my Category controller: def show @category = Category.find(params[:id]) respond_to do |format| format.json { render :json => @category } format.xml { render :xml => @category } end end The controller action works fine for json and xml. However I do NOT want the controller to respond to html format requests. How can I only allow json and xml? This should only happen in the show action. What is the best way to achieve this? Also is there any good tips for DRYing up the respond_to block? Thanks for your help

How much duplicated code do you tolerate? [closed]

谁说我不能喝 提交于 2019-11-29 21:15:21
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 agree extracting the duplicated logic would hurt readability a little. I know DRY is guideline, not an absolute rule. But in general, are you willing to hurt readability in the name of DRY? Nick Dandoulakis Refactoring: Improving the Design of Existing Code The Rule of Three The first time you do something, you just do it. The second time you do

Best practice for reusing python code [closed]

放肆的年华 提交于 2019-11-29 20:09:55
I have write a python library app(which contains several *.py files). And several of my python projects need to reuse the code in the library app. What's the recommended best practice for reusing python code? Currently I have thought out three options: Copy and paste. This is far away from best practice. It violates the DRY principle.(Don't repeat yourself.) Add the folder of the library app to the environment variable PYTHONPATH: export PYTHONPATH=/path/to/library/app . Then every projects on the same computer can reference the code in the library app. And the folder of the library app to sys