News for hackers. Stuff that matters.
Re:“I'm basically giving myself a permanent vacation from be... by Waterluvian on Thursday July 12, 2018 @12:29PM (#17516174)
I think that having strong opinions on how others should be developing software is how communities become toxic like this.

"Shooting yourself in the foot is your fault for not using a linter that detects accidental misuse of assignment!"

Re:“I'm basically giving myself a permanent vacation from be... by jcelerier on Thursday July 12, 2018 @12:32PM (#17516217)
> I think that having strong opinions on how others should be developing software is how communities become toxic like this.

Depends on what you think the answer to "is programming an art or a science" is. People who build bridges are absolutely subject to "strong opinions" on how to build bridges. I am of the opinion that shipping software to others when under a contract without using all the facilities available to prevent problems - linting, static type checking, etc - should be considered at best a breach of contract and ideally criminal negligence.

Re:“I'm basically giving myself a permanent vacation from be... by woah on Thursday July 12, 2018 @01:33PM (#17516818)
Thinking that linting and static typing will stop all errors is foolish (although I prefer both, myself). Cargo culting "best practices" is often useless, and is sometimes used as a crutch by developers who are bad at the much more important and much less quantifiable things, like code readability and architectural simplicity.

Wanting your pet coding preferences to be enforced by criminal law is a sadomasochistic fantasy.

Re:“I'm basically giving myself a permanent vacation from be... by vehementi on Friday July 13, 2018 @02:26AM (#17520898)
> Thinking that linting and static typing will stop all errors is foolish

Did you reply to the wrong post?

Re:“I'm basically giving myself a permanent vacation from be... by jcelerier on Thursday July 12, 2018 @02:47PM (#17517481)
> Thinking that linting and static typing will stop all errors is foolish (although I prefer both, myself).

Thinking that putting a seatbelt will prevent death in a crash is foolish, and yet they are still mandatory

Re:“I'm basically giving myself a permanent vacation from be... by mkesper on Thursday July 12, 2018 @04:41PM (#17518410)
Re:“I'm basically giving myself a permanent vacation from be... by jcelerier on Thursday July 12, 2018 @05:51PM (#17518926)
that's exactly what I mean. Seatbelts won't save you every time but they save you enough times that they are mandatory.

> In another study that examined injuries presenting to the ER pre- and post-seat belt law introduction, it was found that 40% more escaped injury and 35% more escaped mild and moderate injuries.[83]

I don't know how many bugs can use of all possible explicit typing and compiler warnings avert but I'd wager that it would be at least as high a percentage.

Re:“I'm basically giving myself a permanent vacation from be... by wrmsr on Thursday July 12, 2018 @08:38PM (#17519733)
And as a former motorcyclist the idea of putting a seatbelt on a bike is terrifying, but under the umbrella of 'mandatory' I'd have to risk getting cut in half in a fender bender 'because that's how it's done'. If you blindly follow guidelines without understanding why they were put in place and when they should be circumvented you do more harm than good. In this context specifically, a python thread, I've not even found formatters that 'just work' in python to the degree that they do in java - I get and support significant whitespace but that combined with the crazy calling convention frequently lead me to just have to suffix spans of lines with noqa because I'm communicating to the reader something that a dumb bot doesn't understand. And that's not even getting into the heavier stuff, I type annotate everything but mypy is beyond useless for my relevant codebases - it no longer just outputs walls of useless noise it straight up crashes, from the most minimal use of even descriptors much less metaclasses. Tools and policies are essential but at the end of the day humans and situations are still currently authoritative.
Re:“I'm basically giving myself a permanent vacation from be... by icebraining on Thursday July 12, 2018 @12:54PM (#17516463)
Programming languages are not just used for shipping software to others when under a contract. Specially Python, which is often used as a teaching language.
Re:“I'm basically giving myself a permanent vacation from be... by Waterluvian on Thursday July 12, 2018 @12:50PM (#17516414)
You're right. There are times where being right is absolutely imperative.

Maybe it's a know it when you see it kind of thing. When the debate isn't killing people, it's potentially inconvenient extra coding by a developer, and to appreciate the immense social cost those debates can have.

Re:“I'm basically giving myself a permanent vacation from be... by jodrellblank on Thursday July 12, 2018 @11:42PM (#17520374)
I feel like you could have written this https://jjj.blog/2016/08/todays-software-is-terrible/
Re:“I'm basically giving myself a permanent vacation from be... by TooBrokeToBeg on Thursday July 12, 2018 @01:24PM (#17516742)
I'm still puzzled to why would a compiler allow assignment in an explicit conditional (outside of loop syntax). It's like a baked-in blindspot that most people just want to ignore for some reason. Some languages actually guard against this well enough (eg Kotlin) and say "don't". Even with guards in place, it's not all that complicated to work around in the edge cases where you might want to do it.
Re:“I'm basically giving myself a permanent vacation from be... by arkadiytehgraet on Thursday July 12, 2018 @02:02PM (#17517091)
Funny you should mention Kotlin; while I like the language a lot, I believe this particular feature would be of immense help in the following scenario:

Imagine you have a sealed class Foo, with `class Bar(x: String) : Foo()` and `Baz() : Foo()`.

Now, imagine you have a method, returning an object of type Foo: `fun foo(): Foo`

And you want to pattern match on the result of this method:

  when(foo()) {
      is Bar -> ...
      is Baz -> ...
  }
Now, the problem is: how do you access String field x in the first branch? The only way to do it now is to extract the methos call into redundant local variable, and then pattern match it instead of `foo()` directly as I did above.

Now imagine Kotlin had that feature; then we could just do the following:

  when(foo = foo()) {
    is Bar -> foo.x
    is Baz -> ...
  }
Re:“I'm basically giving myself a permanent vacation from be... by bjz_ on Thursday July 12, 2018 @11:15PM (#17520246)
In Rust and Haskell you can do:

    match foo() {
        Bar @ foo => foo.x,
        Baz => ...,
    }
Re:“I'm basically giving myself a permanent vacation from be... by bpicolo on Thursday July 12, 2018 @02:21PM (#17517234)
One option is match statements. Great way to make this sort of inline assignment unnecessary
Re:“I'm basically giving myself a permanent vacation from be... by arkadiytehgraet on Thursday July 12, 2018 @02:24PM (#17517267)
Could you please elaborate more on what you mean by match statements? Is it an already existing feature of Kotlin?
Re:“I'm basically giving myself a permanent vacation from be... by bpicolo on Thursday July 12, 2018 @02:26PM (#17517291)
Ah, I guess `when` is literally Kotlin's equivalent of match.

What I'm thinking of here is Rust's match statements, which do give you the ability to make use of those intermediary values by making use of Rust's enum type.

https://doc.rust-lang.org/book/second-edition/ch06-02-match....

Re:“I'm basically giving myself a permanent vacation from be... by arkadiytehgraet on Thursday July 12, 2018 @02:32PM (#17517337)
I see; I agree that proper pattern matching would indeed solve that as well, as e.g. Scala does.
Re:“I'm basically giving myself a permanent vacation from be... by shakna on Thursday July 12, 2018 @06:25PM (#17519127)
It's handy in C for guarding parts of the language, as you often have to check return values anyway.

    if(malloc(10000 * sizeof foo) == NULL) {
      // Error processing
    } // No need for an else branch here.
Re:“I'm basically giving myself a permanent vacation from be... by the-dude on Thursday July 12, 2018 @06:56PM (#17519309)
Missing assingment.
Re:“I'm basically giving myself a permanent vacation from be... by shakna on Thursday July 12, 2018 @07:34PM (#17519481)

    int success = NULL;

    if(success = malloc(...)) {
    }
    if(success = malloc(...)) {
    }
    if(success = malloc(...)) {
    }
Re:“I'm basically giving myself a permanent vacation from be... by slavik81 on Thursday July 12, 2018 @08:39PM (#17519737)
That will result in undefined behaviour if the value returned by malloc is greater than INT_MAX. If you really need an integer, use intptr_t. But, generally it makes more sense to just use a pointer.

    char* p = NULL;
    if (p = malloc(...)) {
      ...
    }
Re:“I'm basically giving myself a permanent vacation from be... by kelnos on Thursday July 12, 2018 @02:05PM (#17517115)
Because assignment is an expression. It has nothing specifically to do with the position being a conditional.

Conditionals need an expression, and assignment fits the bill, so it works.

Re:“I'm basically giving myself a permanent vacation from be... by TooBrokeToBeg on Thursday July 12, 2018 @06:36PM (#17519195)
> Because assignment is an expression. It has nothing specifically to do with the position being a conditional.

No, but it has a lot to do with the way humans write code. This is the source of the bug, not the logic. eg Why bother having whitespace that the compiler can't use (typically)? Human readability.

The concession to not take into account human failing, is pathological.

Re:“I'm basically giving myself a permanent vacation from be... by hinkley on Thursday July 12, 2018 @10:30PM (#17520103)
The other partial fix is to stop allowing truthy conditionals. Only allow Boolean values and the only time you it wrong is when you’re assigning to booleans.
Re:“I'm basically giving myself a permanent vacation from be... by KSteffensen on Thursday July 12, 2018 @03:58PM (#17518108)
Why is assignment an expression and not a statement? Why should an assignment evaluate to a value?
Re:“I'm basically giving myself a permanent vacation from be... by wool_gather on Thursday July 12, 2018 @09:30PM (#17519910)
Well, if you have the concept of Maybe/Optional, it's quite handy to have a conditional binding as control flow (as in Swift):

    guard let value = myOptional else {
        // Handle absence
    }
    // Use value
Or

    if let value = myOptional {
        // Use value
    }
Re:“I'm basically giving myself a permanent vacation from be... by emmelaich on Thursday July 12, 2018 @09:36PM (#17519933)
Read Tim Peter's essay on the end of the PEP 572.

It's very good.

Re:“I'm basically giving myself a permanent vacation from be... by Dayshine on Thursday July 12, 2018 @01:39PM (#17516875)
Linter?

You mean compiler right? The compiler should be warning for things like this, not relying on 3rd party tools.

So, yes, if you're using a custom compiler, you kinda are to blame for shooting yourself in the foot. Just use the official binaries!

Re:“I'm basically giving myself a permanent vacation from be... by dvlsg on Thursday July 12, 2018 @01:54PM (#17517020)
Depends on the language. Javascript, for example, would use a linter for this warning.
Re:“I'm basically giving myself a permanent vacation from be... by KSteffensen on Thursday July 12, 2018 @04:02PM (#17518135)
It's not that easy for the compiler to provide these warnings when the language is interpreted rather than compiled
Re:“I'm basically giving myself a permanent vacation from be... by TooBrokeToBeg on Thursday July 12, 2018 @06:38PM (#17519210)
The compiler can do anything a linter can do. Multi-pass multithreaded compilers are not unheard of. eg While compiling to temporary storage, I do a linting in another thread. At the end, check for successes and move the artifacts into the output directory.
Re:“I'm basically giving myself a permanent vacation from be... by TwelveNights on Thursday July 12, 2018 @09:24PM (#17519893)
What the poster means is that a compiler may not be as applicable to interpreted languages.