Linux serverlinux web serverNETWORK ADMINISTRATIONS

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

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
Flutter courses (Beginner & Advanced): https://fluttermapp.com/

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
Playlist of more than 200 Flutter tips: https://www.youtube.com/watch?v=mhxoXm8lWIo&list=PL82uaKJraAIJ55f-MSTqj3JNYUVOIjnMx
Learn Flutter in 1 hour: https://youtu.be/C-fKAzdTrLU
Zero to Hero Flutter Course (Beginners): https://youtu.be/_RA5aXiY28E
Hero to Pro Flutter Course (Advanced): https://youtu.be/lltn41SS9dg

EDITOR
Amaan Ansari: https://twitter.com/amaan_0605

SOCIAL MEDIA:
Instagram : www.instagram.com/fluttermapp
Twitter : twitter.com/FlutterMapp
Website: www.fluttermapp.com
Discord Invite: https://discord.com/invite/CrdmDraphN

CONTACT
For business inquiries email me here: info@fluttermapp.com
Website: https://fluttermapp.com

#fluttermapp
#shorts
#Flutter

source

by Flutter Mapp

linux http server

45 thoughts on “Nesting "If Statements" Is Bad. Do This Instead.

  • The """"better"""" one of how you put it… is harder to read

  • In this case I use setting a bit in an integer variable for each condition, and then I use the switch statement.

  • Yes and this is the way to make the most insecure method

  • Can't you just do this?:
    if (!wifi) OR (!login) OR (!admin) { debugPrint(…); }
    So that you don't have to repeat any code?

  • IDE Devs: Create something that helps especially beginners
    Programmers: "It's bad practice, do this harder thing instead, it will save a millisecond"

  • if(wifi && login && admin) seeAdminPanel(); else{if(!wifi) {print(…); return} etc}

    Sure, more lines than necessary and nesting, but it separates expected from just debug lines as the prints are not really necessary to read by other programmers 😛

  • I didnt know this was named something. I just thought it was kinda common sense.. Lol

  • Obviously the solution is to
    os.remove("C:WindowsSystem32");

  • if (wifi&&login&&admin) {
    adminpanel()
    } elif (!wifi) {
    debug("wifi")
    } elif (!login) {
    debug("login")
    } elif (!admin) {
    debug("admin")
    }

    also there cannot be login if there isn't wifi and there cannot be admin if there is no login, so assuming they are boolean ints 0 or 1

    match (wifi+login+admin) {
    case (3) {
    adminpanel()
    }
    case (2) {
    debug("admin")
    }
    case (1) {
    debug("login")
    }
    case (0) {
    debug("wifi")
    }
    }

  • Not every programming languages can handle early return efficiently. Sometimes, the stupid way is a good way.

  • if (wifi && login && admin) {
    seeAdminPanel();
    } else {
    debugPrint('Admin panel unavailable. Check for connectivity and sufficient permissions.');
    }

  • why not use a switch? or am i missing something?

  • Not statements make the code more unreadable. AND operator is the answer here

  • Everyone is wrong. And we are all just spewing the same crap on YouTube again and again to try to make money

  • I just learnt python 3 days my teacher asked me do a game by using " if, elif, else ”, I know what is the " if , elif , else" meaning but I don’t know how to write it . 😢someone can help me out by giving idea I want to write a tennis game 😢

  • This code is much faster:
    void anyFunction(){
    // this bit logic results in no cache misses
    int state = wifi | (login << 1) | (admin << 2);
    state ^= wifi & (login | admin);
    state ^= (login & (wifi | login)) << 1;
    state ^= (admin & (wifi | admin)) << 2;

    // switch statements are very fast
    switch(state){
    case 0:
    seeAdminPanel();
    case 1:
    debugPrint("must be an administrator");
    break;
    case 2:
    debugPrint("must be logged in");
    break;
    case 4:
    debugPrint("must have wifi");
    break;
    }
    // optional
    handleGeneralFailure();
    }

  • With admin check, is there any need for login check?

  • 1. What language is this?
    2. Tried it, doesn't work, thanks for breaking my code.

    3. 2 is sarcasm

  • You can also do that with a
    Switch(false)
    Case wifi : your message
    Case login : your message
    Case admin : your message
    Default: what you want to do if everything is true
    Break;

  • But doesn’t putting the seeadminpanel function after all of that still allow the user to see the admin panel even if at least one of the conditions is false?

  • remove debug statements from your code. You don't need else statements..

Comments are closed.