Even though you can have the output of one function act as the input to another directly, this can be confusing to read. Sometimes, using temporary variable will allow you to clarify your code.
By
Doug Hull
Doug first used MATLAB in 1994, could not figure it out until he got some help in 1995. He is now dedicated to making sure that no one else wastes a year of their life not knowing MATLAB like he did.
While I don’t usually like the deeply nested function calls either, I also am averse to filling up the workspace with unnecessary variables. In cases like this, I often prefer to use comments to document the code and just reassign the variable. For example, instead of this:
I like the combination in #5. I have never gotten into the reuse a variable to build it incrementally code pattern, but I think that it works just fine. The trade off is less variable names at the cost of access to the intermediate states.
If you were to try and debug this, you would have to step through to get the intermediate states. Mine you would not. However when it is done, I have littered the workspace.
Doug, I totally get your point, when you put it in a function. Then I agree with you.
The rest: Thank you for the inspiration. I myself, even though coding for quite a whilie, am pretty new to MATLAB and glad to see a few ideas and hints.
I don’t get the obsession with littering the workspace. Clearly, this is an example, and the code could be in any context, but most often, it would/should be in a function, limiting the scope and lifetime of these variables. To me, the bigger problem of “spamming the workspace” are scripts that assume they are not going to overwrite a variable I’m already using or that assume their plot command will make a new figure and not trash another one.
I’d be interested in a comparison of the two methods from a memory usage point of view to know when or if I might want to use the nested form.
One small point to note with the ‘incremental overwrite’ approach you demonstrate is that it has the potential to prevent the JIT compiler system from performing the fullest degree of optimizations that it is capable of.
From my understanding (which was current as of a couple years ago), the more ‘static’ you keep your code the better the JIT can optimize it – for instance this means not changing the data type/size/complexity/etc of a variable during it’s lifetime.
Interesting point, but I have never observed the effect. I guess this would most likely be noticed in a FOR loop? I will keep an eye out for this in the future!
Good point Doug, but I feel the style should depend on the application. This idea definitely make the code more readable, but leads to more memory requirements. Consider for example cases when the output of functions are huge matrices taking lots of space. It may not be a good idea to use temporary variables like this which are not cleared. A better option may be just to use a single line and write proper comments.
Leave a Reply
About
Doug Hull is a proud MathWorker who is on a mission to help you with MATLAB.
Wouldn’t it be better to add this line add the end of the code to make it really temporary?
Don’t you “spam” your workspace this way?
Some people might say that, but I am not worried for a few reasons:
* I generally make short functions, these variables will be out of scope quickly anyways.
* I use good variable names, so these variables will be understandable
* I am not worried about ‘memory issues’ from variables. If I were, I could clean these out in the appropriate way.
-Thanks for the comment,
Doug
While I don’t usually like the deeply nested function calls either, I also am averse to filling up the workspace with unnecessary variables. In cases like this, I often prefer to use comments to document the code and just reassign the variable. For example, instead of this:
I might do this:
Thus I try to be an avid code commenter.
I do like to align code as Doug does (not always though):
I find it especially useful when writing T-SQL queries.
Oleg
Also (about alignment, which helps to mantain code), using Matt’s code I would write it like:
@Oleg and @Matt
I like the combination in #5. I have never gotten into the reuse a variable to build it incrementally code pattern, but I think that it works just fine. The trade off is less variable names at the cost of access to the intermediate states.
If you were to try and debug this, you would have to step through to get the intermediate states. Mine you would not. However when it is done, I have littered the workspace.
Doug
Doug, I totally get your point, when you put it in a function. Then I agree with you.
The rest: Thank you for the inspiration. I myself, even though coding for quite a whilie, am pretty new to MATLAB and glad to see a few ideas and hints.
Peter
I don’t get the obsession with littering the workspace. Clearly, this is an example, and the code could be in any context, but most often, it would/should be in a function, limiting the scope and lifetime of these variables. To me, the bigger problem of “spamming the workspace” are scripts that assume they are not going to overwrite a variable I’m already using or that assume their plot command will make a new figure and not trash another one.
I’d be interested in a comparison of the two methods from a memory usage point of view to know when or if I might want to use the nested form.
@Oleg and @Matt
One small point to note with the ‘incremental overwrite’ approach you demonstrate is that it has the potential to prevent the JIT compiler system from performing the fullest degree of optimizations that it is capable of.
From my understanding (which was current as of a couple years ago), the more ‘static’ you keep your code the better the JIT can optimize it – for instance this means not changing the data type/size/complexity/etc of a variable during it’s lifetime.
@EBS
Good point, I had not considered that.
Doug
@EBS,
Interesting point, but I have never observed the effect. I guess this would most likely be noticed in a FOR loop? I will keep an eye out for this in the future!
Good point Doug, but I feel the style should depend on the application. This idea definitely make the code more readable, but leads to more memory requirements. Consider for example cases when the output of functions are huge matrices taking lots of space. It may not be a good idea to use temporary variables like this which are not cleared. A better option may be just to use a single line and write proper comments.