Bifalgorithm post · Computer science one · Enumerated Types

Why are enumerated types better than arbitrary integer error codes?

Why are enumerated types better than arbitrary integer error codes? (Computer science one · Enumerated Types) — "The usage of the integers 1, 2, 3 to indicate…

Ideas in this flowchart

  • Error codes should use human-readable identifiers instead of arbitrary integers.
  • Using 1, 2, and 3 for different errors is arbitrary.
  • The integers are magic numbers whose meanings are not apparent.
  • Calling functions must compare returned values with various integers.
  • Users must repeatedly consult documentation to match each integer to an error condition.
  • The errno strategy defines human-readable identifiers for each error code.
  • Macros can be used to define the human-readable error identifiers.
  • An enumerated type provides another solution.
Feed/

Why are enumerated types better than arbitrary integer error codes?

No cover

Channel

Computer science one

1 page

"The usage of the integers 1, 2, 3 to indicate the various errors was arbitrary. These are essentially magic numbers that the calling function would have to deal with by making comparisons with various integers. The numbers themselves are meaningless and someone using them would need to constantly refer to documentation to understand which integer corresponded to which error condition. It would be much better if we could follow the strategy of the errno and define human-readable identifiers for each error code. We could accomplish this by defining macros, but another solution is to use an enumerated type."

Flowchart preview
👍
0
0 comments0 shares

Sign in to view and post comments.