Tips for golfing in VBScript

5

1

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. :)

RAM

Posted 2013-10-24T15:33:21.647

Reputation: 797

My friends would like this :) – ckjbgames – 2017-03-29T01:41:29.190

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) – Taylor Scott – 2018-01-25T04:30:20.220

Oh and you can add syntax highlighting for VBScript (or any VB derivative) using the markdown notation <!-- language-all: lang-vb --> – Taylor Scott – 2018-01-25T04:32:54.270

Answers

2

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."

JesterBLUE

Posted 2013-10-24T15:33:21.647

Reputation: 696

1

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

comfortablydrei

Posted 2013-10-24T15:33:21.647

Reputation: 701

1

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.

RubberDuck

Posted 2013-10-24T15:33:21.647

Reputation: 321

Iff() isn't an intrinsic VBScript function, thus it's not supported. – omegastripes – 2015-12-05T11:15:56.820

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.

– RubberDuck – 2015-12-05T11:35:29.537

2Unfortunately Iff() isn't an intrinsic VBA function as well :D – omegastripes – 2015-12-05T11:42:24.100

Yes it is. Oh. Lol. Damned typo. – RubberDuck – 2015-12-05T11:43:14.907

1

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.

RubberDuck

Posted 2013-10-24T15:33:21.647

Reputation: 321

Newlines only count as one byte on Unix Systems, however, Windows Systems use \r\n (Which is two bytes, 13 and 10 respectively) – ATaco – 2017-03-29T01:20:39.337

0

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 ...

Taylor Scott

Posted 2013-10-24T15:33:21.647

Reputation: 6 709