2

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)
Sarthak
  • 57
  • 6
  • 4
    Why do you want to avoid creating another function? – CryptoFool Sep 09 '20 at 18:32
  • 2
    Does this answer your question? [Can I have macros in Java source files](https://stackoverflow.com/questions/6525059/can-i-have-macros-in-java-source-files) – Savior Sep 09 '20 at 18:36
  • 4
    Just add a method to the appropriate class. Don't try to write C when you write Java, it'll end up a mess (the same is true the other way around). – Joachim Sauer Sep 09 '20 at 18:39
  • @Steve , I added some updates to the original question. Hopefully that should explain my side of the situation. – Sarthak Sep 10 '20 at 21:46
  • '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' - how does the same reasoning not apply to macros? How is `if (cond258(i, j, k))` wrong, but `if (cond258)` suddenly OK? – crizzis Sep 13 '20 at 09:13
  • @crizzis, my opinion might be completely wrong here but I see macros as variables that can be created as per my convenience. I can create 2 integers because they hold different value. But for me a function is a processing unit so similar processes should have a common function. – Sarthak Sep 14 '20 at 21:20

3 Answers3

4

I believe you can't do so without writing another function, or at least, not advisable/practical. Writing another method for conditional statement is actually refactoring your code, Decompose conditional in this case.

Hung Vu
  • 443
  • 3
  • 15
  • I agree with the above answer however I would like to add, should you choose to extract this condition into a function, please try to give it a meaningful name that represents the test that you have extracted. This is actually a good practice. – NimbleNavigator Sep 09 '20 at 19:04
1

You could do something like this.

private static int i = 2;
private static int j = 5;
private static int k = 8;
private static Supplier<Boolean> cond  = ()->i == 2 && j == 5 && k == 8;    
public static void main(String[] args) {
    System.out.println(cond.get()); // prints true
    k = 11;
    System.out.println(cond.get()); // prints false
}
  • I made them static so they could be used in any context (static or instance).
  • The variables can't be local since local values in a lambda must be effectively final.
  • And they will be shared with other classes that instantiate the class that contains them.

Here is an example using instance fields.

int ii = 2;
int jj = 5;
int kk = 8;

Supplier<Boolean> cond = () -> ii == 2 && jj == 5 && kk == 8;
    
public static void main(String[] args) {

    ThisClass tc = new ThisClass();

    // static context so they need to be qualified.
    System.out.println(tc.cond.get()); // prints true
    tc.kk = 11;
    System.out.println(tc.cond.get()); // prints false
    tc.foo();
}
public void foo() {
    // instance method so cond and kk do not need to be qualified
    kk = 8;
    System.out.println(cond.get()); // true
}
WJS
  • 36,363
  • 4
  • 24
  • 39
0

This is how I solved this -

created a new class Triplet -

public class Triplet <F, S, T> {
    
    public F first;
    public S second;
    public T third;
    
    @Override
    public boolean equals(Object obj) {

        if (obj instanceof Triplet <?,?,?>) {
            
            Triplet <?, ?, ?> triplet = (Triplet <?, ?, ?>) obj;            
            return (this.first.equals(triplet.first) && this.second.equals(triplet.second) && this.third.equals(triplet.third));
        } 
        else 
            return false;
    }

    
    public Triplet (F first, S second, T third) throws NullPointerException {
        if(first==null|| second==null || third==null)
            throw new NullPointerException("Can't create object with null values for first or second or third");
        this.first = first;
        this.second = second;
        this.third = third;
    }   
}

Created static triplets -

public static final Triplet<Integer, Integer, Integer> triplet258 = new Triplet<Integer, Integer, Integer>(2, 5, 8);
public static final Triplet<Integer, Integer, Integer> triplet369 = new Triplet<Integer, Integer, Integer>(3, 6, 9);

my if condition changes to -

Triplet<Integer, Integer, Integer> myTriplet = new Triplet<Integer, Integer, Integer>(i, j, k);

if(myTriplet.equals(triplet258) || myTriplet.equals(triplet369))
Sarthak
  • 57
  • 6