Application codes


Link to this posting

Postby Ursego » 21 Feb 2013, 12:10

Never hardcode application codes (like statuses, entity types etc.); instead, use constants or enumerations.

*** BAD code: ***

Code: Select all
if (invStatus == 8)...

*** GOOD code: ***

Code: Select all
if (invStatus == InvStatus.CANCELED)...

For application codes values, use mnemonic strings (like 'OPEN', 'CLSD' and 'CNCLD') rather than meaningless numbers (like 1, 2, 3...) or single letters (like 'O', 'C' and 'N').

Such self-explanatory codes make debugging and exploring data in DB tables much easier - developers don't need to search in the catalog tables (to understand what the code means) or guess (doing incorrect assumptions). These mnemonic strings don't need to be long - VARCHAR(10) is enough, we can always shorten words if needed still keeping them perfectly understandable.

But even having easily-readable codes, we still must use constants because hardcoding is bugs-prone: mistyping a code value doesn't raise a compilation error while mistyping a constant name does.
User avatar
Ursego
Site Admin
 
Posts: 143
Joined: 19 Feb 2013, 20:33



Ketones are a more high-octane fuel for your brain than glucose. Become a biohacker and upgrade yourself to version 2.0!



cron
Traffic Counter

eXTReMe Tracker