Is there a legal use case for DefVar?

Def[Type] Statements generated from hell are supported in VBA, apparently to provide some kind of System Hungarian .

For instance:

 Option Explicit DefBool B DefLng I, L 

These statements have the following side effects:

 Dim bSomething ' <~ that a compile-time Boolean Dim iGiveUp ' <~ that a compile-time Long Dim lNooooo ' <~ that a compile-time Long also 

and

 Public Function IsUseful() ' function returns a compile-time Long End Function 

Do not forget:

 Public Property Get BasicStuff() ' yep, that a compile-time Boolean End Property 

You get the idea. Def[Type] statements are bloody evil and willingly use them in modern code to earn you 10K volts through the keyboard *.

But let our minds open and pretend that they are amazing for a minute.

There are a number of such statements:

  • DefBool for implicitly typed Boolean
  • DefCur for implicitly typed Currency
  • DefDbl for Implicitly Typed Double
  • DefInt for implicitly typed Integer
  • DefLng for Implicitly Typed Long
  • DefSng for Implicitly Typed Single

And here is another one:

  • DefVar for Implicitly Typed Variant

Now, if you did not know this, this is also an implicitly typed variant:

 Dim anything ' no "As [Type]" clause 

Thus, the specified implicit typing Def[Type] contains keywords, well, indicate the implicit type of compilation time, given the range of applicable prefixes of the first letter ...

 DefVar V Dim vSomeVariant ' compile-time Variant (DefVar) Dim foo ' also a compile-time Variant (implicit) Dim bar As Variant ' also a compile-time Variant (explicit) Dim vRedundant As Long ' compile-time Long ("As [type]" clause overrides DefVar) 

Even with an open look, Systems-Hungarian-Friendly, I cannot understand one legitimate use case for DefVar . There is one?


* Your mileage may vary.

+5
source share
3 answers

Deftype statements come from older versions of BASIC. I remember, at least in QBasic, variables were a floating point by default, and you could use a suffix if you wanted another variable. (Thus, X itself would be a number, and you would use X $ instead of String.) If someone were doing math with integers (as was often done), one could use DefInt AZ to declare that "type will be an Integer, not a floating point, so you don’t need to suffix all your variables with % to indicate an integer. Or maybe you would specify one section of the initial letters for integers, and the rest for floating point , which may be useful, especially if you have ported code from FORTRAN or other older languages ​​that e used such agreements different starting letters with different types.

Many Visual Basic was designed to take advantage of the ability to use code snippets from previous versions of BASIC with minimal changes, such as line numbers or using "Let" to assign a variable. I am not a historian, but I think that the “Hungarian systems” came later, especially when people are used to using more letters or two for their variable names, because computers are powerful enough to handle such things. Therefore, I do not agree with your premise, since I think that Deftype is only in BASIC because previous versions had this, and not because it was designed to help provide a specific coding style.

And I suppose that DefVar in particular was added only because they added Variant as a type, and it seemed that Variant should be handled like the other types that they had, although I am wondering if it is possible for a programmer to implement it in the same language that it was as useless as it is now.

Obviously, there is no need for any Deftype, or especially DefVar, for everything that was created at the time when VB6 was "new", and, of course, not for something new.

To answer your real question, the only thing I can really think of is that it would help make it explicit that you intentionally used default unused and implicitly typed variables should be considered options. Perhaps in some modules of your application you used DefInt AZ , and in other modules you used DefVar AZ as the coding standard to ensure that all default values ​​are explicit and can be easily understood. This is a little stretched, but of course I heard about more complex coding standards.

And if nothing else, if you used some kind of external tool to ensure that each file DefVar AZ with Option Explicit and DefVar AZ and possibly with some other standards (like Option Base ), you don’t have to worry about developers introducing others Deftype teams and having their colleagues want their keyboard to work.

+4
source

I remember FORTRAN where the letters IN were reserved for INtegers. My first BASIC in the DEC PDP range had single-letter variables, with one (or more?) Numbers after the letter - so you could have me and I0 to I9. I don’t remember whether the variables from I to N were implicitly integer or the keywords Def * were entered to enforce the rule. Maybe someone who still has the BASIC code ported from 40 years ago, but I would expect Rubberduck to mark them. You will find some relics!

+1
source

Edited to reflect reality (-:

Original mail between ===== lines

==================

Perhaps a little invented, but for DefVar there is a legitimate use case: override another DefXxx . Suppose you decide that almost all of your variables and functions should be of type Long , so you included the operator:

DefLng A

But wait, you'll also want to reserve a couple of letters for the default type. This is achieved by:

DefVar P

DefVar V

It is shorter and perhaps clearer than explicitly defining each range.

==================

After comments and a quick test, it is clear that this is no longer the case if it has ever been. However, I think that what I published later shows a reasonable case for at least one DefXxx :

As an aside, I preface each VB code module with

 Option Explicit DefObj AZ 

This forces me to explicitly enter each variable and function, because almost nothing typed by As Obj will not be saved as a general variable ... it immediately throws an error to say (for example) Abs(x) , a clear indication that I 'forgot assign X to a specific type. Strong typing in an environment that discourages him.

0
source

Source: https://habr.com/ru/post/1276273/


All Articles