Raised This Month: $81 Target: $400
 20% 

[TUT] Code Styling & Good Programming Habits


Post New Thread Reply   
 
Thread Tools Display Modes
Spunky
Senior Member
Join Date: May 2008
Location: Orlando, Fl.
Old 02-23-2009 , 03:17   Re: [TUT] Code Styling
Reply With Quote #31

Switching about every two weeks can throw you off though. I program in C++ for two weeks, then switch to Pawn for two weeks, then switch back. It's not easy because I usually forget like half of what I learned about AMXX (since that's the only thing I would ever use such a painfully simple language like Pawn for) the last time around when I finally come back, even down to basic stuff. x_x
Spunky is offline
Send a message via AIM to Spunky
Hawk552
AMX Mod X Moderator
Join Date: Aug 2005
Old 09-14-2009 , 11:43   Re: [TUT] Code Styling
Reply With Quote #32

I finally got around to writing a plugin that uses this style. You can see it here:
http://forums.alliedmods.net/showthread.php?t=102839

I also added it to the conclusion.
__________________
Hawk552 is offline
Send a message via AIM to Hawk552
GordonFreeman (RU)
Veteran Member
Join Date: Jan 2010
Location: Uzbekistan
Old 01-06-2010 , 02:52   Re: [TUT] Code Styling & Good Programming Habits
Reply With Quote #33

Thanks!
GordonFreeman (RU) is offline
Seta00
The Seta00 user has crashed.
Join Date: Jan 2010
Location: Berlin
Old 01-15-2010 , 11:06   Re: [TUT] Code Styling & Good Programming Habits
Reply With Quote #34

Nice tut, only thing I disagree is with the new line before opening bracket.
If you're a programmer, you can easily recognize identifiers that imply a code block, and the new lines before every block allow for less code to fit in one page.
Seta00 is offline
SnoW
Veteran Member
Join Date: Oct 2008
Location: Finland WisdomNuggets: 8
Old 01-15-2010 , 16:22   Re: [TUT] Code Styling & Good Programming Habits
Reply With Quote #35

Quote:
Originally Posted by Seta00 View Post
Nice tut, only thing I disagree is with the new line before opening bracket.
If you're a programmer, you can easily recognize identifiers that imply a code block, and the new lines before every block allow for less code to fit in one page.
Your reasoning is just unbelievable. If you wanted to have all the code in one line you could, but that's not you want. You want the code to look decent, readable and not ugly and that's why you line break. As simple as that.

Hawk, this is a sticky thread and a lot of people read this, so better to fix.
PHP Code:
#define MyNewIsUserAlive( %1 ) is_user_alive( %1 ) 
SnoW is offline
Send a message via MSN to SnoW
Bugsy
AMX Mod X Moderator
Join Date: Feb 2005
Location: NJ, USA
Old 01-15-2010 , 22:55   Re: [TUT] Code Styling & Good Programming Habits
Reply With Quote #36

Quote:
Originally Posted by Seta00 View Post
Nice tut, only thing I disagree is with the new line before opening bracket.
If you're a programmer, you can easily recognize identifiers that imply a code block, and the new lines before every block allow for less code to fit in one page.
Readability is far more important than line count. If you are worried about fitting more code on one page, reduce your font size . Furthermore, code looks ugly when there's not a new-line before an opening bracket. Yes, I can look at any code and still know what is going on but it is a hell of a lot easier to read with proper line-breaks, spacing, and indentation.


PHP Code:
public Uglyid ) {

}

public 
Niceid )
{


__________________
Bugsy is offline
Hawk552
AMX Mod X Moderator
Join Date: Aug 2005
Old 01-16-2010 , 01:59   Re: [TUT] Code Styling & Good Programming Habits
Reply With Quote #37

Quote:
Originally Posted by SnoW View Post
Your reasoning is just unbelievable. If you wanted to have all the code in one line you could, but that's not you want. You want the code to look decent, readable and not ugly and that's why you line break. As simple as that.

Hawk, this is a sticky thread and a lot of people read this, so better to fix.
PHP Code:
#define MyNewIsUserAlive( %1 ) is_user_alive( %1 ) 
Fixed, thanks.
__________________
Hawk552 is offline
Send a message via AIM to Hawk552
joaquimandrade
Veteran Member
Join Date: Dec 2008
Location: Portugal
Old 01-16-2010 , 02:48   Re: [TUT] Code Styling
Reply With Quote #38

Quote:
Originally Posted by hleV View Post
Nice.

I myself prefer HN.

Didn't knew that this was possible:
Code:
MyFunction( id ) if( is_user_alive( id ) ) { client_print( 0, print_chat, "We are now killing %d", id ) user_kill( id ) }

o_O

About function names... I'm still not decided, but currently I write only the first letter (or letters, if function's type title is made from 2 words, like forward - fw). Like eDeathMsg() for event and cSay for command. Also sPrint() for stock.

BTW is the g_Test pointer (although danielkza said there are no pointers in Pawn) or a normal integer?
Code:
g_Test = register_cvar("amx_test", "1");

I was used to treat it as pointers (so g_Test was g_pTest) but then I realized I have no idea what pointer really is so now I treat is as normal integer (g_Test is now g_iTest). Which one is more correct?
There is some confusion in this old post but for those who read it and don't understand why is pointer mentioned on it and what does pointers have to do with pawn:

In pawn there isn't the concept "pointer" (a variable which value is an address and that, with features of the language, can be used to perform operations on/with the data that is located at that address).

What is happening here is that register_cvar returns an address that in a language like C++ can be used as a pointer but in Pawn it can only be provided to a native in C++ so that it can be used as a pointer. So g_Test holds an address but that isn't enough to make it a pointer because pawn doesn't has pointers.
joaquimandrade is offline
Seta00
The Seta00 user has crashed.
Join Date: Jan 2010
Location: Berlin
Old 01-16-2010 , 13:27   Re: [TUT] Code Styling & Good Programming Habits
Reply With Quote #39

Quote:
Originally Posted by SnoW View Post
Your reasoning is just unbelievable. If you wanted to have all the code in one line you could, but that's not you want. You want the code to look decent, readable and not ugly and that's why you line break. As simple as that.
You didn't understand what I mean, I don't want 1-line code.

Quote:
Originally Posted by Bugsy View Post
Readability is far more important than line count. If you are worried about fitting more code on one page, reduce your font size . Furthermore, code looks ugly when there's not a new-line before an opening bracket. Yes, I can look at any code and still know what is going on but it is a hell of a lot easier to read with proper line-breaks, spacing, and indentation.
Having the opening bracket in the same line of the "code block starter" statement doesn't interfer with readability. Think of you reading some code, when you see an "if" or a "do" or a "while" or anything that imply a code block, you already know that there's a code block coming, and indentation makes that more explicit. But, in the other hand, you can't predict when the code block will end, that's the reason to put the closing bracket alone in one line. More code fitting in the page is for consistency, is way better to have functions that fit in one page, so you don't need to scroll up and down following the execution path.
Seta00 is offline
Bugsy
AMX Mod X Moderator
Join Date: Feb 2005
Location: NJ, USA
Old 01-16-2010 , 13:50   Re: [TUT] Code Styling & Good Programming Habits
Reply With Quote #40

Quote:
Originally Posted by Seta00 View Post
You didn't understand what I mean, I don't want 1-line code.

Having the opening bracket in the same line of the "code block starter" statement doesn't interfer with readability. Think of you reading some code, when you see an "if" or a "do" or a "while" or anything that imply a code block, you already know that there's a code block coming, and indentation makes that more explicit. But, in the other hand, you can't predict when the code block will end, that's the reason to put the closing bracket alone in one line. More code fitting in the page is for consistency, is way better to have functions that fit in one page, so you don't need to scroll up and down following the execution path.
You can explain it to me any way you want, I personally think putting the opening bracket on a new line looks much nicer. Do you put an opening bracket on the same line as a for loop and a switch too?

PHP Code:
for ( new i++ ) {

switch ( 
Val ) { 
__________________
Bugsy is offline
Reply


Thread Tools
Display Modes

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 05:31.


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