Should I use unsigned integers for counting members?

前端 未结 6 1972
自闭症患者
自闭症患者 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 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.

提交回复
热议问题