Nesting 'If Statements' Is Bad. Do This Instead.

preview_player
Показать описание
Never nest your if statement if you have to many of them. With the Guard Clauses technique, you will be able to write cleaner and more readable code. In this video you will learn how to replace the if statement from you code by the guard clauses technique. This technique is actually simple to understand, all you have to do is to reverse the if else condition and put all the code under. You can reverse every if else condition and at the end you will have you function. Keep in mind that some people don't like to have empty return statement, so you can change this part of the code if you need. This was how to replace your big nesting if else statements in your code with the Guard clauses technique.

COURSES

MISSION
Our mission at Flutter Mapp (Flutter Mobile App) is to help purpose driven Flutter developers go full-time doing what they love and making an impact through coding. We achieve this with useful Flutter tips and straight to the point Flutter videos.

RECOMMENDED VIDEOS

EDITOR

SOCIAL MEDIA:

CONTACT

#fluttermapp
#shorts
#Flutter
Рекомендации по теме
Комментарии
Автор

"Weve been hacked what happened!"

"I forgot a return sir"😂

Chevifier
Автор

Ahhh yes. The "early return" technique.

diegovera
Автор

"If you need more than 3 levels of indentation, you're screwed anyway, and should fix your program." - Linux kernel coding style guideline

vufmvbn
Автор

The "AND" operator has left the chat.

crewrangergaming
Автор

I also take this approach, but I really hate hearing "this is wrong". It's just another way to do it that you and I think is less readable, it's not wrong.

sb_dunk
Автор

Just imagine if you forget the return statement in the admin check. Everyone becomes admin

Underliner
Автор

As a security consultant, I find this horrifying. Yes, this works to block non-admins from seeing the admin panel. However, you really should be doing the "isAdmin" check at the top. In more complex codes, doing the check after other code blocks unnecessarily increases the attack surface... there are more chances for something to go wrong and introducing exploitable code. The lesson here is: in any function where the ultimate show-stopper of the function is a crucial security decision, do the check as warly as possible. Not doing so is like having a security guard who watches thieves loading their truck and only gets up to do anything after several minutes of deep meditation about the situation.

zsi
Автор

showing the admin panel as a default is good because coders never make mistakes nor do they ever forget to check edge cases

mikezheng
Автор

To anyone new learning how to program. *THIS IS NOT WRONG*. It’s great to learn how to do something many ways and build your personal coding style off of others and try to balance your coding style with the readability that may suit a team, but it’s not wrong. If you’re new to programming this is called preference. The cool and neat thing about programming is that you can arrive at the same solution many different ways. So don’t be afraid to do this when you feel it’s appropriate

davidfreer
Автор

As some one who writes a fair bit of code everyday this technique is pretty solid. Nesting if statements can get hairy

notagamer
Автор

As someone who is been coding for good while now. To any beginner out there, there is no such thing as a "wrong way", but rather this one way to do it. It all comes down to preference and how we write our code, for me personally I had no trouble reading the nested if statements.

cryptic_daemon_
Автор

Reminds me of my internship days. There was a small function with lots of nested IFs. So I've gone ahead and broke the rule of thumb "If it works, don't touch it". I refactored all of those nested IFs with early returns. Guess what? There was a pieces of code at the end of the function do the calculations. Thanks to my early returns, it f*** the whole thing up.

samsonwu
Автор

Its funny, when I was at university, this exact thing was described as "hard to read" and "bad", with multiple and/or nested if/else statements being preferred.

The grass is always greener.

oblivioncth
Автор

Back a year later after seeing this and it has changed how a i code and the readability of my code thank you so much

RandomYouTubevids-blsc
Автор

As someone who uses “if (thing) return” instead of nesting it is 100% worth learning if you don’t know how. It can seem backwards but seems natural after a bit of practice. It’s more condense, easier to read, and faster to write.

Also, since there seems to be arguing about using “and” and “or” statements you definitely can still use them.

After having to interact with others’ code with nested if statements I really appreciated this method even more.

itsJimpex
Автор

i am just so proud of myself that programming education video just pop up in my feeds.
the usual are :
- video games
- vtuber
- music
- etc etc etc

wielderXII
Автор

It comes naturally I think. I usually start with messy ifs when bootstrapping the logic then I convert it to something more readable

xrayian
Автор

To avoid the multiple return statement fight, just bring your subsequent statements into else if clauses. Now it's a single block where any failure ships to the end. Also allows for continuing code afterward (if minimal changes to existing codebase are required) or other pass through type flows

tychozzyx
Автор

I like this and have done it for decades.
Now I have a name for it.
Thank you.

eugenecbell
Автор

Excellent recommendation! I’ve been doing this for years and it is much more readable!

vrrqhll