Skip to content
loading...

re: There's no "else if" in JS VIEW POST

TOP OF THREAD FULL DISCUSSION
re: How can you have any flow control without some sort of if-statement?

E.g. with FSMs. E. g. with class hierarchy. E.g. with pattern matching. The list might be easily prolonged.

But surely some tasks just require if-statements? What if you receive an integer from user input and want to know if it is positive or not? What if you want to filter out all odd numbers out of a list? Isn't pattern matching on something with two cases just an if-statement with the first case as a condition?

Not really. Well, sometimes, very rare.

What if you receive an integer from user input and want to know if it is positive or not?

This might happen in some beginners’ exercise, not in the real life. I won’t allow users to input both positives and negatives the same way and I would immediately raise on any unexpected input.

What if you want to filter out all odd numbers out of a list?

I would use the standard library filter function/method, like (Elixir example, but it does not matter, the same is available in Ruby, Python, Perl for sure): Enum.filter(input, &is_odd(&1)).

Isn't pattern matching on something with two cases just an if-statement with the first case as a condition?

Pattern matching has many clauses by default, hence the code might be easily extended when needed, unlike if. Also, pattern matching is significally faster. And, the last but not the least, I usually don’t have to choose between two conditions in the middle of the code, because I think about the proper design upfront.

Alright, but then you are just hiding the if-statement within the is_odd method. There is no sane way to implement these types of functions, that take an integet and return a boolean, without if-statements.

I highly doubt that pattern matching can be faster than if-statements in any language, since pattern matching on non-enum types would have to be implemented using conditional jumps (i.e. if-statements) in the underlying machine code or VM code.

I never said I am advocating the senselessness of conditional jumps in the underlying assembly / machine codes. That is obviously impossible.

What I said is if statement as it is, is hardly maintainable.

Hey Aleksei! I'm really interested to know more about your vision about this subject. I'd love to read an article about that. If you have some time to write about this, I would be really glad! :)

I will do, sooner or later. The subject actually bugs me :)

That'd be nice. A lot of this discussion is what not to do, but that isn't worth much without alternatives and example use cases.

Alright, but then you are just hiding the if-statement within the is_odd method. There is no sane way to implement these types of functions, that take an integet and return a boolean, without if-statements.

To be fair - one could also claim that if statements are just hiding the conditional jumps. Creating better abstractions over problematic implementations is a virtue.

That was my point, Idan; if-statements are just conditional jumps, and you need those to be Turing complete. I disagree with the idea that if-statements are always a design flaw, because that just leads to using overabstracted solutions for simple problems, e.g. using pattern matching on a boolean condition.

I agree with your virtue, I just don't see something as integral as the if-statement as a 'problematic implementation'.

I understand whose is in the functional is alergic to IF statement, but a coder can create a good program without a functional approach. Many developers write code in procedural mode, if you use GNU/Linux you'll know that in many files you'll find IF statements

One might use Linux for decades and never ever see a single line of the code.

That’s true, there are very specific circumstances under which if might be the best choice, but all those are exceptional nowadays. 99% of businesses use high-level languages and in high-level language, no matter whether it’s functional or not, ifs are a sin.

99% of businesses ????????
If you consider that COBOL has been back ...
If you consider that many banks prefer continue with old COBOL (not the new one) for the ATM services.
I you consider that big manifactures build products IoT with a poorly code and very buggy.
I think that you're developing for excelence, despite I've seen in your code of elixir-iteraptor some IF statement

Legacy is legacy, there is no doubt (although COBOL was invented as a most imperative language ever.) I admit I exaggerated the percentage, though.

I've seen in your code of elixir-iteraptor some IF statement

I never said I am perfect. If I had 72hrs/day I would be always writing the good code avoiding ifs. This is a solid settlement: I use ifs to save some time, while I still expressly understand I could do better.

I'm intrigued by what you said about if-statements. I'm curious to know how you would write the following callback function.

someRequest(URL, (err, result) => {
    if (err) {
        console.log(err);
    } else console.log(result);
}

I'm still a novice so it's difficult for me to imagine doing some things without if-statements. I would appreciate it if you could show me😅🙂.

You could use something like promises.

someRequest(URL)
.then(result => console.log(result))
.catch(err => console.log(err));

Ooh. That's right. Forgot promises could do that😅. Thanks 😊

code of conduct - report abuse