Stuart’s MATLAB Videos

Watch and Learn

Cleaner code in MATLAB part one of series 12

Posted by Doug Hull,

I am guilty of making some pretty horrible code from time to time, especially when I think no one else is looking. I wanted to take the next few videos to show how you can go from code that is hard to read and maintain to code you proudly sign your name to. This week we will define a simple problem, show some awful code that happens to work, and then make the code better by breaking down a logic check to smaller, easier to understand statements.
PS. It was actually difficult to write code this bad. I ended up writing the code well and messing it up because I could not write such awful code that actually worked properly!

12 CommentsOldest to Newest

Terry Brennan replied on : 1 of 12
Doug - I have found that 2-d geometry problems are best solved in complex coordinates zc = xc+i*yc; z = x+i*y; e = z-zc; if abs(e)>1 & abs(real(e))
Doug replied on : 2 of 12
Terry, Thanks for the comment. As you can see, this code was far from efficient so that I would have something to improve upon. I was not planning on converting the code to imaginary coordinates, but for a more realistic problem, that is an good idea. It looks like the code is cleaner your way. I was planning on focusing on the benefits of good variable names, breaking down logic checks and the like. The actual problem domain is not important, so I was not going to make domain specific code changes. Thanks, Doug
Nick Cheilakos replied on : 3 of 12
We have a mess code but is efficient an we have a clean code but loses in speed. Which code we prefer? In your example which code is faster? The first or the clean version? ps: I know, that if we have clean code we can do debugging very easy.But i dont know if sometimes we earn something else
DanK replied on : 4 of 12
I have the same question as Nick. My solution has generally been to write the complicated code, and then provide the "simple" code as a comment, so that I don't have to give up speed. I also wonder about the merits of cluttering up the variable name space... Is this yet another case, "there is never really one size that fits all"?
Doug replied on : 5 of 12
Dan and Nick, There are times when cryptic code code does give faster results. The profiler, as outlined in an earlier video helps to diagnose that. However, I think that sloppy code, like the kind in my original file, is rarely faster, simply harder to understand. Even if the clearer code were slower, the ease of understanding would outweigh most small performance gains in most situations. That being said, I think it is worth adding another video at the end to compare the two. My instinct is that their will be negligible difference in speed. Doug
DanK replied on : 6 of 12
Doug, I'm thinking mainly of the extra time required in memory allocation and handling, when you are processing large volumes of data. What I am really unsure of here, is what memory usage MatLab makes for intermediate steps in complex computations. Is it allocating the memory just the same as it would if we assigned each intermediate value to a variable and then clearing that allocation at the end of the computation? If so then I would imagine you really don't lose much time. For reference I'm thinking about processing done on arrays that are, for example 20,000x25, which can definitely chew up a bunch of memory. Dan
Loren Shure replied on : 7 of 12
Dan- With respect to temporary expressions, MATLAB cleans up or reuses the memory as it goes. So, for example, assuming all variables and sizes are suitable for this expression (a+b)+(c+d) We end up with 2 intermediate temporaries (not 3). The temporaries start off being for the expressions in parentheses. And when those subexpressions are added, MATLAB is smart enough to know that it can overwrite one of the temps with the final value. So at most here, you have only one extra temporary). There are some extra considerations that MATLAB does for expressions like A = A + 1; when this is inside a function M-file. I will be writing about that soon in one of my upcoming blog articles. --Loren
DanK replied on : 8 of 12
Loren, Thanks for the answer. From that response it does sound like the complicated, ugly, code probably does save a lot of memory allocation, at least when you are dealing with large sets of data. As a side note, it would be interesting if profiler had a mechanism which allowed one to know what part of an expression was using time, rather than just line by line. I'm guessing that that is a far from simple modification, however. Dan
Doug replied on : 10 of 12
File exchange to the rescue:
Nasser replied on : 11 of 12
Thank indeed for ur help but I'm looking for NSGA-II with binary coded (representation) the one that have u sent if real vector