Important alert: (current site time 7/16/2013 2:29:47 AM EDT)
 

article

Complicators and Simplifiers (New Paragraph about the API)

Email
Submitted on: 3/9/2007 12:39:50 PM
By: ULLI 
Level: Intermediate
User Rating: By 20 Users
Compatibility: VB 5.0, VB 6.0
Views: 16732
author picture
(About the author)
 
     This is a project I want to start: Collect hints and examples which might help Newbies (and maybe some of the Oldbies) to improve their coding style and efficiency. Comments therefore are welcome as are suggestions, additions or alterations.

 
winzip iconDownload article

Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. Afterdownloading it, you will need a program like Winzip to decompress it.Virus note:All files are scanned once-a-day by Planet Source Code for viruses, but new viruses come out every day, so no prevention program can catch 100% of them. For your own safety, please:
  1. Re-scan downloaded files using your personal virus checker before using it.
  2. NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.
  3. Scan the source code with Minnow's Project Scanner

If you don't have a virus scanner, you can get one at many places on the net including:McAfee.com

 
Terms of Agreement:   
By using this article, you agree to the following terms...   
  1. You may use this article in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.
  2. You MAY NOT redistribute this article (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
  3. You may link to this article from another website, but ONLY if it is not wrapped in a frame. 
  4. You will abide by any additional copyright restrictions which the author may have placed in the article or article's description.


Other 111 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this article (in the Intermediate category)?
(The article with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments
2/7/2007 11:32:56 AMSteppenwolfe

Good primer..
Not sure I agree with all of the error handling ideas. In general, I would say all errors should be managed without need for a handler, but in open source code, I often use handlers as an exit stratagy if the programmer has not implemented the code properly. Say, setting a run-time property before a control has inited. Sometimes you have to plan for programmer error, and it is simpler to just jump out of the routine with a debug message, then test all possible conditions.
As for module size, I am working on a project that is 20k+ lines, but, it is divided between 12 classes, well commented and organized. I think the size of a function is more important as far as readability goes. A function in excess of 200 lines is generally too proceedural, and a bad idea, (I am reminded of sGrid II, and the 900 line pDraw routine ;o)
(If this comment was disrespectful, please report it.)

 
2/7/2007 4:39:47 PM ULLI

@Steppenwolfe

Is it not so that if you do not react to an error it is simply propagated up to the caller? So, why not leave it at that - the error will eventually pop up at the caller's code anyway and will thus indicate where h i s fault is.
(If this comment was disrespectful, please report it.)

 
2/8/2007 9:26:32 AMRobert Rayment

If you haven't seen it worth looking at: www.aivosto.com/vb.html and click Resources.

(If this comment was disrespectful, please report it.)

 
2/8/2007 9:44:37 AMSteppenwolfe

Ulli,
Not all errors are show-stoppers.. Some will just cause it to jump out of the routine, or worse, from a parent routine, making errors difficult to track.
Ideally, in class coding, there should be no need to test inbound variables for validity, this is the callers responsibility, to test conditions before an object is called. So, creating a large series of nested If statements, solely to catch improper usage, can be arduous, and bloat code unreasonably. This is one argument for a handler, but I think that GoTo has its place also. Good code is a balance of clarity, efficiency, and flexibility. The only argument I have ever heard against GoTo is that it makes code difficult to read, but this is not the case if used in a structured and linear way. It can make code more efficient by bypassing unnecessary statements, and make code easier to read by keeping it within margins. Anyways, enjoyed the read, and would like to see this article evolve.
(If this comment was disrespectful, please report it.)

 
2/9/2007 4:17:10 AM ULLI

Steppenwolfe:
I disagree with you regarding improper usage of a class (and the parameters and variables fed to it). Of course a class (or any other code which is public) must protect itself from poison which could be lethal. That's what Properties are for - check incoming data and reject them if improper by raising an Error; all standard controls (which in fact are classes more or less) work that way and why should we depart from that? Error checking for the code will be consistent, it is optimally designed by the author of the code (who knows his baby best) and the user of the code will be happy that he does not have to do it.

One sentence on GoTo: I am addessing newbies mostly and for them it is "strictly forbidden" because otherwise we'd get the same old spaghetti code which we all despise.
(If this comment was disrespectful, please report it.)

 
2/10/2007 9:14:40 AMEric O''Sullivan

Good idea Ulli but it needs work to be read by a newbe. I'll be sending you a proof read.
(If this comment was disrespectful, please report it.)

 
2/10/2007 9:19:32 AMHeriberto Mantilla Santamaria

Great tutorial Ulli 5*.
(If this comment was disrespectful, please report it.)

 
2/14/2007 5:48:43 PMRobert Onutor

Excellent job

Thanks
(If this comment was disrespectful, please report it.)

 
2/15/2007 9:52:50 AMdafhi

I prefer

If whatever then Exit Sub

because
A. usually 2 fewer lines per sub that uses (otherwise vbCrLf + End If)\
B. One less indent level
(If this comment was disrespectful, please report it.)

 
2/15/2007 10:47:36 AMLight Templer

I agree to ULLI: Encapsulation of classes means error checking for incoming parameters of functions and properties WITHIN the class. Else you can work with public vars and put all code within one big modul. That mostly means writing boring code which blows up the class, but I don't see a better solution. To simplify things an alltime used error handling class and a standard event system to raise errors to the caller is a fine thing to have. (.net is calling here ;-) )
Regards - LiTe
(If this comment was disrespectful, please report it.)

 
2/15/2007 12:26:59 PMULLI

@dafhi

But the code after your first line is If-dependent code - so why not make it obvious by If... and subsequent indenting?

Exit Sub is an implicit GoTo. But we all have our preferences...

---------------------------------

@LiTe

Error checking within the class has to be written only once; if you write it outside the class you may have to repeat it over and over again on every access to a property of the class or worse - to a public variable.

(If this comment was disrespectful, please report it.)

 
2/15/2007 5:11:46 PMdafhi

well, the If-encapsulation is kind of the method everybody knows about. i try to be different if the result is to my liking :B
(If this comment was disrespectful, please report it.)

 
2/15/2007 7:55:32 PMKorejwa2

"Do not use While..Wend anymore." Why not? While/Wend loops can be very useful. For example, if you have a While/Wend loop nested inside a Do/While loop, you can use "Exit Do" from within the inner loop to exit the outer loop. When properly used, While/Wend loops can make code easier to read and more efficient, so there is no reason not to use them. Prove me wrong!
(If this comment was disrespectful, please report it.)

 
2/15/2007 8:41:32 PMSteppenwolfe

Ulli,
There are cases where testing variables caller side is definitely preferable. Say, a class with a large initialization procedure, (like crypto). Why would you want to initialize that object when caller is not ready to access it? (then after all those tests have been performed, why test them again within the class?)It is all about context and discretion. If I am writing classes for my own use, I tend to forego handlers and variable testing, preferring to keep the class fast and light. Projects written for others, a more cautious approach is warranted.
As to the number of times checks might be required (as per the mention above), the converse can also be true. So, as I say, it depends on the context, and is within the coder’s discretion.
(If this comment was disrespectful, please report it.)

 
2/16/2007 4:34:12 AMLight Templer

Ulli, the idea was to put general helpers like a funcs IsPositive() or IsInRange() into the class, a resolver for API error codes to text is fine to have and all details to an error as properties. Rasing such an error object to the caller makes a centralized error handling on caller level easier. LiTe
(If this comment was disrespectful, please report it.)

 
2/16/2007 12:18:01 PMEric O''Sullivan

@Korejwa2
The While..Wend structure is about twice as slow as a Do..While - although as you pointed out, it has an advantage if you're using Exit Do. I might question why you'd need to though. Breaking down the code into two separate procedures would also solve the problem and potentially make the code more readable.
(If this comment was disrespectful, please report it.)

 
2/17/2007 12:43:03 PMRobert Rayment

I think one has to be very specfic when talking about the speed of code. Is it in the IDE, the EXE or the EXE with all Advanced Options checked. Finally there will only be a speed difference if it compiles to slower or faster machine code.
(If this comment was disrespectful, please report it.)

 
2/18/2007 2:49:27 AMAkiti Yadav

A GOOD article. But I do disagree to some points: Error checking inside a class has its ups and downs. I prefer a reasonable balance. In come cases, checking errors outside the class gives better control. At other times error checking inside the class is much preferable. Either way, it is better to error check first and then call the class procedure. Not doing so is waste of CPU time, not to mention undesirable results.

@ using GoTo: I agree with Steppenwolfe. GoTo has its place,but should be used properly. Forget text book examples which illustrate spaghetti code with GoTo. Use it if it gives better & efficient code. But note the following :
-Goto jumps should not criss-cross each other. Dont 'jump into' something. 'Jump out'. Don't use GoTo if you plan to migrate to Dot Net. And...Don’t use lots of Goto's.

@ Option Explicit: I did read somewhere that Microsoft recommends you to remove Option Explicit before compiling. However I am not able to confirm this.

Regards.

(If this comment was disrespectful, please report it.)

 
2/18/2007 8:37:06 AMSteppenwolfe

A good addition to this article would be an explaination of compiler options and there meanings and implications. Like the difference between compiling p-code and native.
(If this comment was disrespectful, please report it.)

 
2/18/2007 11:17:16 AMRobert Rayment

Ulli: If I can be provocative, what do you say about the Def method for variable definitions and GoSub :) ?
(If this comment was disrespectful, please report it.)

 
2/28/2007 4:54:52 PMAvi

Pure gold. Thanks once more...
(If this comment was disrespectful, please report it.)

 
3/3/2007 7:04:49 PMMagic

Sorry for the late vote. 5* project.
(If this comment was disrespectful, please report it.)

 
3/6/2007 7:51:01 AMEric O''Sullivan

what about an explanation of ByVal and ByRef? You mention it several times in relation to the api but you don't seem to say anywhere that vb passes parameters ByRef by default and explain the consequences of this for the unwary programmer.
(If this comment was disrespectful, please report it.)

 
3/6/2007 8:31:00 AMULLI

That'll be explained in the upcoming paragraph about the API, Eric
(If this comment was disrespectful, please report it.)

 
3/8/2007 6:38:32 PMKris Bennett (i00 Productions)

There is nothing wrong with goto if used correctly; it is, as with most things, the (often) poor use of it that tarnishes its name.

What better way to exit multiple nested for loops for example?
(If this comment was disrespectful, please report it.)

 
3/9/2007 7:31:06 AMULLI

@Kris:
In the outer loop check that the inner loop has terminated prematurly.

For i = 1 To m
For j = 1 To n
loop body
If SomeCondition Then
Exit For
End If
Next j
If j <= n Then
Exit For
End If
Next i

(If this comment was disrespectful, please report it.)

 
3/26/2007 2:38:35 AMHamid

Please very nice code links.
(If this comment was disrespectful, please report it.)

 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular article, please click here instead.)
 

To post feedback, first please login.