3

Once upon a time i read this:

Simple is better than complex. Readability counts.

and then.. i came across questions requesting "short pythonic code". The complex short answers often attract more upvotes than simpler alternatives.

It is as if complex == smarts == good. Well,.. not so good 4 months later when he spends 20 extra minutes to understand what he wrote. Arguably, some experienced programmers might find the code rather easy to understand, but i doubt this is the case for most (new) users.

An example:

my_str = '  cat  dog1 snake'

# This ..
for i, j in groupby(enumerate(my_str), lambda x: not x[1].isspace()):
    if i:
        index, item = next(j)


# .. versus this..
for m in re.finditer(r'\S+', my_str):
    index, item = m.start(), m.group() 

Personally i find the second code orders of magnitude more useful than the first.


Here are a few more examples (i ll update it with more examples over time):

example 2 (complex: 7 upvotes, simple: 0)

one liner (.. and one full page of text to explain it)


My reaction to such posts is a downvote. Reason why i downvote it, is to stop the proliferation of complex == smarts == good.

  • Is it just my imagination that short less readable code attracts more upvotes?
  • Is my reasoning flawed?
  • Is a downvote an exaggeration? Should i respond differently? Or perhaps ignore it?
5
  • The first code is awful, I hope it is not from a real example (and of course it cannot be). Commented Mar 8, 2015 at 11:27
  • @AnttiHaapala It actually is a real example.
    – user
    Commented Mar 8, 2015 at 11:31
  • 2
    Here is one similar with more upvotes on the regex one (fortunately). Commented Mar 8, 2015 at 11:35
  • The first snippet is horrible. You have to read it 3 times (or better execute it) before you actually know what it does...
    – ThiefMaster Mod
    Commented Mar 8, 2015 at 11:36
  • 1
    This one is another example ... even more upvotes. If an answer starts with "It'll be tricky to explain" then you know what's coming ;-) It does have a nice explanation, though. Commented Mar 9, 2015 at 18:16

2 Answers 2

10

If someone asks for "short pythonic code", then it's understandable that some people will provide overly-condensed cryptic code in their answers. However, such code is not necessarily more Pythonic; after all The Zen of Python says "Simple is better than complex", "Complex is better than complicated", and "Readability counts".

IMHO, code in answers should try to match the current skill level of the OP. Of course, that's not always easy to determine, but often you can get a fair idea of what's appropriate from the nature of the question, especially if the OP has posted some of their own code.

If someone's a raw beginner in Python and still coming to grips with the basic for loop, it's probably not helpful to supply an answer that uses list comprehensions or generator expressions, especially if they're nested. Similar remarks apply to unnecessary use of fancy module functions, or using a Regular Expression to perform a task that can easily be performed with a simple str method.

OTOH, it doesn't hurt to expose new programmers to such standard Python idioms, if it's done gently, eg post code that shows how to do it with simple for loops, and then show an alternative solution that uses the more advanced techniques. Concise code can be easier to read than simpler, more verbose code, but only if the reader is familiar with the idioms used.

There's an old tradition in programming to write inscrutable one-liners. It goes back to the days of line-oriented terminals connected to mainframes, when there was a method to the madness, but it doesn't make a whole lot of sense these days.

Our "audience" when writing code is the humans who have to read and maintain it. Interpreters (or compilers) don't care how clever your code is, they just do what they've been told to do. :)

In closing, I must quote Kernighan's Maxim:

Everyone knows that debugging is twice as hard as writing a program in the first place. So if you're as clever as you can be when you write it, how will you ever debug it?

"The Elements of Programming Style" , 2nd edition, chapter 2

Also see Understanding this Brian Kernighan quote.

-1

Yes, your reasoning is flawed.

The "complexity" of code is relative. Just because is something is not understandable to people inexperienced in a language does not mean that it is difficult to understand to someone with the experience. This site is for everyone, and that includes experienced people. If we make everything comprehensible to the lowest common denominator then we should all give up now as nothing will ever get better.

Equally, the ability to understand code is important but it's not the sole consideration of a programmer. Sometimes speed or business rules win out.

Who are you to determine that the slow code understandable by the beginner is objectively better than the quick code understandable by expert?

I think it is your imagination that shorter less readable code gets more upvotes. It's my experience that shorter code, that is explained well gets more upvotes.

It's certainly possible that shorter code will get upvotes; if it's understandable to experienced people and they appreciate the cleverness inherent in the answer. Once again, inexperienced people may be unable to understand, but this site is for everyone. An inexperienced person has the option to comment and ask for clarification.

The onus is partly on the inexperienced person to learn more and become more experienced. This is what we all should be aiming for.

You are free to use your downvotes however you wish as long as you don't target individual users. If something is not well explained and you believe it's too complex for someone to understand my first reaction would be to ask the author for clarification. That way maybe you will learn something and if the author updates their post you've both helped people who come to the answer in the future to understand it as well.

3
  • "Who are you to determine that the slow code understandable by the beginner is objectively better.." - The answer to this question lies below in your text: I am the person who can use his downvotes however i wish; and I wish to not see programmers trying x2 more to debug their own code. Your assumption that i don't understand the code is wrong. Also, could you elaborate on when "Simple is better than complex. Readability counts." is wrong? An example would be nice, if possible.
    – user
    Commented Mar 8, 2015 at 11:09
  • The answer isn't below in the text @user5061. You are free to use your votes however you wish, but you're assuming that everyone has the same concerns. You can determine that you think that something is better but not whether it actually is. I'm sorry, I'd thought I had avoided making value judgements on your ability but slipped up in the last paragraph. I've fixed it. Lastly, simple is better than complex. But, sometimes simple just isn't good enough. I don't have an example to hand, but I don't see how it's relevant. Sometimes simple just isn't good enough.
    – Ben
    Commented Mar 8, 2015 at 11:15
  • 2
    Sometimes simple just isn't good enough - I strongly agree. However, sometimes there are simple answers which aren't as popular as a shorter complex answer. Perhaps i should have noted such posts and include links in my question (finding them now would be rather hard). As for asking for clarification, it wouldn't make any difference in most cases; an unnecessarily complex solution will remain unnecessarily complex.
    – user
    Commented Mar 8, 2015 at 11:28

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .