Write Pythonic Code Like a Seasoned Developer Transcripts
Chapter: Foundational Concepts
Lecture: Flat is better than nested

Login or purchase this course to watch this video and the rest of the course contents.
0:01 For this next Pythonic concept, let's go back to the Zen of Python.
0:04 So here I am in the Python REPL,
0:07 and one of the core concepts is that flat is better than nested.
0:11 Now, it turns out this is one of my favorite items here
0:15 and one of my favorite programming concepts,
0:17 because a lot of people seem to do it in the reverse.
0:20 I call this sawtooth style of programming,
0:23 we have lots of loops with "ifs" and conditionals
0:25 and then more loops and so on.
0:28 So let's look at this, how we can apply this in Python.
0:31 Over in PyChram, we have a program
0:34 that is meant to simulate downloading a file,
0:37 and this might not be the best way to do it, obviously it is not the best way,
0:40 but it really is a simple example to highlight this "flat is better than nested".
0:45 So what we want to do is we want to download a file
0:48 and we are going to do a series of tests
0:50 to make sure that we are able to download the file
0:51 or at least we think we'll be successful before we actually try.
0:54 First one uses a little support module here,
0:56 we are going to ask: "Is the download URL set?"
0:59 if it is, then we are going to check the network status
1:02 and then we are going to make sure that the DNS is working
1:04 then finally we are going to check that we have permission to access the file
1:06 and if all those things are true, then we are going to try to download it.
1:11 Otherwise, we are going to say well, this one goes back here
1:14 so it looks like no access
1:15 this one here, no DNS,
1:17 this one here PyCharm even has little like tiny lines
1:21 that are probably hard to see but I can follow back up,
1:23 no network and then finally this one is bad URL.
1:27 This is a serious bad piece of mine,
1:30 I hate code that looks like this, so let's write a different variation of this
1:34 which I am going to call download_flat().
1:40 So let's just reverse these things,
1:42 let's look at our conditionals here,
1:44 instead of having these sort of positive checks,
1:47 yes you can do this and you can do this and you can do this,
1:50 we can return these "if" statements into what are called guarding clauses,
1:54 you don't let the method run if one of them is failed.
1:57 So I can come over here and say "if not check url",
2:01 then we'll say oh that's a bad URL.
2:05 And we can unindent, that's good,
2:09 of course now that we are up here we want to return early,
2:13 we'll say "if not check the network" then we want to say "no network".
2:19 And return, and again, unindent, it's better, again,
2:24 "if not check DNS", do something and then return
2:28 we'll print out that there is no DNS
2:32 and then we'll unindent and finally we'll do this "if not this, return",
2:41 and then if all the guarding clauses pass
2:43 then write at the very edge of our method, not indented at all
2:48 as far as this method is concerned,
2:50 we can write our meaningful code and this makes it so much easier
2:53 to maintain and write, instead of trying to do our actual work way down inside here.
2:59 So this is a very nice way that you can write these guarding clauses
3:03 instead of this what I call sawtooth programming,
3:06 so that you have nice, flat, easy to understand, easy to modify things,
3:10 like for example down here,
3:12 if I want to insert another test, I've got to make sure
3:15 I've paired up correctly with the "else" down here and so on,
3:19 but most importantly, you don't have to work in a hyper-indented way;
3:23 this also works in loops, you wouldn't return out of the loop
3:25 but you would just do a "continue" with the guarding clause
3:28 instead of a test and then put the actual stuff inside the "if" statement.
3:32 "Flat is better than nested", let's see that in a diagram.
3:35 All right, here is the code that we wrote
3:37 that was the sawtooth style with the positive checks,
3:40 "make sure I can do this, make sure I can do that" and so on,
3:42 and we rewrote that by inserting or converting those two guarding clauses
3:47 this flat version is much better,
3:48 we've converted all the positive checks to guarding clauses
3:52 and it's much easier to add and remove those guarding clauses,
3:55 see what the "else" clause is that goes with it
3:57 because now it's right there and most importantly, at the end,
4:00 we get to work in a non-indented way.