Raised This Month: $12 Target: $400
 3% 

[TUT] Code Styling & Good Programming Habits


Post New Thread Reply   
 
Thread Tools Display Modes
Arkshine
AMX Mod X Plugin Approver
Join Date: Oct 2005
Old 01-18-2010 , 13:19   Re: [TUT] Code Styling & Good Programming Habits
Reply With Quote #61

Quote:
Originally Posted by Seta00 View Post
People still don't understand what I mean. The space I save using K&R indentation is filled with COMMENTS. No matter how good your code is indented, no matter how many lines you put between the logical blocks, if your code isn't commented, it's fucking harder to understand it.
I can't agree. You don't need comments if the code is intelligently written. It has also nothing to do with the style indentation.

A code which is written to be the simplest to read, like if you was reading english, and with each line can be understanding and checked without the need to read the whole code, doesn't need comments.
They are disadvantages like indicating what the user wanted to do but not what does really the code or comments can be wrong or not updated.
In such case, it would be more consistent to write a code which is easy to understand. So like coding intelligently, commenting in the same way would be more appropriate, only when necessary or like commenting at the start of the function to provide what does the function, explaining the params etc..

There are severals things you could do to improve the readability :

- Well named the variables ;
- Replacing expression by variables ;
- Replacing numeric values by constants or enums ;
- Replacing expression groups by functions ;
- Replacing arrays by structures ;
- Well named the function ;
- Avoiding repetition ;
- Saving the result in a var ;
- Well organized each part of the code ;
- Well presented the code ;
- Keeping the same style for the whole code ;
- etc, etc.

Using comments, yes, but it should be used with parsimony and placed intelligently.
__________________
Arkshine is offline
Seta00
The Seta00 user has crashed.
Join Date: Jan 2010
Location: Berlin
Old 01-18-2010 , 13:37   Re: [TUT] Code Styling & Good Programming Habits
Reply With Quote #62

I see your point, and I do apply these recommendations on general code, but my motivation here is mainly because optimization is one topic highly discussed in AMXx, since gaming is real-time; and when you optimize a code, you have to deal with loss of readability. There are lots of ways to optimize a function, and a bunch of them imply some loss of readability by using features not commonly used, removing unnecessary variables, creating cache variables, etc.
Seta00 is offline
Arkshine
AMX Mod X Plugin Approver
Join Date: Oct 2005
Old 01-18-2010 , 14:04   Re: [TUT] Code Styling & Good Programming Habits
Reply With Quote #63

Pointless and not worth searching to do the slightest optimization each line, you should start to find the easiest way to implement your algorithm and doing the major optimization. I would prefer the readability instead of a loss of readability because of a trivial optimization with no significant gain. At least for Pawn and this community, it's more appropriate to keep the way of the clarity. In most case the common optimizations doesn't break the readability.
__________________

Last edited by Arkshine; 01-19-2010 at 06:30.
Arkshine is offline
drekes
Veteran Member
Join Date: Jul 2009
Location: Vault 11
Old 05-02-2010 , 19:28   Re: [TUT] Code Styling & Good Programming Habits
Reply With Quote #64

Are these neccesary to get a plugin approved? Because i personally prefer this:
PHP Code:
    register_forward(FM_EmitSound"EmitSound"
instead of this:
PHP Code:
    register_forwardFM_EmitSound "EmitSound" 
And the capital letters and g_ in front of new variable confuse me.
Do i need to use these or is it just a suggestion?

and is this wrong?
PHP Code:
public something(id)
{
    
something_else(id)

Btw: This is the first language i learn so this may sound stupid.
__________________

Quote:
Originally Posted by nikhilgupta345 View Post
You're retarded.

Last edited by drekes; 05-02-2010 at 19:31.
drekes is offline
Send a message via MSN to drekes
wrecked_
Veteran Member
Join Date: Jan 2010
Location: New York (GMT-5)
Old 05-02-2010 , 20:09   Re: [TUT] Code Styling & Good Programming Habits
Reply With Quote #65

Not at all. Some people just find it easier to read if you use spaces. I personally use it because I checked this out back in January, but it's personal preference.
__________________
[ Paid Requests ]
DO NOT PM ME ABOUT BLOCKMAKER
NO PRIVATE SUPPORT
wrecked_ is offline
minimiller
Veteran Member
Join Date: Aug 2007
Location: United Kingdom
Old 05-02-2010 , 21:14   Re: [TUT] Code Styling & Good Programming Habits
Reply With Quote #66

if the code compiles and works as it should, its good enough to be aproved
allthough thats not an excuse to fuck over your indents
in refrence to your 'spaces' question, its personal preference
__________________
minimiller is offline
Send a message via MSN to minimiller
drekes
Veteran Member
Join Date: Jul 2009
Location: Vault 11
Old 05-02-2010 , 21:19   Re: [TUT] Code Styling & Good Programming Habits
Reply With Quote #67

my indents and whitespaces are good, it's just i find this
PHP Code:
if(get_pcvar_num(cvar)) 
easyer to read then this:
PHP Code:
if ( get_pcvar_num cvar ) ) 
__________________

Quote:
Originally Posted by nikhilgupta345 View Post
You're retarded.
drekes is offline
Send a message via MSN to drekes
wrecked_
Veteran Member
Join Date: Jan 2010
Location: New York (GMT-5)
Old 05-02-2010 , 21:24   Re: [TUT] Code Styling & Good Programming Habits
Reply With Quote #68

Most people don't put a space after if statements or functions. Alot of people (including myself) space like so:
Code:
if( condition( id ) == 3 ) {     do     {         this();     }     while( that() ); }

EDIT: I am a douchebag and only made this post to show people that I knew to put a semicolon after the while() in a do-while. I felt it necessary to edit this in because I am disappointed in myself.
__________________
[ Paid Requests ]
DO NOT PM ME ABOUT BLOCKMAKER
NO PRIVATE SUPPORT

Last edited by wrecked_; 04-21-2011 at 03:48.
wrecked_ is offline
drekes
Veteran Member
Join Date: Jul 2009
Location: Vault 11
Old 05-02-2010 , 21:27   Re: [TUT] Code Styling & Good Programming Habits
Reply With Quote #69

I still prefer it without so much spaces.
Especcialy with the ()
If got you like 3 ) i find it much easier like this
)))
then this
) ) )

Because it makes it easier to count, but that is my opponion
__________________

Quote:
Originally Posted by nikhilgupta345 View Post
You're retarded.
drekes is offline
Send a message via MSN to drekes
Kreation
Veteran Member
Join Date: Jan 2010
Location: Illinois
Old 05-02-2010 , 21:29   Re: [TUT] Code Styling & Good Programming Habits
Reply With Quote #70

Quote:
Originally Posted by drekes View Post
I still prefer it without so much spaces.
Especcialy with the ()
If got you like 3 ) i find it much easier like this
)))
then this
) ) )

Because it makes it easier to count, but that is my opponion
But ) ) ) is much much sexier than ))). The first one is like a hot fat chick, and the second one is like a ugly fat chick.
__________________
Hi.
Kreation 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 18:35.


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