Functionality
00:00 First of all, what does it mean?
00:02
Quality code is functional, meaning the code works and does its intended job. Let’s look at some examples. This function called f takes a list of numbers l and returns their average.
00:17 It also checks for edge cases and doesn’t break if you give it an empty list. Do you have a functionality issue here? No, not really. I mean, your intuition probably tells you that there’s something wrong here, especially with the function name and variable name.
00:33 They could be better. But overall, when you run the code, it works. So this is not a functionality issue. This is a readability issue. And you’ll learn more about readability later on.
00:45 But back to functionality, let’s look at another example. You have the same function here with readable variable names, but it doesn’t check for edge cases this time. So for example, if you give it an empty list, it would break.
01:01
Here, you’ve done that and it gave you a ZeroDivisionError. So here you do have a functionality issue.
01:10
Okay, so what can you do? You can always go back and add an if statement like before, or you could raise a ValueError, like here, to avoid the ZeroDivisionError.
01:21 And now when you call the function with an empty list, it won’t fail or crash. And by the way, you could even take this further by adding type hints or type annotations to clarify that the input should be a list of numbers, and something like a string shouldn’t be in it. Now that you understand functional code does what it’s supposed to do, and it doesn’t break on edge cases, you’re ready to move on to readability.
Become a Member to join the conversation.
