Which Is Better Coding?

I'd apply the 6 month rule. If it's not clear immediately what you did when you come back to it, it should have been commented.

Sounds fair to me. Of course you only discover what this really means after coming back to code a few times after six months to discover you have no idea anymore ... while the fact that I *have* done just that is evidence that I don't always follow the so-called inviolable commandments... ;)

I'll take good code any day no matter what the style. I've seen enough bad code with nicely declared variables to convince me that what I like to see in a program is a good programmer behind it. If you have code that you expect to use and maintain for many years, then its well worth time documenting it. As far as your original example goes, I wouldn't need the comments for such a case so no problem there. Much of my code is broken into functions and subs, usually with a kind of "plot summary" in the comments that tell me what I'm doing more than how it works - when I review the code I read the comments to understand what it is meant to do so I can follow the flow quickly and figure out where I want to put my attention. Since I like to break up code into functions and subroutines, "meaningful procedure names" can be a form of self-documentation.

I use a lot of {i, j, s, t, a, x} variables for simple stuff in functions of 10-20 lines. After that I start getting more careful about meaningful identifiers. This is especially helpful in VBA where we don't have block level scope (too bad - I'd really love that).
 
Last edited:

Excel Facts

Excel Wisdom
Using a mouse in Excel is the work equivalent of wearing a lanyard when you first get to college
peterpre, you really do display just a tiny bit of ignorance, this being your first thread on the forum. i support various comments from the regulars (eg rorya) and would add my perspective as follows.

i use sensible variable names and subroutine names which often do not require many extra comments as a result. i write generic subroutines which i regularly recycle which saves me a lot of precious seconds (LOL) and i group related code into seperate modules. oh! - and i religiously indent my code. you have done none of these things in your example.

when i have to revisit an old project (because nothing ever remains static in terms of user demands) i know ill see the same sort of structure and code that i used last week. quick to modify or debug.
 
peterpre, you really do display just a tiny bit of ignorance, this being your first thread on the forum.

I can't see any display of ignorance.

It's a good thing to have different perspectives on a topic and to challenge conventional wisdom. Anything that doesn't stand up to a bit of scrutiny isn't really worth following, after all.
 
It's a good thing to have different perspectives on a topic
True, but I'm starting to feel like this has been done to death !
I didn't understand where this thread was heading right from the start, once a couple of boundaries / opinions had been made the whole thing became moot .....but, we press on.
 
None of us (yes I know, even me) writes consistently perfect code so a little bit of time declaring variables and adding comments for the time it saves at a later date is worth every second imho.

Dom
 
I worked with a guy who had a very similar thought process about not commenting or declaring veriables. I got lumbered with picking up the pieces after he'd left and it was a horrible job to go through and understand what everything was meant to be doing. Some of the code didn't work at all how you'd expect because of a variable that had been typed with fat fingers.

On the plus side I got to tell everyone that although there had been some problems with the data they'd been receiving I'd now fixed it and it would work properly so I should probably thank him for making me look good.

So to reiterate everyone else's comments, yes there are occasions when I might delete Option Explicit and not declare or comment but if it's for a production then I'll comment and declare properly as I'm arrogant enough to believe that it will still be used in years time when I'm no longer responsible for it :)

Nick
 
I'm going to go with Option C. A is lazy, at best, and you don't really want to use an Integer, in either case, which A coerces your numbers to. And B actually forces the issue to be an Integer. On a 32-bit system, you'll want a Long.

You can get into a lot of trouble by not declaring your Variables, try using DAO and ADO in Access sometime:

http://access.mvps.org/access/bugs/bugs0031.htm

Taking 2 seconds to declare your Variables properly will save you a headache. It's good practice.
 
xenou said:

<BIG Snip> This is especially helpful in VBA where we don't have block level scope (too bad - I'd really love that).

I got curious about this so I did some (very little) research on "block level scope". This link sorta convinced me (mind you, I know a LOT LESS then you about this type of stuff) that BLS would be a pretty bad idea. What are your thoughts about this?

http://visualbasic.about.com/od/quicktips/qt/blkscp.htm

Gene Klein
</BIG>
 
Does VBA not have block-level scope? My test here seems to indicate it does, to some extent:

Code:
Sub foo()
Dim cl As Range
For Each cl In Range("A1:A3")
    Debug.Print cl Is Nothing
Next
Debug.Print cl Is Nothing
End Sub

In terms of intrinsic data types, that's a one-liner, e.g.,

Code:
Sub bar()
Dim i As Long
For i = 1 To 3
    Debug.Print i
Next i
Let i = Empty
Debug.Print i
End Sub
 

Forum statistics

Threads
1,215,475
Messages
6,125,028
Members
449,205
Latest member
Eggy66

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top