c++ standard practice: virtual interface classes vs. templates

You’re basically right, dynamic polymorphism (inheritance, virtuals) is generally the right choice when the type should be allowed to change at runtime (for example in plugin architectures). Static polymorphism (templates) is a better choice if the type should only change at compile-time. The only potential downsides to templates are that 1) they generally have to … Read more

Multiple or Single Try Catch [duplicate]

I always try to reduce levels of nesting for readability and maintainability. If you have n try/catch blocks, each handling the same type of exception, why not refactor the code that can throw the exception into methods…it would look something like: try { firstBatchOfTricky(); secondBatchOfTricky(); …. nthBatchOfTricky(); } catch (ItWentBoomException e) { // recover from … Read more

Is there a difference between main(String args[]) and main(String[] args)?

Semantically, they are identical. However, I’d recommend using the latter syntax (String[] args) when declaring arrays. The former syntax is there mainly for compatibility with C syntax. Since String[], as a whole, is the type of the object in Java, it’s more consistent and clear not to split it up. A similar question addresses the … Read more

Get rid of ugly if statements

How about such approach: int getSize(int v) { int[] thresholds = {145, 117, 68, 51, 22, 10}; for (int i = 0; i < thresholds.length; i++) { if (v > thresholds[i]) return i+1; } return 1; } Functionally: (Demonstrated in Scala) def getSize(v: Int): Int = { val thresholds = Vector(145, 117, 68, 51, 22, … Read more