Interestingly, when I was briefly on a Computer Science degree (that turned out to be a waste of time) - we were taught that early returns / guard clauses were considered very poor style, and that every function should have only one return statement
Exactly! When I was a CS student, I remember the teachers stressing out about multiple return statements... It completely changes the internal organization of compilers.
Beside of that, I would write the first example as just:
Yea I pointed out that the function itself is useless, but it serves as a simple example.
As far as it's effects on the compiler, I'd say it very much depends on the language and the use case. Also in my 7 years in industry I've never had to take a minor compiler optimization like that into consideration.
That'd be about right. 1950s structured programming approaches, which were refined and formalized in the 1960s, being taught in 1995.
There are languages, like COBOL, where the language itself doesn't allow you to violate the one return statement. Fortunately, JavaScript is not COBOL.
Well it's interesting to explore WHY they stressed this.
In the Assembly language world having jumps all over the place was a real problem. In the grand scheme of evolution this was an important principle.
Of course higher level languages made this point moot (in my opinion). And in fact, returning early often saves you from having to deal with flags which are really a horrible programming style.
I have a CS degree and I was taught that as well...unfortunately most CS professors don't have much recent industry experience.
Returning early can be very dangerous if you aren't keeping your functions small. If your functions are more than 10-20 lines of code, I'd avoid multiple returns because you'll create a bug factory.
Interestingly, when I was briefly on a Computer Science degree (that turned out to be a waste of time) - we were taught that early returns / guard clauses were considered very poor style, and that every function should have only one return statement
Exactly! When I was a CS student, I remember the teachers stressing out about multiple return statements... It completely changes the internal organization of compilers.
Beside of that, I would write the first example as just:
return (value1 === value2); 😎
Yea I pointed out that the function itself is useless, but it serves as a simple example.
As far as it's effects on the compiler, I'd say it very much depends on the language and the use case. Also in my 7 years in industry I've never had to take a minor compiler optimization like that into consideration.
That's one of the commandments from the structured programming era of programming. Apparently some professors are still living in that era.
When I had that class would have been around '95
That'd be about right. 1950s structured programming approaches, which were refined and formalized in the 1960s, being taught in 1995.
There are languages, like COBOL, where the language itself doesn't allow you to violate the one return statement. Fortunately, JavaScript is not COBOL.
Well it's interesting to explore WHY they stressed this.
In the Assembly language world having jumps all over the place was a real problem. In the grand scheme of evolution this was an important principle.
Of course higher level languages made this point moot (in my opinion). And in fact, returning early often saves you from having to deal with flags which are really a horrible programming style.
I have a CS degree and I was taught that as well...unfortunately most CS professors don't have much recent industry experience.
Returning early can be very dangerous if you aren't keeping your functions small. If your functions are more than 10-20 lines of code, I'd avoid multiple returns because you'll create a bug factory.
Yes, well said: when it’s dangerous and when it’s not.