1

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
  • 1
    Both examples are equally readable to me, yet I prefer the first one since it's shorter. Also, if you return boolean value, shouldn't you return True instead of doing pass? Commented Feb 23, 2015 at 10:27
  • @scriptin it's probably used in a loop Commented Feb 23, 2015 at 10:27
  • @ratchetfreak maybe... who knows? =) I've seen people return results of mixed types a lot. If there is a loop, it should be included to make a better example. Commented Feb 23, 2015 at 10:31
  • 6
    Assuming this is Python, if initial_push not in ('y', 'yes'): is more readable than either. Commented Feb 23, 2015 at 10:32
  • 1
    This cannot possibly be the only time that a user is asked a question in the app. Therefore, per DRY, it should go into an appropriately named helper method, so your example code will be shorter and more readable still. Commented Feb 23, 2015 at 10:33

2 Answers 2

3

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
  • Good point! In fact, the application is intended to help ad-hoc system administrators, such as those who until recently were using Plesk. So I would like to keep the bar low. But your point definitely stands for other projects as well. Commented Feb 23, 2015 at 10:56
2

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.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.