Where do people learn "bad" programming habits (i.e., goto, global, and eval)
16 views (last 30 days)
I will likely be teaching some basic MATLAB this year. I have had little formal instruction on programming, but if I remember the little I have had, global variables and goto statements were not taught and the MATLAB specific eval was not mentioned. It seems these "bad" programming constructs tend to cause more problems then they solve for beginners. How do I avoid a class full of students that want all variables to be global, wish MATLAB had a goto statement and think eval is the bomb.
More Answers (7)
Walter Roberson on 31 Aug 2011
When I was young... we were so poor... that we couldn't afford a structured IF statement... we 'ad to empty out the bit bucket from the local pub, fasten the broken bits together with mud and spit (Oh, you had mud? We would have loved to have 'ad mud!) ...
Bjorn Gustavsson on 31 Aug 2011
In the last minute - that's where I've done most of my bad programming, when the frightening sound of the rapidly approaching deadline have made all my hard-earned good habits abandon ship...
A suggestion that might help students stick to decent programming habits (re-usable functions, documentation et al) is to make the programming tasks so that it becomes natural to re-use stuff they already made. After a while (if you stress it a bit...) they should get the idea that neatly written functions with clean interfaces will save them time in the long run as they can reuse stuff again and again...
Paulo Silva on 31 Aug 2011
I used a lot the goto and label functions with casio graphic calculators when I was in high school, with MATLAB you don't need to use the goto function, if there's a part of the code that is used often make a function with that code and learn to use conditional statements to control code execution, those who still want the goto function on MATLAB don't really know how to program.
When someone in your class says that some function (goto, eval, global) is good you should be prepared with examples where those functions cause problems and show them to the entire class, also show them alternative ways to avoid the use of those functions.
Fangjun Jiang on 31 Aug 2011
One suggestion is to survey them first to see what is their programming background. I think some aspects of MATLAB programming are unique.
- No need to declare variable (except pre-allocate large matrix)
- Matrix operation
Teach them the beauty and easiness of passing data through function input/output arguments instead of using global.
Maybe kids today don't have the baggage of knowing goto at all.
Kelly Kearney on 31 Aug 2011
In my field, at least, bad habits are passed from one generation to the next, since most people learn to code not through classes but through research, where they're building on and/or studying older code. I see this particularly with eval; older scientists started using Matlab version 4 or so, which actually did require eval to do things that can now be solved with a more versatile syntax. That old syntax still works, so it persists.
I try to pontificate on the evils of eval whenever I can to my labmates, but really, most don't care. If it works for the task at hand, it's good enough.
Same reason most big models in my field are still written in Fortran...
Justin on 31 Aug 2011
From my own experience the overuse of the EVAL function is somewhat self-perpetuating. A student does not know how to pass an argument in the proper form so they look it up online and nearly everywhere you look someone is suggesting the EVAL function.
In short: A lot of it comes from not having a good source of information to look to for answers and instead using random google searches.
While the random google search may be a good method for most programers, with MATLAB there are a few functions that are too easily misused and are very popular on help forums.
Richard Brown on 23 Apr 2012
At my university it's usually the engineering school's fault. When students learn to solve ODEs with MATLAB they seem think the correct thing to do is to define all of the ODE parameters as global so that they can write a function of the required
function dy = foo(t, y)
syntax. Anonymous functions and nested functions are considered "too advanced", or not understood by the instructors, and so they're taught to do it the global way.