Tips & Tricks
Working with someone else’s code
The scenario (all too familiar):
You have a new job, and your first programming assignment is to modify a Visual Basic program that was written by another programmer who has left the company. The program is hard to follow. It has no comments, all of the variables are haphazardly named, and all the forms and controls were left with their default names, such as Form1, Form2, Command1, Option1, etc. You’re overwhelmed.
This is not an unusual feeling. In fact, you can feel the same way about your own code a year or so after writing it. So I emphasize these precautionary measures to ensure easy comprehension by others:
1. Option Explicit is coded in all of your modules (this ensures that Visual Basic enforces that all variables are declared before using them)
2. All variables and controls are named using a formal naming convention (i.e. Hungarian Notation)---this makes modifying the program somewhere down the road much easier
3. Code that is repetitive in nature is consolidated into procedures and called from within the program
4. Every module has comments describing its major functions and features
5. Code that is anything out of the ordinary is also commented
Sounds great, but how do these ‘rules’ help you modify someone else’s code? Unfortunately, I’m suggesting you correct the mistakes of the past.
Of course, before you do this, you need to get your supervisor or project leader to ‘buy into’ that plan. Obviously, if he or she is expecting the program modification to be made in about an hour, so taking a week to clean the code and make the modification won’t make you a hit in the Employee cafeteria.
However, I can almost assure you this won’t be the last time this program is modified. Biting the bullet now and cleaning it up will make things much easier on the next junior programmer who finds himself in the uncomfortable position of modifying this code.
Once you have the go-ahead to proceed, the first thing to do is to ensure that Option Explicit is coded in the General Declarations Section of every module in the program. -- if it’s not, add it manually -- and then run the program. Guess what? The program no longer runs. Why?
Coding Option Explicit will cause VB to mark, as a syntax error, every instance of a variable that hasn’t been declared---potentially lethal errors in a program. At this point, you need to identify the variables that haven’t been declared, and explicitly declare them, with a defined type (i.e. Integer, String, Date). While you’re at it, this wouldn’t be a bad time to rename variables using either Hungarian Naming Convention, or a naming convention in use at your company. Using Visual Basic’s Search and Replace features can make this task a lot easier than it sounds.
With all of the variables properly declared and named properly, now is a good time to name those forms and controls in a meaningful way. Keep in mind that when you change the name of a control, the code associated with it can appear to be ‘lost,’ finding its way into the General Declarations Section. To avoid this, first rename any event procedures associated with the control manually with the new name of the control—then bring up the Properties Window for the control and change its Name property there.
At this point, make sure the program runs okay. You’ll find you now have an understanding of how the program works -- so much so, that you should be able to identify code that can be centralized in a subprocedure or function. Along the way, insert comments in your code explaining what’s going on. Write those custom procedures and place them in either a Form Module or a Standard Module (every Visual Basic program I write has one).
Now, with the Visual Basic program nicely sanitized, it’s time to code that simple modification you’ve been asked to make. Hopefully it will be a piece of cake.
Friday, April 28, 2000
Saturday, March 4, 2000
Fan Mail---March 4, 2000
Just received a very nice piece of fan mail about my books in which the author describes the 'novel' approach of my book
I would just like to say that I enjoy your books on Visual Basic. I have two and am going to buy another soon.
These are the first programming books that I have ever read that aren't extremely boring.
The book is more like an interesting novel because of the way you do the dialogue instead of just spitting out pages and pages of boring, hard-to-follow facts.
I bought the first book in the series about beginning Visual Basic so that I could learn Visual Basic and get a summer job doing programming. I am not even done with the first book, but I still bought the book on objects in Visual Basic.
I used to program in Q-Basic back in the ancient days of un-user-friendly DOS and a year ago, I made an attempt at learning Visual C++, but I now know more in Visual Basic than in both Q-Basic andVisual C++.
Thanks!
Danny
I would just like to say that I enjoy your books on Visual Basic. I have two and am going to buy another soon.
These are the first programming books that I have ever read that aren't extremely boring.
The book is more like an interesting novel because of the way you do the dialogue instead of just spitting out pages and pages of boring, hard-to-follow facts.
I bought the first book in the series about beginning Visual Basic so that I could learn Visual Basic and get a summer job doing programming. I am not even done with the first book, but I still bought the book on objects in Visual Basic.
I used to program in Q-Basic back in the ancient days of un-user-friendly DOS and a year ago, I made an attempt at learning Visual C++, but I now know more in Visual Basic than in both Q-Basic andVisual C++.
Thanks!
Danny
Monday, May 3, 1999
5 Great things today---May 3, 1999
People who say thank you
People who believe in something greater than themselves
People who hope
Meatloaf
The Please Touch Museum. My wife went to the Museum, and the baby's stroller was stolen. The museum offered to pay for a replacement.
People who believe in something greater than themselves
People who hope
Meatloaf
The Please Touch Museum. My wife went to the Museum, and the baby's stroller was stolen. The museum offered to pay for a replacement.
Thursday, April 1, 1999
Guy Weaver---Hypocrite
I formerly loved watching professional baseball, especially the Philadelphia Phillies.
However, in 1996, I became so fed up with professional athletes, I decided to give up my full season ticket plan for the Phillies.
The Phils worked with me for two years so that I could hold onto my season tickets, but unfortunately, in 1998 we parted, as a partner of mine, Guy Weaver, owner of JetPlate Inc, took over the seats. (Actually, he called the Phillies Season Ticket office and bypassed thousands of people on Full Season Wait List.)
By the way, Guy, you promised to call me every year and sell me some of my home plate tickets.
Haven't heard from you since.
I think you always complained about someone you were involved with doing that to you with Basketball or Hockey tickets.
Hypocrite.
However, in 1996, I became so fed up with professional athletes, I decided to give up my full season ticket plan for the Phillies.
The Phils worked with me for two years so that I could hold onto my season tickets, but unfortunately, in 1998 we parted, as a partner of mine, Guy Weaver, owner of JetPlate Inc, took over the seats. (Actually, he called the Phillies Season Ticket office and bypassed thousands of people on Full Season Wait List.)
By the way, Guy, you promised to call me every year and sell me some of my home plate tickets.
Haven't heard from you since.
I think you always complained about someone you were involved with doing that to you with Basketball or Hockey tickets.
Hypocrite.
Tuesday, December 1, 1998
My book is outselling Harry Potter
For one day anyway, my book
Learn to Program with Visual Basic 6
is the number one selling book on Amazon--and Harry Potter is second!
Learn to Program with Visual Basic 6
is the number one selling book on Amazon--and Harry Potter is second!
Monday, November 30, 1998
Just published my first book!
I'm happy to announce that I've just published my first book,
Learn to Program With Visual Basic 6
Wrox Publishing
ISBN 1902-745-000
It will be published under Wrox's ActivePath imprint.
You can order copies online at Amazon using this link...
http://www.amazon.com/exec/obidos/ASIN/1590591518/ref=nosim/professorsmileys
Learn to Program With Visual Basic 6
Wrox Publishing
ISBN 1902-745-000
It will be published under Wrox's ActivePath imprint.
You can order copies online at Amazon using this link...
http://www.amazon.com/exec/obidos/ASIN/1590591518/ref=nosim/professorsmileys
Thursday, November 19, 1998
Volunteer Group--what happened to it
Someone just asked what happened to my Volunteer Group?
Question: What happened to your volunteer group---I thought it was a wonderful idea.
Anser. My volunteer group was a great idea
http://www.johnsmiley.com/volunteers.htm
Unfortunately, nothing seemed to be able to solve the distance problem---people working on a single project that are geographically dispersed have some big hurdles to surmount.
I've read recently about a similar program, however, and I think they're doing a better job of administering the program than I had the time to spend. Check them out at:
http://www.vbdesignteam.com
Question: What happened to your volunteer group---I thought it was a wonderful idea.
Anser. My volunteer group was a great idea
http://www.johnsmiley.com/volunteers.htm
Unfortunately, nothing seemed to be able to solve the distance problem---people working on a single project that are geographically dispersed have some big hurdles to surmount.
I've read recently about a similar program, however, and I think they're doing a better job of administering the program than I had the time to spend. Check them out at:
http://www.vbdesignteam.com
Subscribe to:
Posts (Atom)