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

Code styles - prefered, readability & performance


Post New Thread Reply   
 
Thread Tools Display Modes
xerox8521
Senior Member
Join Date: Sep 2011
Old 10-29-2016 , 12:21   Re: Code styles - prefered, readability & performance
Reply With Quote #11

each curly bracket belongs on its own line. It IMHO looks ugly when you write like
PHP Code:
if(cond) { 
as it decreases the readabililty because you cannot clearly say which { belongs to its closing counter part. For some Prop names i tend to use the prop names itself as variable like m_iAmmo or m_hActiveWeapon. Also for the example i would check the convar first before checking if it is an ingame client because why bother if it is a client from ingame or not if it is disabled anyway
xerox8521 is offline
DarkDeviL
SourceMod Moderator
Join Date: Apr 2012
Old 10-29-2016 , 15:43   Re: Code styles - prefered, readability & performance
Reply With Quote #12

Quote:
Originally Posted by xerox8521 View Post
each curly bracket belongs on its own line. It IMHO looks ugly when you write like
PHP Code:
if(cond) { 
as it decreases the readabililty because you cannot clearly say which { belongs to its closing counter part. For some Prop names i tend to use the prop names itself as variable like m_iAmmo or m_hActiveWeapon. Also for the example i would check the convar first before checking if it is an ingame client because why bother if it is a client from ingame or not if it is disabled anyway
It does indeed look ugly when you are wasting an entire line on a single opening bracket, which you can already tell from if/for/while that it is here the code is starting.

I tend to agree with #3 and #4.

Speaking of ugly things, I also find one-liner messages on forums being quite ugly.

If you are currently working on a project, even if it uses the opposite of what you feel for yourself, then follow that project's current coding style in order to keep the coding style of that project identical everywhere.
__________________
Mostly known as "DarkDeviL".

Dropbox FastDL: Public folder will no longer work after March 15, 2017!
For more info, see the [SRCDS Thread], or the [HLDS Thread].
DarkDeviL is offline
KissLick
Veteran Member
Join Date: Nov 2012
Location: void
Old 10-29-2016 , 16:25   Re: Code styles - prefered, readability & performance
Reply With Quote #13

Quote:
Originally Posted by arne1288 View Post
If you are currently working on a project, even if it uses the opposite of what you feel for yourself, then follow that project's current coding style in order to keep the coding style of that project identical everywhere.
+2
__________________
Plugins: TeamGames
Includes: Menu stocks, ColorVariables, DownloadTableConfig

> No help through PM, make a topic.
KissLick is offline
Drixevel
AlliedModders Donor
Join Date: Sep 2009
Location: Somewhere headbangin'
Old 10-29-2016 , 17:19   Re: Code styles - prefered, readability & performance
Reply With Quote #14

I'm probably one of the only coders that uses lines for no reason while saving lines for no reason since I use brackets for every function, just looks cleaner to me.

Code:
public void somefunction()
{
	if (somecheck || someothercheck)
	{
		return value;
	}

	if (notsomecheck)
	{
		return value;
	}

	//do actual stuff
}
Drixevel is offline
Deathknife
Senior Member
Join Date: Aug 2014
Old 10-29-2016 , 17:58   Re: Code styles - prefered, readability & performance
Reply With Quote #15

Quote:
Originally Posted by xerox8521 View Post
each curly bracket belongs on its own line. It IMHO looks ugly when you write like
PHP Code:
if(cond) { 
as it decreases the readabililty because you cannot clearly say which { belongs to its closing counter part. For some Prop names i tend to use the prop names itself as variable like m_iAmmo or m_hActiveWeapon. Also for the example i would check the convar first before checking if it is an ingame client because why bother if it is a client from ingame or not if it is disabled anyway
{ on each line looks weird. With indentation it's really easy to identify which { belongs to which }
__________________
Deathknife is offline
klippy
AlliedModders Donor
Join Date: May 2013
Location: Serbia
Old 10-29-2016 , 18:23   Re: Code styles - prefered, readability & performance
Reply With Quote #16

I like putting { on new line, but not in JavaScript. Oh boy, do those lambdas look ugly with such coding style.

Anyway, arne1288 gave you the best answer - no coding style is the best, just be consistent.

Last edited by klippy; 10-29-2016 at 18:24.
klippy is offline
hleV
Veteran Member
Join Date: Mar 2007
Location: Lithuania
Old 10-30-2016 , 06:13   Re: Code styles - prefered, readability & performance
Reply With Quote #17

For me personally
PHP Code:
condition
{
    
statement
}

if (
x)
{
    
y;

is more readable, however in languages that open blocks with words rather than brackets
PHP Code:
condition open
    statement
close

if x then
    x 
y;
end 
it's better to keep the opening on the same line as condition because
PHP Code:
condition
open
    statement
close

if 
then 
    x 
y
end 
just seems out of place.
__________________

Last edited by hleV; 10-30-2016 at 06:14.
hleV is offline
xerox8521
Senior Member
Join Date: Sep 2011
Old 10-30-2016 , 06:35   Re: Code styles - prefered, readability & performance
Reply With Quote #18

Quote:
Originally Posted by redwerewolf View Post
I'm probably one of the only coders that uses lines for no reason while saving lines for no reason since I use brackets for every function, just looks cleaner to me.

Code:
public void somefunction()
{
    if (somecheck || someothercheck)
    {
        return value;
    }

    if (notsomecheck)
    {
        return value;
    }

    //do actual stuff
}
looks neat and clearly visible without having to guess where A starts and B Ends
xerox8521 is offline
ofir753
Senior Member
Join Date: Aug 2012
Old 10-30-2016 , 06:52   Re: Code styles - prefered, readability & performance
Reply With Quote #19

As you see its a personal preference, anyway an experienced coder could read all of them but I personally prefer the 2nd option.

Last edited by ofir753; 10-30-2016 at 06:52.
ofir753 is offline
DarkDeviL
SourceMod Moderator
Join Date: Apr 2012
Old 10-30-2016 , 07:00   Re: Code styles - prefered, readability & performance
Reply With Quote #20

Quote:
Originally Posted by xerox8521 View Post
looks neat and clearly visible without having to guess where A starts and B Ends
If you really need to "guess" around like that, then you should seriously not be touching any code at all.

(No matter if the code is written the one or other way!)
__________________
Mostly known as "DarkDeviL".

Dropbox FastDL: Public folder will no longer work after March 15, 2017!
For more info, see the [SRCDS Thread], or the [HLDS Thread].
DarkDeviL is offline
Reply



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 19:48.


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