Code clone Medium

Similar code fragments were detected in the same file. This could indicate a deeper problem and reduce code maintainability.

Detector ID
java/code-clone@v1.0
Category
Common Weakness Enumeration (CWE) external icon
-

Noncompliant example

1private boolean doesVideoStreamExistForJobNoncompliant(final String videoStreamArn,
2                                   final String viewAngle,
3                                   final String activityJobArn,
4                                   final ActivityType activityType) throws Exception {
5    try {
6        ActivityJobItem activityJob = null;
7        // Noncompliant: uses similar code fragments in the same file.
8        if (activityType == ActivityType.TRAINING) {
9            activityJob = trainingJobDao.loadTrainingJob(activityJobArn);
10        } else if (activityType == ActivityType.EVALUATION) {
11            activityJob = evaluationJobDao.loadEvaluationJob(activityJobArn);
12        } else if (activityType == ActivityType.FINETUNING) {
13            activityJob = finetuningJobDao.loadFinetuningJob(activityJobArn);
14        }
15        if (activityJob == null) {
16            throw new Exception("Unexpected workflow activity job.");
17        }
18        return containsVideoStreamWithGivenAngleAndArn(videoStreamArn, viewAngle, activityJob);
19    } catch (Exception ex) {
20        log.error("Unable to get video stream data from DynamoDB.", ex);
21        throw ex;
22    }
23}
24private void updateVideoInfoInDynamoDBNoncompliant(final Path videoFilePath,
25                                       final String s3BucketName,
26                                       final String activityJobArn,
27	   final ActivityType activityType) {
28    String videoFileLocation = null;
29    if ((videoFilePath != null) && (s3BucketName != null)) {
30        String videoFileName = videoFilePath.toFile().getName();
31        videoFileLocation = "s3://" + s3BucketName + "/" + S3_OBJECT_KEY_PREFIX + videoFileName;
32    }
33    ActivityJobItem activityJob = null;
34    // Noncompliant: uses similar code fragments in the same file.
35    if (activityType == ActivityType.TRAINING) {
36        activityJob = trainingJobDao.loadTrainingJob(activityJobArn);
37    } else if (activityType == ActivityType.EVALUATION) {
38        activityJob = evaluationJobDao.loadEvaluationJob(activityJobArn);
39    } else if (activityType == ActivityType.FINETUNING) {
40        activityJob = finetuningJobDao.loadFinetuningJob(activityJobArn);
41    }
42    if (activityJob == null) {
43        return;
44    }
45    updateActivityJobItem(activityJob, videoFileLocation, activityType);
46}

Compliant example

1public ActivityJobItem getJobFromArnAndActivityType(String activityJobArn, ActivityType activityType){
2    ActivityJobItem activityJob = null;
3    if (activityType == ActivityType.TRAINING) {
4        activityJob = trainingJobDao.loadTrainingJob(activityJobArn);
5    } else if (activityType == ActivityType.EVALUATION) {
6        activityJob = evaluationJobDao.loadEvaluationJob(activityJobArn);
7    } else if (activityType == ActivityType.FINETUNING) {
8        activityJob = finetuningJobDao.loadFinetuningJob(activityJobArn);
9    }
10    return activityJob;
11}
12private boolean doesVideoStreamExistForJobCompliant(final String videoStreamArn,
13                                            final String viewAngle,
14                                            final String activityJobArn,
15                                            final ActivityType activityType) throws Exception {
16    try {
17        ActivityJobItem activityJob = null;
18        // Compliant: avoids using similar code fragments in the same file.
19        activityJob = getJobFromArnAndActivityType(activityJobArn, activityType);
20        if (activityJob == null) {
21            throw new Exception("Unexpected workflow activity job.");
22        }
23        return containsVideoStreamWithGivenAngleAndArn(videoStreamArn, viewAngle, activityJob);
24    } catch (Exception ex) {
25        log.error("Unable to get video stream data from DynamoDB.", ex);
26        throw ex;
27    }
28}
29private void UpdateVideoInfoInDynamoDBCompliant(final Path videoFilePath,
30                                                final String s3BucketName,
31                                                final String activityJobArn,
32                                                final ActivityType activityType) {
33    String videoFileLocation = null;
34    if ((videoFilePath != null) && (s3BucketName != null)) {
35        String videoFileName = videoFilePath.toFile().getName();
36        videoFileLocation = "s3://" + s3BucketName + "/" + S3_OBJECT_KEY_PREFIX + videoFileName;
37    }
38    ActivityJobItem activityJob = null;
39    // Compliant: avoids using similar code fragments in the same file.
40    activityJob = getJobFromArnAndActivityType(activityJobArn, activityType);
41    if (activityJob == null) {
42        return;
43    }
44    updateActivityJobItem(activityJob, videoFileLocation, activityType);
45}