The usage of Unsigned types
-
Hey, I would like to discuss the value of using Unsigned value types for ensure the validy of your buissnes logic. Just to name some exampels. Does it make sense to let the Linq Extention 'Skip' or 'Take' be, accepting values smaller then 0? No not for me, but then why it allows this behavior by taking a int instead of an uint? Don't mention that the uint takes less memory but this is not the point. I meant that the same space in memory can hold a bigger number ( for int ) then the signed as we got a bit more to store our data. ( sory for the vague explanation ) I Developed a network lib ( currently not OpenSource but soon ;-) ) that takes a Port. i saw that other libs are using an int at this point. This is absolute bad from my point of view. The only valid value type here is the Ushort ( 0 to 65,535 ). It does not allow a non Positiv number and limits the parameter to the buissnes logic. So why are thees value types not used that often?
-
Hey, I would like to discuss the value of using Unsigned value types for ensure the validy of your buissnes logic. Just to name some exampels. Does it make sense to let the Linq Extention 'Skip' or 'Take' be, accepting values smaller then 0? No not for me, but then why it allows this behavior by taking a int instead of an uint? Don't mention that the uint takes less memory but this is not the point. I meant that the same space in memory can hold a bigger number ( for int ) then the signed as we got a bit more to store our data. ( sory for the vague explanation ) I Developed a network lib ( currently not OpenSource but soon ;-) ) that takes a Port. i saw that other libs are using an int at this point. This is absolute bad from my point of view. The only valid value type here is the Ushort ( 0 to 65,535 ). It does not allow a non Positiv number and limits the parameter to the buissnes logic. So why are thees value types not used that often?
I agree.
-
Hey, I would like to discuss the value of using Unsigned value types for ensure the validy of your buissnes logic. Just to name some exampels. Does it make sense to let the Linq Extention 'Skip' or 'Take' be, accepting values smaller then 0? No not for me, but then why it allows this behavior by taking a int instead of an uint? Don't mention that the uint takes less memory but this is not the point. I meant that the same space in memory can hold a bigger number ( for int ) then the signed as we got a bit more to store our data. ( sory for the vague explanation ) I Developed a network lib ( currently not OpenSource but soon ;-) ) that takes a Port. i saw that other libs are using an int at this point. This is absolute bad from my point of view. The only valid value type here is the Ushort ( 0 to 65,535 ). It does not allow a non Positiv number and limits the parameter to the buissnes logic. So why are thees value types not used that often?
Unsigned integers and signed bytes are not CLS Compliant; some languages targeting .NET might not have equivalent types, and would therefore be unable to use methods with unsigned integer parameters. Language Independence and Language-Independent Components : Types and type member signatures[^] Non CLS-Compliant Code in C#[^] Writing CLS-Compliant Code[^]
Jean-Pierre Bachmann wrote:
Don't mention that the uint takes less memory but this is not the point.
Not to mention the fact that it's wrong. ;P
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
-
Hey, I would like to discuss the value of using Unsigned value types for ensure the validy of your buissnes logic. Just to name some exampels. Does it make sense to let the Linq Extention 'Skip' or 'Take' be, accepting values smaller then 0? No not for me, but then why it allows this behavior by taking a int instead of an uint? Don't mention that the uint takes less memory but this is not the point. I meant that the same space in memory can hold a bigger number ( for int ) then the signed as we got a bit more to store our data. ( sory for the vague explanation ) I Developed a network lib ( currently not OpenSource but soon ;-) ) that takes a Port. i saw that other libs are using an int at this point. This is absolute bad from my point of view. The only valid value type here is the Ushort ( 0 to 65,535 ). It does not allow a non Positiv number and limits the parameter to the buissnes logic. So why are thees value types not used that often?
Jean-Pierre Bachmann wrote:
Don't mention that the uint takes less memory
Um. No, it doesn't. A uint occupies the same amount of memory as a signed int - 32 bits, or 4 bytes - and takes the same number of different values. The difference is that it's range of values is different:
int: -2147483648 to 2147483647
uint: 0 to 4294967295Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
-
Hey, I would like to discuss the value of using Unsigned value types for ensure the validy of your buissnes logic. Just to name some exampels. Does it make sense to let the Linq Extention 'Skip' or 'Take' be, accepting values smaller then 0? No not for me, but then why it allows this behavior by taking a int instead of an uint? Don't mention that the uint takes less memory but this is not the point. I meant that the same space in memory can hold a bigger number ( for int ) then the signed as we got a bit more to store our data. ( sory for the vague explanation ) I Developed a network lib ( currently not OpenSource but soon ;-) ) that takes a Port. i saw that other libs are using an int at this point. This is absolute bad from my point of view. The only valid value type here is the Ushort ( 0 to 65,535 ). It does not allow a non Positiv number and limits the parameter to the buissnes logic. So why are thees value types not used that often?
-
Hey, I would like to discuss the value of using Unsigned value types for ensure the validy of your buissnes logic. Just to name some exampels. Does it make sense to let the Linq Extention 'Skip' or 'Take' be, accepting values smaller then 0? No not for me, but then why it allows this behavior by taking a int instead of an uint? Don't mention that the uint takes less memory but this is not the point. I meant that the same space in memory can hold a bigger number ( for int ) then the signed as we got a bit more to store our data. ( sory for the vague explanation ) I Developed a network lib ( currently not OpenSource but soon ;-) ) that takes a Port. i saw that other libs are using an int at this point. This is absolute bad from my point of view. The only valid value type here is the Ushort ( 0 to 65,535 ). It does not allow a non Positiv number and limits the parameter to the buissnes logic. So why are thees value types not used that often?
Because it is annoying to mix int and uint. Unlike in, say, C++, you get casts everywhere. short/ushort is even more annoying. Also even if you did use them, they wouldn't really work the way you'd like them to work (ensuring the input is valid I guess), they'd just be wrong differently. If you cast an int that is accidentally -1 to an uint and give it to Take it will still be a bug. If you cast 10000000 to an ushort and pretend it's a port, it'll be a valid port but it won't be the right port. And so on.
-
Hey, I would like to discuss the value of using Unsigned value types for ensure the validy of your buissnes logic. Just to name some exampels. Does it make sense to let the Linq Extention 'Skip' or 'Take' be, accepting values smaller then 0? No not for me, but then why it allows this behavior by taking a int instead of an uint? Don't mention that the uint takes less memory but this is not the point. I meant that the same space in memory can hold a bigger number ( for int ) then the signed as we got a bit more to store our data. ( sory for the vague explanation ) I Developed a network lib ( currently not OpenSource but soon ;-) ) that takes a Port. i saw that other libs are using an int at this point. This is absolute bad from my point of view. The only valid value type here is the Ushort ( 0 to 65,535 ). It does not allow a non Positiv number and limits the parameter to the buissnes logic. So why are thees value types not used that often?
Jean-Pierre Bachmann wrote:
Don't mention that the uint takes less memory but this is not the point.
uint does not take less memory than a int, they have the same exact size - it changes only the representation from pure binary to 2's complement. Integers are the basic echange unit in the system, they allow checking for overflows and underflows: if you use a ushort in a port number and have something bad that wraps around you won't get a negative number but a perfectly valid port number. If you'd have something that overflows you would still get a perfectly valid port number. Unless you have to store/send 2 bytes values in some form of structure or have to make bit operations on 16bit wide operands there is no real need of shorts - and if you don't have to convert decimal values in 16 bits values you won't need unsigned versions of it either since math operations yields off the same binary signature. Even if you should read a decimal value and convert it in a 0-65535 range it would be better to use an int as a middle step because there may be the possibility of reading a greater number. These are my reasons, stemmed from my experience of course.
Geek code v 3.12 GCS d--- s-/++ a- C++++ U+++ P- L- E-- W++ N++ o+ K- w+++ O? M-- V? PS+ PE- Y+ PGP t++ 5? X R++ tv-- b+ DI+++ D++ G e++>+++ h--- r++>+++ y+++* Weapons extension: ma- k++ F+2 X I use 1TBS
-
Because it is annoying to mix int and uint. Unlike in, say, C++, you get casts everywhere. short/ushort is even more annoying. Also even if you did use them, they wouldn't really work the way you'd like them to work (ensuring the input is valid I guess), they'd just be wrong differently. If you cast an int that is accidentally -1 to an uint and give it to Take it will still be a bug. If you cast 10000000 to an ushort and pretend it's a port, it'll be a valid port but it won't be the right port. And so on.
Jea but this is the point, i am forced to cast my number to an ushort, and if its not possible the fault is on "your" side. I are not even able to cast your 10000000 to ushort because its not possible for that way i enforce you to validate by your self and im force you to thing about your code that that's is obviously a good thing or not? Same as Take.
ushort test;
int test2 = 10000000;
test = (ushort)test2;will compile but cause an error at Runtime. But in your code. in that way i protect my pice of code and enforce you to do the same on your side.
-
Jea but this is the point, i am forced to cast my number to an ushort, and if its not possible the fault is on "your" side. I are not even able to cast your 10000000 to ushort because its not possible for that way i enforce you to validate by your self and im force you to thing about your code that that's is obviously a good thing or not? Same as Take.
ushort test;
int test2 = 10000000;
test = (ushort)test2;will compile but cause an error at Runtime. But in your code. in that way i protect my pice of code and enforce you to do the same on your side.
-
Hey, I would like to discuss the value of using Unsigned value types for ensure the validy of your buissnes logic. Just to name some exampels. Does it make sense to let the Linq Extention 'Skip' or 'Take' be, accepting values smaller then 0? No not for me, but then why it allows this behavior by taking a int instead of an uint? Don't mention that the uint takes less memory but this is not the point. I meant that the same space in memory can hold a bigger number ( for int ) then the signed as we got a bit more to store our data. ( sory for the vague explanation ) I Developed a network lib ( currently not OpenSource but soon ;-) ) that takes a Port. i saw that other libs are using an int at this point. This is absolute bad from my point of view. The only valid value type here is the Ushort ( 0 to 65,535 ). It does not allow a non Positiv number and limits the parameter to the buissnes logic. So why are thees value types not used that often?
Jean-Pierre Bachmann wrote:
This is absolute bad from my point of view. .... So why are thees value types not used that often?
Because people are more concerned, rightfully so, in making sure that the functional logic of the software is correct versus trivial considerations. In general it is probably 'easier' to use an integer. And your library should probably expose it as such and you should add the one statement in your code where it is exposed to do a validation check. And then move on to make sure that the rest of your library works as promised.