#116787 - 2004-03-25 03:44 AM
Diary of a KiXtart noob
|
mrwumpus
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
|
|
|
|
Moderator: Arend_, Allen, Jochen, Radimus, Glenn Barnas, ShaneEP, Ruud van Velsen, Mart
|
1 registered
(mole)
and 1300 anonymous users online.
|
|
|