AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   Proper indenting (https://forums.alliedmods.net/showthread.php?t=10767)

v3x 02-28-2005 15:35

Proper indenting
 
Ok, how do I "properly indent" my code? Apparently that is half of the reason my last plugin got trashed, I was using Avalanche's online auto-indenter too..

xeroblood 02-28-2005 16:23

Well, when you "indent", you're essentially spacing a line of text, and making a visual reference to the depth of the code block. This makes it easier to read (not only for you, but for others too).

Commonly you would use 4 spaces (or 2 or 6 or 8, whatever you like best) or even the tab key when you indent a line of text, but which ever you choose, stick with it, every where u indent use the same amount of spaces (double the spaces if the depth doubles, etc..)..

You would always indent any "Block" of code that is contained within the Curly-Brackets { and }
A "Block" of code is essentially any code that would be surrounded by { and }, like a function contains its own block of code.
This rule applies anywhere you *would* use curly-brackets (even if you dont use them due to single-statements).

An example would better describe it:

Code:
stock MyFunction() {     // This line is spaced 4 times     if( this == that )     {         // This line is spaced 8 times     }     return }

Notice the line spacing is "indented" within each set of curly brackets...

Now, some people have a different coding style, thats okay, whichever works for you, but be Consistent!!
Some people may do it like this:

Code:
stock MyFunction() {     // This line is spaced 4 times     if( this == that ) {         // This line is spaced 8 times     }     return }

That's cool too...But notice each line within the surrounding code-block is indented consistently..

Also, one other thing to note, is when the curly-brackets are omitted, which is perfectly legal for blocks containing only a single code statement..
As in:
Code:
stock MyFunction() {     // This line is spaced 4 times     if( this == that )         // This line is spaced 8 times and is part of the IF Block     else         // This line is spaced 8 times and is part of the ELSE Block     // This line is spaced 4 times and is part of MyFunction Block..     return }

deep example:
Code:
stock MyFunction() {     // This line is spaced 4 times     if( this == that )     {         // This line is spaced 8 times         if( 2B || !2B )         {             // This line is spaced 12 times             if( them == they )             {                 // This line is spaced 16 times             }           }     }     return }

Remember, any block containing more than a single statement requires curly-brackets...

I hope that helps a bit..

XxAvalanchexX 02-28-2005 16:24

My auto-indenter doesn't work with all coding styles, mainly just mine and one other.

v3x 02-28-2005 16:32

Oh, I pretty much get it now..

So all I gotta do is be consistent with what I'm indenting.. For example:

Code:
public someFunction(id)     { // 4 spaces         if(blah == blah) // 8 spaces         { // 8 spaces           // do something - 10 spaces         } // 8 spaces     } // 4 spaces public someFunction2(id)     { // 4 spaces         if(blah == blah) // 8 spaces         { // 8 spaces           // do something - 10 spaces         } // 8 spaces     } // 4 spaces // Not saying that I'm going to indent like that.

xeroblood 02-28-2005 16:47

Ermm, more like this:
Code:
public someFunction(id) { // 0 spaces     if(blah == blah) // 4 spaces     { // 4 spaces         // do something -> 8 spaces     } // 4 spaces } // 0 spaces public someFunction2(id) { // 0 spaces     if(blah == blah) // 4 spaces     { // 4 spaces         // do something -> 8 spaces     } // 4 spaces } // 0 spaces

Try to keep the curly-brackets at the same depth as the block itself when using a curly-bracket on its own line..

v3x 02-28-2005 16:53

Oh, okay... I think I've figured it out now :). Thanks.

Sp4rt4n 02-28-2005 17:37

:/ is this clean code or do i have to work on it a little?

Code:
public checkPing(param[]) {     new id = param[0]     if ((get_user_flags(id) & ADMIN_IMMUNITY) || (get_user_flags(id) & ADMIN_RESERVATION))     {         remove_task(id)         client_print(id, print_chat, "[HPK] Ping checking disabled because of immunity..")         return PLUGIN_CONTINUE     }     new ping, l     get_user_ping(id, ping, l)     if (ping < ping_max)         ++numtests[id]     else     if (numtests[id] > 0) --numtests[id]     if (numtests[id] > ping_times)         kickPlayer(id)     return PLUGIN_CONTINUE }

(sample from my high ping kicker)

v3x 02-28-2005 17:54

Well, what I do is stick it in the compiler to test for loose-indentations :).

xeroblood 02-28-2005 18:21

Quote:

Originally Posted by v3x
Well, what I do is stick it in the compiler to test for loose-indentations :).

That works!


Quote:

Originally Posted by Sp4rt4n
:/ is this clean code or do i have to work on it a little?

Code:
public checkPing(param[]) {     new id = param[0]     if ((get_user_flags(id) & ADMIN_IMMUNITY) || (get_user_flags(id) & ADMIN_RESERVATION))     {         remove_task(id)         client_print(id, print_chat, "[HPK] Ping checking disabled because of immunity..")         return PLUGIN_CONTINUE     }     new ping, l     get_user_ping(id, ping, l)     if (ping < ping_max)         ++numtests[id]     else     if (numtests[id] > 0) --numtests[id]     if (numtests[id] > ping_times)         kickPlayer(id)     return PLUGIN_CONTINUE }

(sample from my high ping kicker)

Beautiful!

But to shorten that one long if statement (if you want) you could include <amxmisc> and use the stock access() function:

Code:
if ((access(id, ADMIN_IMMUNITY) || (access(id, ADMIN_RESERVATION))

Sp4rt4n 02-28-2005 18:47

ah, thank you... ill keep that in mind


All times are GMT -4. The time now is 14:07.

Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.