if
is identical to the body of else
175 // Check which frame we should copy.
176 switch (stContainer.eFrameType)
177 {
178 case PIXEL_FORMATS::eDepthDetection: *(stContainer.pFrame) = m_cvProcFrame; break;179 case PIXEL_FORMATS::eTensorflowDetection: *(stContainer.pFrame) = m_cvProcFrame; break;
180 default: *(stContainer.pFrame) = m_cvProcFrame;
181 }
if
is identical to the body of else
157 // Determine which frame should be copied.
158 switch (stContainer.eFrameType)
159 {
160 case PIXEL_FORMATS::eBGRA: *(stContainer.pFrame) = m_cvFrame; break;161 case PIXEL_FORMATS::eDepthMeasure: *(stContainer.pFrame) = m_cvDepthMeasure; break;
162 case PIXEL_FORMATS::eDepthImage: *(stContainer.pFrame) = m_cvDepthImage; break;
163 case PIXEL_FORMATS::eXYZ: *(stContainer.pFrame) = m_cvPointCloud; break;
if
is identical to the body of else
2186 }
2187
2188 // Check if the spatial map was exported successfully.
2189 if (m_slCamera.getSpatialMapRequestStatusAsync() == sl::ERROR_CODE::SUCCESS)2190 {
2191 // Get and store the spatial map.
2192 m_slCamera.retrieveSpatialMapAsync(slSpatialMap);
if
is identical to the body of else
1480 {
1481 // Covariance error.
1482 if (slReturnCode == sl::FUSION_ERROR_CODE::GNSS_DATA_COVARIANCE_MUST_VARY || slReturnCode == sl::FUSION_ERROR_CODE::INVALID_COVARIANCE)
1483 {1484 // Submit logger message.
1485 LOG_WARNING(logging::g_qSharedLogger,
1486 "Unable to ingest fusion GNSS data for camera {} ({})! sl::Fusion positional tracking may be inaccurate! sl::FUSION_ERROR_CODE "
if
is identical to the body of else
1420 // Get the GNSS fix type status from the given GPS coordinate.
1421 switch (stNewGPSLocation.eCoordinateAccuracyFixType)
1422 {
1423 case geoops::PositionFixType::eNoFix:1424 {
1425 slGNSSData.gnss_status = sl::GNSS_STATUS::SINGLE;
1426 slGNSSData.gnss_mode = sl::GNSS_MODE::NO_FIX;
if
is identical to the body of else
1315 }
1316
1317 // Check if this camera was successfully subscribed to the Fusion instance.
1318 if (slReturnCode == sl::FUSION_ERROR_CODE::SUCCESS)1319 {
1320 // Submit logger message.
1321 LOG_DEBUG(logging::g_qSharedLogger,
if
is identical to the body of else
746 // Determine which frame should be copied.
747 switch (stContainer.eFrameType)
748 {
749 case PIXEL_FORMATS::eBGRA: *(stContainer.pFrame) = imgops::ConvertSLMatToGPUMat(m_slFrame); break; 750 case PIXEL_FORMATS::eDepthMeasure: *(stContainer.pFrame) = imgops::ConvertSLMatToGPUMat(m_slDepthMeasure); break;
751 case PIXEL_FORMATS::eDepthImage: *(stContainer.pFrame) = imgops::ConvertSLMatToGPUMat(m_slDepthImage); break;
752 case PIXEL_FORMATS::eXYZBGRA: *(stContainer.pFrame) = imgops::ConvertSLMatToGPUMat(m_slPointCloud); break;
if
is identical to the body of else
707 // Determine which frame should be copied.
708 switch (stContainer.eFrameType)
709 {
710 case PIXEL_FORMATS::eBGRA: *(stContainer.pFrame) = imgops::ConvertSLMatToCVMat(m_slFrame); break; 711 case PIXEL_FORMATS::eDepthMeasure: *(stContainer.pFrame) = imgops::ConvertSLMatToCVMat(m_slDepthMeasure); break;
712 case PIXEL_FORMATS::eDepthImage: *(stContainer.pFrame) = imgops::ConvertSLMatToCVMat(m_slDepthImage); break;
713 case PIXEL_FORMATS::eXYZBGRA: *(stContainer.pFrame) = imgops::ConvertSLMatToCVMat(m_slPointCloud); break;
if
is identical to the body of else
499 sl::POSITIONAL_TRACKING_STATE slPoseTrackReturnCode;
500
501 // Check if this camera has Fusion instance enabled.
502 if (m_bCameraIsFusionMaster) 503 {
504 // Get tracking pose from Fusion.
505 slPoseTrackReturnCode = m_slFusionInstance.getPosition(m_slCameraPose, sl::REFERENCE_FRAME::WORLD);
if
is identical to the body of else
207 slReturnCode = m_slFusionInstance.subscribe(sl::CameraIdentifier(m_unCameraSerialNumber));
208
209 // Check if this camera was successfully subscribed to the Fusion instance.
210 if (slReturnCode == sl::FUSION_ERROR_CODE::SUCCESS) 211 {
212 // Submit logger message.
213 LOG_DEBUG(logging::g_qSharedLogger, "Initialized FUSION instance for ZED camera {} ({})!", sl::toString(m_slCameraModel).get(), m_unCameraSerialNumber);
if
is identical to the body of else
112 m_slSpatialMappingParams.use_chunk_only = constants::ZED_MAPPING_USE_CHUNK_ONLY;
113 m_slSpatialMappingParams.stability_counter = constants::ZED_MAPPING_STABILITY_COUNTER;
114 // Set or auto-set max depth range for mapping.
115 if (constants::ZED_MAPPING_RANGE_METER <= 0) 116 {
117 // Automatically guess the best mapping depth range.
118 m_slSpatialMappingParams.range_meter = m_slSpatialMappingParams.getRecommendedRange(constants::ZED_MAPPING_RESOLUTION_METER, m_slCamera);
if
is identical to the body of else
184 if (nTimeSinceEpoch % 5 == 0 && !bReopenAlreadyChecked)
185 {
186 // Check if camera was opened with an index or path.
187 if (m_nCameraIndex == -1)188 {
189 // Attempt to reopen camera.
190 bCameraReopened = m_cvCamera.open(m_szCameraPath);
if
is identical to the body of else
787cv::Size TagDetector::GetProcessFrameResolution() const
788{
789 // Check if using a ZED camera.
790 if (m_bUsingZedCamera)791 {
792 // Concatenate camera model name and serial number.
793 return dynamic_cast<ZEDCam*>(m_pCamera)->GetPropResolution();
if
is identical to the body of else
711 if (this->GetThreadState() == AutonomyThreadState::eRunning)
712 {
713 // Check if using ZEDCam or BasicCam.
714 if (m_bUsingZedCamera)715 {
716 // Check if camera is NOT open.
717 if (dynamic_cast<ZEDCam*>(m_pCamera)->GetCameraIsOpen())
if
is identical to the body of else
158void TagDetector::ThreadedContinuousCode()
159{
160 // Check if using ZEDCam or BasicCam.
161 if (m_bUsingZedCamera)162 {
163 // Check if camera is NOT open.
164 if (!dynamic_cast<ZEDCam*>(m_pCamera)->GetCameraIsOpen())
if
is identical to the body of else
182 {
183 // Initialize output color mat if necessary.
184 if (cvOutputColors.empty())
185 {186 // Fill mat with zeros, same size as input point cloud, but with 4 char values in third dimension.
187 cvOutputColors = cv::Mat(cvInputPointCloud.rows, cvInputPointCloud.cols, CV_8UC4, cv::Scalar(0));
188 }
if
is identical to the body of else
182 double dTargetDistance;
183
184 if (bDetectedTagAR)
185 {186 dTargetHeading = numops::InputAngleModulus<double>(dCurrHeading + m_stTargetTagAR.dYawAngle, 0, 360);
187 dTargetDistance = m_stTargetTagAR.dStraightLineDistance;
188 }
if
is identical to the body of else
168 // Determine which device is going to be used for this model.
169 switch (eDeviceType)
170 {
171 case eAuto: m_tpuDevice.type = edgetpu::DeviceType(-1); break;172 case ePCIe: m_tpuDevice.type = edgetpu::DeviceType::kApexPci; break;
173 case eUSB: m_tpuDevice.type = edgetpu::DeviceType::kApexUsb; break;
174 default: m_tpuDevice.type = edgetpu::DeviceType(-1); break;
if
is identical to the body of else
97 }
98
99 // Determine if firmware should be loaded ever time code is started.
100 if (bUSBAlwaysDFU)101 {
102 // Always load firmware.
103 m_tpuDeviceOptions["Usb.AlwaysDfu"] = "True";
if
is identical to the body of else
89 // Determine which power mode should be set.
90 switch (ePowerMode)
91 {
92 case eLow: m_tpuDeviceOptions["Performance"] = "Low"; break; 93 case eMedium: m_tpuDeviceOptions["Performance"] = "Medium"; break;
94 case eHigh: m_tpuDeviceOptions["Performance"] = "High"; break;
95 case eMax: m_tpuDeviceOptions["Performance"] = "Max"; break;
if
is identical to the body of else
337 {
338 // Check if the pools need to be resized.
339 if (m_thPool.get_thread_count() != nNumThreads)
340 {341 // Pause queuing of new tasks to the threads, then purge them.
342 m_thPool.pause();
343 m_thPool.purge();
if
is identical to the body of else
265 {
266 // Check if the pools need to be resized.
267 if (m_thPool.get_thread_count() != nNumThreads)
268 {269 // Pause queuing of new tasks to the threads, then purge them.
270 m_thPool.pause();
271 m_thPool.purge();
if
is identical to the body of else
600 // Acquire a read lock on the path unordered map.
601 std::shared_lock<std::shared_mutex> lkPathsLock(m_muPathMutex);
602 // Check if the map contains the given key.
603 if (m_umStoredPaths.count(szPathName) > 0)604 {
605 // Return the path vector at the given key.
606 return m_umStoredPaths[szPathName];
if
is identical to the body of else
130
131 // Check if the state exists in exitedStates
132 std::unordered_map<statemachine::States, std::shared_ptr<statemachine::State>>::iterator itState = m_umSavedStates.find(eNextState);
133 if (itState != m_umSavedStates.end())134 {
135 // Load the existing state
136 m_pCurrentState = itState->second;
if
is identical to the body of else
513 {
514 // Check if this is a grayscale or color image.
515 if (m_vFrames[nIter].channels() == 1)
516 {517 // Convert frame from 1 channel grayscale to 3 channel BGR.
518 cv::cvtColor(m_vFrames[nIter], m_vFrames[nIter], cv::COLOR_GRAY2BGR);
519 }
Found repeated branches in if/else statements, consecutive repeated branches in switch statements, and identical true and false branches in conditional operators.
In the bad practice example, the then
and else
branches are identical, which can be considered redundant.
The code can be simplified by removing the conditional statement and executing the shared code unconditionally.
However, if this is the intended behavior, there is no reason to use a conditional statement in the first place.
The check also detects repeated branches in longer if/else if/else chains, where it can be harder to notice the problem. In switch statements, the check reports repeated branches only when they are consecutive. This is because the case labels often have a natural ordering, and rearranging them would decrease code readability.
For example:
switch (ch) {
case 'a':
return 10;
case 'A':
return 10;
case 'b':
return 11;
case 'B':
return 11;
default:
return 10;
}
In this case, the check reports that the 'a'
and 'A'
branches are identical (and that the 'b'
and 'B'
branches are also identical), but it does not report that the default
branch is also identical to the first two branches.
If preserving the order of branches is important, the repeated branches can be combined using multiple case labels:
switch (ch) {
case 'a':
case 'A':
return 10;
case 'b':
case 'B':
return 11;
default:
return 10;
}
In this case, the check does not warn for the repeated return 10;
, as it helps preserve the order of branches.
The check also examines conditional operators and reports code where the true and false branches are identical:
return test_value(x) ? x : x;
Unlike if statements, the check does not detect chains of conditional operators.
Note: This check also reports situations where branches become identical only after preprocessing.
Bad Practice
if (test_value(x)) {
y++;
do_something(x, y);
} else {
y++;
do_something(x, y);
}
Recommended
test_value(x); // can be omitted unless it has side effects
y++;
do_something(x, y);