This is coding style based, was wondering if it’s better to put a positive outcome from an if/else decision in the if or else block. For example is:
yes_no = object.get() #Response will be 'yes' or 'no'
if yes_no == 'yes':
yes response
else:
no response
better than:
yes_no = object.get() #Response will be 'yes' or 'no'
if yes_no != 'yes':
no response
else:
yes response
As far as I can tell there’s no difference, as they both look for a ‘yes’ and output a yes response, but is there any sort of styling that people follow or find easier to read?
I’m using python, but since if/else is everywhere I guess other languages are relevant also.
EDIT: Replaced passes with no response, as were unnecessary.
I think that most depends on what the programmer wants to highlight. The IF condition should be either:
Personally I always try to avoid this: