Computer Chess Club Archives


Search

Terms

Messages

Subject: Re: a question about the value of checking bounds

Author: Daniel Clausen

Date: 12:00:41 01/21/03

Go up one level in this thread


Hi

On January 21, 2003 at 14:18:05, Scott Gasch wrote:

>One of the things I catch myself doing a lot in my code is using signed ints
>where I should be using unsigned.  It's easier to type "int" than "unsigned" and
>I'm lazy.  But I am trying to change!  Keep an eye out for this in your code.

Two things:

(a)
Using the right type not only makes the application less buggy, it also makes it
more readable in the sense, the the interface is clearer.

Silly example: unsigned getNumberOfPieces(const Board& board);

It's clear that the number of pieces is never below zero. Using 'unsigned' not
only makes this absolutely clear, you also never have to make sure somewhere,
that the returned number is >=0. (the type already says so)

Additionally, the const-qualifier in the parameter list makes it clear, that the
function won't change anything within the Board structure. (or class) Again,
this not only makes the whole thing a bit safer, but also clearer. (especially
when the name of the method is not that clear - which is a fault in itself
though ;)


(b)
In order to not fall in the trap to use use 'int' where I meant 'unsigned' etc,
I try to use typedefs, which for example map 'unsigned long long' to 'hash_t'.
This has the following advantages:

-it's clearer to read for me. (I can also see from the type in a parameter list,
that the parameter is a hash-value)
-i don't write 'int' where I meant 'unsigned'
-i can easily port to a different platform, which has a different type for an
unsigned 64bit value in this case

Oh, and I love asserts. (at the right places - not _everywhere_)

Sargon



This page took 0 seconds to execute

Last modified: Thu, 15 Apr 21 08:11:13 -0700

Current Computer Chess Club Forums at Talkchess. This site by Sean Mintz.