Raised This Month: $ Target: $400
 0% 

Code Style


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
mottzi
Veteran Member
Join Date: May 2010
Location: Switzerland
Old 10-16-2010 , 11:58   Code Style
Reply With Quote #1

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")

__________________
Quote:
#define true ((rand() % 2)? true: false) //Happy debugging suckers
mottzi is offline
Send a message via MSN to mottzi
nikhilgupta345
Veteran Member
Join Date: Aug 2009
Location: Virginia
Old 10-16-2010 , 12:08   Re: Code Style
Reply With Quote #2

Not really sure it makes that much of a difference. They are both only calling one check. Not sure
__________________
Quote:
Originally Posted by DarkGod View Post
nikhilgupta generates his plugins using sheer awesome.
If you like my work, please
nikhilgupta345 is offline
Send a message via ICQ to nikhilgupta345 Send a message via Yahoo to nikhilgupta345
Firippu
Senior Member
Join Date: Jan 2007
Old 10-16-2010 , 12:09   Re: Code Style
Reply With Quote #3

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.
__________________
Firippu is offline
Sylwester
Veteran Member
Join Date: Oct 2006
Location: Poland
Old 10-16-2010 , 12:21   Re: Code Style
Reply With Quote #4

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

__________________
Impossible is Nothing
Sylwester is offline
Old 10-16-2010, 12:22
Kreation
This message has been deleted by Kreation. Reason: sylwester. D:
Firippu
Senior Member
Join Date: Jan 2007
Old 10-16-2010 , 13:03   Re: Code Style
Reply With Quote #5

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.
__________________
Firippu is offline
abdul-rehman
Veteran Member
Join Date: Jan 2010
Location: Khi, Pakistan
Old 10-16-2010 , 13:58   Re: Code Style
Reply With Quote #6

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

My Plugins For ZP

Inactive due to College and Studies
abdul-rehman is offline
Send a message via Yahoo to abdul-rehman Send a message via Skype™ to abdul-rehman
mottzi
Veteran Member
Join Date: May 2010
Location: Switzerland
Old 10-16-2010 , 14:26   Re: Code Style
Reply With Quote #7

Can you explain this "magic"
Code:
?
__________________
Quote:
#define true ((rand() % 2)? true: false) //Happy debugging suckers
mottzi is offline
Send a message via MSN to mottzi
Enum
Junior Member
Join Date: Oct 2010
Location: ee.uu
Old 10-16-2010 , 14:33   Re: Code Style
Reply With Quote #8

Quote:
Originally Posted by mottzi View Post
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.
Enum is offline
mottzi
Veteran Member
Join Date: May 2010
Location: Switzerland
Old 10-16-2010 , 15:39   Re: Code Style
Reply With Quote #9

owh, thanks.
__________________
Quote:
#define true ((rand() % 2)? true: false) //Happy debugging suckers
mottzi is offline
Send a message via MSN to mottzi
Exolent[jNr]
Veteran Member
Join Date: Feb 2007
Location: Tennessee
Old 10-16-2010 , 15:42   Re: Code Style
Reply With Quote #10

Quote:
Originally Posted by Sylwester View Post
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.
__________________
No private work or selling mods.
Quote:
Originally Posted by xPaw View Post
I love you exolent!
Exolent[jNr] 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 10:22.


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