News for hackers. Stuff that matters.
Re:Clever code is probably the worst code you could write by sgarland on Tuesday May 21, 2024 @06:58PM (#40435132)
That is somewhat annoying, yes. I'm a huge fan of Python's list comprehension, but it's generally accepted to be a normal part of the language, and IMO, more readable:

    print([x for x in range(10) if not x % 2])
    [0, 2, 4, 6, 8]
vs.

    l = []    
    for x in range(10):
        if not x % 2:
            l.append(x)
    print(l)
    [0, 2, 4, 6, 8]
Re:Clever code is probably the worst code you could write by okanat on Tuesday May 21, 2024 @07:42PM (#40435622)
Call me crazy, but I find the second example more readable.

The first example is long line and you have to go back and forth to understand it. The second example is a single top down pass.

Re:Clever code is probably the worst code you could write by crazygringo on Tuesday May 21, 2024 @08:02PM (#40435792)
Agreed, me as well.

The second example reflects how my brain thinks and the intuitive order of what has to happen.

In the first example, it's all out of order.

Re:Clever code is probably the worst code you could write by sgarland on Tuesday May 21, 2024 @08:03PM (#40435795)
I’m sure I’m biased, since I code mainly in Python. List/dict comprehensions read the same to me as the longer form, just more concisely. This can be taken too far, and you can wind up with horribly obtuse one-liners that are just awful.
Re:Clever code is probably the worst code you could write by okanat on Tuesday May 21, 2024 @08:21PM (#40435945)
I write and maintain code in a bunch of languages. Rust has risen to the first place, C is the close second followed by C++ and Python.

All of the languages except C have similar iterator-based stuff that let you write one liners and often with lambdas. I dislike them all. They give way too much leeway and encourage many developers to try to prove how clever they are.

Once you have to debug the code containing them, all of the complexity of the syntactic sugar comes crashing down on you. The debugger starts jumping to weird places, sometimes even optimized out parts of the standard libraries while for loops usually stay debugable.

Re:Clever code is probably the worst code you could write by floxy on Tuesday May 21, 2024 @08:17PM (#40435917)

    x = np.arange(0,10,1)
    print(x[x%2==0])
Re:Clever code is probably the worst code you could write by f33d5173 on Tuesday May 21, 2024 @08:56PM (#40436176)
I've seen people complain about wrapping a comprehension over multiple lines, but I can't imagine packing them all in one. In your example, I would spread it out over five lines, so it would be at as long as the imperative case.

While I like python, I think the unusual order that components of a comprehension are written is to its detriment. It would make more sense if they were in the same order in both examples. The python ternary has a similar issue.

Re:Clever code is probably the worst code you could write by glandium on Tuesday May 21, 2024 @09:16PM (#40436312)
You chose the one example that doesn't need a list comprehension. print(list(range(0, 10, 2)))
Re:Clever code is probably the worst code you could write by sgarland on Tuesday May 21, 2024 @09:27PM (#40436395)
Good call.
Re:Clever code is probably the worst code you could write by psychoslave on Wednesday May 22, 2024 @11:42AM (#40442339)
With ruby 3.4 (prior to that `_1` should be used instead of `it`):

    (0..10).select{it.even?}
Re:Clever code is probably the worst code you could write by psychoslave on Wednesday May 22, 2024 @11:47AM (#40442400)
Also not that the result is `[0, 2, 4, 6, 8, 10]`, unlike the behavior of Python which exclude the value explicitely passed in parameter from the represented range.