Should I use unsigned integers for counting members?

前端 未结 6 1982
自闭症患者
自闭症患者 2021-02-04 14:25

Should I use unsigned integers for my count class members?

Answer

For example, assume a class

TList  = class
private
           


        
相关标签:
6条回答
  • 2021-02-04 14:57

    Don't.

    It's not just going against a programming idiom, it's an explicit request to the compiler to use unsigned arithmetic, that is either prone to anomalous behaviors (if you don't guard against overflows) or to irrelevant runtime exceptions (if you guard against overflows, a temporary overflow will be fatal, f.i when you subtract before adding, even if the final result is positive, and I'm referring to the CPU opcode-level ordering of operations, which may not bear a trivial relationship to what you have in your code).

    Keep in mind "unsigned" does not translate to "positive", it translates to "doesn't have a sign", which is different. The term "unsigned" was picked for good reason (and naming it "Cardinal" in Delphi was a poor choice IMO).

    Unsigned types are for raw storage specifications, bitwise operations, ASM code, embedded controllers and other specialty uses. When you're doing high-level programming, you should forget you ever heard about unsigned types.

    0 讨论(0)
  • 2021-02-04 15:03

    No, definitely not. Delphi idiom is to use integers here. Don't fight the language. In a 32 bit environment you'll not have more elements in the list, except if you try to build a bitmap.

    Let's be clear: every programmer who is going to have to use your code is going to hate you for using a Cardinal instead of an integer.

    0 讨论(0)
  • 2021-02-04 15:04

    Unsigned integers are almost always more trouble than they're worth because you usually end up mixing signed and unsigned integers in expressions at some point. That means that the type will need to be widened (and probably have a performance hit) to get correct semantics (ideally the compiler does this as per language definition), or else you'll need to be very careful in your range checking.

    Take C/C++ for example: size_t is the type of the integer for memory sizes and allocation, and is unsigned, but ptrdiff_t is the type for the offset you get when you subtract one pointer from another, and necessarily is signed. Want to know how many elements you've allocated in an array? Perhaps you subtract the first element address from the last+1 element address and divide by sizeof(element-type)? Well, now you've just mixed signed and unsigned integers.

    0 讨论(0)
  • 2021-02-04 15:05

    Moral: use iterators and foreach when you can, because it avoids this question altogether.

    0 讨论(0)
  • 2021-02-04 15:07

    Boundary conditions frequently present problems. Allowing for a type that can go negative may just shift the issue. Perhaps it shifts it in a way that's easier to debug, perhaps not. I started off using integers for counting loops like that, but later on switched to cardinals to help me catch errors.

    0 讨论(0)
  • 2021-02-04 15:20

    Regarding your statement that "I think it's in general a good principle to always use the least general (ergo the most special) type possible." - actually I think it's a good principle to use the data type that will cause you least angst and trouble.

    Generally for me that's a signed int since:

    1. I don't usually have lists with 231 or more elements in them.
    2. You shouldn't have lists that big either :-)
    3. I don't like the hassle of having special edge cases in my code.

    But it's really a style issue. If 'purity' of code is more important to you than brevity of code, your method is best (with modifications to catch the edge cases). Myself, I prefer brevity since edge cases tend to clutter the code and reduce understanding.

    0 讨论(0)
提交回复
热议问题