Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactored Image Class: Addressed Complex Conditional and Eliminated Duplicate Code in getInstance Methods #1235

Open
wants to merge 5 commits into
base: master
Choose a base branch
from

Conversation

samruddhithakor
Copy link

@samruddhithakor samruddhithakor commented Nov 26, 2024

Description of the new Feature/Bugfix

  1. Complex Conditional (True Positive):
  • Project Name: OpenPDF
  • Package Name: com.lowagie.text
  • Type Name: Image
  • Method Name: getInstance
  • Cause of the Smell:
    A complex conditional at line 569 (c1 == 'M' && c2 == 'M' && c3 == 0 && c4 == 42) uses multiple logical operators and hardcoded values, reducing readability and increasing the risk of errors.
  • Fix:
    Introduced named constants and helper methods to simplify the logic and improve maintainability.
  1. Duplicate Code (False Negative):
  • Project Name: OpenPDF
  • Smell: Duplicate Code
  • Missed Case:
    The methods getInstance(URL url) and getInstance(byte[] imgb) shared identical logic for reading image data and identifying image types using magic numbers.
  • Fix:
    Extracted the shared logic into a utility function, loadImageFromHeader.
    This addresses the DRY (Don't Repeat Yourself) principle, reducing maintenance overhead and ensuring consistency.

Unit tests for Refactoring

  • Unit tests for getInstance(URL url) and getInstance(byte[] imgb) confirm shared logic without breaking functionality.
  • Unit tests validate the refactored complex conditional logic.

Compatibilities Issues

  1. Complex Conditional:
  • No breaking changes; the logic is refactored into helper methods for better readability.
  1. Duplicate Code:
  • Backward compatibility is maintained with no changes to method signatures or behavior.

Your real name

Samruddhi Dharmeshkumar Thakor

Testing details

  1. Run mvn clean compile to ensure the project compiles without errors.
  2. Execute mvn test to verify all unit tests pass.

@samruddhithakor samruddhithakor changed the title Refactored Image Class: Eliminated Duplicate Code in getInstance Methods Refactored Image Class: Addressed Complex Conditional and Eliminated Duplicate Code in getInstance Methods Nov 26, 2024
Copy link

sonarcloud bot commented Nov 26, 2024

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant