I recently added this code to a Github project I’m working on:
if not initial_push=='y' and not initial_push=='yes':
print('Aborting.')
return False
Since this is a public repository that I’m hoping people will contribute to, I am considering refactoring that bit as the slightly more readable code:
if initial_push=='y' or initial_push=='yes':
pass
else:
print('Aborting.')
return False
If my goal is to keep the open-source project accessible to outside contribution then should I use the more readable but clumsy second method? I am actually interested in the answer for the general case, not just this specific example.
10
Let’s invert the logic here, to keep in the spirit of Demorgan’s law:
Would you want “programmers” to contribute to your project even though they have problems with elementary logic? Is that a net benefit to you? Or will they be time wastes? Does your project have enough volunteers to review their code?
1
I don’t think either of those two is really more complex or more difficult than he other. I’d choose the one without the unnecessary pass
branch, because that’s an unnecessary branch.
If you consider all the things that keep people from contributing to Open Source (it needs time, some programming ability, motivation, getting a Github account, making sense of the organization of a whole program, getting over the fear of not being good enough to contribute, actually creating a decent pull request, et cetera), I can’t imagine that the choice of how to spell an if statement like this is ever going to make the difference between somebody contributing or not.
But in general, readable and clumsy don’t usually go together. Almost always, the way that is more readable is also the less clumsy way, and the clumsy way takes more lines, more unnecessary cruft and is therefore less readable.
So: always go for maximum readability.