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

Code styles - prefered, readability & performance


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
shanapu
Veteran Member
Join Date: Apr 2015
Location: .de
Old 10-28-2016 , 21:20   Code styles - prefered, readability & performance
Reply With Quote #1

I've seen here many different coding styles. And I asked myself - and now I ask you - which of these two examples do you prefer in consideration of the readability, maintain & performance.
Or is there even a performance deviation at all?

PHP Code:
public Action Some_Command(int clientint args)
{
    if (
IsClientConnected(client))
    {
        if (
CVAR.BoolValue)
        {
            if (
GetClientTeam(client) == 1)
            {
                
// Do some fanzy stuff
            
}
            else 
PrintToChat(client"nope");
        }
        else 
PrintToChat(client"nope");
    }
    
    return 
Plugin_Handled;

PHP Code:
public Action Some_Command(int clientint args)
{
    if (!
IsClientConnected(client))
        return 
Plugin_Handled;
    
    if (!
CVAR.BoolValue)
    {
        
PrintToChat(client"nope");
        return 
Plugin_Handled;
    }
    
    if (
GetClientTeam(client) != 1)
    {
        
PrintToChat(client"nope");
        return 
Plugin_Handled;
    }
    
    
// Do some fanzy stuff
    
    
return Plugin_Handled;

Ok, there are many other variation possibilities to save some lines. But they will clearly decrease the readability.

PHP Code:
public Action Some_Command(int clientint args){
    if (
IsClientConnected(client)){
        if (
CVAR.BoolValue){
            if (
GetClientTeam(client) == 1){
                
// Do some fanzy stuff
            
}else PrintToChat(client"nope");
        }else 
PrintToChat(client"nope");
    }return 
Plugin_Handled;

PHP Code:
public Action Some_Command(int clientint args){
    if (!
IsClientConnected(client)) return Plugin_Handled;
    if (!
CVAR.BoolValue){
        
PrintToChat(client"nope");
        return 
Plugin_Handled;
    }
    if (
GetClientTeam(client) != 1){
        
PrintToChat(client"nope");
        return 
Plugin_Handled;
    }
    
// Do some fanzy stuff
    
return Plugin_Handled;

Point readability: What's in your opinion most important for a clean & readable code?
Comments?
How to name variables/function?
Use 'many' space lines / shrink the size?
Grouping of functions/forwards/...?
old/new syntax?
Something I've forgotten?
and why?

or do you just feel like
"f?#k readability! what is style? most important is the functionality!"?


Maybe you got some other code style examples with their dis-/advantages.
__________________
coding & free software
shanapu is offline
Guccci
AlliedModders Donor
Join Date: Aug 2016
Old 10-28-2016 , 21:50   Re: Code styles - prefered, readability & performance
Reply With Quote #2

example #2 spacing and neatness/straightness
Guccci is offline
Deathknife
Senior Member
Join Date: Aug 2014
Old 10-28-2016 , 22:00   Re: Code styles - prefered, readability & performance
Reply With Quote #3

PHP Code:
public Action Some_Command(int clientint args) {
    if (!
IsClientConnected(client))
        return 
Plugin_Handled;
    
    if (!
CVAR.BoolValue) {
        
PrintToChat(client"nope");
        return 
Plugin_Handled;
    }
    
    if (
GetClientTeam(client) != 1) {
        
PrintToChat(client"nope");
        return 
Plugin_Handled;
    }
    
    
// Do some fanzy stuff
    
    
return Plugin_Handled;

__________________
Deathknife is offline
OSWO
Senior Member
Join Date: Jul 2015
Location: United Kingdom, London
Old 10-28-2016 , 22:13   Re: Code styles - prefered, readability & performance
Reply With Quote #4

This is all user preference. You can't be asking an answer to an opinion.

Also if you use spaces and not tabs. End yourself.

I personally use this style:

PHP Code:
public Action function() {
    if (
IfStatement) {

    } else if (
ElseIfStatement) {

    }

    if (
Plugin_Handled) {
        return 
Plugin_Handled;
    }

    if (
Plugin_Stop) {
        return 
Plugin_Stop;
    }

__________________
SourceTimer | WeaponSkins++ | BasePlugins++ https://github.com/OSCAR-WOS

Last edited by OSWO; 10-28-2016 at 22:18.
OSWO is offline
xines
Veteran Member
Join Date: Aug 2013
Location: Denmark
Old 10-28-2016 , 22:27   Re: Code styles - prefered, readability & performance
Reply With Quote #5

My List would be:
  1. Performance.
  2. Readability.

Performance first because, i would rather drive a ugly car thats working correctly than driving a pretty car that can't even start. Also along the way you can use the commenting style to define your functions and their use for yourself & others to know:

1:
PHP Code:
//My Comment 
2:
PHP Code:
/*My Comment*/ 

Q. How to name variables/function?
A. Simple as short as you possible can without breaking the readability.
Example: using function "OnGameFrame" ticks about 66 times a secound tho it depends on the Servers FPS rate, so there is a big difference here in having variables named with 50 bytes than with 10 bytes.

Q. Use 'many' space lines / shrink the size?
A. Just stick to use TABS which equals to 4 spaces.

Spoiler for why to use tabs (Stolen from some random site):
Spoiler



Q. old/new syntax?
A. NEW SYNTAX Why even question this.
__________________
xines is offline
nosoop
Veteran Member
Join Date: Aug 2014
Old 10-28-2016 , 22:43   Re: Code styles - prefered, readability & performance
Reply With Quote #6

Group related functionality when possible and don't repeat things if you don't have to. If the control flow is weird, put failure states in early.

Maintainability's important. You'll want to know what you're looking at when you come back to it after a few months.

If you asked me to implement Some_Command with all the same functions, I would've done it like this:

Code:
public Action Some_Command(int client, int argc) {
    // We do want to print a message to the client on any failure, so enclose it all with the client check.
    // Though really, you can just check "if (client)" since they'll be connected for that particular callback unless it's called by the server (index 0).
    if (IsClientConnected(client)) {
        if (hCvar.BoolValue && GetClientTeam(client) == 1) {
            // Do your stuff.
            // If doing stuff gets really long and complex, I'd move it to a new DoYourStuff(int client) function at some point.
        } else {
            // Don't repeat yourself -- the above conditions can be compacted down to one check.
            // Rewrite the block and expand on it if you have to later.
            PrintToChat(client, "nope");
        }
    }
    return Plugin_Handled;
}
Of course, not that many comments; the simpler stuff should be self-documenting, larger blocks get a quick explanation of purpose.
__________________
I do TF2, TF2 servers, and TF2 plugins.
I don't do DMs over Discord -- PM me on the forums regarding inquiries.
AlliedModders Releases / Github / TF2 Server / Donate (BTC / BCH / coffee)
nosoop is offline
Mitchell
~lick~
Join Date: Mar 2010
Old 10-28-2016 , 23:02   Re: Code styles - prefered, readability & performance
Reply With Quote #7

Code:
public Action Some_Command(int client, int args) { if (!IsClientConnected(client)) { return Plugin_Handled; } if (!CVAR.BoolValue) { PrintToChat(client, "nope"); return Plugin_Handled; } if (GetClientTeam(client) != 1) { PrintToChat(client, "nope"); return Plugin_Handled; } return Plugin_Handled; }
courtesy of my fuckitup.py script.
Mitchell is offline
Powerlord
AlliedModders Donor
Join Date: Jun 2008
Location: Seduce Me!
Old 10-28-2016 , 23:21   Re: Code styles - prefered, readability & performance
Reply With Quote #8

Quote:
Originally Posted by Mitchell View Post
Code:
public Action Some_Command(int client, int args) { if (!IsClientConnected(client)) { return Plugin_Handled; } if (!CVAR.BoolValue) { PrintToChat(client, "nope"); return Plugin_Handled; } if (GetClientTeam(client) != 1) { PrintToChat(client, "nope"); return Plugin_Handled; } return Plugin_Handled; }
courtesy of my fuckitup.py script.
This isn't JavaScript, you don't need to minify it. ;)
__________________
Not currently working on SourceMod plugin development.
Powerlord is offline
fakuivan
Senior Member
Join Date: Nov 2015
Old 10-29-2016 , 00:07   Re: Code styles - prefered, readability & performance
Reply With Quote #9

Quote:
Originally Posted by xines View Post
Q. How to name variables/function?
A. Simple as short as you possible can without breaking the readability.
Example: using function "OnGameFrame" ticks about 66 times a secound tho it depends on the Servers FPS rate, so there is a big difference here in having variables named with 50 bytes than with 10 bytes.
Wut? Isn't the jit dealing with that kind of optimization?

Last edited by fakuivan; 10-29-2016 at 00:07. Reason: derp
fakuivan is offline
headline
SourceMod Moderator
Join Date: Mar 2015
Old 10-29-2016 , 01:37   Re: Code styles - prefered, readability & performance
Reply With Quote #10

Quote:
Originally Posted by fakuivan View Post
Wut? Isn't the jit dealing with that kind of optimization?
Uhh what? Variable naming doesn't matter after compilation, he's referring to some sort of convention.


When I was learning SourceMod ThatOneGuy taught me how to stay completely organized by prefixing all variables with their type and if they're global or not so it's simple to tell, for example...

This really helped me when I was starting
PHP Code:
/* Globals */
ga_iSomeArray[MAXPLAYERS 1];
g_iSomeInt;
g_fSomeFloat;
gcv_iSomeCvarInteger

/* Locals */
iSomeLocalVariable 
As far as your example you posted my spin would be something like this...

Spoiler

In the end - assuming everything's working okay - it's all about readability and if my padded, spacey (and in my opinion) clean style makes it easier for someone to understand then that's all that matters to me. I don't write for just myself

However recently I've been doing camel notation on SM 1.7 things that have a class

Last edited by headline; 10-29-2016 at 01:45.
headline 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 02:24.


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