Ranter
Join devRant
Do all the things like
++ or -- rants, post your own rants, comment on others' rants and build your customized dev avatar
Sign Up
Pipeless API
From the creators of devRant, Pipeless lets you power real-time personalized recommendations and activity feeds using a simple API
Learn More
Comments
-
7Raiden8756yI somehow agree, but only to the extent of not having
if (classInstance.Type() == someClassEnumerator.ParticularClassType()
{
// cast the generic class into another type
// do something
}
That's absolutely bad and solvable by polymorphism -
No:
If (foo) {
// 100 lines of code
}
Yes:
if (!foo) {
return;
}
Use early return / early exit.
Code should be readable - if an if statement becomes too large (or a functions cyclomatic complexity) you are doing it wrong! (McCabe, 1974) -
rant1ng44626yAll I'm saying is..
$myPoint = All::ImSayingIs('entire point')->You::can([
Write::code()]mostly(like::this());
If(You::ReallyWant())
But::dontCheckMySyntax() -
gruff5526yI don’t mind multiple returns if they are at the top of the method and it reduces a layer of nesting and the method is doing something simple like setting a few properties from a passed argument
-
@Irene I find this ... Very controversial. In my experience it's quite the opposite that happens. When you return early, you can focus on the algorithm - you've split the function in two blocks, sanitizing / error handling and the algorithm.
I don't think that this leads to bugs, quite the opposite is true - but this depends on language and how you return. I'm mainly a PHP dev, but have Basic knowledge of many other languages...
How you return clarification:
Early return by not using return is the source of many severe errors, including the famous and dreaded TLS errors. goto statement and other 'archaic' ways of 'rerouting' the program flow in a violent way.
Language clarification:
Especially in C / C++ (embedded this seems likely) I can think of bugs, especially regarding memory allocation. But C has variable declaration placement since STD 99 - still the propability exists that you forget to free unnecessary variables before returning...
Related Rants
As an "OOP Programmer" you have one job to do:
Kill all the if statements.
Sometimes with fire.
rant
if maybe if
programming
oop