-
Notifications
You must be signed in to change notification settings - Fork 11.4k
[5.5] Clean elses #22425
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[5.5] Clean elses #22425
Conversation
good luck |
@carusogabriel What bugs does this fix? What problems does it resolve? Does this simplify anything for our users? |
@sisve Is a mantra that I learned and follow 🙏:
We can simplify ways (I don't know if it is the correct word here) in our code, instead of two, just one. If we've already exited the code, nothing else will be executed, soo, why |
I just can't get why he doesn't merge it. It's cleaner, reduce complexity and other stuff 😞 |
That's your personal opinion.
That's your personal opinion.
That's slightly vague... |
@sisve About reducing complexity: https://www.slideshare.net/guilhermeblanco/php-for-adults-clean-code-and-object-calisthenics (slide 33 and beyond) and https://www.ibm.com/developerworks/rational/library/3101.html (Alternatives section) |
The slideshow has 15 slides with code changes, and on slide 48 claims that some of the changes has reduced the cyclomatic complexity. However, the presence of an else-clause does not affect cyclomatic complexity at all. So the argument about reduced cyclomatic complexity is not about the else-clauses, but the other changes he does over the 15 slides. |
To be clear; I am not saying that your suggestions will make the world a worse place. I'm just pointing out that these things are primarily opinion-based, and as such, may not conform to the mind of The Merger. One would imagine that having some kind of coding guidelines for these things would help in the future... |
No problem, opinions and points something diverges 😅 About |
Don't typically merge these. Only merging this one because it has only 1 small merge conflict against master. Thanks 😅 |
Nice... Now others with PRs get to do extra work, because of some personal preferences... |
And furthermore, code-folding is no longer possible. Enjoy. @taylorotwell In my humble opinion, these syntactical preferences should either be ignored (as you have done in the past) or articulated in a standard that is posted clearly to https://laravel.com/docs/master/contributions#coding-style . These are frivolous changes that waste peoples' time and create unnecessary merge conflicts (as can be seen in this specific instance). |
Yeah these changes may seem to be useless, but they help us undertand the framework, thanks @carusogabriel |
Why is a function more difficult to understand when you have code paths clearly defined? If I want to determine what happens when an if statement fails, it's easier to scan for an else statement than to scan for braces, especially in nested code blocks. Previously, if I wanted to know what a function returns, all I had to do was scan backwards for a return statement and check if it was in an if/else block. Now I need to scan the entire function for returns that may or may not be in conditional blocks. Making code less obvious, less pedantic or less explicit does not make it cleaner or more readable, it does the exact opposite. The next logical step here is to just use ternary statements for all of these methods like |
This!!! ^ Very well stated, @36864 👏 And @jleonardolemos , I am very interested in your explanation as to how changes of this nature "help us understand the framework". You might start by refuting what the astute gentleman stated, just above. |
Guard clauses are good thing to keep code clean and readable. They are much more useful for simplifying nested conditionals, but it's not bad to keep same code style over all project. |
This PR was submitted following a mantra that @carusogabriel personally learned and follows: "There is no else". Emphasys: personally. I know of a lot of devs who would consider this mantra dangerous. It is "using something that is supposed to be relative (case by case) as absolute and then forcing others to adhere to it". @36864 said:
I thought that Laravel was proud to be written in clean, readable code, since that is the mantra of folks close to the framework. Hence my failure to understand why @taylorotwell finally caved in, after ignoring such requests for a while. |
What do you mean with 'extra work'? |
@a-komarev Ultimately, I'm okay with either style being used. What I'm not okay with is the style being changed for no reason other than someone thinking it's "more readable", and making the code inconsistent. Inconsistency is what really annoys me here. If early returns are the holy grail and |
@36864 I think because it's much harder to remove |
What? Let's look at a real example from this PR: (comments removed for brevity)
This was refactored to just remove the final else statement. Removing the elseif statement would have required the following additional changes:
If I were to make a PR regarding this, it would be to revert the changes made. The only file where removing the else block can make sense, under the guise of "we don't need it because guard statements" in this entire PR is Migrator.php since the if statement is handling what could be interpreted as an error (attempting to rollback migrations when there are no migrations to rollback). |
@36864 Good catch. You are right, in this example its pretty clear. I was talked about making changes in all framework code where |
The next thing you know, we'll be arguing about tabs vs. spaces! Oh, wait... The only sensible argument in this thread is that this preference needs to be standardized, one way or another (and so far, only three people, myself excluded, have advocated for as much). I care a lot more about making useful contributions to the code-base than reverting each other's work. This one's on you, Taylor. You set a precedent with this one, and it smacks of favoritism. |
I've removed the
else
s when we have already returned something 🚮