Uli's Web Site
[ Zathras.de - Uli's Web Site ]
Other Sites: Stories
Abi 2000
Stargate: Resurgence
Lost? Site Map!
     home | blog | moose | programming | articles >> blog

 Blog Topics

15 Most Recent [RSS]

 Less work through Xcode and shell scripts
2011-12-16 @600
 iTunesCantComplain released
2011-10-28 @954
 Dennis Ritchie deceased
2011-10-13 @359
 Thank you, Steve.
2011-10-06 @374
 Cocoa Text System everywhere...
2011-03-27 @788
 Blog migration
2011-01-29 @520
 All you need to know about the Mac keyboard
2010-08-09 @488
 Review: Sherlock
2010-07-31 @978
 Playing with Objective C on Debian
2010-05-08 @456
 Fruit vs. Obst
2010-05-08 @439
 Mixed-language ambiguity
2010-04-15 @994
 Uli's 12:07 AM Law
2010-04-12 @881
 Uli's 1:24 AM Law
2010-04-12 @874
 Uli's 6:28 AM Law
2010-04-12 @869
 Uli's 3:57 PM Law
2010-04-12 @867


Globals, instance variables, and Visual Studio

Just got around to reading Charles Petzold's article on Visual Studio's code-writing aids and found this interesting snippet:

In the pre-object oriented days of C programming, we had local variables and global variables, variables inside of functions, and variables outside of functions. One objective of good programming in C was in keeping the number of global variables to a minimum. Because any global variable could be modified by any function, it was easier to get the sense of a program if there simply werenít very many of them ó if functions communicated among themselves strictly through arguments and return values.

Sometimes, however, C programmers got a little, letís say, sleepy, and made a lot of variables global that didnít really need to be, because it was just easier that way. Rather than add an extra argument to a function, why not just store it as a global?

Global variables are basically gone in object-oriented programming, except that fields are now the new global variables, and they can be abused just as badly.
I think that's a good point to keep in mind. And I'm not talking about the stupid don't use globals mantra some people follow. Globals are good, as long as you use them correctly and sparingly. Modern OO-systems encourage you to use accessors not just for instance variables, but also for globals.

The similarities don't end there: While instance variables are good in general, they're "global" to their class, and can be abused just as well, and can make your class's code just as unreadable. In procedural C programming, you have static variables, which are the same as a global, but can only be accessed from inside the function they were declared in. It'd be nice if there was a similar construct for instance variables, i.e. a per-instance static or so... Or is there a pattern you folks can recommend that beautifully avoids this without a new language construct?

To offer a concrete example, let's take asynchronous mouse-tracking inside a view, where you typically use ivars to keep track of what item is currently highlighted etc. Any alternatives to ivars? Is this actually a Good Thing(tm) ?

Reader Comments: (RSS Feed)
No comments yet
Or E-Mail Uli privately.

Created: 2005-12-21 @530 Last change: 2005-12-21 @569 | Home | Admin | Edit
© Copyright 2003-2023 by M. Uli Kusterer, all rights reserved.