DRY Ruby Initialization with Hash Argument

You don’t need the constant, but I don’t think you can eliminate symbol-to-string: class Example attr_reader :name, :age def initialize args args.each do |k,v| instance_variable_set(“@#{k}”, v) unless v.nil? end end end #=> nil e1 = Example.new :name => ‘foo’, :age => 33 #=> #<Example:0x3f9a1c @name=”foo”, @age=33> e2 = Example.new :name => ‘bar’ #=> #<Example:0x3eb15c @name=”bar”> … Read more

When to use std::size_t?

A good rule of thumb is for anything that you need to compare in the loop condition against something that is naturally a std::size_t itself. std::size_t is the type of any sizeof expression and as is guaranteed to be able to express the maximum size of any object (including any array) in C++. By extension … Read more

Is there a downside to adding an anonymous empty delegate on event declaration?

Instead of inducing performance overhead, why not use an extension method to alleviate both problems: public static void Raise(this EventHandler handler, object sender, EventArgs e) { if(handler != null) { handler(sender, e); } } Once defined, you never have to do another null event check again: // Works, even for null events. MyButtonClick.Raise(this, EventArgs.Empty);

LBYL vs EAFP in Java?

If you are accessing files, EAFP is more reliable than LBYL, because the operations involved in LBYL are not atomic, and the file system might change between the time you look and the time you leap. Actually, the standard name is TOCTOU – Time of Check, Time of Use; bugs caused by inaccurate checking are … Read more