<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
		>
<channel>
	<title>Comments on: Trying to Match Behavior of a New Function to an Existing One</title>
	<atom:link href="http://blogs.mathworks.com/loren/2006/07/26/trying-to-match-behavior-of-a-new-function-to-an-existing-one/feed/" rel="self" type="application/rss+xml" />
	<link>http://blogs.mathworks.com/loren/2006/07/26/trying-to-match-behavior-of-a-new-function-to-an-existing-one/</link>
	<description>Loren Shure works on design of the MATLAB language at MathWorks. She writes here about once a week on MATLAB programming and related topics.</description>
	<lastBuildDate>Mon, 13 Feb 2012 13:24:10 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
	<item>
		<title>By: Peter Meilstrup</title>
		<link>http://blogs.mathworks.com/loren/2006/07/26/trying-to-match-behavior-of-a-new-function-to-an-existing-one/#comment-30527</link>
		<dc:creator>Peter Meilstrup</dc:creator>
		<pubDate>Wed, 12 Aug 2009 23:57:27 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.mathworks.com/loren/?p=47#comment-30527</guid>
		<description>I suppose you mean adding two more functionless lines to make it something like this:

&lt;pre&gt;
1     function y = expMuSig(x,mu,sigma)
2     %Exponential function with parameters mu and sigma.
3     try 
4         y = exp(-0.5 * ((x - mu)./sigma).^2) ./ (sqrt(2*pi) .* sigma);
5     catch theRealError
6        theErrorWhichIsALie = MException(&#039;LSBlog:sizeMismatch&#039;,&#039;Non-scalar arguments must follow MATLAB dimension matching rules.&#039;);
7        theErrorWhichIsALie = addCause(theErrorWhichIsALie, theRealError);
8        throw(theErrorWhichIsALie);
9   end
&lt;/pre&gt;

Again, what information does &quot;using exceptions and adding a cause&quot; add to the error report that was not already present (i.e. that the program failed while calling expMuSig)?

It is writing &quot;error handling&quot; code in a cargo-cult manner because you *think* you should be writing error handling code everywhere. But the code is carefully constructed to be idempotent to not writing any error handling code at all, other than it additionally gives back some false information that you have to dig down to the &quot;cause&quot; to reveal. What you&#039;ve managed to prove conclusively is that this is a case where you should actually not write &quot;error handling&quot; code, if that code does not actually handle errors.</description>
		<content:encoded><![CDATA[<p>I suppose you mean adding two more functionless lines to make it something like this:</p>
<pre>
1     function y = expMuSig(x,mu,sigma)
2     %Exponential function with parameters mu and sigma.
3     try
4         y = exp(-0.5 * ((x - mu)./sigma).^2) ./ (sqrt(2*pi) .* sigma);
5     catch theRealError
6        theErrorWhichIsALie = MException('LSBlog:sizeMismatch','Non-scalar arguments must follow MATLAB dimension matching rules.');
7        theErrorWhichIsALie = addCause(theErrorWhichIsALie, theRealError);
8        throw(theErrorWhichIsALie);
9   end
</pre>
<p>Again, what information does &#8220;using exceptions and adding a cause&#8221; add to the error report that was not already present (i.e. that the program failed while calling expMuSig)?</p>
<p>It is writing &#8220;error handling&#8221; code in a cargo-cult manner because you *think* you should be writing error handling code everywhere. But the code is carefully constructed to be idempotent to not writing any error handling code at all, other than it additionally gives back some false information that you have to dig down to the &#8220;cause&#8221; to reveal. What you&#8217;ve managed to prove conclusively is that this is a case where you should actually not write &#8220;error handling&#8221; code, if that code does not actually handle errors.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Loren</title>
		<link>http://blogs.mathworks.com/loren/2006/07/26/trying-to-match-behavior-of-a-new-function-to-an-existing-one/#comment-30524</link>
		<dc:creator>Loren</dc:creator>
		<pubDate>Wed, 12 Aug 2009 11:06:14 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.mathworks.com/loren/?p=47#comment-30524</guid>
		<description>Peter-

This post was written before MATLAB had exceptions.  I agree that now it would be better written using exceptions and adding a cause.

--Loren</description>
		<content:encoded><![CDATA[<p>Peter-</p>
<p>This post was written before MATLAB had exceptions.  I agree that now it would be better written using exceptions and adding a cause.</p>
<p>&#8211;Loren</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Peter Meilstrup</title>
		<link>http://blogs.mathworks.com/loren/2006/07/26/trying-to-match-behavior-of-a-new-function-to-an-existing-one/#comment-30523</link>
		<dc:creator>Peter Meilstrup</dc:creator>
		<pubDate>Wed, 12 Aug 2009 07:32:53 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.mathworks.com/loren/?p=47#comment-30523</guid>
		<description>Goodness, that&#039;s ugly. What your code segment does there is to take useful information that was already in the error message and hide it behind your own message (containing something about argument size rules with is pure presumption on your part -- the input might be bad for any number of other reasons, making your error message a lie.)

The code segment is idempotent to actually not doing any error checking at all, except it provides even less information to the programmer when it fails. Given the choice between no error checking at all, and that unnecessary catch block the replaces a stack trace with a likely incorrect error message, I prefer no checking.

The purpose of exceptions is to provide useful information to the programmer via an error message and stack trace, and to give code an opportunity to clean up from failures. If you&#039;re not willing to further either purpose, don&#039;t catch exceptions.</description>
		<content:encoded><![CDATA[<p>Goodness, that&#8217;s ugly. What your code segment does there is to take useful information that was already in the error message and hide it behind your own message (containing something about argument size rules with is pure presumption on your part &#8212; the input might be bad for any number of other reasons, making your error message a lie.)</p>
<p>The code segment is idempotent to actually not doing any error checking at all, except it provides even less information to the programmer when it fails. Given the choice between no error checking at all, and that unnecessary catch block the replaces a stack trace with a likely incorrect error message, I prefer no checking.</p>
<p>The purpose of exceptions is to provide useful information to the programmer via an error message and stack trace, and to give code an opportunity to clean up from failures. If you&#8217;re not willing to further either purpose, don&#8217;t catch exceptions.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Loren</title>
		<link>http://blogs.mathworks.com/loren/2006/07/26/trying-to-match-behavior-of-a-new-function-to-an-existing-one/#comment-4162</link>
		<dc:creator>Loren</dc:creator>
		<pubDate>Thu, 27 Jul 2006 11:48:32 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.mathworks.com/loren/?p=47#comment-4162</guid>
		<description>Paul-

I am not sure I understand your question about error checking in MATLAB functions, but I think you are asking if we are always careful about the state of &lt;kbd&gt;lasterror&lt;/kbd&gt;.  I can&#039;t answer definitively, but my guess is that we&#039;re &lt;i&gt;mostly&lt;/i&gt; but not perfectly good about it.  It is something we are aware of and have on a (long) list of things to watch for.

--Loren</description>
		<content:encoded><![CDATA[<p>Paul-</p>
<p>I am not sure I understand your question about error checking in MATLAB functions, but I think you are asking if we are always careful about the state of <kbd>lasterror</kbd>.  I can&#8217;t answer definitively, but my guess is that we&#8217;re <i>mostly</i> but not perfectly good about it.  It is something we are aware of and have on a (long) list of things to watch for.</p>
<p>&#8211;Loren</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Loren</title>
		<link>http://blogs.mathworks.com/loren/2006/07/26/trying-to-match-behavior-of-a-new-function-to-an-existing-one/#comment-4161</link>
		<dc:creator>Loren</dc:creator>
		<pubDate>Thu, 27 Jul 2006 11:46:32 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.mathworks.com/loren/?p=47#comment-4161</guid>
		<description>Oliver-

&lt;i&gt;I&lt;/i&gt; don&#039;t find the code I wrote unclear, but I definitely agree with you that yours is even clearer and that others could be confused about &lt;kbd&gt;continue&lt;/kbd&gt;.  I&#039;m glad you learned something more about it by reading the doc!

--Loren</description>
		<content:encoded><![CDATA[<p>Oliver-</p>
<p><i>I</i> don&#8217;t find the code I wrote unclear, but I definitely agree with you that yours is even clearer and that others could be confused about <kbd>continue</kbd>.  I&#8217;m glad you learned something more about it by reading the doc!</p>
<p>&#8211;Loren</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Paul</title>
		<link>http://blogs.mathworks.com/loren/2006/07/26/trying-to-match-behavior-of-a-new-function-to-an-existing-one/#comment-4160</link>
		<dc:creator>Paul</dc:creator>
		<pubDate>Thu, 27 Jul 2006 06:57:27 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.mathworks.com/loren/?p=47#comment-4160</guid>
		<description>Hello, I have made the experience that it can help a lot if people get further information about the error to be able to fix it. The simplest method would be to add lasterr one line above the error statement so the original error is printed and the user gets a better idea what might be wrong. Else the information is lost after the error in the catch block is thrown.
One question, how sure can I be about error checking in Matlab functions?</description>
		<content:encoded><![CDATA[<p>Hello, I have made the experience that it can help a lot if people get further information about the error to be able to fix it. The simplest method would be to add lasterr one line above the error statement so the original error is printed and the user gets a better idea what might be wrong. Else the information is lost after the error in the catch block is thrown.<br />
One question, how sure can I be about error checking in Matlab functions?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Oliver A. Chapman, PE</title>
		<link>http://blogs.mathworks.com/loren/2006/07/26/trying-to-match-behavior-of-a-new-function-to-an-existing-one/#comment-4156</link>
		<dc:creator>Oliver A. Chapman, PE</dc:creator>
		<pubDate>Thu, 27 Jul 2006 01:16:04 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.mathworks.com/loren/?p=47#comment-4156</guid>
		<description>As so frequently happens, this week&#039;s column was a good exposure to new (for me) styles of programming.

First, regarding the primary lesson from the column: Yes, the try / catch method seems like a robust method to deal with the issue of error handling.  We all should be in favor of techniques that make code easy to maintain.  Thanks for illustrating the technique.  Plus, it takes fewer modules of code and fewer lines of code.  All these are good.

Now for the unexpected detail:  In your first (complicated) batch of code you have:
&lt;pre&gt;
16    for k = 1:length(varargin)
17        if isscalar(varargin{k})
18            continue
19        end
20        nonscalarsizes{end+1} = int2str(size(varargin{k})); 
21    end
&lt;/pre&gt;
At first, I couldn&#039;t follow what was happening with the &quot;if&quot; statement and the &quot;continue&quot;.  I thought the &quot;continue&quot; would take you out of the &quot;if&quot; statement.

Although I&#039;ve criticized a few sections of MatLab documentation in the past, the &quot;continue&quot; section served very well in this case.  The documentation was clear that the continue statement terminated this cycle of the &quot;for&quot; loop.

So, I was confused, I read the documentation and now I understand.  Success!

But, I don&#039;t think this coding technique is sound because many others who will read the code will also make the same assumption that I did.  Isn&#039;t it clearer to do a negative test?  Something like this:
&lt;pre&gt;
16    for k = 1:length(varargin)
17        if ~isscalar(varargin{k})
18            nonscalarsizes{end+1} = int2str(size(varargin{k}));
19        end
20        
21    end
&lt;/pre&gt;
You even save a line of code.</description>
		<content:encoded><![CDATA[<p>As so frequently happens, this week&#8217;s column was a good exposure to new (for me) styles of programming.</p>
<p>First, regarding the primary lesson from the column: Yes, the try / catch method seems like a robust method to deal with the issue of error handling.  We all should be in favor of techniques that make code easy to maintain.  Thanks for illustrating the technique.  Plus, it takes fewer modules of code and fewer lines of code.  All these are good.</p>
<p>Now for the unexpected detail:  In your first (complicated) batch of code you have:</p>
<pre>
16    for k = 1:length(varargin)
17        if isscalar(varargin{k})
18            continue
19        end
20        nonscalarsizes{end+1} = int2str(size(varargin{k}));
21    end
</pre>
<p>At first, I couldn&#8217;t follow what was happening with the &#8220;if&#8221; statement and the &#8220;continue&#8221;.  I thought the &#8220;continue&#8221; would take you out of the &#8220;if&#8221; statement.</p>
<p>Although I&#8217;ve criticized a few sections of MatLab documentation in the past, the &#8220;continue&#8221; section served very well in this case.  The documentation was clear that the continue statement terminated this cycle of the &#8220;for&#8221; loop.</p>
<p>So, I was confused, I read the documentation and now I understand.  Success!</p>
<p>But, I don&#8217;t think this coding technique is sound because many others who will read the code will also make the same assumption that I did.  Isn&#8217;t it clearer to do a negative test?  Something like this:</p>
<pre>
16    for k = 1:length(varargin)
17        if ~isscalar(varargin{k})
18            nonscalarsizes{end+1} = int2str(size(varargin{k}));
19        end
20
21    end
</pre>
<p>You even save a line of code.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Loren</title>
		<link>http://blogs.mathworks.com/loren/2006/07/26/trying-to-match-behavior-of-a-new-function-to-an-existing-one/#comment-4155</link>
		<dc:creator>Loren</dc:creator>
		<pubDate>Wed, 26 Jul 2006 19:12:33 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.mathworks.com/loren/?p=47#comment-4155</guid>
		<description>Aravind-
&lt;p&gt;
What you are seeing is the difference between &lt;kbd&gt;/&lt;/kbd&gt; and &lt;kbd&gt;./&lt;/kbd&gt;.  Try &lt;kbd&gt;x./sigma&lt;/kbd&gt; and you will see the output is also empty.  For the slash operator, which is matrix division, the dimensions have to agree but for the point-wise ones, where we allow scalar expansion, we have the output the size of the nonscalar input.  Empty is considered, with size 0x0, to not be a scalar (which has size 1x1) and therefore its dimensions propagate through.
&lt;p&gt;
--Loren</description>
		<content:encoded><![CDATA[<p>Aravind-</p>
<p>
What you are seeing is the difference between <kbd>/</kbd> and <kbd>./</kbd>.  Try <kbd>x./sigma</kbd> and you will see the output is also empty.  For the slash operator, which is matrix division, the dimensions have to agree but for the point-wise ones, where we allow scalar expansion, we have the output the size of the nonscalar input.  Empty is considered, with size 0&#215;0, to not be a scalar (which has size 1&#215;1) and therefore its dimensions propagate through.</p>
<p>
&#8211;Loren</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Loren</title>
		<link>http://blogs.mathworks.com/loren/2006/07/26/trying-to-match-behavior-of-a-new-function-to-an-existing-one/#comment-4154</link>
		<dc:creator>Loren</dc:creator>
		<pubDate>Wed, 26 Jul 2006 19:08:40 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.mathworks.com/loren/?p=47#comment-4154</guid>
		<description>Aslak-
&lt;p&gt;
I use a message id when I use &lt;a href=&quot;http://www.mathworks.com/access/helpdesk/help/techdoc/ref/error.html&quot; rel=&quot;nofollow&quot;&gt;&lt;kbd&gt;error&lt;/kbd&gt;&lt;/a&gt; because we use the id to help us show messages in Japanese for the Windows version of MATLAB for Japan.
&lt;p&gt;
I &lt;b&gt;always&lt;/b&gt; try to use a message id when I add a &lt;a href=&quot;http://www.mathworks.com/access/helpdesk/help/techdoc/ref/error.html&quot; rel=&quot;nofollow&quot;&gt;&lt;kbd&gt;warning&lt;/kbd&gt;&lt;/a&gt; since that is how users can selectively turn warnings on and off.
&lt;p&gt;
--Loren</description>
		<content:encoded><![CDATA[<p>Aslak-</p>
<p>
I use a message id when I use <a href="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/error.html" rel="nofollow"><kbd>error</kbd></a> because we use the id to help us show messages in Japanese for the Windows version of MATLAB for Japan.</p>
<p>
I <b>always</b> try to use a message id when I add a <a href="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/error.html" rel="nofollow"><kbd>warning</kbd></a> since that is how users can selectively turn warnings on and off.</p>
<p>
&#8211;Loren</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Aravind Seshadri</title>
		<link>http://blogs.mathworks.com/loren/2006/07/26/trying-to-match-behavior-of-a-new-function-to-an-existing-one/#comment-4152</link>
		<dc:creator>Aravind Seshadri</dc:creator>
		<pubDate>Wed, 26 Jul 2006 18:18:15 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.mathworks.com/loren/?p=47#comment-4152</guid>
		<description>I was just curious. When

EDU&gt;&gt; x = 10;
EDU&gt;&gt; mu = 5;
EDU&gt;&gt; sigma = [];
EDU&gt;&gt; expMuSig(x, mu, sigma)

ans =

     []

The function returns []. I went back and checked it. When x is scalar, sigma = [] and if I do

EDU&gt;&gt; x/sigma
??? Error using ==&gt; mrdivide
Matrix dimensions must agree.

but if perform an array division

EDU&gt;&gt; x./sigma

ans =

     []

Could you explain what is happening.</description>
		<content:encoded><![CDATA[<p>I was just curious. When</p>
<p>EDU&gt;&gt; x = 10;<br />
EDU&gt;&gt; mu = 5;<br />
EDU&gt;&gt; sigma = [];<br />
EDU&gt;&gt; expMuSig(x, mu, sigma)</p>
<p>ans =</p>
<p>     []</p>
<p>The function returns []. I went back and checked it. When x is scalar, sigma = [] and if I do</p>
<p>EDU&gt;&gt; x/sigma<br />
??? Error using ==&gt; mrdivide<br />
Matrix dimensions must agree.</p>
<p>but if perform an array division</p>
<p>EDU&gt;&gt; x./sigma</p>
<p>ans =</p>
<p>     []</p>
<p>Could you explain what is happening.</p>
]]></content:encoded>
	</item>
</channel>
</rss>

