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