AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   Wich method is better? (https://forums.alliedmods.net/showthread.php?t=154712)

zeus 04-12-2011 08:02

Wich method is better?
 
This
Code:

public init()
{
      if(var==a)
            function_a()
      else if(var=b)
            function_b()
}

public fuction_a()
        do something

public fuction_b()
        do some other things

Or this
Code:

public init()
{
      if(var==a)
            function(a)
      else if(var=b)
            function(a)
}

public fuction(x)
        do something with x


reinert 04-12-2011 08:34

Re: Wich method is better?
 
It depends what you want to do...

hleV 04-12-2011 08:35

Re: Wich method is better?
 
Depends on what you're doing.
Code:
switch (var) {         case a: // Do stuff         case b: // Do stuff }

zeus 04-12-2011 11:42

Re: Wich method is better?
 
For example i have this code
Code:

public check(id)
{
    if(is_user_retarded(id))
          cmd_retard(id)
    else if(is_user_idiot(id))
          cmd_idiot(id)
}

public cmd_retard(id)
    client_print(id,print_chat,"You are retarded !!!")

public cmd_idiot(id)
    client_print(id,print_chat,"You are an idiot !!!")

Is it better if I do something like this?
Code:

public check(id)
{
    if(is_user_retarded(id))
          cmd_retard_or_idiot(id,1)
    else if(is_user_idiot(id))
          cmd_retard_or_idiot(id,2)
}

public cmd_retard_or_idiot(id,x)
{
    if(x==1)
        client_print(id,print_chat,"You are retarded !!!")
    else if(x==2)
        client_print(id,print_chat,"You are an idiot !!!")
}


Arkshine 04-12-2011 11:59

Re: Wich method is better?
 
There is not really one better.

You can do things differently. Don't know what you plan to do exactly, but you can use a var which contains the "status" of the player.

PHP Code:

enum UserStatus
{
    
UserNormal,
    
UserRetarded,
    
UserIdiot
};

UserStatusDescriptionUserStatus ][] =
{
    
"You are normal !!!",
    
"You are retarded !!!",
    
"You are an idiot !!!"
};

new 
UserStatus:Status33 ];

public 
checkid )
{
    new 
UserStatus:currentStatus Statusid ];
    
    if( 
currentStatus != UserNormal )
    {
        
client_printidprint_chatUserStatusDescriptioncurrentStatus ] )
    }


You get the idea.

zeus 04-14-2011 07:32

Re: Wich method is better?
 
Thx for help :d

fmcTheKing 04-14-2011 19:18

Re: Wich method is better?
 
PHP Code:

(var == 'a') ?
    
function_a(a) :
    
function_b(a) ; 



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

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