PtokaX forum

Development Section => HOW-TO's => Topic started by: pHaTTy on 20 April, 2004, 12:11:22

Title: HOW-To: Develop your own scripting style
Post by: pHaTTy on 20 April, 2004, 12:11:22
HOW-To: Develop your own scripting style
-----------------------------------------

ok alot of ppl asked me why there is something called scripting style if all scripts look the same and are set out the same, so here is how-to on devel ur own scripting style..and lol, im bored atm and not upto doing actually any scripting so here goes...


Hello = 1;

if Hello == 1 then
doafunction()
else
doafunction()
end;


ok heer an example, a simple way of scripting ^

another way u can do it is


Hello = 1;

if Hello == 1 then doafunction()
else doafunction() end;


depening on u, it can be easier or harder to read, for me its more simple more compact less code to read down.....the reason for this is, code is not compile by style as long as it understands what the code means it compiles it, they are basically commands for the compiler to generate the code

function Main()
doanotherfucn()
hello = 2;
Kicksomeonesass()
destroysntax()
killptokax = 2;
Callkill()
end



ok just a lame main function now lets put it all in one line or spread out in differ formats, so it can look less messy

function Main() doanotherfucn() hello = 2; Kicksomeonesass()
destroysntax() killptokax = 2; Callkill()
end


here is 1 way

function Main()
doanotherfucn() hello = 2; Kicksomeonesass() destroysntax() killptokax = 2; Callkill()
end


heer is another

function Main()
doanotherfucn() hello = 2;
Kicksomeonesass() destroysntax()
killptokax = 2; Callkill()
end


and another =)

now what about return 1 and end etc ok lets look

i will use a script i wrote earlier for the example

--//NickChanger 0.02 by Phatty
--//Under development lol (bored)

--//Globals
BOT = ".D.R.";
Command1 = "!nick";
Command2 = "!clear";

function Main()
Nick = {}
end;

function DataArrival(user,data)
if strsub(data, 1, 1) == "<" then
local data=strsub(data,1,strlen(data)-1)
local s,e,cmd = strfind(data, "%b<>%s+(%S+)")
local s,e,msg = strfind(data, "%b<>%s+(.+)")
if strlower(cmd) == Command1 and user.bOperator then
local s,e,newnick = strfind(data, "%b<>%s+%S+%s+(%S+)")
if newnick then
Nick[user.sName] = newnick;
user:SendData(BOT,"You have changed your chat nick to "..newnick)
return 1;
end;
elseif strlower(cmd) == Command2 and user.bOperator then
Nick[user.sName] = nil;
user:SendData(BOT,"You have cleared your fake nick!")
return 1;
end;
local sName = Nick[user.sName]
if sName then
SendToAll(sName,msg)
return 1;
end;
end;
end;

ok here is script and look standard coding style, now lets stop it going right down the page and change the ends

--//NickChanger 0.02 by Phatty
--//Under development lol (bored)

--//Globals
BOT = ".D.R.";
Command1 = "!nick";
Command2 = "!clear";

function Main()
Nick = {}
end;

function DataArrival(user,data)
if strsub(data, 1, 1) == "<" then
local data=strsub(data,1,strlen(data)-1)
local s,e,cmd = strfind(data, "%b<>%s+(%S+)")
local s,e,msg = strfind(data, "%b<>%s+(.+)")
if strlower(cmd) == Command1 and user.bOperator then
local s,e,newnick = strfind(data, "%b<>%s+%S+%s+(%S+)")
if newnick then
Nick[user.sName] = newnick;
user:SendData(BOT,"You have changed your chat nick to "..newnick)
return 1;
end;
elseif strlower(cmd) == Command2 and user.bOperator then
Nick[user.sName] = nil;
user:SendData(BOT,"You have cleared your fake nick!")
return 1;
end;
local sName = Nick[user.sName]
if sName then
SendToAll(sName,msg)
return 1;
end; end; end;


put lets also drop down the variables and the return 1 with the ends

--//NickChanger 0.02 by Phatty
--//Under development lol (bored)

--//Globals
BOT = ".D.R."; Command1 = "!nick"; Command2 = "!clear";

function Main()
Nick = {}
end;

function DataArrival(user,data)
if strsub(data, 1, 1) == "<" then
local data=strsub(data,1,strlen(data)-1)
local s,e,cmd = strfind(data, "%b<>%s+(%S+)")
local s,e,msg = strfind(data, "%b<>%s+(.+)")
if strlower(cmd) == Command1 and user.bOperator then
local s,e,newnick = strfind(data, "%b<>%s+%S+%s+(%S+)")
if newnick then
Nick[user.sName] = newnick;
user:SendData(BOT,"You have changed your chat nick to "..newnick)
return 1; end;
elseif strlower(cmd) == Command2 and user.bOperator then
Nick[user.sName] = nil;
user:SendData(BOT,"You have cleared your fake nick!")
return 1; end;
local sName = Nick[user.sName]
if sName then
SendToAll(sName,msg)
return 1; end; end; end;


ok and lets do the rest

[code]
--//NickChanger 0.02 by Phatty
--//Under development lol (bored)

--//Globals
BOT = ".D.R."; Command1 = "!nick"; Command2 = "!clear";

function Main() Nick = {} end;

function DataArrival(user,data)
if strsub(data, 1, 1) == "<" then local data=strsub(data,1,strlen(data)-1)
local s,e,cmd = strfind(data, "%b<>%s+(%S+)") local s,e,msg = strfind(data, "%b<>%s+(.+)")
if strlower(cmd) == Command1 and user.bOperator then local s,e,newnick = strfind(data, "%b<>%s+%S+%s+(%S+)")
if newnick then Nick[user.sName] = newnick;
user:SendData(BOT,"You have changed your chat nick to "..newnick)
return 1; end;
elseif strlower(cmd) == Command2 and user.bOperator then Nick[user.sName] = nil;
user:SendData(BOT,"You have cleared your fake nick!")
return 1; end;local sName = Nick[user.sName]
if sName then SendToAll(sName,msg)
return 1; end; end; end;

ok now u have 1 compact script, harder or easier to read, dunno for you but i find it easier, saves keep changing line =)

it is possible to write it all in 1 line but i aint gonna do that make the page to large lol, ok enjoy updating ur scripting style, and hope you's find a better way for you to script, more comfortable =)
Title:
Post by: NotRabidWombat on 20 April, 2004, 15:29:51
Ask any programmer, compact is BAD.

1) Tabify properly.
2) One command per a line.
3) Use meaningful variable *names*.
4) Don't use globals.
5) Stop using 2 instead of to (u instead of you, etc). You're NOT l33t. Oh, this is posting style.

-NotRabidWombat
Title:
Post by: pHaTTy on 20 April, 2004, 17:16:21
QuoteOriginally posted by NotRabidWombat
Ask any programmer, compact is BAD.

1) Tabify properly.
2) One command per a line.
3) Use meaningful variable.
4) Don't use globals.
5) Stop using 2 instead of to (u instead of you, etc). You're NOT l33t. Oh, this is posting style.

-NotRabidWombat

5) Stop using 2 instead of to (u instead of you, etc). You're NOT l33t. Oh, this is posting style.

haha since when did 2 become elite? and u?

its short hand way of typing,

7h47 pR084b1y \/\/0[_]1D 8? D1##3R =)

2) One command per a line.

dont matta, it dont make any differ to the compiler, and if ya say it is, well ya can say what ya like =) free world for opinions =)

hmm lets see

elite

to = ']['[]
you = Y[][_]  (cant do the elite y)

so try make sure ya knwo what ya talking bout first, many ppl use u for you and 2 for to, says typing more then ya have 2 =)
Title:
Post by: plop on 20 April, 2004, 17:46:14
QuoteOriginally posted by NotRabidWombat
Ask any programmer, compact is BAD.

1) Tabify properly.
2) One command per a line.
3) Use meaningful variable.
4) Don't use globals.
5) Stop using 2 instead of to (u instead of you, etc). You're NOT l33t. Oh, this is posting style.

-NotRabidWombat
number 5 is somehow hammered into me, when i make a help menu or some other feedback from the scripts i reguarly have TO go back and change the 2's.
1-4 are going fine and are indeed a must, makes it a lot easyer 2 read, and saves you from thinking what a variable was doing when you see the code back after a while.

plop
Title:
Post by: [NL]Pur on 20 April, 2004, 17:52:34
Quote1) Tabify properly.
2) One command per a line.
3) Use meaningful variable.
4) Don't use globals.
5) Stop using 2 instead of to (u instead of you, etc). You're NOT l33t. Oh, this is posting style.


i also think that rule number 5,  not really matters.
i often use the 2 when converting stuff
like,   avi2mpg and i think it's readable
Title:
Post by: NotRabidWombat on 20 April, 2004, 19:08:32
The fifth comment was a criticism on using compact instead of clear. Posts are more difficult to read when you use b4, 2, u, r, etc. Likewise, code is more difficult to read when you write compact rather than readable ie: "let's just remove all white space".

I guess some did not get the joke.

Pur. What if I want mpeg2avi? Is that mpeg2 or mpeg? Would I write mpeg22avi? Is there a new mpeg22 out? What if I wrote MpegToAvi. Hey, that's only one character longer, AND there's no confusion.

Nah, I must be CRAZY.

Phatty.
"7h47 pR084b1y \/\/0[_]1D 8? D1##3R =)"
Yeah, how much time did you waste typing that out and how much time am I going to waste trying to decypher it.

"many ppl use u for you and 2 for to, says typing more then ya have 2 =)"

Many people are lazy and stupid.

I know what is leet is. I DESPISE it. I also recognize that this lazy writing stems from its creation. I DESPISE it just as much.

"dont matta" "ppl"

Thank you for proving my point

"so try make sure ya knwo what ya talking bout first"

Maybe you should learn a programming style (and how to type words) before posting a "HOW-TO" on style.

-NotRabidWombat
Title:
Post by: pHaTTy on 20 April, 2004, 19:26:17
LoL

heh heh, now i like those last 2 comments, but ya gotta try better then that lol

and ehmmm, actually i write in c, c++, vb, and php, if that aint enough tell me =)

oh btw the os i wrote was not completed that was in asm n c =)

mind i have to admit asm is a real bi7ch

oh annddd maybe i can ask em when i get to london, what they think about styles when they writing games, we will see =)

and the elite well didnt take no time at all and decypher dont have to you can read it liek anyother words =)
Title:
Post by: YetAnotherStylist on 20 April, 2004, 20:21:37
Interesting, you only need to learn like 36 relation-mappings (26 letters and 10 numbers) between characters
and other encoded characters to classify yourself as "elite". Why arent we all "elite" then?
Just for the child in yourself: all you are doing by writing this "elite-style" is to create an empty hull.
The reason is most likely that you are not qualified (e.g. this does not equal elite) in any other way of life.

And about your collection of programming "skills":
Just by the way, the relationship between "being qualified" and "showing off how many programming
languages one is 'expert' in" is likely to be in inverse proportion.

Just my 2 (yes it is TWO, not TO) cents ...
Title:
Post by: pHaTTy on 20 April, 2004, 20:30:05
QuoteOriginally posted by YetAnotherStylist
Interesting, you only need to learn like 36 relation-mappings (26 letters and 10 numbers) between characters
and other encoded characters to classify yourself as "elite". Why arent we all "elite" then?
Just for the child in yourself: all you are doing by writing this "elite-style" is to create an empty hull.
The reason is most likely that you are not qualified (e.g. this does not equal elite) in any other way of life.

And about your collection of programming "skills":
Just by the way, the relationship between "being qualified" and "showing off how many programming
languages one is 'expert' in" is likely to be in inverse proportion.

Just my 2 (yes it is TWO, not TO) cents ...


hmm wel to you, i dont class myself as elite, in fact i dont even type it, i type what i type and dont call it elite, but im showing that he is wrong by saying its l33t by saying 2 instead of to or two etc........
Title:
Post by: [NL]Pur on 20 April, 2004, 20:58:46
QuotePur. What if I want mpeg2avi? Is that mpeg2 or mpeg? Would I write mpeg22avi? Is there a new mpeg22 out? What if I wrote MpegToAvi. Hey, that's only one character longer, AND there's no confusion.

ok, i can see the confusion :)


some other points:

i think that a short comment at certain points is nice.
i hate comment that are almost stories, some ppl tend todo it at the beginning of there file.

about the varibles names, if you have international
group of programmers it's harder too understand the
code if they use there own language for varible names.
(better use english).

i think an (un)written rule in programming concerning
variable names is that the first letter of the first word is
lowercase, all words following are uppercase. for
example:

with methods

getInstance()


variable names:
justAnotherString








oh , yetAnotherThing it's nice to use 1 nickname when
posting on 1 forum.
Title:
Post by: NotRabidWombat on 20 April, 2004, 21:19:20


Saying I am incorrect regarding my l33t statement does not change the fact that writing/coding in this manor is lazy and thoughtless.

If someone can not write clear, coherent sentences, I doubt that person can effectively code.

-NotRabidWombat
Title:
Post by: NotRabidWombat on 20 April, 2004, 21:28:33
Pur.

The lower case first letter of variables is typically intended to describe the type of varible ie:

sPersonName - string
iPersonAge - signed int
ulPersonSocialSecurity - unsigned long
ptrPerson - ptr

This helps with type casting and avoiding truncation.

-NotRabidWombat
Title:
Post by: pHaTTy on 20 April, 2004, 21:34:31
QuoteOriginally posted by NotRabidWombat


Saying I am incorrect regarding my l33t statement does not change the fact that writing/coding in this manor is lazy and thoughtless.

If someone can not write clear, coherent sentences, I doubt that person can effectively code.

-NotRabidWombat


hmmm well ok i admit ikinda agree with ya there, but it depends if u are working in a team, and if u are in a rush to get it done, if ya alone doing ur own project, its faster n comfortable todo it ur own way =)
Title:
Post by: NotRabidWombat on 20 April, 2004, 21:35:54
How about this for style!

1) Put the constant in front of the variable in a comparison.

if( 1 == var ) OR if( nil ~= var )
or for you c++ guys
if( null == var)

Helps avoid accidental assignments in conditions.

2) Error handling.

There is a reason there is an assert function and type function in lua. There is also a reason for throw/try/catch in C++. Use them.

3) Comments

Comment the parameters, purpose, and return value of every function.

4) Use hash tables rather than switches (if/elseif/elseif...) blocks.

Switches are sequential and SLOW. They perform O(N). A hash table typically performs at O(1). Use them to call functions from commands.

-NotRabidWombat
Title:
Post by: NotRabidWombat on 20 April, 2004, 21:41:16
Phatty,

We're not alone on projects here. Almost all source is posted here which is wonderful. People can learn, reuse, and then help. I would hate to see people develop bad programming habits.

-NotRabidWombat
Title:
Post by: pHaTTy on 20 April, 2004, 21:47:01
QuoteOriginally posted by NotRabidWombat
Phatty,

We're not alone on projects here. Almost all source is posted here which is wonderful. People can learn, reuse, and then help. I would hate to see people develop bad programming habits.

-NotRabidWombat

lets just remember, this is also a place to learn, and this was based on styles, so if people use lua for there projects like my upcoming lai project then they will have there own useful and comfortable style =)
Title:
Post by: NotRabidWombat on 21 April, 2004, 21:51:07
"useful and comfortable style"

So why include bad styles in the HOW-TO?

-NotRabidWombat
Title:
Post by: Skrollster on 21 April, 2004, 22:42:07
QuoteOriginally posted by (uk-kingdom)pH?tt?
oh annddd maybe i can ask em when i get to london, what they think about styles when they writing games, we will see =)

Hmm.. game programers isn't too good then it comes to optimizing applications..

i would guess that they produce wores code then M$ ugly code...
Title:
Post by: pHaTTy on 21 April, 2004, 23:32:49
QuoteOriginally posted by Skrollster
QuoteOriginally posted by (uk-kingdom)pH?tt?
oh annddd maybe i can ask em when i get to london, what they think about styles when they writing games, we will see =)

Hmm.. game programers isn't too good then it comes to optimizing applications..

i would guess that they produce wores code then M$ ugly code...

lmfaooo nah, actually they do some of the best coding ive seen, and id like to know all their little coding tricks tbh, heh
Title:
Post by: pHaTTy on 21 April, 2004, 23:34:46
QuoteOriginally posted by NotRabidWombat
"useful and comfortable style"

So why include bad styles in the HOW-TO?

-NotRabidWombat

bad style, where? to me that aint something there can be, u can code any way u like as long as when ya compile the compiler can read it fine then dont matta =)
Title:
Post by: NotRabidWombat on 21 April, 2004, 23:51:29
WHAT?!

So you think combining all source files and removing all white space would be a "fine" practice?! We might as well try to read machine language.

No, a strict, readabe style is a must. A style that limits bugs is another must.

I suppose goto's are another "fine" programming style. Who needs object oriented programming?!

tail -f /dev/forum | grep -v "pH?tt?"

-NotRabidWombat
Title:
Post by: pHaTTy on 23 April, 2004, 11:34:07
hahaha *smirk*

hmm i would say something nice but i aint lame, so you get on with ur temper tamptrums and

its not removing all the spaces lol

if someone == blah then
            dosumit()
end;

is just as readable as

if someone == blah then dosumit() end;

heh so i thnk next thing u will complain about is () brackets etc

if (someone == blah) then
     dosumit()
end;

next to complain about ppl shud use em? i dont and wont bother no point but i know you do, its ur style it also is pretty pointless for the coding that is done here.......

lets take vb for example here:

what wud be the point of

dim something as string
dim somethingelse as long
dim RabidWombat as newobject

or wud it be easier of it was done

dim something as string, somethingelse as long, RabidWombat as newobject

so now lua:

local something = "\n";
local somethingelse = 1;
local RabidWombat = sumit:sumit()

local something = "\n"; local somethingelse = 1; local RabidWombat = sumit:sumit()

n come to think of it

local something,somethingelse,RabdiWombat = "\n",1,sumit:sumit()

abit like

something = nil;
sumitelse = nil;

better do

something,sumitelse = nil,nil


programming u have to be quick to adapt to knew styles and new languages, if u cant ur gonna be dropped like a ton of bricks, so please stfu, grow up and come into the real world, its about learning not just 'you'

if someone finds it easier to do that ^^^ in 1 line then they can go do that in there own projects its not all here just aboiut programming for ptokax lol, yea this is ptokax lua forum but you still can learn for other apps, so stop going on like a 10year old that cant get his own way.....

EDIT: 1 more thing, as i said in the orig post, its someone asked, now this thread is showing u that there is a such thing as different styles of coding, maybe good and bad for there own different reasons, but it doesnt stop ppl from knowing what those different styles are,

for example if u knew what styles are and 'own' projects were u'd realise its much easier todo ur own coding style, ive seen some of your lua code and is pretty differ,

for other ppl that use the space tabs some use the tab key....so its still another way of styling code.......this is here for everyone to read, if they go try then thats up2 them u cant control other people......if they dont then thats ok, at least they know they can do such a thing........ok no more posts on this topic.....later.....
Title:
Post by: plop on 23 April, 2004, 15:45:53
sorry 2 say it but rabit is not the child here, it's you phatty.
ppl come here 2 learn, so teach them the correct way.
learning them a bad style of coding makes it harder for them, and harder for us 2 help.
the compiler might not have trouble reading a script, humans can.
humans write/debug the code so thats more important.

plop
Title:
Post by: pHaTTy on 23 April, 2004, 17:38:51
QuoteOriginally posted by plop
sorry 2 say it but rabit is not the child here, it's you phatty.
ppl come here 2 learn, so teach them the correct way.
learning them a bad style of coding makes it harder for them, and harder for us 2 help.
the compiler might not have trouble reading a script, humans can.
humans write/debug the code so thats more important.

plop

lol, there is no proper way to code

someone doing

void something()
{
}

is same as

void something(){
}

its a style, if ur trying to tell ppl how to code, then ur the child, and this is not for n00bs its for ppl that wonder about coding styles, i have been asked and i did fullfil there request....so ur wrong also =)



looks styles that dont matta and are still readable in c++

void Check()
{
if (Rabid == True)
{
Leave = True;
}
else //what is the point of this?
{
Leave = false;
}

}
void Check()
{
if (Rabid == True)
{
Leave = True;
}
else Leave = false;
}
void Check(){
if (Rabid == True){
Leave = True;
}
else Leave = false;
}

altho of course this a false function but wud do same thing
Title:
Post by: Cyberia on 23 April, 2004, 18:22:29
Think everyone uses the style what's handy for him/her.
You can't put everything into standards  8)
Title:
Post by: NotRabidWombat on 26 April, 2004, 03:02:33
First and foremost, I'd like to apologize to phatty for becoming hostile. I am sorry. Being a loud mouthed jerk is what I do best.

Secondly, I'd like to re-illustrate the importance of style in coding for the purposes of sharing code.

So far all of your examples have been simple one liners that can (and usually do) escape the restrictions of style for readability.

But apply something like:
block {
}

-OR-

block
{
}

and you can have problems. Using the latter allows you to line brackets up to ensure you did not forget one, limiting the probability of errors. Also, nesting can also become confusing.

Likewise, adding ( ) around conditions ensures more complex statements are well illustrated.


local something = "\n";
local somethingelse = 1;
local RabidWombat = sumit:sumit()

-OR-

local something = "\n"; local somethingelse = 1; local RabidWombat = sumit:sumit()

The first allows the programmer to insert helpful comments before or with the intializer. Typically, it much easier to read single line statements rather than a combined line.

The only benefit to your example styles is a few less characters to be typed. While all the styles I have proposed are restrictive, they are an attempt to:
a) Limit errors
b) Improve readability
Try reading some C legacy code your company locked away just long enough to let all the original developers leave, who all used different styles (most of them difficult to follow).

I am not trying to put everything into standards. I am merely voicing the commonalties I have discovered in my experience programming.

-NotRabidWombat
Title:
Post by: pHaTTy on 26 April, 2004, 14:30:19
QuoteOriginally posted by NotRabidWombat
First and foremost, I'd like to apologize to phatty for becoming hostile. I am sorry. Being a loud mouthed jerk is what I do best.

np, least it made a hot thread, was sumit todo as well =D
Title:
Post by: [NL]Pur on 26 April, 2004, 16:08:58
OK, i think we can agree on the following things :)


        - if you work on a project involving more then 1 ppl
          try to maintain 1 style.

        - Readablitly is importent not if something is
          short or long.

       
      -  Rabid is in his element when he is a loud
         mouthed jerk


did i forget any points?



/edit> - Nobody has a wrong style
Title:
Post by: Cyberia on 26 April, 2004, 17:19:59
lol  :D
Title:
Post by: NotRabidWombat on 26 April, 2004, 17:44:45
- Nobody has a wrong style,

Trust me, there are wrong styles.

-NotRabidWombat
Title:
Post by: pHaTTy on 27 April, 2004, 16:05:17
QuoteOriginally posted by NotRabidWombat
- Nobody has a wrong style,

Trust me, there are wrong styles.

-NotRabidWombat

nah i still disagree, altho i think i cudnt cope with

if someone == someoneelse then dosumit() end; if someoneelse == sumit then test = continue() if test == 1 then return 1; else return 2; end; end;
but it stil doesnt make it wrong, just means ya dont like it, and its something that others cant understand or read, the word ya looking for is inapproriate way of styling code
Title:
Post by: VidFamne on 27 April, 2004, 16:22:55
if Some_Standard ~= nil then
code_style = good
else code_style = bad
 end
Title:
Post by: ((UKSN))shad_dow on 06 May, 2004, 16:20:14
mmm well to me theres only 1 kind of script or spelling style ... hehehe thats simple as in simple to read , simple to spell, simple to syntax check , simple to correct

in other words  ..

 K.i.S or kis or  and now i not calling any body > K.I.S.S or K.I.S.S`s

and na i aint going to spell in full words i let u lot do That . PMFSL x 345345   :D  :D  :D

(was board so no flameing me )