Raised This Month: $ Target: $400
 0% 

Proper indenting


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
v3x
Veteran Member
Join Date: Oct 2004
Location: US
Old 02-28-2005 , 15:35   Proper indenting
Reply With Quote #1

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..
__________________
What am I doing these days? Well, I run my own Rust server. It's heavily modded. If you'd like to join, the ip is 167.114.101.67:28116

I also created a website called Rust Tools. It will calculate and tell you the raw amounts of resources needed to craft items.
v3x is offline
xeroblood
BANNED
Join Date: Mar 2004
Location: Toronto, Canada
Old 02-28-2005 , 16:23  
Reply With Quote #2

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..
xeroblood is offline
Send a message via MSN to xeroblood
XxAvalanchexX
Veteran Member
Join Date: Oct 2004
Location: abort73.com
Old 02-28-2005 , 16:24  
Reply With Quote #3

My auto-indenter doesn't work with all coding styles, mainly just mine and one other.
__________________
No longer around. Thanks your support, everyone! As always:
THIS ONES FOR YOU
3000 PTS
XxAvalanchexX is offline
v3x
Veteran Member
Join Date: Oct 2004
Location: US
Old 02-28-2005 , 16:32  
Reply With Quote #4

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.
__________________
What am I doing these days? Well, I run my own Rust server. It's heavily modded. If you'd like to join, the ip is 167.114.101.67:28116

I also created a website called Rust Tools. It will calculate and tell you the raw amounts of resources needed to craft items.
v3x is offline
xeroblood
BANNED
Join Date: Mar 2004
Location: Toronto, Canada
Old 02-28-2005 , 16:47  
Reply With Quote #5

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..
xeroblood is offline
Send a message via MSN to xeroblood
v3x
Veteran Member
Join Date: Oct 2004
Location: US
Old 02-28-2005 , 16:53  
Reply With Quote #6

Oh, okay... I think I've figured it out now . Thanks.
__________________
What am I doing these days? Well, I run my own Rust server. It's heavily modded. If you'd like to join, the ip is 167.114.101.67:28116

I also created a website called Rust Tools. It will calculate and tell you the raw amounts of resources needed to craft items.
v3x is offline
Sp4rt4n
Senior Member
Join Date: Dec 2004
Old 02-28-2005 , 17:37  
Reply With Quote #7

:/ 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)
Sp4rt4n is offline
Send a message via MSN to Sp4rt4n
v3x
Veteran Member
Join Date: Oct 2004
Location: US
Old 02-28-2005 , 17:54  
Reply With Quote #8

Well, what I do is stick it in the compiler to test for loose-indentations .
__________________
What am I doing these days? Well, I run my own Rust server. It's heavily modded. If you'd like to join, the ip is 167.114.101.67:28116

I also created a website called Rust Tools. It will calculate and tell you the raw amounts of resources needed to craft items.
v3x is offline
xeroblood
BANNED
Join Date: Mar 2004
Location: Toronto, Canada
Old 02-28-2005 , 18:21  
Reply With Quote #9

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))
xeroblood is offline
Send a message via MSN to xeroblood
Sp4rt4n
Senior Member
Join Date: Dec 2004
Old 02-28-2005 , 18:47  
Reply With Quote #10

ah, thank you... ill keep that in mind
Sp4rt4n is offline
Send a message via MSN to Sp4rt4n
Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


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


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