You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
But for simple values that come from either a constant or a variable already declared, the simplicity is undeniable.
Proposal
Allow ternary operators where the condition and true/false assignments have to come from variables or constants, never from functions or other nested ternary operators.
Ternary operators can sometimes become complex, but this proposal prevents that from happening, the best of both worlds
The use of ternary operators should be a choice of the developer, people that don't like to use that can always go back to using if-else statements, I don't believe the reasoning behind the article is a good enough reason to take away the choice from developers. My 2 cents
The text was updated successfully, but these errors were encountered:
Proposal Details
Problem
I'm sure you get this a lot. I always wondered why ternary operators were not built into the language and found this https://golangdocs.com/ternary-operator-in-golang
I personally don't think that this
is easier to read than this
Which makes me disagree that "makes the code easier to read" (in most cases)
Why?
When do ternary operators make statements harder to read?
E.g:
E.g:
E.g:
But for simple values that come from either a constant or a variable already declared, the simplicity is undeniable.
Proposal
Allow ternary operators where the condition and true/false assignments have to come from variables or constants, never from functions or other nested ternary operators.
Example of some allowed expressions:
Example of complex expressions that could be simplified if needed:
Case for complex condition:
Instead of
Declare the condition in a boolean variable first
Case for complex values provider:
Instead of
Use a value provider function
Conclusion
Ternary operators can sometimes become complex, but this proposal prevents that from happening, the best of both worlds
The use of ternary operators should be a choice of the developer, people that don't like to use that can always go back to using
if-else
statements, I don't believe the reasoning behind the article is a good enough reason to take away the choice from developers. My 2 centsThe text was updated successfully, but these errors were encountered: