I have a if condition in java that reoccurs at many places in the code. I want to avoid writing the whole condition again and again. In C, I could have done this with #define
#define cond ((i==2) && (j==5) && (k==8))
int main() {
if(cond)
}
How can I achieve the same in java? I can probably create another method that evaluates this condition -
main() {
if(cond())
}
cond() {
return (i==2) && (j==5) && (k==8);
}
but I wanted to know if I can avoid creating another function.
UPDATE -
I realized I should add more details/edit to support my argument. Lets say I have 2 conditions and I want to check both-
#define cond258 ((i==2) && (j==5) && (k==8))
#define cond369 ((i==3) && (j==6) && (k==9))
I can create 2 functions -
cond258(i, j, k) {
return (i==2) && (j==5) && (k==8);
}
cond369(i, j, k) {
return (i==3) && (j==6) && (k==9);
}
this doesn't look like a good approach to me. Both functions are doing sort of similar things so they should be converted to single function -
cond(i, j, k, first, second, third) {
return (i==first) && (j==second) && (k==third);
}
but then that makes my if condition unreadable -
if(cond(i, j, k, 2, 5, 8) || cond(i, j, k, 3, 6, 9))
so instead if I could have some aliases, I could simply write this as
if(cond258 || cond369)