Hacker News new | ask | show | jobs
by strommen 4428 days ago
HN Challenge: name a scenario in which defining a constant as 86400 is better then defining it as (60 * 60 * 24).

Or, are there languages where you can't declare a constant with a multiplication expression?

3 comments

> are there languages where you can't declare a constant with a multiplication expression?

obviously PHP.

  php > const X = 1;
  php > echo X;
  1
  php > const Y = 1 * 2;

  Parse error: parse error, expecting `','' or `';'' in php shell code on line 1
  php > echo Y;
  Y
Fixed in the (upcoming) PHP 5.6.

https://wiki.php.net/rfc/const_scalar_exprs

To me it's a coding style thing. The argument for 86400 would be, too many operators makes things damn confusing. When your define expands in the preprocessor to a function sixteen operators long, you have made a mess.
I find myself with pre-optimization issues on my new code and define constants multiplied out rather than add that .000000001% extra overhead of doing the multiplication - it's a sickness.
Actually in all likeliness your compiler will do tgat for you anyway.