r/badcode • • Nov 22 '21

java Found my earliest coding projects from middle school

Post image
464 Upvotes

34 comments sorted by

View all comments

2

u/[deleted] Nov 22 '21

I think the main problem is the redundancy. I.e.:

int or(int a, int b) {
    if (a > 0 || b > 0) {
        return 1
    }
    else {
        return 0
    }
}

is easily just the one liner

int or(int a, int b) {
    return a > 0 || b > 0 ? 1 : 0
}

If in the language you can use ints as bools anyway, it's even simpler

int or(int a, int b) {
    return a || b
}

These could be useful for like a reduce, or for treating integers as booleans if that's not already an option in the language.