|
-
Jan 11th, 2010, 06:58 PM
#1
Thread Starter
PowerPoster
Keeping code "clean"
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 
Indenting your code is one way:
Code:
Private Sub Form_Load()
For b = 1 To 1000
For c = 1 To 1000
DoEvents
Next c
Next b
End Sub
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 Function init()
For b = 1 To 1000
For c = 1 To 1000
DoEvents
Next c
Next b
End Function
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.
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
Last edited by smUX; Jan 11th, 2010 at 07:49 PM.
Reason: indent changing
Well, everyone else has been doing it :-)
Loading a file into memory QUICKLY - Using SendKeys - HyperLabel - A highly customisable label replacement - Using resource files/DLLs with VB - Adding GZip to your projects
Expect more to come in future
If I have helped you, RATE ME! :-)
I love helping noobs with their VB problems (probably because, as an amateur programmer, I am only slightly better at VB than them :-)) but if you SERIOUSLY want to get help for free from a community such as VBForums, you have to first have a grounding (basic knowledge) in VB6, otherwise you're way too much work to help...You've got to give a little if you want to get help from us, in other words!
And we DON'T do your homework. If your tutor doesn't teach you enough to help you make the project without his or her help, FIND A BETTER TUTOR or try reading books on programming! We are happy to help with minor things regarding the project, but you have to understand the rest of it if you want our help to be useful.
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|