Donnerstag, 25. September 2008

StackOverflow

I've found a great tool for programming questions: StackOverflow

It's a very web 2.0 way to find solutions for software development questions. If you're serious about your job, you should check it out to know how it works. Used in the right situation, it can save you large amounts of time.

Samstag, 20. September 2008

0

A short but important rule:

Whenever you write code with an integer divison operation, write code around it that avoids or handles a division by zero. 

This should happen completely automatic when you use the division operator. It should immediately cross your mind that this call could be dangerous.

If you don't obey this rule, sooner or later your code will blow up. In C/C++ your whole process will die (a legacy of the days when software was on punched cards and the mainframe would stop processing on errors). In more modern languages, you will get thrown out of your method with a division-by-zero exception if you don't have an exception handler around it.
 
Typical error situations: 
  • Calculating averages (value DIV number-of-samples) with a zero number of samples. 
  • Variation: Dividing by time duration (x DIV (time_after MINUS time_before), but the duration was below one second and in the same second of system time, so your duration value became zero. 
  • Using some floating point value smaller than 1 as divisor when your compiler creates an integer division from what you thought was a floating point division (this is especially dangerous in languages that convert easily between numerical types and don't have a separate integer division operator) Also occurs when code is bulk-converted to non-floating-point embedded hardware.
  • Hidden division in modulo calculations. a MOD b with b=0 will typically cause the same trouble as a division by zero.
Recommended reading to understand the importance and danger of zero: 
Charles Seife, Zero: The biography of a dangerous idea. 
(German Title: Zwilling der Unendlichkeit)