Path: news.mathworks.com!newsfeed-00.mathworks.com!newsfeed2.dallas1.level3.net!news.level3.com!postnews.google.com!l42g2000hsc.googlegroups.com!not-for-mail
From: ImageAnalyst <imageanalyst@mailinator.com>
Newsgroups: comp.soft-sys.matlab
Subject: Re: eval, feval, global
Date: Sun, 30 Mar 2008 17:59:45 -0700 (PDT)
Organization: http://groups.google.com
Lines: 116
Message-ID: <5f332995-1a52-4cb2-bf08-59807891b2bf@l42g2000hsc.googlegroups.com>
References: <fsebf0$43u$1@fred.mathworks.com> <fsp7th$j9u$1@fred.mathworks.com> 
NNTP-Posting-Host: 75.186.67.199
Mime-Version: 1.0
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable
X-Trace: posting.google.com 1206925186 19900 127.0.0.1 (31 Mar 2008 00:59:46 GMT)
X-Complaints-To: groups-abuse@google.com
NNTP-Posting-Date: Mon, 31 Mar 2008 00:59:46 +0000 (UTC)
Complaints-To: groups-abuse@google.com
Injection-Info: l42g2000hsc.googlegroups.com; posting-host=75.186.67.199; 
User-Agent: G2/1.0
X-HTTP-UserAgent: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 
Xref: news.mathworks.com comp.soft-sys.matlab:459980


On Mar 30, 8:18=A0pm, "Steven Lord" <sl...@mathworks.com> wrote:
> "Yuri Geshelin" <geshe...@hotmail.com> wrote in message
>
> news:fsp7th$j9u$1@fred.mathworks.com...
>
>
>
>
>
> > "Yuri Geshelin" <geshe...@hotmail.com> wrote in message
> > <fsebf0$43...@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. =A0It
> works perfectly fine on its own.
>
> Now I write a function, baz, that uses a global variable x. =A0It too work=
s
> perfectly fine on its own.
>
> Finally, someone else finds your foo function and my baz function, which a=
re
> perfect for the application he or she is writing. =A0That person creates a=

> function, foobarbaz that uses your foo function and my baz function. =A0Th=
ey
> find that the foobarbaz function doesn't work, and are confused as to why =
it
> doesn't work. =A0Each of the subfunctions works well on their own, and eac=
h
> 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. =A0It
> wouldn't be _that_ hard to notice that both subfunctions are working with
> the same global variable. =A0Now picture a larger project, where there are=
 ten
> or twenty functions involved. =A0Tracing 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. =A0I 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 fro=
m
> 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. =A0That helps in debugging.
>
> In the example I posted above, if foobarfaz has to pass parameters as inpu=
ts
> 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
> sl...@mathworks.com- Hide quoted text -
>
> - Show quoted text -


----------------------------------------------------------------
But Steven, can you pass your structure variable into a callback
function for a button on your GUI?  As far as I know the answer is
No.  You're limited to the 3 default callback arguments (hObject,
etc.).  Let's say your callback function needs to use some variable
that has been set by some other function in your program.  It's this
situation where I use global variables because I don't know of other
anyway to get variables into the function (i.e. ones that aren't
retrievable from one of the other GUI controls).  If there is a way to
get variables into a callback function without using global variables,
let me know.
Thanks,
ImageAnalyst