Path: news.mathworks.com!not-for-mail
From: "Steven Lord" <slord@mathworks.com>
Newsgroups: comp.soft-sys.matlab
Subject: Re: eval, feval, global
Date: Sun, 30 Mar 2008 20:18:11 -0400
Organization: The MathWorks, Inc.
Lines: 83
Message-ID: <fspak3$787$1@fred.mathworks.com>
References: <fsebf0$43u$1@fred.mathworks.com> <fsp7th$j9u$1@fred.mathworks.com>
Reply-To: "Steven Lord" <slord@mathworks.com>
NNTP-Posting-Host: lords.dhcp.mathworks.com
X-Trace: fred.mathworks.com 1206922691 7431 144.212.105.187 (31 Mar 2008 00:18:11 GMT)
X-Complaints-To: news@mathworks.com
NNTP-Posting-Date: Mon, 31 Mar 2008 00:18:11 +0000 (UTC)
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2900.3138
X-RFC2646: Format=Flowed; Original
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.3198
Xref: news.mathworks.com comp.soft-sys.matlab:459979



"Yuri Geshelin" <geshelin@hotmail.com> wrote in message 
news:fsp7th$j9u$1@fred.mathworks.com...
> "Yuri Geshelin" <geshelin@hotmail.com> wrote in message
> <fsebf0$43u$1@fred.mathworks.com>...
>> 1) Why are eval, feval considered not elegant and banned
> in
>> programming contests? I often use eval, why should a good
>> programmer stay away from it?
>>
>> 2) Same question about GLOBAL.
>>
>> Yuri
>>
>
> Sorry for bringing this up again. It's about the second
> issue: the use of GLOBAL. When John said that it "can make
> debugging an interesting task", I thought I had grasped the
> subject and agreed with him. But now I think I
> misunderstand something.

Let's say you write a function, foo, that uses a global variable x.  It 
works perfectly fine on its own.

Now I write a function, baz, that uses a global variable x.  It too works 
perfectly fine on its own.

Finally, someone else finds your foo function and my baz function, which are 
perfect for the application he or she is writing.  That person creates a 
function, foobarbaz that uses your foo function and my baz function.  They 
find that the foobarbaz function doesn't work, and are confused as to why it 
doesn't work.  Each of the subfunctions works well on their own, and each 
piece of code in the subfunctions that work with the x variable appear to be 
working correctly.

Now this is a small example, with just three functions involved.  It 
wouldn't be _that_ hard to notice that both subfunctions are working with 
the same global variable.  Now picture a larger project, where there are ten 
or twenty functions involved.  Tracing how each one interacts with the 
others via global variables will quickly become messy.

> Let me present you with a situation, which I ran across. It
> probably has to do with John&#8217;s remark. I was writing a GUI.
> It took me a while, I would put it off and get back to it
> in months. I was using dozens of GLOBAL variables. One day
> I realized that I needed to fix a bug in a button&#8217;s
> callback. I quickly wrote up a function, which takes care
> of the glitch. This should have been very simple, but I got
> an error message at the stage, when I least expected it.
> Here is what happened. I had global variable PROP and
> forgot about it, because I wasn&#8217;t working on my GUI for a
> month. Now, in order to accomplish the task at hand, I
> happened to use variable PROP again, but for a totally
> different purpose... And again, I declared it as global. No
> comments.....

Don't worry.  I don't think you're alone in having done that at one time or 
another.

> This was what I recalled when John said that globals "can
> make debugging an interesting task". I do realize that this
> is a potential source of errors. But in terms of my
> example, please explain me how exactly I should follow
> these John&#8217;s recommendations:
>
> "Stuff your parameters into a structure that gets passed
> around. Use nested functions."

If all of your parameters are stored in a struct array that get passed from 
function to function, any change to the parameters have to be made to the 
struct array and returned from those functions, so you know where and when 
those parameters were changed.  That helps in debugging.

In the example I posted above, if foobarfaz has to pass parameters as inputs 
to foo and baz, then any changes to the parameters in foo won't affect baz 
unless they are returned from foo and the modified parameters passed into 
baz.

-- 
Steve Lord
slord@mathworks.com