| Start |
|---|
| Variable Names Variables hold data. Name them after what they're used for, not after their data type or scope, using a noun. If you feel compelled to number your variables (e.g. Names of variables that represent an iteratable set of values - e.g. an array, a Some common VBA naming conventions go thus: For procedure-level Variables:
For module-level Variables:
For Constants:
However Hungarian NotationName them after what they're used for, not after their data type or scope. "Hungarian Notation makes it easier to see what the type of a variable is" If you write your code such as procedures adhere to the Single Responsibility Principle (as it should), you should never be looking at a screenful of variable declarations at the top of any procedure; declare variables as close as possible to their first usage, and their data type will always be in plain sight if you declare them with an explicit type. The VBE's Ctrl+i shortcut can be used to display a variable's type in a tooltip, too. What a variable is used for is much more useful information than its data type, especially in a language such as VBA which happily and implicitly converts a type into another as needed. Consider Compare to:
The bottom example names it This is useful, because IntelliSense by itself doesn't display <img src="https://i.stack.imgur.com/3FAHc.png" alt="IntelliSense showing "CanReadFile(ByVal path As String, outComment As String) As Boolean" when the actual signature specifies outComment as explicitly passed by reference ("ByRef")" /> Which leads to... Hungarian Done Right Hungarian Notation originally didn't have anything to do with variable types (opens new window). In fact, Hungarian Notation done right is actually useful. Consider this small example ( Compare to:
Of course there's a better way to convey it all, using proper abstraction and real words that can be pronounced out loud and make sense - as a contrived example: |
| Procedure Names Procedures do something. Name them after what they're doing, using a verb. If accurately naming a procedure is not possible, likely the procedure is doing too many things and needs to be broken down into smaller, more specialized procedures. Some common VBA naming conventions go thus: For all Procedures:
For event handler procedures:
Event handlers are usually automatically named by the VBE; renaming them without renaming the object and/or the handled event will break the code - the code will run and compile, but the handler procedure will be orphaned and will never be executed. Boolean Members Consider a Boolean-returning function: Compare to: The Compared to: Consider using prefixes such as |
Read Article
https://develop.horje.com/learn/1434/reference