Remembering to Return and Keeping It Readable
00:00 Let’s take a look at a couple more best practices, starting with remembering to return the return value. A common error in writing functions is to actually forget to return a value for when the function finishes.
00:15 This can be solved by making considerations for a return value when you are first defining your function. This makes sense because when you’re starting your function definition, you’re already thinking about what parameters it should take and what it should return.
00:31 So make a little template for yourself, like this one, to write code which uses a return result before you start writing the body of your function. Start with the function header, where of course you would write a suitable function name and indicate parameter variables it will actually use. Then write statements to account for the return value, leaving blank space for the body of the function.
00:58
Once you have this written, you can then fill in the body with what the function will actually do to compute a meaningful result. Be prepared to change the variable name result
to something meaningful as well.
01:13
Another best practice is to not write complicated expressions for the return value. Using a complex expression in a return
statement makes debugging more difficult. It’s hard to keep track of what’s being computed.
01:30
Here’s an example to compute the variance of a collection of data. We have an expression to compute the mean, then in the return
statement, we have the rest of the computation of variance.
01:42 This is very complicated and it’s difficult to follow what’s happening here. We can still use it and see that it works. Let’s import that file and then compute variance on a set of data.
01:59 I’m using the same data from the tutorial, or least I’m trying to.
02:05 And we do get the result, but it’s slight leap of faith that the computation is being done correctly. Instead, it’s better to break the computation down into smaller pieces.
02:18
Make use of temporary variables to store intermediate values as the computation progresses. Here’s that same function broken down into smaller parts. First, we store the size of the data in the variable n
. Then we compute the mean, as before.
02:38
Now, however, we save the sum of the squares of the differences between each data
value and the mean to its own variable. Notice the use of sum()
with a list comprehension.
02:51
This was in the previous version as well. We create a list of (x - mean)
squared values from the original list data
, then use the list function sum()
on that new list.
03:07
And in this version of the function, we save that to an intermediate variable. In the previous version, it was just part of our huge, ugly return
statement.
03:20 And finally, we return what is essentially the mean of those values. Here, it is much easier to see how this computation is being performed.
03:39
just making sure we are using the one from the var2
module,
03:47 use the same set of data—
03:53 we get the same result. And because it was easier to see how this computation was being performed, we have a lot more faith that this answer is indeed correct.
04:04 Next, we’ll compare returning values to modifying globals.
Become a Member to join the conversation.