Good programming practice or not ? 
Author Message
 Good programming practice or not ?

Hi all,

I use the Type End Type for my global variables. Is this a good Idea or
not?

                                                Thanks,

                                                 John

Global GVar(1) As GVarType

   Type GVarType
      ShowAllCondiments As Integer
      RememberFoodItemChoice As Integer
      RememberCondimentChoice As Integer
      RememberCondimentOption As Integer
      CatChoice As Integer
      '---remember the Category PrimaryKey
      CategoryPrimaryKey As String
      CategoryPriority As Integer
      StartingTotal As Integer
      HowManyLeft As Integer
      NowWorkingOnThisMany As String
      ThisManyLeftToGo As String
      OriginalWorkingTotal As String
      ShowYouChose As Integer
      ShowWorkingOn As Integer
      CameFromCategoryWindow As Integer
      CamefromCondimentWindow As Integer
      CameFromPickAQuantityWindow As Integer
      '---used in cmdCancelFooditemQuantity
      CancelFoodItemQuantity  As Integer
      '---used in Sub ShowAllCondiments
      SelectedFromAllCondiments As Integer
      CondimentDeleted As Integer
      '---used in Sub CheckForDuplicates
      DuplicateCondimentFound As Integer
      HoldCondimentOptionConflict As Integer
      CantuseHoldWasPickedFirst As Integer
      CollectingCategory As Integer
      CollectingQuantity As Integer
      CollectingFoodName As Integer
      CollectingFoodCost As Integer
      CollectingCondimentQuantity As Integer
      CollectingCondimentOption As Integer
      CollectingCondimentNameNumber As Integer
      CollectingCondimentCost As Integer
      CollectingAllCondimentFlag As Integer
      '---used in sub PickaQuantity
      ItsBeenAdded As Integer
      HoldOriginalTotal As Integer
      MultipleFoodItemSelectionMode As Integer
      '---set in sub PickCondiment
      IncreaseCondQuantityMode As Integer
      ButtonIncreaseCondPressed As Integer
      SubTotal As Single
      MinimumDelivery As Single
      MinimumDeliveryString As String
      DeliveryCharge As Single
      DeliveryChargeString As String
      ViewMe{*filter*}scription As Integer
      PrintedOptimized As Integer
      RestoreStartingTotal As Integer
      AllowRecalculation As Integer
   End Type



Sat, 30 Oct 1999 03:00:00 GMT  
 Good programming practice or not ?



                oGSV Ethics Gradient (Doug Wilson)

Quote:
>I use the Type End Type for my global variables. Is this a good Idea or
>not?

>                                                Thanks,

>                                                 John

>Global GVar(1) As GVarType

>   Type GVarType
>      ShowAllCondiments As Integer
>      RememberFoodItemChoice As Integer
>      RememberCondimentChoice As Integer
>      RememberCondimentOption As Integer
>      CatChoice As Integer
>      '---remember the Category PrimaryKey
>      CategoryPrimaryKey As String
>      CategoryPriority As Integer
>      StartingTotal As Integer
>      HowManyLeft As Integer
>      NowWorkingOnThisMany As String
>      ThisManyLeftToGo As String
>      OriginalWorkingTotal As String
>      ShowYouChose As Integer
>      ShowWorkingOn As Integer
>      CameFromCategoryWindow As Integer
>      CamefromCondimentWindow As Integer
>      CameFromPickAQuantityWindow As Integer
>      '---used in cmdCancelFooditemQuantity
>      CancelFoodItemQuantity  As Integer
>      '---used in Sub ShowAllCondiments
>      SelectedFromAllCondiments As Integer
>      CondimentDeleted As Integer
>      '---used in Sub CheckForDuplicates
>      DuplicateCondimentFound As Integer
>      HoldCondimentOptionConflict As Integer
>      CantuseHoldWasPickedFirst As Integer
>      CollectingCategory As Integer
>      CollectingQuantity As Integer
>      CollectingFoodName As Integer
>      CollectingFoodCost As Integer
>      CollectingCondimentQuantity As Integer
>      CollectingCondimentOption As Integer
>      CollectingCondimentNameNumber As Integer
>      CollectingCondimentCost As Integer
>      CollectingAllCondimentFlag As Integer
>      '---used in sub PickaQuantity
>      ItsBeenAdded As Integer
>      HoldOriginalTotal As Integer
>      MultipleFoodItemSelectionMode As Integer
>      '---set in sub PickCondiment
>      IncreaseCondQuantityMode As Integer
>      ButtonIncreaseCondPressed As Integer
>      SubTotal As Single
>      MinimumDelivery As Single
>      MinimumDeliveryString As String
>      DeliveryCharge As Single
>      DeliveryChargeString As String
>      ViewMe{*filter*}scription As Integer
>      PrintedOptimized As Integer
>      RestoreStartingTotal As Integer
>      AllowRecalculation As Integer
>   End Type

"Good programming practice" is a very, very subjective thing.  There
are lots of ideas out there, and you have to decide what makes sense
for you, your own programming style, and your particular situation.

But in general, the use of global variables is not considered good
programming practice.

When globals are used, they are viewed as a "necessary evil" and kept
to an absolute minimum.  The kinds of information you are storing in
your globals are quantities, totals, and application status (current
selections, valid operations, etc.).  These are properties or
attributes of the various entities that your application deals with.

Good programming practice generally strives for modularity.
Modularity generally pays off with faster, easier maintenance, code
re-use, and long-term application flexibility and stability.

In your case (and in extremely general terms) a modular design would
eliminate the need for global variables altogether by keeping
information about each module inside the module itself and
establishing an interface by which other modules can access this
information.  This is the basis of object oriented development.

I'm not sure why you've chosen to "bundle" all your globals within a
user defined type.  It would seem to make access to them more
difficult during development and time-consuming at runtime (Ex:
"GVar(1).ShowAllCondiments = 1" as opposed to simply
"ShowAllCondiments = 1") without any obvious benefit.  Perhaps there
is something I am missing here.

In addition, the way you've declared your type ("Global GVar(1) As
GVarType") will actually create an array of GVarTypes.  Depending on
your Option Base statement (or lack thereof) you could be creating two
or more of these types in memory.  I'm not sure if this is your
intention or not.

"Since our problems have been our own creation
 They also can be overcome."

                        -- George Harrison (Cloud Nine)

* - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - *

        If you are a human who wants to reply via email,
    please remove the word "SPAMBLOCK" from my email address.

        If, on the other hand, you are a spam-merchant,
      gathering new victims for your indiscriminate spew,
   read the warning below before adding my name to your list.

       And may your server {*filter*}on undeliverable mail!

US Code Title 47, Sec.227(a)(2)(B), a computer / modem / printer
meets the definition of a telephone fax machine.  Sec. 227
(b)(1)(C), it is unlawful to send any unsolicited adverti{*filter*}t
to such equipment.  Sec.227(b)(3)(C), a violation of the
aforementioned Section is punishable by action to recover actual
monetary loss, or $500, whichever is greater, for each violation.

* - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - *



Mon, 01 Nov 1999 03:00:00 GMT  
 Good programming practice or not ?

Quote:
>"Good programming practice" is a very, very subjective thing.  There
>are lots of ideas out there, and you have to decide what makes sense
>for you, your own programming style, and your particular situation.

>But in general, the use of global variables is not considered good
>programming practice.

Striving to minimize global variables is good because it allows a module in
your project to be reused outside it without any code changes.  As it is now,
you'd also have to copy some globals, and you'd have to reproduce behaviour
outside your module to set these, and it would get messy.

However, using globals does not necessarily cause problems:

You may not want/need to reuse certain code,
and it speeds coding and execution time.

Some Guidelines to observe when using globals:

Use each variable for just 1 thing (as specific as possible).
For flags, preferably set and clear them only in 1 place.
Use a LongDescriptiveName that says what that 1 thing is.

Good candidates for global variables, are values that are accessed in many
functions throughout the application such as:

MainDB
ActiveMasterRecordID
MyComponentIsBusyDrawing (especially in VB3 or earlier)

Often developers replace globals with a private module variable (mMainDB) and
a public function that only returns the value (Function GetMainDB() as
database).  This might only be useful (in this case) if someday error
and integrity checking is added inside the function  (may or may not be a good
idea "It worked fine a second ago, do you really want to check again?")

Subtotals and totals tend to be double edged.  There is a temptation to cache
results in order to avoid recalculating, but you have to make sure to always
recalculate when base data changes.

The most problematic use of globals (or actually, inter function
communication) is not using specific enough variables.  For instance using
MyFormIsBusy instead of MyFormIsLoading, MyFormIsCalculating and
MyFormIsPainting.  The reason you often run into trouble by not setting
specific enough flags is that when a flag is set by two different procedures,
it can be cleared by a single procedure, usually with undesirable side
effects.

Anyways, I agree with minimizing the amount of globals.  Globals don't always
get you in trouble, but most globals have the potential to cause some.

Quote:
>When globals are used, they are viewed as a "necessary evil" and kept
>to an absolute minimum.  The kinds of information you are storing in
>your globals are quantities, totals, and application status (current
>selections, valid operations, etc.).  These are properties or
>attributes of the various entities that your application deals with.

>Good programming practice generally strives for modularity.
>Modularity generally pays off with faster, easier maintenance, code
>re-use, and long-term application flexibility and stability.

>In your case (and in extremely general terms) a modular design would
>eliminate the need for global variables altogether by keeping
>information about each module inside the module itself and
>establishing an interface by which other modules can access this
>information.  This is the basis of object oriented development.

One of the most useful benefits this approach provides the ability enhance
your code to do some checking, or setting of other variables, should that
become necessary.  It still tends to break the modularity of some functions by
making them dependent on other modules.

Quote:
>I'm not sure why you've chosen to "bundle" all your globals within a
>user defined type.  It would seem to make access to them more
>difficult during development and time-consuming at runtime (Ex:
>"GVar(1).ShowAllCondiments = 1" as opposed to simply
>"ShowAllCondiments = 1") without any obvious benefit.  Perhaps there
>is something I am missing here.

I think the idea was paritally to enforce some naming convention.  Generally,
using a lower case g as variable prefix is sufficient.

Quote:

>In addition, the way you've declared your type ("Global GVar(1) As
>GVarType") will actually create an array of GVarTypes.  Depending on
>your Option Base statement (or lack thereof) you could be creating two
>or more of these types in memory.  I'm not sure if this is your
>intention or not.

Also, probably some of these variables are global to the entire application,
while others are Active record data, that you might somehow need extra copies
of.  You probably don't need extra copies of everything in there.


Thu, 04 Nov 1999 03:00:00 GMT  
 Good programming practice or not ?

Thanks for your very informative answer.
     I changed my Global GVar(1) As GVarType to Global GVar(0) As GVarType
I hope this gives me just one instance of my array.
     The reason I chose using the Type End Type for my variables was to
give myself more control over them. I felt that GVar(0).ShowAllCondiments
= True, is just as easy as ShowAllCondiments = True and by bundeling them
all in a Type End Type I have all my variables in one place.
    Am I using more system overhead with this method?

                                                  Thanks again

                                                   John :)



Thu, 04 Nov 1999 03:00:00 GMT  
 Good programming practice or not ?

Thanks for your very informative answer.
     I changed my Global GVar(1) As GVarType to Global GVar(0) As GVarType
I hope this gives me just one instance of my array.
     The reason I chose using the Type End Type for my variables was to
give myself more control over them. I felt that GVar(0).ShowAllCondiments
= True, is just as easy as ShowAllCondiments = True and by bundeling them
all in a Type End Type I have all my variables in one place.
    Am I using more system overhead with this method?

                                                  Thanks again

                                                   John :)



Thu, 04 Nov 1999 03:00:00 GMT  
 Good programming practice or not ?

Thanks for your very informative answer.
     I changed my Global GVar(1) As GVarType to Global GVar(0) As GVarType
I hope this gives me just one instance of my array.
     The reason I chose using the Type End Type for my variables was to
give myself more control over them. I felt that GVar(0).ShowAllCondiments
= True, is just as easy as ShowAllCondiments = True and by bundeling them
all in a Type End Type I have all my variables in one place.
    Am I using more system overhead with this method?

                                                  Thanks again

                                                   John :)



Thu, 04 Nov 1999 03:00:00 GMT  
 Good programming practice or not ?

Thanks for your very informative answer.
     I changed my Global GVar(1) As GVarType to Global GVar(0) As GVarType
I hope this gives me just one instance of my array.
     The reason I chose using the Type End Type for my variables was to
give myself more control over them. I felt that GVar(0).ShowAllCondiments
= True, is just as easy as ShowAllCondiments = True and by bundeling them
all in a Type End Type I have all my variables in one place.
    Am I using more system overhead with this method?

                                                  Thanks again

                                                   John :)



Thu, 04 Nov 1999 03:00:00 GMT  
 
 [ 7 post ] 

 Relevant Pages 

1. Good programming practice? - Generic options

2. CREATING wizards in vb: best programming practice?

3. good programming practice!!?? and VB controls

4. Access Development Best Practices Chat 10/03/02

5. change in join syntax best practices?

6. best practice

7. Good practice question

8. VBA "good practice" re If statements

9. Good Design Practice?

10. Best practice for data-related business objects

11. VB.NET Good GUI Framewerk Practices

12. Best Practices: DataSet

 

 
Powered by phpBB® Forum Software