Is there a way to simplify this code? """ if( hDevData.GrindersProfile[idx].bIsConnected…
- User
Is there a way to simplify this code? """ if( hDevData.GrindersProfile[idx].bIsConnected != 0 ) { if( BLE_AT_IsBleConnected( hDevData.GrindersProfile[idx].sAddr ) ) { // TODO: write or read messages } else { hDevData.GrindersProfile[idx].bIsConnected = 0; hDevData.GrindersProfile[idx].bIsVisible = 0; } } else { if( BLE_AT_IsBleConnected( hDevData.GrindersProfile[idx].sAddr ) ) { hDevData.GrindersProfile[idx].bIsConnected = 1; } else { BLE_AT_CMD_Connect( hDevData.GrindersProfile[idx].sAddr ); } } """
- Assistant
Yes, the given code can be simplified by reducing the redundancy in the logic paths. You can achieve this by evaluating the connection status once and then acting accordingly. Here's a simplified version:
cint isConnected = BLE_AT_IsBleConnected(hDevData.GrindersProfile[idx].sAddr); if (isConnected) { if (hDevData.GrindersProfile[idx].bIsConnected == 0) { hDevData.GrindersProfile[idx].bIsConnected = 1; } // TODO: write or read messages } else { if (hDevData.GrindersProfile[idx].bIsConnected != 0) { hDevData.GrindersProfile[idx].bIsConnected = 0; hDevData.GrindersProfile[idx].bIsVisible = 0; } else { BLE_AT_CMD_Connect(hDevData.GrindersProfile[idx].sAddr); } }