Page 1 of 1 1
Topic Options
#101090 - 2003-05-18 10:48 PM Write understandable posts
kholm Offline
Korg Regular
*****

Registered: 2000-06-19
Posts: 714
Loc: Randers, Denmark
Tokenizing scripts.

After Ruud had implemented tokenizing of scripts from KiX 4.2, there is no excuse for NOT using descriptive variable names and omitting comments.

In previous vesions the length of variable names could impact the execution time of scripts (specially recursive scripts)

So please, in the future, don't use variable names like: $, $_, $!, $1, $2 etc... when you post a script.

Anyway we/i dont use the Scripts/UDF's in the form they are posted on the board, but use them as inspirations, so please don't hide the gold!

I will supply two scripts that executes in same time, but is in readable/Not readable form.

Readable script:
code:
Function ListFiles($DirectoryName)
; List all filenames in $DirectoryName and all subdirectories beneth it

$FileName = Dir($DirectoryName + "\*.*")
While @Error = 0 And $FileName
If $FileName <> "." And $FileName <> ".."
If GetFileATTR($DirectoryName + "\" + $FileName) & 16 ; This is a directory, list files using recursion
; Recursion:
; In this situation we want to list all files in a directory, and all files in all subdirectories under this subdirectory
; So the function ListFiles(), The function we are inside calls itself when it meets subdirectories
ListFiles($DirectoryName + "\" + $FileName) ; Recursion (call MySelf with new parameter) ;)
Else
? $DirectoryName + "\" + $FileName
EndIf
EndIf
$FileName = Dir()
Loop

EndFunction

Hard to read script
code:
Function L($S)
$=Dir($S + "\*.*")
While @Error=0 And $
If $<>"." And $<>".."
If GetFileATTR($S+"\"+$) & 16
L($S+"\"+$)
Else
?$S+"\"+$
EndIf
EndIf
$=Dir()
Loop
EndFunction

The code i used for testing:
code:
Break On

; Dummy-run to fill buffer
L(%WinDir%)

$Start = @Ticks
ListFiles(%WinDir%)
$ElapsedLong = @Ticks + $Start

$Start = @Ticks
L(%WinDir%)
$ElapsedShort = @Ticks + $Start

? 'Elapsed time long script : ' + $ElapsedLong
? 'Elapsed time Short script: ' + $ElapsedShort

Get $x
Return

-Erik

Top
#101091 - 2003-05-18 10:52 PM Re: Write understandable posts
Lonkero Administrator Offline
KiX Master Guru
*****

Registered: 2001-06-05
Posts: 22346
Loc: OK
mm...
not sure about hiding the gold but at least making it hard to change makes it also hard to make it fail.

so, I think crypting in this way for UDFs is good.
_________________________
!

download KiXnet

Top
#101092 - 2003-05-18 10:53 PM Re: Write understandable posts
Sealeopard Offline
KiX Master
*****

Registered: 2001-04-25
Posts: 11165
Loc: Boston, MA, USA
Addendum: The exception is KiXgolf where you can still use one-character variables.

Other than that, I do have to agree with Erik that especially UDFs should be readable as other might want to uderstand them, too. Just the fact that one posts a UDF normally shows that that person is a skilled coder. So, there's no need for obfuscation.
_________________________
There are two types of vessels, submarines and targets.

Top
#101093 - 2003-05-18 11:01 PM Re: Write understandable posts
Les Offline
KiX Master
*****

Registered: 2001-06-11
Posts: 12734
Loc: fortfrances.on.ca
Up to now this is all just conjecture. Ruud has not revealed what exactly he means by tokenize. The vars could very well remain as plain text.

Passwords and text strings could still be ripped without the need for even a de-tokeenizer.

If the pre-tokenizer in the current version is any indication, a few simple tests would determine if var length affects performance. From my recollection of converstions on this topic, var length still affects performance.
_________________________
Give a man a fish and he will be back for more. Slap him with a fish and he will go away forever.

Top
#101094 - 2003-05-18 11:24 PM Re: Write understandable posts
kholm Offline
Korg Regular
*****

Registered: 2000-06-19
Posts: 714
Loc: Randers, Denmark
Lonk,

Make your UDF's non-cryptic, in this way you make it easyer to resuse them. As i said in my starting post: "Scripts/UDF's" should only be used as inspirations [Wink]

Les,

The scripts i posted was to prove that once the script is read/tokenized the execution time for $DirectoryName is the same as for $S

-Erik

Top
#101095 - 2003-05-19 12:42 AM Re: Write understandable posts
Lonkero Administrator Offline
KiX Master Guru
*****

Registered: 2001-06-05
Posts: 22346
Loc: OK
erik, well...
I think UDFs should be used just for using them.
no need to recode everything.

anyway, I have been providing lot cleaner code lately...
wait and see, maybe I will vote for hungarian notation at some point.

now I just say that as long as the code is functioning, the user does not need to know what it does.
not all programmers know what's in windows.h or in iostream.h and still they use them!
_________________________
!

download KiXnet

Top
#101096 - 2003-05-19 08:08 AM Re: Write understandable posts
masken Offline
MM club member
*****

Registered: 2000-11-27
Posts: 1222
Loc: Gothenburg, Sweden
Amen to that kholm [Wink]

When posting scripts here, I also think it's a good idea to be as clear as possible, rather that optimizing a few bytes etc. That could be done by the enduser instead by simply using a search and replace tool in their favorite text editor.

[ 19. May 2003, 08:11: Message edited by: masken ]
_________________________
The tart is out there

Top
#101097 - 2003-05-19 08:16 AM Re: Write understandable posts
Jochen Administrator Offline
KiX Supporter
*****

Registered: 2000-03-17
Posts: 6380
Loc: Stuttgart, Germany
Yeah Joeel !

Give that up finally ! We all want to be able to steal ya code [Razz]
_________________________



Top
#101098 - 2003-05-19 09:16 AM Re: Write understandable posts
Lonkero Administrator Offline
KiX Master Guru
*****

Registered: 2001-06-05
Posts: 22346
Loc: OK
I've not been optimizing!
I've been clear as possible:
"do not screw my code!"

and:
"this script is made cryptic so you would not have the temptation to break it and blame my a*s"

how it could be clearer? [Wink]
_________________________
!

download KiXnet

Top
#101099 - 2003-05-19 11:03 AM Re: Write understandable posts
Richard H. Administrator Offline
Administrator
*****

Registered: 2000-01-24
Posts: 4946
Loc: Leatherhead, Surrey, UK
quote:
"this script is made cryptic so you would not have the temptation to break it and blame my a*s"

how it could be clearer?

You could explain what an "a*s" is and why I should want to blame it for anything.

Top
#101100 - 2003-05-22 12:23 AM Re: Write understandable posts
Glenn Barnas Administrator Offline
KiX Supporter
*****

Registered: 2003-01-28
Posts: 4402
Loc: New Jersey
Lonk:

Maybe you should get rid of the donkey (a*s) that everyone seems to be blaming. I can't imagine that with hooves, he could be writing clear code anyway. [Wink]

I have to agree on the comments & var name stuff.. If I need to optimize code that much I probably wouldn't be writing in kix, anyway (KixGolf not withstanding). And then, I could always optimize the UDF if I needed to.

I bit my tongue when someone chided me about all the "b-s" in front of some of my UDF posts. I believe that if you also discuss how you use it and possibly give the reason behind writing it, you can make it's use more obvious and possibly give someone an idea for solving a similar challenge.

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

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 1782 anonymous users online.
Newest Members
ManuvdWielNL, Sir_Barrington, batdk82, StuTheCoder, M_Moore
17887 Registered Users

Generated in 0.13 seconds in which 0.013 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