Global Variables and Good Software Engineering

only for RuBoard - do not distribute or recompile

Global Variables and Good Software Engineering

Just a quick note on the method behind the madness here. Purists will deride my hack above (making the top-level window widgets global in scope), and technically speaking, they would be correct. It is probably more correct to tightly hide all widgets and pass them around as parameters to functions; however, I have my reasons for the method I have chosen , and it has to do with hard-learned lessons from several IT shops I have worked in.

First and foremost, I believe code should have longevity, which in my opinion means it will probably have to change in the future. Some of the studies on software development indicate that the sum total of work performed to get a version 1 product out the door will be dwarfed by the work needed to modify and maintain the code over its lifetime, which is normally much, MUCH longer than anybody anticipated. So I put ease of modification high on my list, but I also think global variables are bad. Thus, I feel the mechanism used in the previous example strikes a balance.

So why do I care if it is easy to modify if I am going to modify it? I know all about passing parameters in and out, you say. Well, mostly because the person who initially wrote the code probably will not be the one to modify it. Most likely, he or she will move on to other projects and probably other companies. Someone else will likely take ownership of that code and be responsible for maintaining it. In that case, if they understand the items in the following list, they completely understand the paradigm behind my application:

  • The widget tree, in concept and application

  • The principle that the top-level window widgets are the only global variables

  • The principle that the lookup_widget() function gives them access to any widget in any window (they just have to know which window and the widget name )

This is much simpler than teaching a person who is new to C about passing around a list of parameters of pointers to widgets. Plus, it still keeps child widgets under wraps because they cannot be accessed directly.

As part of your overall project development strategy you will have to weigh the trade-offs and decide which widgets should be variable in scope and which should not. In this book, you will use the previous example as a template for your applications for the reasons just stated.

only for RuBoard - do not distribute or recompile


MySQL Building User Interfaces
MySQL: Building User Interfaces (Landmark)
ISBN: 073571049X
EAN: 2147483647
Year: 2001
Pages: 119

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net