Raised This Month: $22 Target: $400

[TUT] Code Styling & Good Programming Habits

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

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.

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.

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.

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.

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.

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?
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

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 10:52.

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