Live data from Hacker News

New Ways to Be Told That Your Python Code Is Bad

nickdrozd.github.io

221–230 of 262 posts

Re: New Ways to Be Told That Your Python Code Is Bad

#221

Earlier quoted context omitted.

If you don't like awkward ugly clunky syntax, you probably shouldn't program in Python. And, for that matter, if you like correctness checking, you also shouldn't program in Python...

Serious question, what alternative(s) to Python would you recommend with nicer syntax?

Honestly, I've been positively surprised by F#. Nice syntax, functional-first, pragmatic design.

Re: New Ways to Be Told That Your Python Code Is Bad

#222
post #213

Earlier quoted context omitted.

I prefer this Python syntax: x = condition() and 4 or 5

If you know the first value is truthy, sure. x = condition() and [] or True is not equivalent to x = [] if condition() else True Now, I have seen a genuine application of `X and A or B` before. That occurs when you're checking a condition before, say, popping from a list -- but if the list is empty, you still want a default value.

Can you give me an example where the two return different values?

Re: New Ways to Be Told That Your Python Code Is Bad

#223
I just feel like this author could have chosen a better example. The if/else block that he complains about is easy to read and simple to understand. The ternary he advises raises the bar significantly for anyone trying to read the code, while having no practical effect on runtime or usability.

Based on my 25+ years of experience I strongly believe that the "if/else" that this author complains about is actually BETTER than the ternary he recommends. This is yet another example of the endemic problem where software engineers think "harder to read is a virtue".

Re: New Ways to Be Told That Your Python Code Is Bad

#224

Ah, leave people alone. I stopped using ternary expressions because someone told me not to. I could start using them again, whatever. I use while loops when the burden of describing the loop as an iteration is too high (too much of a stretch), or when I'm writing something that really isn't a composition of forEach/map/filter/reduce (gasp). For me, it's whatever I need to do to get through code review without arguing…

I think the biggest proof that linting in general is not condescending against any one style is the frequent existence of multiple mutually exclusive linter checks, one for each style.

Consistent style is really nice.

Re: New Ways to Be Told That Your Python Code Is Bad

#225
post #217

Ah, leave people alone. I stopped using ternary expressions because someone told me not to. I could start using them again, whatever. I use while loops when the burden of describing the loop as an iteration is too high (too much of a stretch), or when I'm writing something that really isn't a composition of forEach/map/filter/reduce (gasp). For me, it's whatever I need to do to get through code review without arguing…

> Don't add linter checks for these things, it's condescending. Well as he states, these checks are not turned on by default. I have trouble seeing why someone who liked these extensions enough to turn them on manually would feel condescended to by them.

[deleted]

Re: New Ways to Be Told That Your Python Code Is Bad

#226

Earlier quoted context omitted.

If you don't like awkward ugly clunky syntax, you probably shouldn't program in Python. And, for that matter, if you like correctness checking, you also shouldn't program in Python...

Serious question, what alternative(s) to Python would you recommend with nicer syntax?

For web stuff: JavaScript

For data science stuff: Julia

Re: New Ways to Be Told That Your Python Code Is Bad

#227

Earlier quoted context omitted.

What's so monstrous about it? It's practically English: cssClass = 'selected' if isCurrentTab else 'deselected'

You have a weird perception of English. "If the tab is selected, the CSS class is 'selected', else it's deselected. Or in pseudocode if isCurrentTab cssClass = 'selected' else cssClass = 'deselected' Even ternaries read weird. "css class is current tab HUH?? selected COLON! deselected"

Both read grammatically fine to me. If you prefer to do things in 4 lines rather than one, and repeat the variable, that's up to you I guess - a lot of people seem to agree - but honestly I don't get what the fuss is about.

Re: New Ways to Be Told That Your Python Code Is Bad

#228

Earlier quoted context omitted.

God bless gofmt and tools like prettier.

Python has Black, which uses the same premise as gofmt. Guido doesn't like it, though, but the community seems to.

Which brings up another point -- that's precisely the difference between having the format be essentially part of the language from day one, and shoehorning it in later. The utility of auto formatting comes from instantly eliminating the squabble and bikeshedding.

Re: New Ways to Be Told That Your Python Code Is Bad

#229
post #127

Earlier quoted context omitted.

I've actually found that there are a number of situations where I really want: while True: ... if condition: break ... and kinda wish there was a nicer syntax for this.

Rust has `loop {}` which I thought was weird at first, but also ended up use more than I would have expected.

One especially nice part of rust's `loop` is since it's guaranteed to run at least once and to terminate, you can break with a value!

    let foo = loop {
        // ...
        if condition {
            break value;
        }
        // ...
    }

Re: New Ways to Be Told That Your Python Code Is Bad

#230
post #213

Earlier quoted context omitted.

If you know the first value is truthy, sure. x = condition() and [] or True is not equivalent to x = [] if condition() else True Now, I have seen a genuine application of `X and A or B` before. That occurs when you're checking a condition before, say, popping from a list -- but if the list is empty, you still want a default value.

Can you give me an example where the two return different values?

When condition() is truthy, the first evaluates to True and the second evaluates to []

Try it out. https://ideone.com/GciAs8

Post reply on HN