MATLAB Ranting, Part I: try-catch-statements done wrong

I am currently trying to get MATLAB (a high-level language and interactive environment for computational problems) to work properly with some piece of visualizing hardware and ever so often I find small pieces of MATLAB terribly annoying, to say the least.

Keep in mind that MATLAB is a tool with a very specific purpose. And in the scope of this purpose it works quite well. But as a programming language, MATLAB is bad. Very bad. Generic tasks are nearly unaccomplishable because of restrictions in the language obviously introduced for design decisions having a simple mind in - well - mind.

I stumbled upon many those restrictions (and stumble is the right word here, trust me), and I planned on making one big 'Why MATLAB is a bad programming language' article but now I collected so many bads and no-noes and stuff that I could feed this blog for years alone with this that I decided to make a new series on this.

Everyone knows try-catch-statements. Matlab has them, too.


try
  statement_1
  statement_2
  %...
catch
  statement_1
  statement_2
  %...
end

The only problem is that user interrupts aren't catched by this block. So I made a research on the web. And I found some entries in forums regarding the clean-up problem. The most ridiculous solution I found was the notion that since destructors in MATLAB 8 are called every time, have your clean-up code in a dummy class sporting only such a destructor. But then, after much researching and teeth grinding, I finally found why Ctl-C isn't catched. To sum it up, all evidence points to it was a design decision. A design decision! So that those pesky catches won't end up in an endless loop! The example given on the page is the following:


for k=1:100
  try
    pause(1);
  catch
  end
end

Of course I can see how this would end up in an unescapeable loop. But my experience with C++ tells me that no catch statement should handle arbitrary errors ever. Exactly to prevent those things. So most decent programmers would have come up with something like this:


for k=1:100
  try
    pause(1);
  catch
    err = lasterror;
    if strcmp(err.identifier, 'MATLAB:interrupt')
      rethrow(err);
    end
  end
end

(Did you notice how I am saving the value of lasterror() first before accessing the elements (and yet calling the function without the parentheses)? That's another reason why MATLAB drives me crazy.)

MATLAB is not designed with decent programmers in mind. MATLAB is designed for mathematicians and the like, people who don't want to think about things like that. And that's fine. But this makes it very hard for me to guarantee a safe state after cancellation. The script could always be cancelled via Ctrl-C and leave the system messed up with my path additions, my global variables, my open files, hardware, et cetera. I can call a cleanup function manually, sure, but this isn't how it should be.

Long story short: Regardless what purposes as a tool it might serve, as a programming language MATLAB is one of the worst I have ever seen. And believe me, I have seen many.

Thanks for tuning in,
Daniel