SA-MP Forums Archive
Optimize this code - Printable Version

+- SA-MP Forums Archive (https://sampforum.blast.hk)
+-- Forum: SA-MP Scripting and Plugins (https://sampforum.blast.hk/forumdisplay.php?fid=8)
+--- Forum: Scripting Help (https://sampforum.blast.hk/forumdisplay.php?fid=12)
+--- Thread: Optimize this code (/showthread.php?tid=655984)



Optimize this code - Fairuz - 04.07.2018

how do I optimize this code to reduce lines and makes more understandable?

PHP код:
        if(GetVehicleEngineType(modelid) == 'H')
        {
            
format(typestr16"Human Powered");
        }
        else if(
GetVehicleEngineType(modelid) == 'D')
        {
            
format(typestr16"Diesel");
        }
        else if(
GetVehicleEngineType(modelid) == 'P')
        {
            
format(typestr16"Petrol");
        }
        else if(
GetVehicleEngineType(modelid) == 'E')
        {
            
format(typestr16"Electric");
        } 



Re: Optimize this code - Fairuz - 04.07.2018

Quote:
Originally Posted by ******
Посмотреть сообщение
Having said that, you can switch on character constants.
The wiki only provides number so I'm confused how to do it with strings.


Re: Optimize this code - Calisthenics - 04.07.2018

For a start, do not call GetVehicleEngineType function 4 times.
if/else if can be replaced with a switch.
format is useless when can set string directly.

pawn Код:
switch (GetVehicleEngineType(modelid))
{
    case 'H': typestr = "Human Powered";
    case 'D': typestr = "Diesel";
    case 'P': typestr = "Petrol";
    case 'E': typestr = "Electric";
}