jpbro / VbPcre2

PCRE2 Wrapper for VB6
Other
14 stars 16 forks source link

Variable names prefixes #13

Closed dragokas closed 7 years ago

dragokas commented 7 years ago

I like your prefixes naming style. I noticed:

m mo lo so ma_ ...

2-nd letter is a type of variable (o - object, a - array). 1-st is a vision.

I curious, how do you call these abbreviations: m - module? And other s, m, l ? How about global (public in module) ?

I just liked such style, and want to know more, before (and if) I plan to use it in my projects too.

Thank you for explanations.

jpbro commented 7 years ago

Hi @dragokas, it's story time I guess ;)

The prefixing convention is one I came up with after coding in VB6 for a number of years.

When I first started programming in VB, I was new-ish to programming in general and not very disciplined with variable names. I would often have variable names without any prefix, e.g. Variable1 As Long, Variable2 As String, etc... the problem with that was sometimes variable names would conflict with method names or object names, or it would be difficult to figure out where they were declared (procedure level, module level, etc...), and difficult to figure out what type they were until I located their declaration. Not such a big deal for small projects I guess, but as my main work project grew it became unwieldy.

For method parameter names, it is still quite common to use an unadorned variable name (like MyMethod(Parameter As String, Parameter2 As String)), but this too can result in collisions with other method names.

So, like many VB6 programmers, I adopted Hungarian notation as I believe it is called, and ended up with variable names like strVariable, lngVariable, mlngVariable, etc... This solved the problem of figuring out the scope and the data type, but overtime I found it occasionally problematic - sometimes I would change a variable type but forget to update the variable name, and would end up with deceptive variable names like strVariable As Long, or lngVariable As String for example.

You can also end up with variable names like strInGridName As String which make find and replace a bit annoying (say you wanted to find/replace string with something else - you might end up matching the strInG in strInGridName unless you are careful).

There may have been other things that brought me to my current convention, but those are the main points that I remember).

What I decided that in that vast majority of cases I was only interested in where the declaration was located, and whether or not the variable referred to an intrinsic type, an object of any type, or an array. If I had that information encoded in the prefix, then I could easily work with the variables, and when I needed more information on the type I could quickly find the declaration.

So I decided that each variable should have a prefix, a separator character (underscore) and a descriptive name, and that this would give me enough information to work efficiently with the variables, make it easy to do finds and search and replaces without false matches, and make it reasonably quick to find the declaration when necessary.

The first character of the prefix is its scope:

The second character can be omitted (in which case the variable type is one of the standard types like String, Long, etc...), or it can be one of the following:

The next character is always an underscore, and all subsequent characters are part of the descriptive name for the variable.

I'll admit the system isn't perfect, but it covers the vast majority of my use cases and I think it has made me more productive and efficient, while still providing a good amount of information while minimizing the chance of deceptive names (it's rarer to move from an intrinsic type to an object type and forget to update the variable name then to move from one intrinsic type to another in my experience).

A possible improvement would be for arrays of objects to have a 3 letter prefix such as lao_ instead of just la_ which I usually use, but to be honest I usually store objects in a collection when I need to hold a bunch of them so that I can do a For Each when I need to loop through them, so I rarely encounter arrays of objects.

Thanks for the question...I know my convention is unusual so I wasn't sure if it would put people off or not, but it's nice to hear that you found it intriguing!

dragokas commented 7 years ago

Yea, I like it more than sVar or strVar in some cases. I have not yet developed the style of naming most convenient for me and well readable for others. Your information is very usefull. Thank you for so detailed explanation. It was very interesting story :)