Unsigned Vs. Signed Generally

Started by
19 comments, last by NLDEV 13 years, 3 months ago
Hi,
I'm just wondering whether you prefer unsigned types or signed types if you know the variable is supposed not to be negative and you doesn't need such large numbers that don't fit the signed type to be stored in the variable.

For example:
for(int i = 0; i < numElements; ++i){}
versus
for(unsigned int i = 0; i < numElements; ++i){}

or

void someMethod(int numberOfSomething){}
versus
void someMethod(unsigned int numberOfSomething){}


I prefer signed types since I don't have to mix signed types with unsigned ones then and I can return (pass) some error value (i.e. -1).

What about you?:-)
Advertisement
I used unsigned quite a bit, mostly because it makes error checking look like:
[font="'Courier New"]assert( input < max );
instead of:
[font="'Courier New"]assert( input >= 0 && input < max );

However, not having to cast between signed/unsigned can also make other things cleaner... and I've seen the argument that you should simply never use unsigned in order to remove the possibility of casting bugs (data loss/mutation).
I guess it doesn't matter too much, typically I'll go for a uint for anything that counts something and doesn't need a special error condition. Mostly this is just to cut out the compiler warnings anytime you compare an int versus something like vector.size().
[size=2]My Projects:
[size=2]Portfolio Map for Android - Free Visual Portfolio Tracker
[size=2]Electron Flux for Android - Free Puzzle/Logic Game
I use size_t for number/count of elements. But for iterating I prefer iterators, not incrementing index.
I'm with you on the "-1 for error values" bandwagon. Especially nice when you have a function that is supposed to return an ID of something, you can reserve the -1 as a "not found" type of a result.
Comrade, Listen! The Glorious Commonwealth's first Airship has been compromised! Who is the saboteur? Who can be saved? Uncover what the passengers are hiding and write the grisly conclusion of its final hours in an open-ended, player-driven adventure. Dziekujemy! -- Karaski: What Goes Up...
I always use uint when I know the value cant be negative.

Generally, I reserve zero for invalid / not set id's across the board and provide additional functionality to check for extended error information. Checking for extended information would in this case require an additional function call but most of the time im only interested if a function succeeded or not, and don’t need to identify the specifics of why it failed.

- Kevin.
http://www.kevin-fell.co.uk
It depends extensively on the context -- I use what I feel is best for the specific problem at hand. In general I'd prefer to use unsigned types if the range cannot be negative. But for example, in C# the unsigned types are not CTS compliant so I don't use them in the interfaces of my APIs.
At least in C++, it is customary to always use unsigned types - size_t, actually - for counts and sizes. Error conditions or not found conditions are communicated by using the maximum representable value, as e.g. in std::string::npos. Use std::numeric_limits< ... >::max() to get at this error value.

Obviously everybody is entitled to their own conventions, so simply take this as a FYI to make your code more consistent with the standard library style
Widelands - laid back, free software strategy
I stuck to size_t in C++ — this avoided various signed/unsigned comparison warnings — but .NET's use of int for everything in C# means I do the very opposite for the same reasons there. I don't particularly like magic numbers like -1 though — all too often it introduces corner cases where none was really needed. Worse yet when people don't name their magic numbers.

This is one of my gripes with .NET's string functions.
Out of curiosity (and it'll probably make total sense to me in hindsight rolleyes.gif, but)...

This is one of my gripes with .NET's string functions.

What is? The magic numbers required to use (some of) them?

Edit: Err, duh, you mean the search functions i.e. IndexOf.
Never-mind me.

This topic is closed to new replies.

Advertisement