Jump to content
Sign in to follow this  
phronk

Scripting Etiquette?

Recommended Posts

I've always wondered if there was a reason beyond readability for some of the scripting styles I've seen. For example:

1. Tab spacing lines of code

2. Creating a totally empty line in between two lines of code

3. Spaces after the semicolon

4. Additional empty lines after the end of a script/code

5. Function scripts being located in a folder named "functions" or "fnc" (Does the name effect functionality for the script being read by the engine?)

6. No capitalized letters in commands in scripts (i.e. isplayer, instead of isPlayer)

There's others that I can't think of right now, but these are the ones that come to mind right now and have been bugging me lately since I'm trying to get more involved in scripting. I'd like to work on writing scripts that will be read by the engine the fastest, disregarding readability as a scripter. I don't care about making the script look fancy and clean if it means I'll get even a tiny bit more results in writing it as a blob.

The underlying question behind all the above examples is: are all those examples just for readability and cleanness for the scripter to read himself? Or is there more of a functionality aspect behind it that somehow influences how the engine reads the script. Are extra spaces/lines necessary at times for the engine to read better? Are capitalized letters slower at processing? Naming conventions of folders? Etc.

I know this is a pretty abstract question but I'm hoping an experienced programmer/scripter could provide some insight on these matters. Thank you!

Share this post


Link to post
Share on other sites

There's like a million threads on this already but:

1 - 4 is just for readability (you can preprocess which strips out white space etc)

5 - again, for neatness/so you know where they are

6 - generally, you don't have to put isPlayer (the engine reads it as isplayer). Some data comparisons are sometimes case-sensitive (like in find isEqualTo etc)

Just read this, most of what you are asking is touched on in there.

http://forums.bistudio.com/showthread.php?192052-Creating-Cleaner-Looking-Scripts

I'd like to work on writing scripts that will be read by the engine the fastest, disregarding readability as a scripter. I don't care about making the script look fancy and clean if it means I'll get even a tiny bit more results in writing it as a blob.

That is the number 1 worst thing you could do imo. Yes make it fast, but never at the expense of being able to understand your own code!

Edited by Das Attorney

Share this post


Link to post
Share on other sites

One thing in short supply while scripting, is time.

As Das said

Messy, unreadable code = more time it takes to get anything done.

I value my time more than a theoretical nano-second performance gain achieved with messy code. I could think of nothing worse than removing all line breaks, spaces and comments from a script while I'm working on it. Perhaps you could publish all your release versions like this, but as for the copy you work with, its a bad idea IMO.

Share this post


Link to post
Share on other sites

You can write your code anyway you want. If you don't want to capitalize, comment, or space things... Then don't. Name your folders, files, and functions anyway you like, as well. It's all up to the scripters or users preference.

I will advise to keep things neat and commented. Makes it much easier to read and understand later on. Especially when you start writing super complicated scripts over hundreds of lines. I think neat code is 100x more useful then the negligible about of time gaine on preprocessing.

Share this post


Link to post
Share on other sites

It would also be better to be readable if you ever plan to copy/paste any code here when asking for help.

Share this post


Link to post
Share on other sites

It really doesnt matter to the engine. it reads everything as one line (disregards formatting as it doesn't need to know what it looks like, it only cares about the actual content) until it sees a semi-colon then it starts a 'new line' and reads again until it sees another semi-colon and so on.

EDIT: commands themselves need all parameters on the same line in order to function, which makes them an exception to the above technically.

Share this post


Link to post
Share on other sites

Very helpful, thanks!

Share this post


Link to post
Share on other sites

1. Programmers choice - Doing this seperates the code into obvious blocks that can be debugged faster and easier. Aids in code readability for programmers who have never seen the code before.

2. Programmers choice - Doing this can make it more obvious that everything in that code block is a variable declaration, and everything below is the "meat" of the code.

3. Programmers choice - I don't know of anyone that does this. I do know, however, that when you post your code here in the forum using the php tag, spaces are (incredibly annoyingly) added to the end of every line. There is no benefit to this, it literally only increases filesize and preprocessing time.

4. Programmers choice - I don't know of anyone that does this either. However, I have occasionally left a few newlines at the end of some of my old scripts if I planned to keep adding to them but never got around to it.

5. Programmers choice - Having a folder named "functions" that you drop all your scripts into keeps your project organized.

6. Programmers choice - I personally perfer to camel case everything, including scripting commands. If you've ever downloaded one of my scripts, you'll notice that. Not using camel case has no effect whatsoever on the speed or functionality of a script.

I'd like to work on writing scripts that will be read by the engine the fastest, disregarding readability as a scripter. I don't care about making the script look fancy and clean if it means I'll get even a tiny bit more results in writing it as a blob.

This will not make you a better programmer, it will make you a worse programmer. Please write this on a sticky note and attach it to your computer monitor.

The underlying question behind all the above examples is: are all those examples just for readability and cleanness for the scripter to read himself? Or is there more of a functionality aspect behind it that somehow influences how the engine reads the script. Are extra spaces/lines necessary at times for the engine to read better? Are capitalized letters slower at processing? Naming conventions of folders? Etc.

All of those things are for ease of the programmer to put his project down, wait a month, come back and still be able to add on to it without breaking it instantly. Whitespace (spaces, tabs, newlines) are purely for humans, when a file is preprocessed all whitespace is removed. It doesn't matter if you use 1 space to seperate 2 commands or 100 newlines, the computer will remove all of them during preprocessing (although you may suffer from a slight delay from the computer having to read a ton of extra characters and then remove them).

I hope that clears some things up, if you think of any of the other things you wanted to ask, feel free to shoot; that's why we're here.

Share this post


Link to post
Share on other sites

EDIT: commands themselves need all parameters on the same line in order to function, which makes them an exception to the above technically.

Like DreadedEntity mentioned; whitespace don't matter. Only readability.

For example this works fine with its crazy spacing:

					hint 
("
				wow " 	
+
		str
(
	weapons
					player 
select 
		0
	) 				
					)
;

Edited by Greenfist

Share this post


Link to post
Share on other sites

We have some internal coding guidelines, let me answer to you questions according to them.

1. Tab spacing lines of code

8 (mainly because of compatibility with Windows Notepad, where it cannot be changed)

2. Creating a totally empty line in between two lines of code

Not required. Handy when separating large blocks of code, useless when used between each line.

3. Spaces after the semicolon

Required when code continues after semicolon on the same line.

4. Additional empty lines after the end of a script/code

Not required.

5. Function scripts being located in a folder named "functions" or "fnc" (Does the name effect functionality for the script being read by the engine?)

Functions folder is used in addons. Missions doesn't need to follow this rule. Doesn't affect functionality, it's only for sorting.

6. No capitalized letters in commands in scripts (i.e. isplayer, instead of isPlayer)

Capitalization is recommended, but not required.

Share this post


Link to post
Share on other sites

Thank you, King Moricky, as well as everyone else! That answers the bulk of my questions. I can probably just Google the rest of the questions, such as if there are folder names that pertain to missions, same as how "functions" folder pertains to addons.

Although, I guess another one that wouldn't hurt to ask is if scripts with many lines are bad, performance wise. Or does it really depend on what's in the code to make it so long? (Not including comments and spaces)

Share this post


Link to post
Share on other sites
Thank you, King Moricky, as well as everyone else! That answers the bulk of my questions. I can probably just Google the rest of the questions, such as if there are folder names that pertain to missions, same as how "functions" folder pertains to addons.

Although, I guess another one that wouldn't hurt to ask is if scripts with many lines are bad, performance wise. Or does it really depend on what's in the code to make it so long? (Not including comments and spaces)

It's dependent on what the code is. Like everyone mentioned here computers read the code way faster than your average human, if it's simple enough 1 or 100.000 lines can be read & processed before I could read your question.

[color=#FF8040][color=#191970][b]while[/b][/color] [color=#8B3E2F][b]{[/b][/color][color=#000000]true[/color][color=#8B3E2F][b]}[/b][/color] [color=#191970][b]do[/b][/color] [color=#8B3E2F][b]{[/b][/color][color=#191970][b]hint[/b][/color] [color=#7A7A7A]"You'll need to ctrl+alt+delete"[/color][color=#8B3E2F][b]}[/b][/color][color=#8B3E2F][b];[/b][/color] [color=#006400][i]//Single line[/i][/color]


[color=#006400][i]//A lot of lines:[/i][/color]
[color=#191970][b]hint[/b][/color] [color=#7A7A7A]"It"[/color][color=#8B3E2F][b];[/b][/color]
[color=#191970][b]hint[/b][/color] [color=#7A7A7A]"all"[/color][color=#8B3E2F][b];[/b][/color]
[color=#191970][b]hint[/b][/color] [color=#7A7A7A]"depends"[/color][color=#8B3E2F][b];[/b][/color]
[color=#191970][b]hint[/b][/color] [color=#7A7A7A]"on"[/color][color=#8B3E2F][b];[/b][/color]
[color=#191970][b]hint[/b][/color] [color=#7A7A7A]"what"[/color][color=#8B3E2F][b];[/b][/color]
[color=#191970][b]hint[/b][/color] [color=#7A7A7A]"the"[/color][color=#8B3E2F][b];[/b][/color]
[color=#191970][b]hint[/b][/color] [color=#7A7A7A]"code"[/color][color=#8B3E2F][b];[/b][/color]
[color=#191970][b]hint[/b][/color] [color=#7A7A7A]"is."[/color][color=#8B3E2F][b];[/b][/color]
[color=#191970][b]hint[/b][/color] [color=#7A7A7A]"Not"[/color][color=#8B3E2F][b];[/b][/color]
[color=#191970][b]hint[/b][/color] [color=#7A7A7A]"it's"[/color][color=#8B3E2F][b];[/b][/color]
[color=#191970][b]hint[/b][/color] [color=#7A7A7A]"lenght."[/color][color=#8B3E2F][b];[/b][/color][/color]

Made with KK's SQF to BBCode Converter

Edited by 654wak654

Share this post


Link to post
Share on other sites

If you have big and heavy scripts, preload them so they won't cause loading lag (highly unlikely anyway).

Anyway, it hardly matters if the script is 3kb or 30kb. It's more important what's inside. With bad coding (not styling) the 3kb script can be a lot worse than the 30kb.

Share this post


Link to post
Share on other sites

Code performance is a thing on its own. If you don't know it yet, here are some basic hints:

https://community.bistudio.com/wiki/Code_Optimisation

Basically what it comes down to is to make up your mind on how many operations will be performed executing your code. Example:

[color=#FF8040][color=#1874CD]_someArray[/color] [color=#8B3E2F][b]=[/b][/color] [color=#8B3E2F][b][[/b][/color][color=#8B3E2F][b]][/b][/color][color=#8B3E2F][b];[/b][/color]
[color=#8B3E2F][b]{[/b][/color]
   [color=#191970][b]if[/b][/color] [color=#8B3E2F][b]([/b][/color][color=#191970][b]isPlayer[/b][/color] [color=#000000]_x[/color] [color=#8B3E2F][b]&[/b][/color][color=#8B3E2F][b]&[/b][/color] [color=#191970][b]count[/b][/color] [color=#1874CD]_someArray[/color] [color=#8B3E2F][b]<[/b][/color] [color=#FF0000]10[/color][color=#8B3E2F][b])[/b][/color] [color=#8B3E2F][b]{[/b][/color]
       [color=#1874CD]_someArray[/color] [color=#191970][b]pushBack[/b][/color] [color=#000000]_x[/color][color=#8B3E2F][b];[/b][/color]
   [color=#8B3E2F][b]}[/b][/color][color=#8B3E2F][b];[/b][/color]
[color=#8B3E2F][b]}[/b][/color] [color=#191970][b]forEach[/b][/color] [color=#191970][b]playableUnits[/b][/color][color=#8B3E2F][b];[/b][/color][/color]

The above code is relatively inefficient as it counts the whole array on each iteration. Better would be:

[color=#FF8040][color=#1874CD]_someArray[/color] [color=#8B3E2F][b]=[/b][/color] [color=#8B3E2F][b][[/b][/color][color=#8B3E2F][b]][/b][/color][color=#8B3E2F][b];[/b][/color]
[color=#1874CD]_i[/color] [color=#8B3E2F][b]=[/b][/color] [color=#FF0000]0[/color][color=#8B3E2F][b];[/b][/color]
[color=#8B3E2F][b]{[/b][/color]
   [color=#191970][b]if[/b][/color] [color=#8B3E2F][b]([/b][/color][color=#191970][b]isPlayer[/b][/color] [color=#000000]_x[/color] [color=#8B3E2F][b]&[/b][/color][color=#8B3E2F][b]&[/b][/color] [color=#1874CD]_i[/color] [color=#8B3E2F][b]<[/b][/color] [color=#FF0000]10[/color][color=#8B3E2F][b])[/b][/color]
   [color=#8B3E2F][b]{[/b][/color]
       [color=#1874CD]_someArray[/color] [color=#191970][b]pushBack[/b][/color] [color=#000000]_x[/color][color=#8B3E2F][b];[/b][/color]
       [color=#1874CD]_i[/color] [color=#8B3E2F][b]=[/b][/color] [color=#1874CD]_i[/color] [color=#8B3E2F][b]+[/b][/color] [color=#FF0000]1[/color][color=#8B3E2F][b];[/b][/color]
   [color=#8B3E2F][b]}[/b][/color][color=#8B3E2F][b];[/b][/color]
[color=#8B3E2F][b]}[/b][/color] [color=#191970][b]forEach[/b][/color] [color=#191970][b]playableUnits[/b][/color][color=#8B3E2F][b];[/b][/color][/color]

Kudos to Killzone_Kid for his SQF to BBCode Converter.

This example shows that even a few more lines of code could mean better performance. Hence, saving a few lines of code does not automatically make it more efficient.

Have a nice Play!

Johnny

Edited by Heeeere's Johnny!

Share this post


Link to post
Share on other sites
Code performance is a thing on its own. If you don't know it yet' date=' here are some basic hints:

[url']https://community.bistudio.com/wiki/Code_Optimisation[/url]

Basically what it comes down to is to make up your mind on how many operations will be performed executing your code. Example:

[color=#FF8040][color=#1874CD]_someArray[/color] [color=#8B3E2F][b]=[/b][/color] [color=#8B3E2F][b][[/b][/color][color=#8B3E2F][b]][/b][/color][color=#8B3E2F][b];[/b][/color]
[color=#8B3E2F][b]{[/b][/color]
   [color=#191970][b]if[/b][/color] [color=#8B3E2F][b]([/b][/color][color=#191970][b]isPlayer[/b][/color] [color=#000000]_x[/color] [color=#8B3E2F][b]&[/b][/color][color=#8B3E2F][b]&[/b][/color] [color=#191970][b]count[/b][/color] [color=#1874CD]_someArray[/color] [color=#8B3E2F][b]<[/b][/color] [color=#FF0000]10[/color][color=#8B3E2F][b])[/b][/color] [color=#8B3E2F][b]{[/b][/color]
       [color=#1874CD]_someArray[/color] [color=#191970][b]pushBack[/b][/color] [color=#000000]_x[/color][color=#8B3E2F][b];[/b][/color]
   [color=#8B3E2F][b]}[/b][/color][color=#8B3E2F][b];[/b][/color]
[color=#8B3E2F][b]}[/b][/color] [color=#191970][b]forEach[/b][/color] [color=#191970][b]playableUnits[/b][/color][color=#8B3E2F][b];[/b][/color][/color]

The above code is relatively inefficient as it counts the whole array on each iteration. Better would be:

[color=#FF8040][color=#1874CD]_someArray[/color] [color=#8B3E2F][b]=[/b][/color] [color=#8B3E2F][b][[/b][/color][color=#8B3E2F][b]][/b][/color][color=#8B3E2F][b];[/b][/color]
[color=#1874CD]_i[/color] [color=#8B3E2F][b]=[/b][/color] [color=#FF0000]0[/color][color=#8B3E2F][b];[/b][/color]
[color=#8B3E2F][b]{[/b][/color]
   [color=#191970][b]if[/b][/color] [color=#8B3E2F][b]([/b][/color][color=#191970][b]isPlayer[/b][/color] [color=#000000]_x[/color] [color=#8B3E2F][b]&[/b][/color][color=#8B3E2F][b]&[/b][/color] [color=#1874CD]_i[/color] [color=#8B3E2F][b]<[/b][/color] [color=#FF0000]10[/color][color=#8B3E2F][b])[/b][/color]
   [color=#8B3E2F][b]{[/b][/color]
       [color=#1874CD]_someArray[/color] [color=#191970][b]pushBack[/b][/color] [color=#000000]_x[/color][color=#8B3E2F][b];[/b][/color]
       [color=#1874CD]_i[/color] [color=#8B3E2F][b]=[/b][/color] [color=#1874CD]_i[/color] [color=#8B3E2F][b]+[/b][/color] [color=#FF0000]1[/color][color=#8B3E2F][b];[/b][/color]
   [color=#8B3E2F][b]}[/b][/color][color=#8B3E2F][b];[/b][/color]
[color=#8B3E2F][b]}[/b][/color] [color=#191970][b]forEach[/b][/color] [color=#191970][b]playableUnits[/b][/color][color=#8B3E2F][b];[/b][/color][/color]

Kudos to Killzone_Kid for his SQF to BBCode Converter.

This example shows that even a few more lines of code could mean better performance. Hence, saving a few lines of code does not automatically make it more efficient.

Have a nice Play!

Johnny

Let's not forget that pushBack returns index of inserted element:

[color="#FF8040"][color="#1874CD"]_someArray[/color] [color="#8B3E2F"][b]=[/b][/color] [color="#8B3E2F"][b][[/b][/color][color="#8B3E2F"][b]][/b][/color][color="#8B3E2F"][b];[/b][/color]
[color="#8B3E2F"][b]{[/b][/color]
   [color="#191970"][b]if[/b][/color] [color="#8B3E2F"][b]([/b][/color][color="#191970"][b]isPlayer[/b][/color] [color="#000000"]_x[/color] [color="#8B3E2F"][b]&[/b][/color][color="#8B3E2F"][b]&[/b][/color] [color="#8B3E2F"][b]{[/b][/color][color="#1874CD"]_someArray[/color] [color="#191970"][b]pushBack[/b][/color] [color="#000000"]_x[/color] [color="#8B3E2F"][b]=[/b][/color][color="#8B3E2F"][b]=[/b][/color] [color="#FF0000"]9[/color][color="#8B3E2F"][b]}[/b][/color][color="#8B3E2F"][b])[/b][/color] [color="#191970"][b]exitWith[/b][/color] [color="#8B3E2F"][b]{[/b][/color][color="#8B3E2F"][b]}[/b][/color][color="#8B3E2F"][b];[/b][/color]
[color="#8B3E2F"][b]}[/b][/color] [color="#191970"][b]forEach[/b][/color] [color="#191970"][b]playableUnits[/b][/color][color="#8B3E2F"][b];[/b][/color][/color]

Made with KK's SQF to BBCode Converter

Will break as soon as you have 10 players in someArray.

Share this post


Link to post
Share on other sites

How you dare debunking my less-lines-not-equals-better-performance-argument?! xD

Of course you're right, just for the sake of the argument, I didn't take too much brain activity into this.

Share this post


Link to post
Share on other sites

onEachFrame {(getPos player) nearObjects ['All',20000];};

^ One line of hell

Also guys dont forget the semicolon ( ; ) at the end of each line of code or else it will lag the server into oblivion.

;)

_someArray = [];

{

if (isPlayer _x && {_someArray pushBack _x == 9}) exitWith {};

} forEach playableUnits;

nice!

Edited by MDCCLXXVI

Share this post


Link to post
Share on other sites

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now
Sign in to follow this  

×