AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   Code Style (https://forums.alliedmods.net/showthread.php?t=140772)

mottzi 10-16-2010 11:58

Code Style
 
Hello Alliedmods,

My Question: What is more efficient?

Method 1:

PHP Code:

if(headshot)
{
        
client_print0print_chat"Omg. HeadShot.")
        return 
PLUGIN_HANDLED
}
    
        
client_print0print_chat"lol... Normal kill"

Method 2:

PHP Code:

if(headshot)
{
        
client_print0print_chat"Omg. HeadShot.")
}
else
{
        
client_print0print_chat"lol... Normal kill")



nikhilgupta345 10-16-2010 12:08

Re: Code Style
 
Not really sure it makes that much of a difference. They are both only calling one check. Not sure :)

Firippu 10-16-2010 12:09

Re: Code Style
 
Method 2 will allow more code in the function if headshot is true, method 1 will not. However, if that code was alone in the function, it would not matter. Method 2 is better over all.

Sylwester 10-16-2010 12:21

Re: Code Style
 
Firippu you are wrong about method 2 being better.
It makes no difference in efficiency, but you can use it to make your code easier to read.

Instead of this:
PHP Code:

public function(){
    if(
condition1){
        
//code1
        
if(condition2){
            
//code2
            
if(condition3){
                
//code3
                
if(condition4){
                    
//code4
                    
if(condition5){
                        
//code5
                        
if(condition6){
                            
//code6
                            
if(condition7){
                                
//code7
                            
}
                        }
                    }
                }
            }
        }
    }


It can look like this:
PHP Code:

public function(){
    if(!
condition1)
        return
    
//code1
    
if(!condition2)
        return
    
//code2
    
if(!condition3)
        return
    
//code3
    
if(!condition4)
        return
    
//code4
    
if(!condition5)
        return
    
//code5
    
if(!condition6)
        return
    
//code6
    
if(!condition7)
        return
    
//code7



Firippu 10-16-2010 13:03

Re: Code Style
 
Like I said, if the code was alone(like the example mottzi posted), it would not matter which one you choose. However, if you wanted to execute more code after it, regardless if headshot is true or false, you would have to use method 2 or else it wouldn't reach it, unless headshot was false. Method 2 is more flexible, not more efficient.

abdul-rehman 10-16-2010 13:58

Re: Code Style
 
This way its faster:
Code:
client_print( 0, print_chat, "%s", headshot ? "Omg. HeadShot." : "lol... Normal kill")

mottzi 10-16-2010 14:26

Re: Code Style
 
Can you explain this "magic"
Code:

?

Enum 10-16-2010 14:33

Re: Code Style
 
Quote:

Originally Posted by mottzi (Post 1326649)
Can you explain this "magic"
Code:

?

is how the "if" but in a print/hud, etc.
and ":" is how the else in a if too.

mottzi 10-16-2010 15:39

Re: Code Style
 
owh, thanks.

Exolent[jNr] 10-16-2010 15:42

Re: Code Style
 
Quote:

Originally Posted by Sylwester (Post 1326448)
Firippu you are wrong about method 2 being better.
It makes no difference in efficiency, but you can use it to make your code easier to read.

Actually, method #2 is a bit more efficient because it would only use 1 return statement at the end, if it used one.
Method #1 would use several return statements.
When these are put into the assembly code, method #1 has many jumps which could be avoided by using method #2.
I don't remember where this was discussed or who said it, though.


All times are GMT -4. The time now is 10:22.

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