Okay, this is not so much a VB6-specific post (pretty much all languages would benefit) nor is it a question, but I felt it needed posting and as I am a VB6 coder I should post it here rather than another section (mods, feel free to move it if it should be elsewhere).
I don't consider myself to be a great coder, I'm only amateur, but I know a fair amount of tricks that help me improve my own code and I thought I'd explain for a number of other people who post here.
A lot of people post their code here and I notice each time that their code is hard to read, and sometimes this is part of the problem...if you can't read your code without carefully looking at each line, you're sometimes going to miss a problem that might normally jump out at you.
Note that the code I am using as an example is JUST an example...it wouldn't really do anything, and it could be done better anyway...and I know it could :lol:
Indenting your code is one way:
If this was indented, it'd look like:Code:Private Sub Form_Load()
For b = 1 To 1000
For c = 1 To 1000
DoEvents
Next c
Next b
End Sub
Straight away, you can see the flow of the code, you can see that there's a for/next and inside it is another for/next and inside that is some code that does something. However, there's more you can do to help you with readability. In this case we're assuming that this small section of code is initialising a global variable, so why not put the code into a function and store it in a module?Code:Private Sub Form_Load()
For b = 1 To 1000
For c = 1 To 1000
DoEvents
Next c
Next b
End Sub
Now, in your Form_Load you just put "init" and it'll run the code in the function...you've turned the Form_Load from 5 lines of code into one line and the name of the function tells you what it's doing. If you need to add to the init section of your code, you can go to the module and do the work.Code:Private Function init()
For b = 1 To 1000
For c = 1 To 1000
DoEvents
Next c
Next b
End Function
Many people use tricks like this to simplify their code, and there's a good reason...it makes your life a hell of a lot easier. Also, if you have a lot of form initialisation (label captions, locations, etc) you can do the same thing from a function in a module. If you had (for instance) a label called label1 on form1 you would change the caption from Form_Load with label1.caption = "Hello" but in a module you'd put form1.label1.caption = "Hello". It's this one minor difference that you need to remember when using module functions to affect your form elements.
This suggestion doesn't sound like much, and it may even sound like more work and perhaps a larger resulting code/exe, but when it comes to reading through your code when you have a bug (or even when you want to add something to a certain section of your code) you'll find it saves tons of time...functions aren't JUST for bits of code you might want to reuse elsewhere! If you have lots of small clips of code that you use in a sub that can be split up into multiple functions, storing them in a function would improve readability by a ton...you can imagine a 100+ Form_Load sub which is reduced down to 5 lines of code which describe exactly what that sub is doing (and no, commenting the code doesn't help as much as you might think, although you should still comment your functions for readibility as well) and if you need to find a specific section of the Form_Load you should know from memory what you called it and can go look at the function you want...you can even split modules up by section or by whatever sorting method you want to use.
Hope this helps the way it helps me when I'm coding :afrog:

