0

While storing single character value for the purpose of status, gender, delete_flag etc. which is better data type string or int. Suppose ::

status = A/I or status = 1/0
gender = M/F or gender = 1/0
delete_flag = Y/N or delete_flag = 1/0

In such scenarios where string or int can be used, which is better option as per code optimization.

  • 1
    What about bools? – Matthew James Briggs Oct 10 '15 at 05:21
  • Thanks for the comment. "Bools" we can have only two choices true/false. But with string or int we can handle scenarios like `paid_status = Paid/Unpaid/Delivered/Unknown or paid_status = 1/2/3/4`. – user4221591 Oct 10 '15 at 06:19
  • Was just pointing out that all of your examples were Boolean in nature. – Matthew James Briggs Oct 10 '15 at 07:14
  • 1
    @MatthewJamesBriggs If they were Boolean in nature, variable would have to be named IsMale / IsFemale. But there is really a third one, Mixed. – Nikhil Vartak Oct 10 '15 at 07:50
  • 1
    Possible duplicate of [Is micro-optimisation important when coding?](http://programmers.stackexchange.com/questions/99445/is-micro-optimisation-important-when-coding) – gnat Oct 10 '15 at 08:00
  • Seems to me your best option to store a single character, assuming that it can be any one of a large number of (or all possible) characters would be `char`. If you want to store a single value from a small number of possibilities, then the suggestion to use an `enum` is probably your best bet. – Jim Mischel Oct 12 '15 at 15:39

1 Answers1

4

Many languages support an enumeration type which is backed by an integer but has friendly scoped names in the language proper.

C#, like many others, call theirs enum. Whenever you use it, ensure you're only working with the numeric value at the boundary of your code if at all, and that you are careful about not changing the order or value of enumerands if you have persistent data represented with the enum.

Even if your value is truth:y or yes-no:y, consider a small enum anyway for clarity in function signatures, particularly if it's unclear what meanings map to true and false. The same holds to assigning meaning to numeric values.

No-one likes to see f(true, 0, 0, null, false, false, 3), not even with IDE functionality revealing the signature on hover or button press.

Performance-wise, you're not going to see any difference between a char and an integer. Strings will incur whatever (non-)penalties of references and whatever it costs to produce a known string literal.

Either will likely be negligible, optimize for readability and bug intolerance instead.

If your question actually is about databases, consider a foreign key into a separate table, that way you can ensure that only valid values end up in the column and it's reasonably extensible.

MSDN on enum

Lars Viklund
  • 2,106
  • 1
  • 17
  • 14
  • 1
    And actually, under certain circumstances, a `char` may cost slightly more than an `int`-backed enum. On x86 architectures, if you have a 16-bit `char` in the 16-bit `ax` register, an additional instruction may be needed to clear the remaining 16 bits of the 32-bit `eax` register, or the remaining 48 bits of the 64-bit `rax` register, before that register can be used for certain operations, for example pushing it into the stack so as to pass the char as a parameter to a function. – Mike Nakis Oct 10 '15 at 07:17