Page 1 of 1 1
Topic Options
#116787 - 2004-03-25 03:44 AM Diary of a KiXtart noob
mrwumpus Offline
Fresh Scripter

Registered: 2003-09-04
Posts: 14
Loc: California, USA
03/25/2004 – Inspired and empowered by Richard’s sage advice over in this thread, last night I added the line $=SetOption("Explicit","ON") at the top of my simple administrator’s domain logon script. It was my first step toward becoming organized and intentional in my scripting. When I arrived to work early the next morning, this e-mail was waiting for me: “Jason I have gotten an error this morning logging into servers. The error is in command prompt window and bginfo does not load...”

At 7:30 AM my noob senses couldn't immediately forsee what might be wrong with my my script, but as I logged in to a server a little voice in my head scolded me for both directly modifying and not even testing a production script. Sure enough, almost immediately my formerly healthy script beeped with ERROR : undefined variable [$apps]! on line 17 and the window closed.

I found line 17 ($apps = '%SYSTEMROOT%\System32' ) and laughed at myself. Of course! Had I RTFM (which I proceeded to do) then I would have known: “If implicit declaration is disabled, all variables must be explicitly declared before they can be used.” Obvious enough. So I added GLOBAL $apps,$log before my two implicit variables and then logged out and tried it again. Beep! Same error, different line! This was getting annoying. I drilled down and sure enough I found more implicit variables. They were everywhere! So I declared each one with DIM and tried the script another time and got ANOTHER ERROR.

It turned out that even the borrowed User Defined Functions at the bottom of my script, which were written by veterans, contained implicit variables that required the same treatment. My noob frustration was high, but I had learned some important lessons the hard way:

    Lesson 1: RTFM. If I weren't a noob I probably would have consulted the manual before adding $=SetOption("Explicit","ON") to the top of an existing script, particularly in a production environment. However, I'm just a noob who learns the hard way. After my mistake, I did RTFM and did finally fix the problem. However, equally important to the lesson of RTFM, I have learned, is to BTFM.
    Lesson 2: BTFM. Believe the **** manual! Noobs like me forget or do not realize that we are dealing with an absolute, binary, ON/OFF world when scripting. When the manual says “If implicit declaration is disabled, all variables must be explicitly declared before they can be used” it means that. It isn't advice, it is law. Had I understood and believed that I would not have had to keep fixing my script after I RTFM.
    Lesson 3: Clean Code Requires Good Habits. Taking Richard's advice and forcing myself to explicitly declare variables requires that I develop good habits, because using that option requires that I be more careful with my scripting and with the scripts I borrow from others. Veterans may not agree with the suggestion, but for a noob who tends toward sloppiness, I need all the guidance and good habits I can get.
P.S.: This diary idea was also Richard's but I can't blame him if everyone laughs at me.
_________________________
Jason

Top
#116788 - 2004-03-25 10:41 AM Re: Diary of a KiXtart noob
Co Offline
MM club member
***

Registered: 2000-11-20
Posts: 1342
Loc: NL
I like this noob ... Never forget once we were all newbies/noobs too!

If I haven't said it before: Jason, welcome to this Board!!!
_________________________
Co


Top
#116789 - 2004-03-25 11:17 AM Re: Diary of a KiXtart noob
Jochen Administrator Offline
KiX Supporter
*****

Registered: 2000-03-17
Posts: 6380
Loc: Stuttgart, Germany
Jason,

thanks for this reminder, I found two of mine UDFs will fail on explicit on.
Will be corrected !
_________________________



Top
#116790 - 2004-03-25 02:10 PM Re: Diary of a KiXtart noob
Glenn Barnas Administrator Offline
KiX Supporter
*****

Registered: 2003-01-28
Posts: 4402
Loc: New Jersey
English major, huh? Pretty D&#$ good tech writing!

FWIW - I put every UDF that I download into a simple script that turns on Explicit and NoVarsInStrings and call the new function. I then either modify the function or pass back a note to the original poster requesting the mods before I place it into my library. Most of us will readily update our UDFs to support these settings, and nearly all new UDFs posted do as well.

Again, welcome!

Glenn
_________________________
Actually I am a Rocket Scientist! \:D

Top
#116791 - 2004-03-25 02:20 PM Re: Diary of a KiXtart noob
Shawn Administrator Offline
Administrator
*****

Registered: 1999-08-13
Posts: 8611
This was the best read i've had in months - well done. Your very articulate - Welcome to the board.

-Shawn

Top
#116792 - 2004-03-25 03:38 PM Re: Diary of a KiXtart noob
Sealeopard Offline
KiX Master
*****

Registered: 2001-04-25
Posts: 11165
Loc: Boston, MA, USA
Yeah, that'll be the next step, adding the Code:
$rc=SETOPTION('NoVarsInStrings','ON')

next to the Explicit statement.

Also, another lesson that should have been learned is this:

Any time you make a code change, you MUST test that change!

You should even have some kind of test suite to make sure your changes work, it's a little bit more work in the beginning, however, it assures that the script will then work. I normally spend more time testing a UDF than actually writing it.


Edited by sealeopard (2004-03-25 05:49 PM)
_________________________
There are two types of vessels, submarines and targets.

Top
#116793 - 2004-03-25 04:47 PM Re: Diary of a KiXtart noob
Bryce Offline
KiX Supporter
*****

Registered: 2000-02-29
Posts: 3167
Loc: Houston TX
Nice post! but watch out....

While $=SetOption("Explicit","ON") is easy to fix by using DIM/GLOBAL...

Using $=SETOPTION('NoVarsInStrings','ON') can have even more unforeseen side effects, and in some cases a tricky rewrite of code will be needed, especially if you use such commands like EXECUTE(). I have seen EXECUTE() code that when using Explicit work with Explicit ON but not work with Explicit OFF.

Bryce

Top
#116794 - 2004-03-31 09:46 PM Re: Diary of a KiXtart noob
mrwumpus Offline
Fresh Scripter

Registered: 2003-09-04
Posts: 14
Loc: California, USA
Guys, thanks for the encouraging feedback. I was working on my second entry until I had to drive 7 hours (starting at 12:00 AM) to see my newborn niece. And now that I'm back writing it, I've been sidetracked by some wonderful discoveries thanks to RTFM! Next entry should prove at least as entertaining, if not more useful to newbies, than my first...
_________________________
Jason

Top
Page 1 of 1 1


Moderator:  Arend_, Allen, Jochen, Radimus, Glenn Barnas, ShaneEP, Ruud van Velsen, Mart 
Hop to:
Shout Box

Who's Online
0 registered and 2220 anonymous users online.
Newest Members
Viginette, ManuvdWielNL, Sir_Barrington, batdk82, StuTheCoder
17888 Registered Users

Generated in 0.071 seconds in which 0.04 seconds were spent on a total of 12 queries. Zlib compression enabled.

Search the board with:
superb Board Search
or try with google:
Google
Web kixtart.org