How does everybody feel about sticking to good programming techniques...aka how Billsoo,Thinker,and probably everyone on this forum codes(except me :))...is it worth the extra time you have to spend???...or is it just a matter of getting it done when u have a deadline...as long as u deallocate all memory for variables and close all connections...just wondering how you all felt bc in college all my profs obviously stressed this and I was wondering if that is the way it should be...regards
jcd
Laurent 10-02-2001, 07:24 AM i realy think it's worth it, sometime you go quick just to try something and it works but then when you come back because of an error or for modifications then you need to look a lot longer to the code to find what you need. the best thing is to document a lot, do the error trapping, and write the code clearly. sometimes it may be longer but it's worth it
I'll be among the best soon, very soon!!!
I pay a lot of attention to this, partly because I code in a team and we all have to understand each other's code quickly.
Thinks to pay attention to are :
variables
eg text = TxtVariable, number = NumVariable, object = ObjVariable, counter = CntVariable (and not 'i')
procedure headers : explain what they do, who made them, when etc.
indentations : find the 'if' for a given 'end if' a lot quicker than without indentations
variable resets : spare your memory by killing all vars that aren't needed any more
form/module naming : not 'Form1' or 'Main' but 'FrmMain'
scoping : eg. don't make globals when you don't need to
class properties : use get/let/set instead of a single public var
And so on ... if you make a set of ground rules for yourself or your team, you can make up a lot of time by following them.
Just my idea of course ...
<font color=green>Do or do not
There is no try</font color=green>
Laurent 10-02-2001, 07:37 AM these are all great tips for team work! i'm working alone since i started programming, must be cool to be part of a big team
I'll be among the best soon, very soon!!!
Laurent,
I'd use them anyway. You never know - maybe someday you'll meet someone who's interested in working with you, then when you compare code he/she'll be happy you did, and you'd probabely want him/her to use certain rules too !
<font color=green>Do or do not
There is no try</font color=green>
Laurent 10-02-2001, 07:55 AM most of these i already use every day, there's maybe just the counters... small bad habit
I'll be among the best soon, very soon!!!
:)
I had that one too when I came to work here ...
<font color=green>Do or do not
There is no try</font color=green>
I work solo and my job includes writing PERL,ASP, VB, and C scripts and being the DBA(which in reality I am not truly qualified to do any of them ;) )...so throughout the day I am constantly switching and the time I have to do stuff is very "short"...that is why my programming techniques have really gone down hill and i think it may hurt me in the future...thank you for all the suggestions...maybe I will be more careful and take the extra time(if I have it)
regards
jcd
For me personally, I never understood why the not i policy seems to be so standard. If I have two nested loops, then i and j seem just as valid as cntOutsideCounter and cntInsideCounter or whatever.
If I'm looping through Cells in Excel or something that gives the counter an appropriate name, then I can see it, but I don't think anyone would just name any kind of variable that isn't just a counter or a temporary variable i.
Then again, I've always said that all engineers I know are at least a little bit nerdy, and all programmers I know are at least a little bit nerdy and a little bit lazy. So maybe that's why I like to use i and j.
LOL
"The LORD is my strength and my shield" - Psalms 28:7
Actually, good programming techniques can save you time - at least in the long run. If you know that lblDiameter is a label and txtDiameter is a textbox both corresponding to a variable name Diameter (or numDiameter or sngDiameter) then you don't need to wonder (or take time to look up) what the textbox's name is that you need to do something to for that variable. If that made any sense to anyone besides me...
That's just one small example though.
"The LORD is my strength and my shield" - Psalms 28:7
So suppose you have a loop with about 10 counters, will you call them i through s ? That's not very stylish, is it ;)
<font color=green>Do or do not
There is no try</font color=green>
LOL
Touche'!
But after only 3 nested loops, I think I'd have trouble even following the logic of more loops, so it's not something I'd probably need to worry about. LOL
Actually, my problem is that I've always been the only programmer at both companies I've worked for, so my style is a work in progress. I've seen a lot of variations and I like things about most so I tend to take bits and pieces from each. For the most part, they tend to be pretty similar though.
"The LORD is my strength and my shield" - Psalms 28:7
Mill you do have a very good point...I find myself going back through all the code and making it "good" and pretty(take out bits of commented code) and probably spend more time doing that then I would if I simply wrote it that way....I think though each programmer has his own style above beyond indenting,etc... if I want an obscure name for a variable I will use it but pay the price later...I guess I am just stubborn :)
regards
jcd
Mill - you should have a look at my sorter from the first coding contest - I've used very readable var names 8D
<font color=green>Do or do not
There is no try</font color=green>
Timbo 10-02-2001, 07:38 PM well I'm no cosmic hexpert, but 'good programming' has saved me hours/days when a client asks me to enhance an app I wrote for them 6 mths ago!
I've also adopted/evolved what I call 'total integration conventions' which basically means that all code modules/objects are linked by index numbers and/or name.
i.e. An Excel app may have a 'frmReports' which launches code contained in 'bas01Report_Currency' which outputs a report onto a worksheet called 'Report Currency' with a XL codename 'Sheet01' -you get the idea..
This might seem pedantic especially at the start of what is assumed to be a small project - but I think everyone knows how these "samll" projects evolve!
As for 'i' or 'j' ? what happens if you need to use 'i' as an index for a control collection or something? It helps to have an idea what the counter represents as well as what it's value is.
"He who dares my son!.. He who dares.." - Del-Boy Trotter
BillSoo 10-02-2001, 08:16 PM Good programming techniques, like structured programming, object oriented programming, naming standards and code formatting are all designed to
a) allow code reuse
b) ease maintainability
c) increase readability, esp. for others.
For most of my programming career, I've been the sole company programmer so I've developed my own style, but I can appreciate other styles and I can, and have, change to meet the company standard.
I think the main thing is that whatever style you have, it should be consistent. This will get you through most companies just fine.
Lately, I've been thinking about code simplification. I've been working with a small team and the other programmers have made some very complex code that I found hard to follow. Typically I could cut the amount of code in half (or more) and speed it up several times into the bargin. It's not that the other programmers were bad, but they were rushed. In cases like this, it can really pay off to design first, then code. Or even just think about what you are doing before you do it.
Finally, I just want to mention one of my favorite naming standards. One that pretty much nobody uses anymore: Type Suffixes. Normally everybody and his dog use type prefixes (sFilename, dPercent, iCount etc.). I like to use type suffixes like Filename$, Percent#, Count% etc.
I feel that it is just as descriptive as prefixes and has the added bonus of absolute meaning. For instance, one of the trickiest bugs I had to find was caused because someone had defined a variable as:
dim iVariable as Double
The name threw me off for a while because I was assuming that it was an integer when in fact it was a double. Likewise, I've found bugs caused by people defining variables like:
dim sFirstName, sLastName, sMiddleName as string
which under VB6, only declares sMiddlename as a string, the others are variants. Using type suffixes, the statement
dim FirstName$,LastName$,MiddleName$
always works.
However since, as I said, everybody uses type prefixes, I now compromise with:
dim sFirstname$, sLastName$, sMiddleName$
which at least allows me to save some space in my declarations.
"I have a plan so cunning you could put a tail on it and call it a weasel!" - Edmund Blackadder
Thinker 10-02-2001, 09:17 PM If I am working with a program that I will have to work with again in the
future, I have to make it as structured, logical, and readable as I can.
I hate commenting code, so I like code that explains itself. It isn't always
possible because sometimes the program requirements demand a fast,
(or flexible,) obscure algorithm. That is when I try to force myself to
comment. When it comes to loop counters, I long ago gave up on single
character variables. I once had to do a mass search and replace for some
single character variable. From that day on, I decided to use xx, yy, zz,
and on those very rare occasions where I have gone 4 nested loops, ww.
Very easy to spot, short, easily searchable and replaceable, easy to type,
and I can easily tell which loop I am in.
I think therefore I am... sometimes right. images/icons/wink.gif
I absolutely use good naming conventions, indentations, and commenting, as well as OOP 100% of the time. I never use i or n or anything similar. I will use something like intIndex or strText. I comment every ‘chunk’ of code so when I come back to it at a later date I can pick up on the logic at least ten times quicker. Sometimes this slows me down but if you look at a whole years worth of work, you will find that this will save you hundreds of hours on average.
I also go through the code and clean it up as best as I can and try to make it as efficient as possible.
I do have one bad habit that seems to work for me though. I rarely pseudocode or do any preplanning at all. I just dig in and start coding. Once I see some results, it helps me get going and thinking in the right direction. Most professional programmers will plan out and pseudocode most problems before they start but I just can’t break that habit.
JDT
Banjo 10-03-2001, 03:07 AM I tend to use a combination of the above. I write my pseudocode in VB with proper types and Function definitions but just layout the logic using comments and rudimentary conditionals. When I'm happy with the structure I start filling in code. This also has the added benefit of allowing my to test each new piece of code as I go along.
BlueRaja 10-03-2001, 07:11 AM Does anyone use Ctrl&j with long variable names?
I've only recently found out about it, but it really helps me with long variable names like strEmployeeSurname, intAverageCostofTask etc... If you can't remember the name of the variable, or can't be bother to type it you'd just type in "str<ctrl&J>" and you get a drop down list. - plus it encourages me to start all my variables with bln,str,int etc.. as it makes them easy to find.
I hadn't, but I will now
cheers images/icons/wink.gif
anhmytran 10-03-2001, 09:00 AM I always apply common technique in my code, so that my code
can be customized by universal programmers, and by a specific
standard in any organization.
To some small projects for testing, I may not apply this technique.
These projects are to be abandoned.
AnhMy_Tran
KesleyK 10-03-2001, 09:02 AM Well, one I didn't see here (maybe because it's too obvious) but ALWAYS, ALWAYS, ALWAYS use Option Explicit!!! Arrggh!
Anyway, I too am guilty of the x, y and z integer counters (then m, n, o and p if I get beyond 3 nests)...sorry.
__________
HOOOaaaaa! Semper Fi!
Laurent 10-03-2001, 09:08 AM option explicit is a must!!!!!
I'll be amongst the best any minute now !
Banjo 10-03-2001, 09:27 AM I tend to avoid x, y and z because they all too often conflict with position variable especially in Mouse events.
Squirm 10-03-2001, 09:48 AM I us i and j and also p and q and r for loops, and also for storing temp numbers. Basically any variables I dim become multi-purpose, so if I dimension a variable i just for a loop, I might also use it to temporarily store a number somewhere else......
<pre>Dim i As Integer
Dim j As Integer
For i = 1 To 10
Some Code
Next i
i = Instr(1, strText, ":")
If i Then
j = Instr(i, strText, "+")
End If</pre>
Derek Stone 10-03-2001, 01:40 PM In all actuality though it makes no difference how you code, because it all gets turned into ASM.
Variable names go "bye-bye", comments are gone, and that "bad" keyword that we know as GoTo is turned into a JMP command.
Of course this is invalid if you work in an environment were someone else has to read your code.
However, we must all strive to avoid memory leaks, errors, and poor interfaces, all problems that plaguw the majority of software out there.
So just remember:
<pre>Dim s$</pre>
is the same as:
<pre>Dim strMyThirtyMileLongReallyWasteOfTimeInefficentVariableName As String
</pre>
Regards,
-cl
Timbo 10-03-2001, 06:57 PM Nice one BlueRaja, i've acquired 2 new techniques in 24 hours - any more for any more??
as an aside, crazed-lunatic, I think BillSoo's point about strString$ was more about readability and screen space than what Mr. ASM does with our code when we're not looking images/icons/smile.gif - images/icons/wink.gif - images/icons/laugh.gif - [no offence etc] ..
"He who dares my son!.. He who dares.." - Del-Boy Trotter
Robby 10-03-2001, 06:59 PM I never used Ctrl+J, but I've always relied on Ctrl+SpaceBar (same thing). It is a great way to find out that you may have miss-spelled a word( I'm all thumbs).
I try to use generic names within a Function or Sub so that I can reuse them.
I don’t like signed posts, why? We are so used to them that we ignore the last line of the post.
Derek Stone 10-03-2001, 07:12 PM I agree with Bill...
-cl
Merrion 10-04-2001, 03:58 AM Just a thought on using i,j,k in loops.
Usually we are looping through a set of somethings like records or cells or whatever...it is useful to name your loop counter after the something involved.
Thus suppose we have an array of arrays of students gardes we can loop thus:
<pre>
Dim cntStudent As Long
Dim cntTest As Long
For cntStudent = 1 To MAX_STUDENTS
For cntTest = 1 To MAX_TESTS
Debug.Print "Student #" & arStudentNames(cntStudent) & " scored " & arTestScores(cntStudent,cntTest)
Next cntTest
Next cntStudent
</pre>
Now replace that with i and j and it becomes a whole lot easier to give the wrong student the wrong score.
HTH,
Duncan
Another thing I tend to use is MixedCase names.
TxtThisVariable instead of txtthisvariable
This way, if you are typing your code, and you do a line feed, you can see all caps popping up in your variables, except when you've mistyped one, so it makes it a lot easier to find your 'spelling mistakes' ;)
<font color=green>Do or do not
There is no try</font color=green>
Laurent 10-04-2001, 05:23 AM i always use this approach as well, the code is always easyer to read!
I'll be amongst the best any minute now !
PrOpHeT 10-04-2001, 08:35 AM Is there an official reference or is it all a matter of personal prefference?
If one exist, does anyone know where it is?
"No one can know everything, only so much as to drive us to look for the rest." -PrOpHeT-
KesleyK 10-04-2001, 08:40 AM I do a similar approach, except I use txt, cmd, frm, mod, etc. in LC and mix-case the following variable name.
__________
HOOOaaaaa! Semper Fi!
Laurent 10-04-2001, 08:40 AM there's a few conventions but nothing official, most of it is more stylish or preference. BUT if you work for a companie where there's a lot of programmers in a team, it becomes almost a law (more than a simple convention). i realy don,t know any reference for this, sorry!
I'll be amongst the best any minute now !
Merrion 10-04-2001, 09:01 AM The prefix conventions are documented in the VB professional edition documentation, and the book Code complete which is published by MS press has a more complete (natch) list.
Banjo 10-04-2001, 10:14 AM I do as well. However, I always start function name with a capital to differentiate them from variables.
BillSoo 10-04-2001, 11:36 AM The naming conventions ("Hungarian Notation") was invented by C programmers. But since windows API is written by and for C programmers, that's why it has migrated to the VB world.
"I have a plan so cunning you could put a tail on it and call it a weasel!" - Edmund Blackadder
Derek Stone 10-04-2001, 04:59 PM I created my own notation:
<pre>nInteger
nLong
nDouble
nShort
nByte
bBoolean
sString
oObject
vVariant
cCommandButton
tTextBox
pPictureBox
iImageBox
lLabel
fFrame
</pre>
...and so on.
This notation is three times more efficient. I hate inefficient things. :)
Regards,
-cl
ChiefRedBull 10-05-2001, 06:48 AM Just to be awkward I use a mix of all the notations mentioned! images/icons/wink.gif
iInteger
lLong
cmdButton
picTureBox
sString$ etc etc... heh heh
Chief
"How are we to learn, if those that know will not teach... ?" - Me.
Mystic 10-06-2001, 11:26 PM well i would like to say that this really helped me but after reading this from begining to end i an more confused then i first was. i think i will stick to what i know
Mystic
Banjo 10-07-2001, 02:46 AM Well, there are so many different/conflicting views on what style is best that it is often hard to figure it out.
I think you've got the right idea though, develop your own style. Just make it consistant and clear.
Hajü Schwippert 10-10-2001, 08:08 AM When I started some bigger projects (for me that is images/icons/smile.gif) I had this question to and did a search and found the Reddick VBA Coding & Naming Conventions which I found really helpful for learning about these principles.
With the time, in some points I created my own conventions (who doesn't?)
Laurent 10-10-2001, 08:12 AM did you find this on the net or in a book somewhere?
Hajü Schwippert 10-17-2001, 08:12 AM I found these on the net as *.pdf documents. Do you want me to mail them to you?
Laurent 10-17-2001, 08:14 AM sure! my email is
laurent_programmeur@hotmail.com
it's always good to see other techniques!
CornMaster 10-18-2001, 01:01 PM I problem I run into a lot is naming my variables and text boxes that use them alike. :-(
I also do stuff like this: I have a form called password and a text box for the password called password, and an invisible box that loads the password from a file. All these password wanabes gives me a headache.... :-)
Banjo 10-19-2001, 04:28 AM The simply way around that is to prefix it with the object class. E.g:
frmPassword
txtPassword
lblPassword
|