AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting (https://forums.alliedmods.net/forumdisplay.php?f=107)
-   -   Code styles - prefered, readability & performance (https://forums.alliedmods.net/showthread.php?t=289747)

shanapu 10-28-2016 21:20

Code styles - prefered, readability & performance
 
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!"? :bee:


Maybe you got some other code style examples with their dis-/advantages.

Guccci 10-28-2016 21:50

Re: Code styles - prefered, readability & performance
 
example #2 spacing and neatness/straightness

Deathknife 10-28-2016 22:00

Re: Code styles - prefered, readability & performance
 
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;



OSWO 10-28-2016 22:13

Re: Code styles - prefered, readability & performance
 
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;
    }



xines 10-28-2016 22:27

Re: Code styles - prefered, readability & performance
 
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.

nosoop 10-28-2016 22:43

Re: Code styles - prefered, readability & performance
 
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.

Mitchell 10-28-2016 23:02

Re: Code styles - prefered, readability & performance
 
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.

Powerlord 10-28-2016 23:21

Re: Code styles - prefered, readability & performance
 
Quote:

Originally Posted by Mitchell (Post 2465862)
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. ;)

fakuivan 10-29-2016 00:07

Re: Code styles - prefered, readability & performance
 
Quote:

Originally Posted by xines (Post 2465853)
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?

headline 10-29-2016 01:37

Re: Code styles - prefered, readability & performance
 
Quote:

Originally Posted by fakuivan (Post 2465874)
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


All times are GMT -4. The time now is 13:00.

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