dry

How do you “not repeat yourself” when giving a class an accessible “name” in C++?

怎甘沉沦 提交于 2019-12-05 05:14:55
Consider the following: class Base { public: virtual std::string getName() = 0; ... }; class Derived1 : public Base { public: static std::string getClassName() { return("Derived1"); } std::string getName() { return("Derived1"); } ... }; class Derived2 : public Base { public: static std::string getClassName() { return("Derived2"); } std::string getName() { return("Derived2"); } ... }; The idea is that if you have the derived class passed as, say, a template parameter, then you can get its class name via getClassName , while if you have it passed as a pointer to base class, you can get the name

Using Marshmallow without repeating myself

你。 提交于 2019-12-05 02:21:50
According to the official Marshmallow docs, it's recommended to declare a Schema and then have a separate class that receives loaded data, like this: class UserSchema(Schema): name = fields.Str() email = fields.Email() created_at = fields.DateTime() @post_load def make_user(self, data): return User(**data) However, my User class would look something like this: class User: def __init__(name, email, created_at): self.name = name self.email = email self.created_at = created_at This seems like repeating myself unnecessarily and I really don't like having to write the attribute names three more

How can you be DRY with a programming language that doesn't have Reflection? [closed]

China☆狼群 提交于 2019-12-05 01:14:48
问题 As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance. Closed 9 years ago . Any programming language that does not have a suitable reflection mechanism I find seriously debilitating for rapidly changing

Django: Reuse form fields without inheriting?

我的未来我决定 提交于 2019-12-05 01:04:43
问题 If I have two forms, based on different base classes (say, Form and ModelForm), but I want to use a few fields in both, can I reuse them in a DRY way? Consider the following scenario: class AfricanSwallowForm(forms.ModelForm): airspeed_velocity = forms.IntegerField(some_important_details_here) is_migratory = forms.BooleanField(more_important_details) class Meta: model = AfricanBird class EuropeanSwallowForm(forms.Form): airspeed_velocity = forms.IntegerField(some_important_details_here) is

Creating html templates using PHP

Deadly 提交于 2019-12-04 21:38:10
问题 Im learning a lot about how MVC frameworks work by looking around and studying existing ones. It seems that every framework I see has a layout where each method in each controller has its own template file. So there will be a login template, a logout template, register, so on and so on. My question is, how and why would you create a template for the entire page all within one file. Lets say you wanted to show the login form on more than one page, wouldn't you have to create the login form for

RESTful grails application: DRYing up UrlMapping

风流意气都作罢 提交于 2019-12-04 21:33:50
问题 Let's say we have a grails web application exposing several resources. tags urls users The application has a classical web-interface which the users interact with and some administration. We want to expose the resources from the application to clients via a RESTful API and we don't want that part of the app to clutter up the controllers and code we already have. So we came up with the following: If the web interface offers host/app_path/url/[list|show|create] we want the REST API to be at

DRY Controllers in Rails 3.2

浪子不回头ぞ 提交于 2019-12-04 20:02:26
问题 Following code climate analysis, I found that my controllers are not DRY as it could be. The methods like: def index @animals = current_user.animals.valid_animals.search(params[:search], params[:page]) respond_to do |format| format.html # index.html.erb format.json { render json: @animals } end end Are basically equal in all the controllers. Basically, the scaffold rails generated code are "the same" in all controllers. How can I made it more clean and dry in a real good way? thanks in

JSLint “eval is evil.” alternatives

与世无争的帅哥 提交于 2019-12-04 19:16:44
问题 I am have some JavaScript functions that run on both the client (browser) and the server (within a Java Rhino context). These are small functions - basically little validators that are well defined and don't rely upon globals or closures - self-contained and portable. Here's an example: function validPhoneFormat(fullObject, value, params, property) { var phonePattern = /^\+?([0-9\- \(\)])*$/; if (value && value.length && !phonePattern.test(value)) return [ {"policyRequirement": "VALID_PHONE

How to loop chained calls elegantly in JavaScript/CoffeeScript?

偶尔善良 提交于 2019-12-04 19:16:39
I'm using Soda to write Selenium tests in Node.js and I have a situation where I have to press the down key several times. The code currently looks like this: browser .chain .setSpeed(200) .session() .open('/') .click("id=save") .focus(editor) .keyDown(editor, '\\40') .keyDown(editor, '\\40') .keyDown(editor, '\\40') .keyDown(editor, '\\40') .keyDown(editor, '\\40') .keyDown(editor, '\\40') .keyDown(editor, '\\40') .keyDown(editor, '\\40') .keyDown(editor, '\\40') .keyDown(editor, '\\40') ... How could I DRY this up? Just using a loop like this does not work with this lib: var b = browser

Does Android Content Provider authority definition break the DRY rule?

不羁的心 提交于 2019-12-04 18:09:00
Android's Content Provider must have : At least one authority must be specified. So for example in Google's samples android-BasicSyncAdapter AndroidManifest.xml there is <provider android:name=".provider.FeedProvider" android:authorities="com.example.android.basicsyncadapter" android:exported="false" /> Then to implement this CP one need to define the same String inside the CP like in android-BasicSyncAdapter FeedProvider.java CONTENT_AUTHORITY public static final String CONTENT_AUTHORITY = "com.example.android.basicsyncadapter"; As we have to define this String twice, isn't this basically