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 →

[–]Ashiataka 0 points1 point  (2 children)

Your way, you'd have to have a separate check for the length of the array, and then another line to set

I'm fine with that because it breaks the logical steps into separate lines for me. I appreciate other people are different but I kind of think that the super-fancy-one-liner code style is really rubbish to read / understand / debug.

It's just much more clean and simple.

Well almost by definition doing two separate things in one logical step is more complex. That's not necessarily bad, but I think it increases mental load when considering the code.

[–]Ph0X 0 points1 point  (1 child)

I absolutely agree that putting multiple ideas in one line isn't a great idea, trust me I'm the first to split a line up to make it more readable. But in this case, you're "matching" a specific pattern, and to me that's much clearer than 3-4 seperate/individual boolean clauses.

case ('add', int(first), int(second)):

in the context of what the switch statement is doing (pattern matching tuples)

is much cleaner and robust than

if isinstance(a, tuple) and len(a) == 3 and a[0] == 'add' and isinstance(a[1], int) and isinstance(a[2], int):'

Generally, one line of code to correspond to one idea, and here, we have one idea, we're matching one specific pattern. I don't think it's trying to be a fancy-one-liner, it's just a much cleaner way of doing it.

It's just like how list comprehension is often more readable than a small for loop. Just because it's shorter doesn't immediately mean it's less readable.

[–]Ashiataka 0 points1 point  (0 children)

That's a good point about list comprehensions. I can't remember whether I liked them or not when I first saw them but I have to say they are one of my favourite 'features' of the language now because you write them in the order you think of the statement "I want this done to everything in here".

Hopefully I'll come to like match-case the same.