View Single Post
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