Raised This Month: $7 Target: $400
 1% 

[TUT] Code Styling & Good Programming Habits


Post New Thread Reply   
 
Thread Tools Display Modes
Hawk552
AMX Mod X Moderator
Join Date: Aug 2005
Old 02-08-2009 , 14:12   Re: [TUT] Code Styling
Reply With Quote #11

Quote:
Originally Posted by danielkza View Post
I believe Pawn is the kind of language that needs HN the most. Not having explicit typing makes it impossible to people not familiar with the code to find out what a cell actually holds without reading through a chunk of it. That information should be present in the variable itself, not in how it's used.
I would argue that since "types" are really just tags which can be changed or detagged, HN is irrelevant. Bools especially are a meaningless tag which usually create more confusion than help. The biggest problem is that almost all function returns are untagged cells rather than bools, which defeats the purpose of them. In C/C++, bools are used because they use much less memory (1 byte I believe). Since this doesn't reduce memory usage in Pawn, there's no reason to tag them as such.

This is obviously problematic because you either have to never use bools or just not tag them as being bools. This is a bad precedent because you now have similar problems with floats. For example, in a set_task() call, the third parameter is a standard cell:

PHP Code:
set_task Float:time, const function[], id 0parameter[]=""len 0flags[]=""repeat 
What if you want to send a float as the id? You're going to have to detag it, then retag it once it gets there. You can get into a mess with stuff like this at other times too. What if you have to pass a string as an array separated by delimiters (i.e. "25 1 91 83 4")? Is it really a string, or is a really an array?

I used to use HN and I thought it was great for a few months. Some of my scripts are still using it, actually. But I really started turning away from it when I began developing APIs. Its major downfall, as far as I'm concerned, is that it discourages good variable naming. For example, I might use "i" as an iterator but according to HN it might be "iI". What if it's a global pointer to a handle for a null terminated string? As crazy as that sounds, I've had things like that before. The tag would look something like "g_phsz" which is just ridiculous and confusing. If you decide not to tag that as such, you are inconsistent. That's why I suggest only tagging globals and pointers.

Quote:
Originally Posted by danielkza View Post
And as there are no pointers in Pawn, I prefer to use the word 'handle', but both are perfectly fine as far as you're consistent in your code.
Right, but it's being passed into a function that calls it a pointer. The most important thing, though, is to differentiate it. Calling it a handle makes sense but may be confusing when you look at things like SQLx which call everything handles.

The point is, though, that if you detag a pointer, you're going to get useless data. If you detag a bool, you're going to get a cell which is just as useful. That's more what I was talking about.

Quote:
Originally Posted by danielkza View Post
Also, I prefer to include type prefixes even in global vars:
g_iUseCount, g_szWebsite, etc
Yes, I included those in my HN explanation.

Quote:
Originally Posted by danielkza View Post
2 prefixes that you might see in some code (including mine), not included in your post:
h for handles: Handle:hSQLConnection
v for vectors: Float:vPlayerOrigin[3]
Agreed. "v" is non-standard so I'll add it but I'll add a note saying it's not part of HN.

Quote:
Originally Posted by danielkza View Post
And finally, I use the 'doxygen style' to document functions possibly used a lot, or that may interest other authors in case there is a sub-plugin system or anything like that:
PHP Code:
/**
 * Print command activity.

 * @param id           Calling admin's id
 * @param key[]      Language key
 * @param any:...    Arguments to the format function (w/out the id/lang key)
 
 * @return    True on success, False on fail
**/

stock UTIL_PrintActivity(const id, const szLangKey[],any:...) 
Agreed again. I'll add that too.

Quote:
Originally Posted by hleV View Post
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?
You would call it g_pTest according to HN. My way of tagging it is just with p_ (so it would be p_Test) because you are almost never going to use a pointer that isn't global.

Technically, the only correct tag would be g_iTest, but since the data it stores is useless without first passing it into get_pcvar_<x>(), it's a bad idea to tag it as what it actually is.
__________________
Hawk552 is offline
Send a message via AIM to Hawk552
Hawk552
AMX Mod X Moderator
Join Date: Aug 2005
Old 02-08-2009 , 14:15   Re: [TUT] Code Styling
Reply With Quote #12

Quote:
Originally Posted by XxAvalanchexX View Post
I thought that I read somewhere that it is more efficient for the assembly to use one "new" statement for all of your vars (where applicable), as opposed to a different "new" for each one. Is there any truth to that?
I don't think so just because of the way the assembly is mapped out. It can't declare a bunch of variables in sequence in a way that would be different from one at a time. Either way, zeroing it is the most expensive part by far and that still happens even when condensing it.
__________________
Hawk552 is offline
Send a message via AIM to Hawk552
XxAvalanchexX
Veteran Member
Join Date: Oct 2004
Location: abort73.com
Old 02-08-2009 , 22:20   Re: [TUT] Code Styling
Reply With Quote #13

Quote:
Originally Posted by Hawk552 View Post
I don't think so just because of the way the assembly is mapped out. It can't declare a bunch of variables in sequence in a way that would be different from one at a time. Either way, zeroing it is the most expensive part by far and that still happens even when condensing it.
I see. Cool, thanks!
__________________
No longer around. Thanks your support, everyone! As always:
THIS ONES FOR YOU
3000 PTS
XxAvalanchexX is offline
anakin_cstrike
Veteran Member
Join Date: Nov 2007
Location: Romania
Old 02-09-2009 , 10:54   Re: [TUT] Code Styling
Reply With Quote #14

Very nice thread. I agree with everything.

By the way, i have a question about semicolons. I know it has been discussed before, but i don't understand what is actually doing.
Let's say, i have a code with no identition wich ignores you advices (tabs, spaces, brackets, etc.. ). If i put "#pragma semicolon 1" it will automatically make identitation or what ? sorry for asking without testing.
__________________

anakin_cstrike is offline
AntiBots
Veteran Member
Join Date: May 2008
Location: Brazil
Old 02-09-2009 , 11:00   Re: [TUT] Code Styling
Reply With Quote #15

Quote:
Originally Posted by anakin_cstrike View Post
Very nice thread. I agree with everything.

By the way, i have a question about semicolons. I know it has been discussed before, but i don't understand what is actually doing.
Let's say, i have a code with no identition wich ignores you advices (tabs, spaces, brackets, etc.. ). If i put "#pragma semicolon 1" it will automatically make identitation or what ? sorry for asking without testing.
http://en.wikipedia.org/wiki/Semicolon#Computing_usage

Well if semicolon is in 1

You need to close all lines with ;

Example without semicolon
PHP Code:
if( is_user_alive(id) )
{
         
set_pev(idpev_maxspeed300.0)
         
set_pev(idpev_gravity0.45)

with semicolon
PHP Code:
if( is_user_alive(id) )
{
         
set_pev(idpev_maxspeed300.0); set_pev(idpev_gravity0.45);

or like this:
PHP Code:
if( is_user_alive(id) )
{
         
set_pev(idpev_maxspeed300.0); 
         
set_pev(idpev_gravity0.45);

__________________
AntiBots is offline
Send a message via ICQ to AntiBots Send a message via MSN to AntiBots Send a message via Skype™ to AntiBots
anakin_cstrike
Veteran Member
Join Date: Nov 2007
Location: Romania
Old 02-09-2009 , 11:03   Re: [TUT] Code Styling
Reply With Quote #16

Ahh...thanks.
PS: it closes automatically or i have to do that?
__________________

anakin_cstrike is offline
AntiBots
Veteran Member
Join Date: May 2008
Location: Brazil
Old 02-09-2009 , 11:06   Re: [TUT] Code Styling
Reply With Quote #17

Quote:
Originally Posted by anakin_cstrike View Post
Ahh...thanks.
PS: it closes automatically or i have to do that?

You need to closes all lines execpts "if( )" "{ }" and blank lines,
others like params you need to put the ( ; ) to Closes.

EDIT: For new, static lines you need to...
You dont new for lines that you open { }

Example

public somethin(x) // Here no.
{
//
}
__________________

Last edited by AntiBots; 02-09-2009 at 11:09.
AntiBots is offline
Send a message via ICQ to AntiBots Send a message via MSN to AntiBots Send a message via Skype™ to AntiBots
anakin_cstrike
Veteran Member
Join Date: Nov 2007
Location: Romania
Old 02-09-2009 , 11:14   Re: [TUT] Code Styling
Reply With Quote #18

I know you're anti - bots, but i know that. I've asked something else.
However, i do understand know.
__________________

anakin_cstrike is offline
Hawk552
AMX Mod X Moderator
Join Date: Aug 2005
Old 02-09-2009 , 11:15   Re: [TUT] Code Styling
Reply With Quote #19

Quote:
Originally Posted by anakin_cstrike View Post
I know you're anti - bots, but i know that. I've asked something else.
However, i do understand know.
All #pragma semicolon 1 does is throw an error whenever you missed a semicolon.
__________________
Hawk552 is offline
Send a message via AIM to Hawk552
Exolent[jNr]
Veteran Member
Join Date: Feb 2007
Location: Tennessee
Old 02-09-2009 , 11:57   Re: [TUT] Code Styling
Reply With Quote #20

Quote:
Originally Posted by AntiBots View Post
PHP Code:
if( is_user_alive(id) )
{
         
set_pev(idpev_maxspeed300.0); set_pev(idpev_gravity0.45);

Don't put 2 statements on one line.
__________________
No private work or selling mods.
Quote:
Originally Posted by xPaw View Post
I love you exolent!
Exolent[jNr] 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:06.


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