Bonjour,
Dans son message, < Peter Hewett > écrivait :
In this message, < Peter Hewett > wrote:
|| Hi Jean-Guy Marcil
||
|| Said <I like to use public Booleans that I use to test for conditions if
those
|| conditions have repercussions in many modules or procedures... Is that a
bad
|| habit?>
||
|| That's one of the reasons I'm a strong proponent for class modules. If
you
|| encapsulate stuff in a class you can often do away with Pubic flags and
|| implement and use a Property. I guess like most things you may just have
to use
|| them, but I try to design out Globals and limit the maximum scope of
variables
|| to Private.
I have to learn more about class modules... Somehow, except for a few
applications where the procedure is clearly outlined somewhere (e.g. there
are tons of examples for creating Document event in Word VBA on the net, I
can use those), I tend to shy away from them because I do not understand
them as well as I should... Any good links on learning more about class
modules?
||
|| This gets to be more of a problem with large projects, trying to work out
what's
|| setting/clearing Public variables can be a nightmare as there are no
clear
|| functional boundaries.
Totally true, I have experienced this in larger projects where I had 6 or 7
public flags... after a while you do not know what is set and where.... It
can get difficult to maintain and build upon.
TIA
||
|| Also with procedures I always explicitly declare their scope.
||
|| Cheers - Peter
||
||
|| "Jean-Guy Marcil" <
[email protected]>, said:
||
||| Bonjour,
|||
||| Dans son message, < Peter Hewett > écrivait :
||| In this message, < Peter Hewett > wrote:
|||
||||| Hi Jean-Guy Marcil
|||||
||||| There are a number of techniques to get around this problem.
||| Modularisation is
||||| one of them and just plain good programming practice. By
||| 'modularisation" I
||||| mean break the code down in sub procedures or even better yet class
modules.
||||| Each procedure should be relatively short, this is an arbitrary value
judgement
||||| call. I use something in the order of 60-100 lines max per procedure.
Normally
||||| much shorter. I don't worry about the amount of code in standard
module,
||| I just
||||| use them for functional grouping. Likewise with class or UserForm
||| modules I
||||| tend to keep all code that relates to the class in one module rather
than spread
||||| it over multiple modules.
|||||
||||| Within each procedure use block structure and indentation to make the
||| code as
||||| readable as possible. To skip certain parts you can use
If/Then/Else/EndIf. If
||||| you need to test for the same condition more than once and the test is
expensive
||||| declare and use a temporary variable. VB/VBA creates invisible
temporary
||||| variables anyway, so you might as well declare and use your own! The
||| code may
||||| be a tad more verbose (like me!) but it's more efficient. Also use
With blocks
|||
||| I like to use public Booleans that I use to test for conditions if those
||| conditions have repercussions in many modules or procedures... Is that a
bad
||| habit?
|||
||||| where you can. Since a With block only provides a shortcut to one
||| object, I
||||| often assign other expensive object references to a temporary variable
||| for use
||||| within the With block if I refer to it more than once or twice.
|||||
||||| By expensive i mean it drills down through a number of objects as in:
||||| ActiveDocument.Range.ParagraphFormat.Borders.InsideColor
|||||
||||| Every time you use a dot operator in an object reference VB/VBA
creates a
||||| temporary variable! If your using COM then every dot operation is a
COM call
||||| (high overhead stuff).
|||
||| Thanks for the reminder on that point... I have read this before, but
tend
||| to forget abut it when writing code...
|||
|||||
||||| I write a "lot" of code and apart from "On Error Goto ?" I've not used
a GoTo
||||| statement in the last 7 years. If you ever get close to a situation
||| where you
||||| think "GoTo" it's probably time to rethink, redesign, re-implement
your code!
|||
||| I agree with the general principle... I remember one particular project
I
||| had to redesigned because of that particular problem. But I think that
If
||| you use one GoTo as a shortcut.... it's not that bad...
|||
|||||
||||| If you've had a chance to look at the "FormField Tab Order" add-in I
emailed
||||| you, you'll see the above put into practice.
|||
||| Sorry... been busy with work and the kids lately... will get around to
it
||| eventually! Promise!
|||
|||||
||||| Oh yeah, while I on my soapbox commenting your code is essential, not
a nice
||||| optional extra. On average for every 100 lines of code I write there
are 40
||||| lines of comments. It also essential to comment "why" your code is
doing
||||| something and not the "what". The "what" tends to be self evident
from
||| the code
||||| (once you understand the language).
|||
||| Absolutely right... I have been whackedon the side of the head by my
||| laziness to comment... I remember a client asking for modifications 2
years
||| after I wrote some code.... In those 2 years I had learned new
techniques
||| and stuff.... So I had no idea what why I had written the code the way I
||| had... Had to debug step by step ... A real waste of time! But once I
get
||| into a groove, I forget about slowing down to write comments... Have to
whip
||| myself into shape regarding this ;-)
|||
||| Thanks for taking the time!
|||
|||||
||||| Cheers - Peter
|||||
|||||
||||| "Jean-Guy Marcil" <
[email protected]>, said:
|||||
|||||| Bonjour,
||||||
|||||| Dans son message, notre ami < Peter Hewett > nous laissait savoir que
:
|||||| In this message, our friend < Peter Hewett > informed us that:
||||||
||||||
|||||||| Hi Jean-Guy Marcil
||||||||
|||||||| GoTo's are really a hang over from pre block structured code days.
||| With the
|||||||| exception of error handlers in VB/VBA where you don't have a choice
||| and have to
|||||||| use a very specific form of GoTo. They are actively discourage as
||| they cause
|||||||| branches in the logic break the block structure. Add more than a
couple to your
|||||||| code and you end up with spaghetti, just like in the good old days!
||| Even the
|||||||| online help discourages their use.
||||||||
|||||||| VB.Net implements block structured error handling with
Try/Catch/Finally/End
|||||||| Try.
||||||
||||||
|||||| Ok, I see, they are frowned upon because if they are overused they
can cause
|||||| nightmares when trying to maintain code.
|||||| I guess that if you only use it here and there, there's no harm, but
I
||| can
|||||| see the problems if you start branching out all over the place with a
bunch
|||||| of them.
||||||
|||||| Can I bother you with another question then?
|||||| Very often you have a fairly long module containing a lot of user or
|||||| document interaction. Depending on the user input or the conditions
||| found in
|||||| the document, you want your code to skip certain parts of the module.
|||||| Without using GoTo, how do you get the code to jump over certain
parts
||| if
|||||| they do not apply? Is it better in such a situation to break the
module
|||||| apart in a bunch of little modules and call each smaller module as
|||||| appropriate... or are there other ways of handling this?
||||||
|||||| Thanks.
|||||
||||| HTH + Cheers - Peter
|||
||| --
||| Salut!
||| _______________________________________
||| Jean-Guy Marcil - Word MVP
||| (e-mail address removed)
||| Word MVP site:
http://www.word.mvps.org
|||
|||
||
|| HTH + Cheers - Peter
--
Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site:
http://www.word.mvps.org