Coding style
#1

Hello
I want to know a "pro" coding style (convention?) since I am rewriting my entire gamemode, fixing things and optimizing my code.
I was been using the Allman identiation style and an "ugly" name for the variables, so I decided to change every name of the variables, using this time the "Hungarian notation" (Am I right?), because it seems to be clearer and easier to understand and with Notepad++ easier to find the variable names.
My old style:
PHP код:
new Last_Shot[MAX_PLAYERS]; 
New style I want to use
PHP код:
gTCSinceLastShot[MAX_PLAYERS]; 
Where gTCSince means GetTickCount() since the last shot.


I think it is a good idea using capital letter for constants but I am really confused since I started to learn Java, where the classes starts with a uppercase letter, and unlike pawn and my coding style, the methods always start with a lower case letter(except the constructor method)
Can you recommend me a very good coding style for PAWN?
I have read this https://en.wikipedia.org/wiki/Hungar...#Disadvantages and some SA-MP members don't like this convention, why?
PD: I definitely won't change my Allman identiation style.
Reply
#2

https://en.wikipedia.org/wiki/Hungarian_notation

The main point of Hungarian Notation is that you know what the variable's scope, type and purpose is from its name. For example, the variable you mentioned would be something like g_iPlayerTickSinceLastShot[MAX_PLAYERS];

the g_ shows it's a global variable, the 'i' prefix shows it's an integer, and then you have a CLEAR name. The thing you missed out was the 'Player' word. I always stress this to anyone. Take this example:

pawn Код:
new Float:health;
What's wrong here? Well, we know it's going to store a health value, but the health of what? I always use lower camel case for local variables (as per H. notation) and make sure to include the reference in the variable name, so:

pawn Код:
new Float:fVehicleHealth;
The 'f' denotes that it is a float, and you can clearly tell from the variable name that it stores the vehicle's health. You could even go a step further and be more specific and call it 'fCurrentVehicleHealth'.

I would steer clear of over-using abbreviations like 'TC' for tick count, just use 'Tick(s)'. You don't want to be looking at a script and think 'wtf is TC' and have to Ctrl+F all over the place. Abbreviations should only be used if they can't be misunderstood, for example I use 'Cur' instead of Current, 'IDX' for index (as many do) and so on.

Also, on a related note, try and keep variable names short. It's not fun typing 'PlayerTimeSinceLastConnectionToServer' 50 times.

Regarding the Allman indentation style: I don't see why you wouldn't use it. I hate K&R (bracket on the same line). It looks messy.
Reply
#3

There is a difference between Systems Hungarian and Apps Hungarian. Systems Hungarian encodes the variable's type in its name (i.e. i for integer, f for float, the ever notorious sz for string, etc). Frankly this is utterly stupid, since a variable's type can and should be deducted from its name. I don't need "sz" to tell me that "name" is a string. Apps Hungarian on the other hand encodes the purpose of the variable, like g for global, and that is of course a lot more useful.

There are a few more conventions I tend to stick to:
  • All constants are written in ALL_CAPS, with underscores. That includes constants defined with #define and const, but also names of enumerators and the enumerator specifiers themselves as these are all also constants.
  • Variable names always start with a lowercase letter and use lowerCamelCase, no underscores.
  • Function names always start with an uppercase letter and use UpperCamelCase, no underscores.
  • A function does something and must therefore always include a verb in its name (get, set, show, hide, create, destroy, etc).
  • No abbreviations of any kind in variable or function names unless the abbreviation is well known.
Reply
#4

Thank you for the information
Reply
#5

"pro code" is your own style, I don't get why would people learn to code how other people code, you just make it harder for yourself to code.

Want to know how the real master codes ? In Assembly. I don't always use that language, but when I do, chicks can't stop from hiring me
Reply
#6

"Pro-style" is only how you want to do it. I prefer the standards, basically what Vince said.
Quote:
Originally Posted by Vince
Посмотреть сообщение
There is a difference between Systems Hungarian and Apps Hungarian. Systems Hungarian encodes the variable's type in its name (i.e. i for integer, f for float, the ever notorious sz for string, etc). Frankly this is utterly stupid, since a variable's type can and should be deducted from its name. I don't need "sz" to tell me that "name" is a string. Apps Hungarian on the other hand encodes the purpose of the variable, like g for global, and that is of course a lot more useful.

There are a few more conventions I tend to stick to:
  • All constants are written in ALL_CAPS, with underscores. That includes constants defined with #define and const, but also names of enumerators and the enumerator specifiers themselves as these are all also constants.
  • Variable names always start with a lowercase letter and use lowerCamelCase, no underscores.
  • Function names always start with an uppercase letter and use UpperCamelCase, no underscores.
  • A function does something and must therefore always include a verb in its name (get, set, show, hide, create, destroy, etc).
  • No abbreviations of any kind in variable or function names unless the abbreviation is well known.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)