The Design of Software (CLOSED)

A public forum for discussing the design of software, from the user interface to the code architecture. Now closed.

The "Design of Software" discussion group has been merged with the main Joel on Software discussion group.

The archives will remain online indefinitely.

Coding Standards question

If I need to do a lot of conversion between seconds and nanoseconds, is it bad form to define a ONE_BILLION constant? (I'm American, so yes, billion)

If not, does anyone have any suggestions?  Littering the code with 1000000000 a bunch of times looks terrible and is a typo away from creating real runtime bugs.
billion dollar baby
Friday, December 01, 2006
 
 
const long nanoseconds_per_second = 1000000000;
Christopher Wells Send private email
Friday, December 01, 2006
 
 
Personally I would use NANOSECS_PER_SEC as it conveys why the  multiply is being done.
sgf
Friday, December 01, 2006
 
 
Thanks!
billion dollar baby
Friday, December 01, 2006
 
 
+1 to encode the meaning, not the value -- the value is already there. Additionally, such constants have a feeling of "I've used this constant for the case that the rules of mathematics and the value of one billion ever happen to change..." and may end up on thedailywtf. ;)
Secure
Saturday, December 02, 2006
 
 
+1 for NANOSECS_PER_SEC. I will also make it define as

const long NANOSECS_PER_SEC=1000*1000*1000;

And place it in the common file header.
Anthony McDaniel
Saturday, December 02, 2006
 
 
Personally, I define or const them, simply because I'm not actually a very good typist.

And in the past I've spent quite a lot time chasing down things like conversions of seconds into nanaseconds which don't have enough zeros in them...
Katie Lucas
Saturday, December 02, 2006
 
 
Are we using floating-point here?  (Please say "yes", or else I hope this is not C where "long" is only 32 bits...)

In floating point one can use the "constant" 1E9 which is reasonably typo-proof.
David Jones Send private email
Saturday, December 02, 2006
 
 
If your goal is to maximise clarity and minimise the possibility of a typo causing a bug, then inline functions should be every bit as efficient as using constants, and they make the intent even clearer:

1. nanoseconds = seconds * 1000000000;

Bad - what is this calculation doing? Is the constant correct?

2. nanoseconds = seconds * NANOSECS_PER_SEC;

Better - but which way is the calculation going?  Is the operator correct?

3. nanoseconds = seconds_to_nanoseconds(seconds);

Best - does what it says, says what it does.

Obviously the advantage of form 3 over form 2 is fairly small if you have well-commented code and competent programmers.  :)
Iago
Saturday, December 02, 2006
 
 
A function is better in case you might want to add to it in the future (e.g. to log or throw if there's an overflow); or define two class (Seconds and Nanoseconds), with conversion operators or constructors between them.
Christopher Wells Send private email
Saturday, December 02, 2006
 
 
I always find that the nameing convention
nanoseconds = nanoseconds_from_seconds(seconds);
is a little clearer.  And I think Joel suggested that somewhere as well.
danielsn
Saturday, December 02, 2006
 
 
Timesystems that use nanoseconds frequently use 64 bits, which in most compilers you can specify by "long long".
Extra large Send private email
Saturday, December 02, 2006
 
 
+1 nanoseconds_from_seconds()
Here's that article: http://www.joelonsoftware.com/articles/Wrong.html
dev1
Saturday, December 02, 2006
 
 
>> In floating point one can use the "constant" 1E9 which is reasonably typo-proof.

Yup, I was thinking this too.  I'd personally be inclined to either use 1e9 if the conversation only took place in a few spots or a function if used more often.  Constants like ONE_BILLION or NANOSECONDS_PER_SECOND just look ridiculous to me.
SomeBody Send private email
Sunday, December 03, 2006
 
 
Using a function / define also helps you if you accidentally get the conversion backwards.  Quick, millijoules -> joules if *1000 or /1000?  I actually made this mistake in some scientific code I was working on - it was nice to only have to change one place when i realized my numbers were off by 6 orders of magnitude.
danielsn
Sunday, December 03, 2006
 
 

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics
 
Powered by FogBugz