dry

Change boolean flags into template arguments

ぐ巨炮叔叔 提交于 2019-12-06 06:25:11
Suppose I have a versatile function with about four boolean flags: int do_something(int arg, bool flag1, bool flag2, bool flag3, bool flag4) { for(int i = 0; i < 1000000; i++) { if(flag1) // Do something 1 if(flag2) // Do something 2 if(flag3) // Do something 3 if(flag4) // Do something 4 //Do something else 5 } } But I don't want to incur any costs for branching on these flags in the inner loop so I change them to templates (allowing the compiler to optimize away the conditionals): template<bool flag1, bool flag2, bool flag3, bool flag4> int do_something_helper(int arg) { for(int i = 0; i <

How to achieve this functionality using Generics?

不羁岁月 提交于 2019-12-06 03:57:39
I am not experienced in using .Net generics. I am designing a software system for and Investment Holding Company in .Net 4.0. The company has Retail business and IntellectualRights business. BookShop and AudioCDShop are examples of Retail business. EngineDesignPatent and BenzolMedicinePatent are examples of IntellectualRights business. These two business types are totally unrelated. The investment company has a concept called InvestmentReturn. It is the profit gained from each business. For each “Business Type” (Retail, IntellectualRights ), the calculation logic is different for Investment

Same Laravel resource controller for multiple routes

£可爱£侵袭症+ 提交于 2019-12-06 03:50:04
问题 I am trying to use a trait as a typehint for my Laravel resource controllers. The controller method: public function store(CreateCommentRequest $request, Commentable $commentable) In which the Commentable is the trait typehint which my Eloquent models use. The Commentable trait looks like this: namespace App\Models\Morphs; use App\Comment; trait Commentable { /** * Get the model's comments. * * @return \Illuminate\Database\Eloquent\Relations\MorphMany */ public function Comments() { return

DRY IDisposable Pattern

我是研究僧i 提交于 2019-12-06 03:04:51
A lot of my classes repeat the below code to implement IDisposable. This seems to violate the DRY (Don't Repeat Yourself) principle. I could avoid some of the work by creating an AbstractDisposable base class, but that seems inappropriate / wouldn't work if I needed to extend other existing objects (assuming those objects weren't themselves disposable). Another option would be to use a template/meta language where I could specify lists of managed and unmanaged resources for each class and have the generic Dispose Pattern auto generated when I build my project - but so far I've not played with

Rails 3 strip whitespace before_validation on all forms

為{幸葍}努か 提交于 2019-12-06 00:31:30
问题 I'm relatively new to Rails and a bit surprised this isn't a configurable behavior...at least not one I've been able to find yet?!? I would have thought that 99% of forms would benefit from whitespace being trimmed from all string & text fields?!? Guess I'm wrong... Regardless, I'm looking for a DRY way to strip all whitespace from form fields (of type :string & :text) in a Rails 3 app. The Views have Helpers that are automatically referenced (included?) and available to each view...but

try… except… except… : how to avoid repeating code

好久不见. 提交于 2019-12-05 18:58:46
I'd like to avoid writting errorCount += 1 in more than one place. I'm looking for a better way than success = False try: ... else: success = True finally: if success: storage.store.commit() else: storage.store.rollback() I'm trying to avoid store.rollback() in every except clause. Any idea on how to do this? count = 0 successCount = 0 errorCount = 0 for row in rows: success = False count += 1 newOrder = storage.RepeatedOrder() storage.store.add(newOrder) try: try: newOrder.customer = customers[row.customer_id] except KeyError: raise CustomerNotFoundError, (row.customer_id,) newOrder.nextDate

Share Constants Between PHP and JavaScript [duplicate]

点点圈 提交于 2019-12-05 14:17:38
问题 This question already has answers here : Closed 7 years ago . Possible Duplicate: Pass a PHP string to a Javascript variable (and escape newlines) I have several constants in a PHP application I'm developing. I've defined a Constants class and the defined the constants as const VAR_NAME = value; in this class. I would like to share these constants between my JavaScript and PHP code. Is there a DRY (Don't Repeat Yourself) mechanism to share them? class Constants { const RESOURCE_TYPE_REGSITER

Generating WAR and EAR artifacts inside the same Gradle project

那年仲夏 提交于 2019-12-05 12:14:03
Suppose that I have a Gradle web project with a standard web project layout src/main/webapp . Project myproject-web , build.gradle : apply plugin: 'java' apply plugin: 'eclipse-wtp' apply plugin: 'war' I know that I can wrap the resulting .war file generated by myproject-web inside an .ear if I define a secondary project (say, myproject-ear ) which applies the ear plugin and references myproject-web as a dependency: Project myproject-ear , build.gradle : deploy project(':myproject-web') I know it's a bit pointless to create an .ear file with only one .war inside (but that's how I'm required to

Rails I18n: Better way of interpolating links?

吃可爱长大的小学妹 提交于 2019-12-05 06:34:25
Is there a cleaner, content_tag-ish way of doing this? (I don't want HTML in my YML) en.yml: expert_security_advice: "Go <a href='{{url}}'>here</a> for expert security advice." layout.html.erb: <%= t(:expert_security_advice, :url => "http://www.getsafeonline.org") %> The best I could come up with: en.yml: expert_security_advice: "Go *[here] for expert security advice." layout.html.erb: <%= translate_with_link(:expert_security_advice, "http://www.getsafeonline.org") %> application_helper.rb: include ActionView::Helpers::TagHelper def translate_with_link(key, *urls) urls.inject(I18n.t(key)) { |s

DRY (Don't Repeat Yourself) and if assignements

醉酒当歌 提交于 2019-12-05 06:31:57
问题 I think I'm forgetting something evident but I can't seem to find a way to assign a value if it validates a condition remaining as DRY as possible... Some code to explain what I mean ... a = (b > 1) ? b : c; or even a = (a > 1) ? a : b; So of course here it's no big deal but if a was to be replaced by an method call, (maybe a yield return there) or whatever, I would then have to call it twice... Only thing I see is stocking it in a variable which would then be as the code above... Any better