Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Classes - Best Practices?
Message
Information générale
Forum:
Visual FoxPro
Catégorie:
Classes - VCX
Divers
Thread ID:
00763778
Message ID:
00765448
Vues:
17
>Why do you really need to UNDEFINE them?
I UNDEFINE constants when I'm using a procedure file with more than one routine in it. I place the #DEFINEs at the beginning of the routine and the #UNDEFINEs at the end of the routine so that I can use the constant names again if need be. Why would I want to reuse constant names? Read on.

> What sort of local constants are you trying to create that you want only scoped to a method and not to the whole class?...Again a little context of what you are doing will help us give you some options on the ways to do it in VFP.

Okay, here's the context:
Well, it's mostly just my preferred way to work, but rather than use literals in my code, I use constants. Here is a really simplistic example: Instead of using something like:
chrWhatever = "" 
FOR numCounter = 1 TO 10
Instead, I'll use:
chrWhatever = conZeroLengthString  
FOR numCounter = conMinumumLength TO conMaximumLength
If I know the value at design time, I will code it in a constant instead of using a literal. I TRY to do that EVERY time. The idea actually comes from the book "Practical Standards for Microsoft Visual Basic" which has quite a few good ideas that are language-independent - not that you necessarily have to agree with all of them. The following quote is from that book:

"When you hard-code a string in a procedure, the effect is similar to that of using magic numbers. All the reasons for eliminating magic numbers also apply to eliminating hard-coded strings; if you know the value at design time, use a constant rather than hard-coding the text or the number."

The reason for constants that are scoped locally is mostly portability. I'm always trying to make my code as generic as possible. I always ask myself "How easy would it be to place this piece of code in another class/routine/application and have it run without any changes?" - even if I have no intention of ever doing it. Locally-scoped constants mean that I don't have to carry class-level or public constants along with the routine to make it work.

As with variables, I try to scope constants locally unless there is a need for other methods/routines to know about them. I try not to give them a scope that is broader than necessary so that I don't create unnecessary coupling.

There are lots of arguments one way or the other as to whether or not this is necessary. I don't really want to debate the issue. I just personally find it to be a better way to work.

I hope this helps give you the context you were looking for.

- John
John Groft
Consultant
Computer Task Group, Inc.
Précédent
Répondre
Fil
Voir

Click here to load this message in the networking platform