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

Refactor: centralize type handling for simple operations such as add and subtract, and add branch coverage for this #167

Open
akerfel opened this issue Mar 1, 2022 · 1 comment

Comments

@akerfel
Copy link

akerfel commented Mar 1, 2022

In the files FenwickTree.java, Matrix.java and SegmentTree.java (all in the folder \src\com\jwetherell\algorithms\data_structures), there are comments saying TODO: This is ugly and how to handle number overflow?. See example below.

These functions do the same things, therefore some refactoring could decrease code repetition. Specifically, a class could be created which holds functions for adding, subtracting, multiplying and comparing the different types BigDecimal, BigInteger, Long, Double, Float and Integer.

Furthermore, there seems to be no branch coverage for using these different types, since all tests just use Integers. Tests for the different types should be added.

/* TODO: This is ugly and how to handle number overflow? */
if (this.sum instanceof BigDecimal || data.sum instanceof BigDecimal) {
    BigDecimal result = ((BigDecimal)this.sum).subtract((BigDecimal)data.sum);
    this.sum = (N)result;
} else if (this.sum instanceof BigInteger || data.sum instanceof BigInteger) {
    BigInteger result = ((BigInteger)this.sum).subtract((BigInteger)data.sum);
    this.sum = (N)result;
} else if (this.sum instanceof Long || data.sum instanceof Long) {
    Long result = (this.sum.longValue() - data.sum.longValue());
    this.sum = (N)result;
} else if (this.sum instanceof Double || data.sum instanceof Double) {
    Double result = (this.sum.doubleValue() - data.sum.doubleValue());
    this.sum = (N)result;
} else if (this.sum instanceof Float || data.sum instanceof Float) {
    Float result = (this.sum.floatValue() - data.sum.floatValue());
    this.sum = (N)result;
} else {
    // Integer
    Integer result = (this.sum.intValue() - data.sum.intValue());
    this.sum = (N)result;
}
@akerfel
Copy link
Author

akerfel commented Mar 1, 2022

We are a group of three students from the Royal Institute of Technology in Stockholm, Sweden. In the course we are currently taking we are suppsed to contribute to an open source project. Could we be assigned this task?

@akerfel akerfel changed the title Refactor: centralize type handling for simple operations such as add and subtract. Refactor: centralize type handling for simple operations such as add and subtract, and add branch coverage for this Mar 1, 2022
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

No branches or pull requests

1 participant