Programming style: should you return early if a guard condition is not satisfied?

I prefer the first style, except that I wouldn’t create a variable when there is no need for it. I’d do this: // Style 3 public SomeType aMethod() { if (!guardCondition()) { return null; } SomeType result = new SomeType(); doStuffToResult(result); doMoreStuffToResult(result); return result; }

Toilet Seat Algorithm

Yes, there is a basic O(1) algorithm. I start with the assumption both people start “ticking” at t=0. I believe the solution should generalize to different starting times, but it isn’t hard to extend from one “free end” of the timeline to two ends. Assume n <= m. Then our timeline looks like this (an … Read more

Collapse and Capture a Repeating Pattern in a Single Regex Expression

Read this first! This post is to show the possibility rather than endorsing the “everything regex” approach to problem. The author has written 3-4 variations, each has subtle bug that are tricky to detect, before reaching the current solution. For your specific example, there are other better solution that is more maintainable, such as matching … Read more

Should I always/ever/never initialize object fields to default values?

Think long term maintenance. Keep the code as explicit as possible. Don’t rely on language specific ways to initialize if you don’t have to. Maybe a newer version of the language will work differently? Future programmers will thank you. Management will thank you. Why obfuscate things even the slightest? Update: Future maintainers may come from … Read more

Is there an algorithm for converting quaternion rotations to Euler angle rotations?

This looks like a classic case of old technology being overlooked – I managed to dig out a copy of Graphics Gems IV from the garage and it looks like Ken Shoemake has not only an algorithm for converting from Euler angles of arbitrary rotation order, but also answers most of my other questions on … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)