jeudi 31 octobre 2019

Any alternative to redundant var in OR conditions? [duplicate]

Is there any smarter and shorter way in Java, to avoid repeating long varnames in OR conditions?

if (objectOneWithLongName.getNumber() == 12 || objectOneWithLongName.getNumber() == 13) {
  someCode();
}

Of cause we could locally shorten the var, but i think this reduces the readability in meaning in most cases.

int x = objectOneWithLongName.getNumber();
if (x == 12 || x == 13) {
  someCode();
}

i wish there would be anything like this:

if (objectOneWithLongName.getNumber() == [12 || 13]) {
  someCode();
}

I think this would be a huge advantage for coders, to have something like that.
what do you think?
is it a bad idea and if so, why?
Where there already any thoughts on this subject by the creator of java and they dismissed it?

Aucun commentaire:

Enregistrer un commentaire