Optimize this code
#1

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");
        } 
Reply
#2

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.
Reply
#3

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";
}
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)