Basic Data Types

I’ve been reading the single most influential book every programmer should read since last month and there is a really nice chapter about basic data types. Actually, when I read the chapter’s title, I thought reading it would be a waste of time, but I was wrong. There are some quick tips that really worth its reading and is about them that we will be talking today.

Magic Numbers

The first tip is avoiding magic numbers, which are literal numbers spread all over the code with no explanation. For example:

for ( int i = 0; i < 65; i++) { ... } [/sourcecode] Does anyone here know what 65 means? So, if your language supports named constants, use them in place of the number itself. If named constants are not supported, you can use global variables too. Avoiding magic numbers provide tree advantages:

  • Changes can be made in a more confident way.
  • Changes can be made in a more easy way.
  • You improve your code legibility.

The code above can be improved by using a macro, as shown below:

#define MAX_ENTRIES 65
for ( int i = 0; i < MAX_ENTRIES-1; i++) { ... } [/sourcecode] Remember: the only literal values that should occur in your program are 0 and 1. The others should be placed with a more descriptive name. Integer Operations

Be careful when dividing two integers. Keep in mind that 7/10 it’s not 0,7. The result is, in most cases, 0 (zero), varying from one language to other. In real world, we’ve learned that 10*(7/10) = (10*7)/10 = 7, which is not that same in integer’s arithmetic: 10*(7/10) = 0 because 7/10 = 0.

Other problem is integer overflows. When operating integers, such as additions and multiplications, you must have in mind the biggest possible value. For example, if you multiply 250*300 the result is 75.000, but if the maximum possible value is 65.535, the result is 9.464 due to a integer overflow (75.000 – 65.536 = 9.464).

Float Operations

The main consideration about float numbers is that most decimal fractions can’t be represented with precision, with basically 7 or 15 digits. Because of that, avoid operating float numbers with different sizes. For example, what’s the result of 1.000.000,00 + 0,1? Probably, this operation will return 1.000.000,00 because 32 bits don’t offer significant digits between the interval 1.000.000 and 0,1. The same happens here: 5.000.000,02 – 5.000.000,01 probably will return 0,0.

A classic problem with float numbers is the incremental of 0,1 in a for loop.

double nominal = 1.0;
double sum = 0.0;

for ( int i = 0; i < 10; i++ ) { sum += 0.1; } if ( nominal == sum ) { System.out.println("Same"); } else { System.out.println("Different"); } [/sourcecode] As you already know, the result is “Different”. After the loop, sum = 0,999999999999999 and the comparison fails. The work around here is using an acceptable precision interval and a boolean function to determine the equality of both numbers. [sourcecode language='java'] final double ACCEPTABLE_DELTA = 0.00001; boolean Equals( double n1, double, n2)  { if ( Math.abs ( n1 - n2 ) < ACCEPTABLE_DELTA ) { return true; } return false; } [/sourcecode] If you are working with currency values, this solution is not recommended. An alternative is passing float numbers to 64bits integer, by multiplying them for 100. After the operations has completed, pass the values again to float, by dividing them for 100. Boolean Variables

Boolean variables are useful to document your program. Instead of testing a boolean expression, you can assign it to a variable, making your code easy to read. For example:

if ( (elementIndex < 0)       || (MAX_ELEMENTS < elementIndex)       || (elementIndex == lastElementIndex) ) { … } [/sourcecode] This piece of code can be improved by creating local variables, as below: [sourcecode language='cpp'] finished = (elementIndex < 0) || (MAX_ELEMENTS < elementIndex); repeatedEntry = (elementIndex == lastElementIndex); if ( finished || repeatedEntry) { … } [/sourcecode] Some languages have a predefined boolean type, such as Java and C++, others don’t. But it’s possible to define your own Boolean type. In C, for example, you may use the code below: [sourcecode language='cpp'] typedef int BOOLEAN enum Boolean { True = 1, False = (!True) }; [/sourcecode] Conclusion

We have covered Integers, Float and boolean variables. In addition, the book chapter also mentions characters and strings, enum types and arrays, but these are topics for a future post.

See you,


Fernando

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s