Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Coding Style

Status
Not open for further replies.

pullingteeth

Programmer
Sep 26, 2003
128
US
Hello, I'm working with a self-taught programmer on a project, and her coding style is driving me nuts. I attempted to suggest that we adopt a standard, but she vehemently defended her style, saying it was "personal choice" and "made most sense to her". Here's some of her code snippets; what arguments would you make to her to convince her of the error of her ways? (Or should I just chill out about it?)

Code:
If Location = "1" then
    Event = 1
        Elseif Location = "2" then
        Event = 2
            Elseif Location = "3" then
            Event = 3
                Else
                Event = 4
                    end if

Code:
If Status = "A10" and Location = "S" then 'Receiving Stop File
    a = 0
    
    SearchVal1 = myscreen.getstring (14,9,1)
        If SearchVal1 = "S" then
        Reason1 = Trim(myscreen.getstring(14,14,50))
        a = a + 1
        End if

        B = val(6 - a)
        End if
 
Very difficult. I can't imagine there's much anyone can say to help that you haven't already thought of.

1. Don't let it become a personal battle between her style and your style. It has to be to do with general programming conventions/ long term maintenance etc

2. Show her some examples of code in Access books which you consider to be good style (without necessarily saying that). It shouldn't be hard to find examples of 'properly' indented code - even MSKB articles or online Help will do as examples of the 'universal' style.

Practically any programming book or manual will suggest the 'accepted' indentation structure in the interests of readability.

 
Perhaps you can discuss with her the concepts of blocks. Although VBA is not truly a block-structured language, the concepts of block structuring can be used to your advantage with respect to style, with one simple basic rule - All lines of code which lie in a specific 'block' begin in the same column.

Let's assume that the first line of the snippet is in Block 1. An If statement starts a new block, but the if statement itself is not inside the block it creates. The new block begins with the first statement following the If statement. Similarly, and Else (or that dreaded ElseIf) closes the previous block, and then starts a new block. But just as the If is not in the block it creates, the Else (ElseIf) is not in the previous block that it is closing, nor is it in the new block that it creates.
[tt]
Block 1: If Location = "1" then
' Start Block
Block 2: Event = 1
' End Block
Block 1: Elseif Location = "2" then
' Start Block
Block 2: Event = 2
' End Block
Block 1: Elseif Location = "3" then
' Start Block
Block 2: Event = 3
' End Block
Block 1: Else
' Start Block
Block 2: Event = 4
' End Block
Block 1: End If
[/tt]
At the end of the snippet, we're back in the same block that we started. Using Else instead of ElseIf may be even easier to See
[tt]
B1: If Location = "1" then
B2: Event = 1
B1: Else
B2: If Location = "2" then
B3: Event = 2
B2: Else
B3: If Location = "3" then
B4: Event = 3
B3: Else
B4: Event = 4
B3: End If
B2: End If
B1: End If
[/tt]
And the second snippet would be as follows:
[tt]
B1: If Status = "A10" and Location = "S" then
B2: a = 0
B2: SearchVal1 = myscreen.getstring (14,9,1)
B2: If SearchVal1 = "S" then
B3: Reason1 = Trim(myscreen.getstring(14,14,50))
B3: a = a + 1
B2: End if
B2: B = val(6 - a)
B1: End if
[/tt]
Similarly, new block would begin immediately after a Do or Do While and end immediately before the Loop statement. Same for a For/Next loop. Everything between, but not including the For and Next statements are in a new block
A Select Case opens a new block, and each individual Case entry closes the previous block, and open a new block. The End Select statement is unique in that it closes two blocks. The last individual case block, and the overall Select Case block
[tt]
B1: Select Case Location
B2: Case "1"
B3: Event = 1
B2: Case "2"
B3: Event = 2
B2: Case "3"
B3: Event = 3
B2: Case Else
B3: Event = 4
B1: End Select
[/tt]


Good Luck
--------------
To get the most from your Tek-Tips experience, please read FAQ181-2886
As a circle of light increases so does the circumference of darkness around it. - Albert Einstein
 
You could point out that even if there isn't an industry standard for VBA per se, there are certain conventions that a majority of people consider to simply be good practice. Accepting this it would be really quite rude to whoever would need to maintain her code to force them to adopt her non-standard practices. Tell her that if she writes a beautifier that will convert her code into a more standard format then she can code however she wants to and the rest of the world be damned.

[red]"... isn't sanity really just a one trick pony anyway?! I mean, all you get is one trick, rational thinking, but when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick[/red]
 
Somewhat 'more forcefully'? ...

Get / use a 'Pretty print' procedure ane run it on some of their proceudures / modules. Show the original and the pretty print samples to the wayward employee and camly announct that this is the standard and they will adhere to it or the issue goes up th chain - with the example(s).

Aparently you are in a small org, otherwise you wouldn't be in such a situation, or at least you would have others to help with the peer pressure, so I see it as a basic necessity for you to have, use and enforce a common sytle, including the block indent(s), naming conmventions for variables, procedures and modules. Without these, future maintence and development will strangulate in the spaghetti.

Obviously, my suggestion is more direct (harsh) than some others, but I do not believe you do yourself, yhour co-worker(s) or your employer justice unless you broach the issue forcefully.





MichaelRed
mlred@verizon.net

 
...And least you dont have to GOTO's and any obvious unexpected EXIT's.

Who will be supporting the code
- just her - Let her swim in her own design. (And hopefully your finger nails will remain intact ;-) )
- others - Now you have a problem.

Here is an idea.
Get some of her old code. See if can see some task you can pretend to need to extract from it. (You might even do the coding your way in advance.) Get her to help you figure it out. Chances are she is pretty smart and has a good memory, hence her assertive style. But it is unlikely, she will remember convoluted code from way back.

Now bring out the big guns...
- Explain to her, politely, that code not only has to work, but it has to be supported. Many software houses spend millions support code. Some code is easier to support than others.
- Show her a couple of coding books and how standards is not a novel idea -- it is an accepted practice.
- Show her your code example, and because it follows standards, includes documentation, is much easier to tweak.

I remember with my wife, I had to be very careful on this type of issue. She spent many years establishing herself in the IT sector - fighting tooth and nail, above and beyond the guys to establish credibility. She learned to be "assertive".

Coding can be an ego thing - my code is smaller, niftier and better than yours, vs. constructive criticism.

Spegetti Western anyone?

Good luck
Richard

 
Going back to the original code why didnt she do:

Event = Location

We all have coding and development styles that are unique - conventions can be followed to a point. My weakness is in interation where I cant help myself and use: i, j, k, n, m, x, y, z without thinking!!!!!

Commenting code is also a must - ie I must do it at some point - probably - if I get time - oh look the bar is open!!!

If at first you don't succeed, try for the answer.
 
GummowN - To answer your question, probably because she didn't want Event = 5 when Location = 5.

conventions can be followed to a point.
At what point is it okay to no longer follow the agreed upon conventions?

Good Luck
--------------
To get the most from your Tek-Tips experience, please read FAQ181-2886
As a circle of light increases so does the circumference of darkness around it. - Albert Einstein
 
Cajun - she probably did but couldnt come up with any more convoluted code.

As I mentioned above. When I iterate eg

For i = 1 to 1000
Do something
Next i

It comes from writing code on paper before I write it on the screen, dbs, rst, tdf, qdf get written, but integers just slip into i, j, etc

It is also import to know when convention adds more to the database/code then it add valuse. Do you always you with statements?

We all have naming conventions for queries. We could all understand them, but wouldnt choose another persons.

Perhaps I am playing devils advocate, but just because it is a convention it doesnt make it the quickest/best/most efficent solution.

Unconventional doesnt mean bad.

If at first you don't succeed, try for the answer.
 
hmmmmmmmmmmmmmmmmmmmm ...
GummowN said:
but just because it is a convention it doesnt make it the quickest/best/most efficent solution.
... on the other hand, the generic programming community has long ago eschewed the 'quickest/best/,ost efficient' in favor of the agreed conventions, starting with the use of higher level languages [e.g. VB(A)] and coding styles.




MichaelRed
mlred@verizon.net

 
Sometimes you have to be unconventional eg when delivering a tactical rather then stratgeic solution.

If at first you don't succeed, try for the answer.
 
I still think the issue of code maintenance outweighs all others in coding convention discussions. It may be because I've had to maintain such crappy code before. If she is the only one that will ever touch the code, then she should be able to code however she wants. If not then she should at least pay lip service to what are considered to be industry good practices in code style. Of course if she says she will be the only person to ever work on the code, have her put it in writing so she can never move to another job or project. :)

[red]"... isn't sanity really just a one trick pony anyway?! I mean, all you get is one trick, rational thinking, but when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick[/red]
 
GummowN said:
It is also import to know when convention adds more to the database/code then it add valuse. Do you always you with statements?
What exactly are you asking here?

Adding on to MichaelRed's comments. The agreed upon conventions take into account long-term software maintenance costs, code consistency, and the ease at which another programmer can step in. Overall efficiency includes many other aspects of software engineer than just banging out some code.

Unconventional can sometimes be a benefit, but it can also be quite detrimental, especially in the long run.

Good Luck
--------------
To get the most from your Tek-Tips experience, please read FAQ181-2886
As a circle of light increases so does the circumference of darkness around it. - Albert Einstein
 
I am not defending what the way she has coded, just that we are all quilty of not following all conventions - especially when delivering a tactical solution.

If you build a database and support it until its death then code anyway you want - but who has ever done that?

If at first you don't succeed, try for the answer.
 
I meant use "With" statements?

What I am trying to highlight is coding practice can become poor when a tactical solution is required for a tight deadline as opposed to a long term strategic solution.

If at first you don't succeed, try for the answer.
 
Yes, I do use With statements whenever possible. I've also learned over the years that poor coding practices for what you term tactical solutions end up requiring considerably more maintenance costs and in some cases, rewrites to proper coding standards and conventions down the road. More often than not, these tactical solutions turn into, or become integral parts of, long term strategic processes. I personally don't see, nor do I accept, that as a valid reason to become undisciplined in the practice of your profession.


Good Luck
--------------
To get the most from your Tek-Tips experience, please read FAQ181-2886
As a circle of light increases so does the circumference of darkness around it. - Albert Einstein
 
I am not saying abandon all conventions. My coding is a lot more conventional then some of my colleagues, and a lot better then some of the code I have inherited - call Functions for example.

However, that doesnt mean I always follow convention. I dont always name things exactly like convention says. I use i, j, k for integers too often.

However my code is very readable, quick to bug fix, and easy to pick up.

Those who create strategic solutions from tactical solutions are only thinking tactically.

If at first you don't succeed, try for the answer.
 
While these 'discussions' will end with no one being convinced of any new point of view, I will still cast my vote and opinion to the 'conventions' side. Long ago, individuals like Yourdon espoused structured programming, primarily as the simplification of maintenance. Somewhere from the depths of reading these various books on the numerous variations on the theme, I ran across the famous graph of cost to repair vs defect found in software. It was (IS) a startling (near exponential) curve, with (obviously?) a much lower cost to repair earlier in the life cycle of a bit of code / design. In that light (amongst MANY), there can be virtually NO excuse for violation of 'standards. If a soloution method is overwhelmingly superior -but requires violation of the (agreed to) standards of the organization- it may be grounds for review/amendment of the standard, however until such review/amendment is acutally implemented the soloution should still remain verbooten. Any real 'organization' includes a QA group which is empowered to make such decisions and can (easily?) decide wheather tyhe viarition is appropiate. Small companies . organizations may not have a formal (QA) organization, but no one individual should have the power / influence to disrupt the company / organization to the extent of instantiating practivces and procedures whic incur future cost well in excess of their current (immedidate benefit).

It is also well worth noting that the discussion is the result of a request for assistance in confronting a MAJOR variation of commonly accepted standards and practices, not the sopmewhat lesser violation of naming conventions for loop control indicies.

While I respect GummowN's right to express opinions, I also believe his 'example' is sufficiently removed from the original issue to be irrlevant to the overall discussion.



MichaelRed
mlred@verizon.net

 
As with everything there can be a huge difference between interpretive and literal understandings. I am not advocating an abandoning of conventions, or that each developer should choose a style of their own and rigidly stick to it.

I could analogy code writing to driving a car. We all know the speed limits, and we try to stick to them, but we dont always. Even the best of us will creep over a little bit once in a while.

The same goes for coding. Convention is the speed limit.

We all know there is a big difference between:
completely disregarding convention
occasional lapses through laziness or necessasity
lack of understanding or knowledge

When you first started using Access and VBA did you know that Puclic Variables are best stored in a module called "Globals"?

If at first you don't succeed, try for the answer.
 
I guess it would be appropriate to ask pullingteeth if anything said herein will be useful in dealing with the current situation.

Good Luck
--------------
To get the most from your Tek-Tips experience, please read FAQ181-2886
As a circle of light increases so does the circumference of darkness around it. - Albert Einstein
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top