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

Detect shared statements in branches #442

Open
Luro02 opened this issue Feb 19, 2024 · 0 comments
Open

Detect shared statements in branches #442

Luro02 opened this issue Feb 19, 2024 · 0 comments
Labels
D-hard duplicate-code enhancement New feature or request new-lint A new lint.

Comments

@Luro02
Copy link
Collaborator

Luro02 commented Feb 19, 2024

What it does

Detects common statements at the start or end of conditional branches like if/else and suggests moving them before the if branch or after it.

To what can this be applied?

  • if/else branches
  • switches
  • (methods?)

Some things to be careful of:

  • the condition should not be used in the statements (for example below it should only move the statement if it does not access this.first)
  • conditions might have side-effects
  • else ifs might be difficult to work with / hard to give sensible feedback to the student -> I would suggest ignoring it

Lint Name

DUPLICATE_CODE

Category

general

Advantage

No response

Drawbacks

No response

Example

if (this.first == null) {
    ListCell newListCell = new ListCell(new int[this.arraySize], this.last, null);
    this.first = newListCell;
    this.last = newListCell;
    this.numberOfCells++;
    // returns always true here, because adding to a new array isn´t problematic
    // (array size > 0)
    return newListCell.addElement(element);
} else {
    ListCell newListCell = new ListCell(new int[this.arraySize], this.last, null);
    this.last.next = newListCell;
    this.last = newListCell;
    this.numberOfCells++;
    return newListCell.addElement(element);
    // returns always true here, because adding to a new array isn´t problematic
    // (array size > 0)
}

Could be written as:

ListCell newListCell = new ListCell(new int[this.arraySize], this.last, null);

if (this.first == null) {
    this.first = newListCell;
} else {
    this.last.next = newListCell;
}

this.last = newListCell;
this.numberOfCells++;
return newListCell.addElement(element);
@Luro02 Luro02 added enhancement New feature or request new-lint A new lint. D-hard labels Feb 19, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
D-hard duplicate-code enhancement New feature or request new-lint A new lint.
Projects
None yet
Development

No branches or pull requests

1 participant