3 ms·
I think the `switch` pattern is only applicable when you're checking for a large number of configurations that are of boolean type - ie. isReadOnly, isAdmin, is
by env123 7y ago
I think the `switch` pattern is only applicable when you're checking for a large number of configurations that are of boolean type - ie. isReadOnly, isAdmin, isUser, isSuperUser, hasPaid, isActivated, isBanned, etc - it basically depends on your use case, unusual pattern to use, but it has its merits if applied in the right time by the right developer
case isReadOnly: // change something;
case isAdmin: doAdminStuff();
case hasPaid: updateFinance();
case isActivated: updatedUserInfo();
and so on...
- Zenst 7y agoVery true, then somebody else ends up maintaining the code and gets a ticket to add this feature and chaos plays out. One thing I learned early on - true and false vary across platforms. Hence I learned to define true and false at the start of the code with realTrue=(1=1) and realFalse=(1=0). As some systems have 1 or any positive value as true, some have -1 as true. It can get messy, bit like endians (big or little) the same holds true for true and false.
- VBprogrammer 7y ago> Hence I learned to define true and false at the start of the code with realTrue=(1=1) and realFalse=(1=0) I'm sure the platforms / language you use make this a sensible choice but I'm not going to lie, if I came across that in someones code I'd be screaming WTF at the top of my lungs.
- Zenst 7y agoI learned to code in the 70's, predating C99 and having to embrace many diverse platforms (we had more than one mainframe maker back then ;). But nice explanations here: https://stackoverflow.com/questions/17010041/why-define-true-1-1-in-a-c-boolean-macro-instead-of-simply-as-1 https://stackoverflow.com/questions/17010041/why-define-true... TL;DR portability