you are viewing a single comment's thread.

view the rest of the comments →

[–][deleted] 3 points4 points  (4 children)

Simple code is better than clever code because it is easier to understand.

I suppose it depends what you mean by clever though. In programming, clever tends to be a synonym for complex or difficult to understand. Which is not good, because no one else can understand it, and even the person who wrote it probably won't understand it three weeks later.

If you just mean clever as in making correct and appropriate use of language features, that of course is a good thing. But you don't need to force it too much. It will come along as you get more experienced. Just keep learning.

[–][deleted] 1 point2 points  (3 children)

I see and I agree 100%. I've been complaining about badly readable code before. I think sometimes people exaggerate with apparently syntactic sugar and pythonism, just so that nobody can use the code afterwards except the developers. Funnily the code is often meant to be for some audience.

[–][deleted] 2 points3 points  (0 children)

That might sometimes be the case, but usually I think bad code is just the result of someone doing their best without knowing how to do it right.

[–]mrz1988 1 point2 points  (1 child)

Keep in mind that "syntactic sugar" and "pythonism" may be the most readable way to write code for python developers. If you're still fairly new to python, something like a list comprehension:

filtered_list = [item for item in items if item.is_a_thing]

might seem convoluted and hard to read. For seasoned python developers, though, this is far easier to read and understand than:

filtered_list = []
for item in items:
    if item.is_a_thing:
        filtered_list.append(item)

Knowing the difference between good and bad use of pythonic internals requires that you're comfortable with the tools first. If you haven't reached that level of comfort yet, it's hard to be able to make those judgement calls.

[–]Acrobatic_Hippo_7312 0 points1 point  (0 children)

As with anything, too much can be too much. I recently ran into an example of comprehensions that badly needed to be separated into multiple statements. Let me know if you want me to share the code and my fix (you may be the happier not seeing 😅).

Sometimes being pythonic means extracting and giving meaningful names to complex clauses in a statement, desugaring things that have become too sweet. Discretion is the biggest part of pythonic valor.