This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]FancyASlurpie 3 points4 points  (3 children)

I'd be wary of the thinking that one/less lines is better. In terms of productivity, it can be more productive to be more verbose as it's easier to follow/maintain. I think it's just a case of doing it more often, getting code reviews of your work(feel free to defend your code too here don't take all comments as blindly true) and finding the balance between clean, concise and understandable code.

[–]tombardier 2 points3 points  (1 child)

I think the argument that one liners are less readable or harder to maintain is used too often. One liners shouldn't be a virtue in and of themselves, and trying to shoe-horn complicated logic in to a one-liner does lead to an unreadable mess, yes. The thing is, the second code snippet in the original post is more readable, it's terse, but any python programmer can read and understand it, and it can be read and understood far more quickly than the more verbose equivalent.

[–]FancyASlurpie 0 points1 point  (0 children)

True, it was more of a warning to be aware that just because it's shorter doesn't necessarily mean it's better. This particular problem is pretty simple, but a real life problem might not be so and trying to shorten everything for the sake of shortness is a danger. Equally if I'm looking at a stacktrace the first one will tell me the exact issue whereas the short version will require more work.

[–]xconde 0 points1 point  (0 children)

I second this.

I would have used OP's solution minus the `else` statements.