5
\$\begingroup\$

What general tips do you have for golfing in VBScript? I'm looking for ideas that can be applied to code golf problems in general that are at least somewhat specific to VBScript. Please post one tip per answer.

Thanks to Marcog for the original idea. :)

\$\endgroup\$
  • \$\begingroup\$ My friends would like this :) \$\endgroup\$ – ckjbgames Mar 29 '17 at 1:41
  • \$\begingroup\$ Just in case anyone is editing vbs in notepad and just running the file - you can save yourself from having to see lots of MsgBoxs by replacing the MsgBoxs with WScript.Echos and running the VBScipt file in cmd.exe with CScript (CScript C:\filepath\file.vbs) \$\endgroup\$ – Taylor Scott Jan 25 '18 at 4:30
  • \$\begingroup\$ Oh and you can add syntax highlighting for VBScript (or any VB derivative) using the markdown notation <!-- language-all: lang-vb --> \$\endgroup\$ – Taylor Scott Jan 25 '18 at 4:32
2
\$\begingroup\$

Use an empty variable instead of ""

Uninitialized variables are set to a special value: empty. When empty is cast to a string it becomes "".

Contrived example (using z instead of ""):

s=InputBox(z)
For i=1To 5
    For j=1To i
        r=r&s
    Next
    MsgBox r
    r=z
Next

For numeric types Empty is 0, and for booleans it is false. This can cut down on initialization code.

a=inputbox("Enter Starting Value")
b=a
while done=0
    i=i+1
    b=b*2
    done=(b>10000 or i>10)
wend
msgbox "Your "&a&" rabbit(s) have turned into "&b&" rabbit(s) in "&i&" years."
\$\endgroup\$
1
\$\begingroup\$

Use IIf instead of If...Then

IIf() is essentially a ternary operator. You can potentially save quite a few bytes.

For example:

var = "World"

If foo Then
    MsgBox "Hello" & var
Else
    MsgBox "Goodbye" & var
End If 

Vs.

MsgBox IIf(foo, "Hello", "Goodbye") & "World"

Note: VBA only. VBScript doesn't support this one.

\$\endgroup\$
  • \$\begingroup\$ Iff() isn't an intrinsic VBScript function, thus it's not supported. \$\endgroup\$ – omegastripes Dec 5 '15 at 11:15
  • \$\begingroup\$ Well, I'll be damned. I can't believe I never knew knew that. You're right. I'm going to leave it here because the tip will be useful for vba folks. \$\endgroup\$ – RubberDuck Dec 5 '15 at 11:35
  • 2
    \$\begingroup\$ Unfortunately Iff() isn't an intrinsic VBA function as well :D \$\endgroup\$ – omegastripes Dec 5 '15 at 11:42
  • \$\begingroup\$ Yes it is. Oh. Lol. Damned typo. \$\endgroup\$ – RubberDuck Dec 5 '15 at 11:43
1
\$\begingroup\$

Shorter IF Then: save 10 bytes

If you terminate an IF/THEN clause with a carriage return, you don't need to use an END IF.

Example:

For x=1 to 1
  IF x=1 THEN MSGBOX "No Error"
Next
\$\endgroup\$
1
\$\begingroup\$

Use Line Separators

Newlines count as two bytes ("\n"), but a colon is only one.

For i = 0 To 10
    MsgBox "Hello"
Next

Vs.

For i = 0 To 10: MsgBox "Hello": Next

Note: You can't use the line separator for If statements.

\$\endgroup\$
  • \$\begingroup\$ Newlines only count as one byte on Unix Systems, however, Windows Systems use \r\n (Which is two bytes, 13 and 10 respectively) \$\endgroup\$ – ATaco Mar 29 '17 at 1:20
0
\$\begingroup\$

Condense For statements

In For statements (and in fact any statement comprised of a " or numeric literal followed by a reserved command) the space that separates any numeric and a following word may be removed

This means that

For i = 1 To 100 Step 2
...
Next

May be condensed down to

For i=1To 100Step 2
...
Next

and following this logic,

If t = "TEST" Then ...

may be condensed to

If t="Test"Then ...
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.