However, options 2 and 3 make for a nice, readable code flow. Which option reads best? I would not pick 1, even though it might seem like the obvious choice. Now fill in the blanks in this piece of code: if : Consider these options for a debug mode variable: Booleans are often used in conjunction with if statements. Let’s continue down this path of writing readable code by looking at booleans. Don’t forget to come back here though, we’re only getting started. Go ahead and open some of your recent code and do this right now. Sometimes functions have to be a bit longer. You’ll find that you often can. If you can’t, that’s OK too. Look closely and see if functionality can be split into smaller (well-named) functions. To give you an idea: if your function contains more than 10 lines of code, that’s a smell. This type of code documents itself it’s readable code! Only when needed, the reader can dive in deeper. Ideally, your code is a sequence of function calls like this that almost read like human language. They don’t need to dive into the function to see what it does. Without reading your code in detail, people will know what it does. Name them accordingly.įor example, you might want to create a function that reads a file into a string and name it read_file_to_str(file_name: str). 2 Booleans should make for good sentencesĮxtract as much as you possibly can into functions and make these functions do one thing.
0 Comments
Leave a Reply. |