Struggling with classes and how to access them properly...

Started by
10 comments, last by Estauns 22 years, 2 months ago
quote:Original post by SabreMan
There are two things in your post which I find contentious. Please don''t take this as I am picking on you, I just wanted to add my opinion to what you wrote.

No problem, that is the purpose of all that...
quote:
Firstly, I don''t like this idea that people think OO, or that every computer program should be constructed using purely OO techniques. Not that you said exactly that, but it''s something I keep hearing and you reminded me of it. In short, there are some problems which can be expressed elegantly without using OO techniques, and so the issue boils down to "good design versus bad design" rather than "OO versus non-OO".

Nobody can say that OO is always the BEST and ONLY solution, but OO always offers solutions. Because OO requires a good and rational structuration of problems, thinking problems with OO will produce a better understanding of them.
quote:
Secondly, I felt that your solution is a poor answer to the question of global variables. You''ve made a subtle move to having a globalised command class in your application, which you are using as a dumping ground for what was previously global. This is merely paying lip-service to the notion of avoiding global variables. OK, you only have one global variable, but you haven''t addressed the reasons of why you should avoid global variables.

Well, you are right; I didn''t give my opinion about global variables. When I began to program, I was using global variables for almost anything (like most of beginners do, I suppose). As the time and the projects were passing, I realized that this model was a problem and would prevent me for being able to achieve bigger projects because:

1) Global variables are confusing: after a long time you don’t always remember what the variable is for, who you have planed to use it, etc. Imagine when you have 40 or more globals ;

2) Global variables are dangerous: you can do anything anywhere with a global, even commit the worst (bogus assignations, wrong usage);

3) Global variables are inaccurate : a global is initialized at the beginning of the program even it is used in 0.1% of the code ;

4) Global variables are often the symptom of a bad problem analysis: globals are just like saying: the problem can not be think as a system, just like a myriad of standalone phenomenon’s.

For all the reasons (and much more), I am using OO coding. I don’t want to be proselyte, but, to my opinion, it is a good way to think.

DworD
DworD
Advertisement
quote:I''ve actually been wondering the same thing, how do the "big boys" avoid a lot of global variables? Anyone know?


I tend to declare an application class that holds all the functions and variables of my program.

  class CApplication{public:  CApplication();  BOOL   Create();  BOOL   Run();  SOMETYPE   SomeVariable;  CDisplay   *m_pDisplay;};  


I tend to modularise everything. Eg for display, I''ll have a CDisplay class that holds all the routines to draw stuf etc..

But that''s just me. I hate globals.

Oli


All the best problems start with C

.:: K o s m o s E n g i n e ::.

This topic is closed to new replies.

Advertisement