ST17H26尽量避免switch语句
項目中某個函數由于使用了switch-case語句,當代碼增加到某個程度的時候,已進入該函數,則程序跑飛。改成if結構,則問題解決。
switch(packType)
{
case CMD_RANDOM_NUMBER:
return CmdRandomResponse(appData);
break;
case CMD_MUTEX_AUTH:
//return CmdMutexAuthResponse(appData);
break;
case CMD_OPEN_DOOR:
return CmdOpenDoorResponse(appData);
break;
case CMD_UPDATE_TIME:
//return CmdUpdateTimeResponse(appData);
break;
case CMD_READ_TIME:
//return CmdReadTimeResponse(appData);
break;
case CMD_WRITE_SN:
return CmdWriteSnResponse(appData);
break;
case CMD_WRITE_KEY:
return CmdWriteKeyResponse(appData);
break;
case CMD_DEVICE_BOND:
return CmdDeviceBondResponse(appData);
break;
case CMD_DEVICE_UNBOND:
//return CmdDeviceUnbondResponse(appData);
break;
case CMD_READ_BOND_INFO:
//return CmdReadBondInfoResponse(appData);
break;
case CMD_READ_VERSION:
//return CmdReadVersionResponse(appData);
break;
default:
break;
}
if(packType == CMD_RANDOM_NUMBER)
{
return CmdRandomResponse(appData);
}
else if(packType == CMD_MUTEX_AUTH)
{
return CmdMutexAuthResponse(appData);
}
else if(packType == CMD_OPEN_DOOR)
{
return CmdOpenDoorResponse(appData);
}
else if(packType == CMD_UPDATE_TIME)
{
return CmdUpdateTimeResponse(appData);
}
else if(packType == CMD_READ_TIME)
{
return CmdReadTimeResponse(appData);
}
else if(packType == CMD_WRITE_SN)
{
return CmdWriteSnResponse(appData);
}
else if(packType == CMD_WRITE_KEY)
{
return CmdWriteKeyResponse(appData);
}
else if(packType == CMD_DEVICE_BOND)
{
return CmdDeviceBondResponse(appData);
}
else if(packType == CMD_DEVICE_UNBOND)
{
return CmdDeviceUnbondResponse(appData);
}
else if(packType == CMD_READ_BOND_INFO)
{
return CmdReadBondInfoResponse(appData);
}
else if(packType == CMD_READ_VERSION)
{
return CmdReadVersionResponse(appData);
}
else
{
return CMD_ERROR;
}
總結
以上是生活随笔為你收集整理的ST17H26尽量避免switch语句的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: ST17H26代码优化的疑问
- 下一篇: ST17H26 tea加密java版本