The below class works fine, in that it declares my four Booleans and performs some logic to assign them an appropriate state.

I don't like the getMultiCheck method. Can it be written any better? It's supposed to check for the states of all four Booleans and if they all return false, return false itself, it does it's job, I'm just wondering can it be written any better?

public Boolean A {get; set;}
public Boolean B {get; set;}
public Boolean C {get; set;}
public Boolean D {get; set;}

public myClass(){

    A = //SOME LOGIC
    B = //SOME LOGIC
    C = //SOME LOGIC
    D = //SOME LOGIC

public Boolean getMultiCheck(){

    Boolean checkr = true;

    if(A == false || B == false || C == false || D == false){
        checkr = false;

    return checkr;    
  • 2
    Have you tried return (A&B&C&D);
    – highfive
    Sep 4 '14 at 9:00
  • 1
    @highfive Curiously that doesn't work in Apex. Try the following in Execute Anonymous Boolean getBool() { Boolean a, b, c, d; a = false; b = false; c = false; d = false; return (a&b&c&d); } Boolean r = getBool(); System.debug(r); Sep 4 '14 at 9:19
  • Oops, it should be return (A && B && C && D); ryt?
    – highfive
    Sep 4 '14 at 9:27
  • That would return true if they are all true. Sep 4 '14 at 9:30

Before you start to improve your code, you need to look at your logic.

You say that you want to return false when all of your booleans are false. However you code uses the || operator, which is OR not AND. The AND operator is &&.

Rather than doing == false you can use the NOT operator (!) to check the inverse of your boolean.

Boolean b = true;
System.debug(!b); // Prints false

Boolean b2 = false;
System.debug(!b2); // Prints true

With that all said, you'd think I'm going to suggest that you should write your code as follows:

return !A && !B && !C && !D;

Actually, what I think you should do is think about what you're doing slightly differently.

Rather than thinking 'if ALL of my booleans are false, return false', think of it as 'if ANY of my booleans are true return true'.

Which gives you the following:

return (A || B || C || D);
return (A || B || C || D); 

This would be a solution to check if the method returns FALSE OR TRUE. In Case if you want to code it out, try ternary operations

Checkr = (A || B || C || D) ? TRUE : FALSE ; // something like this to simplify your code

  • What is the purpose of the ternary operator here? You've effective written if(expression) return true; else return false;. In which case you might as well just use the value of the expression. Sep 4 '14 at 9:35
  • The user who asked this question has used a loc variable , instead of using IFelse statements , we can go for ternary to reduce the number of lines of code and its easy to add a test class for that. My first solution was to provide a single step answer. RETURN (A||B||C||D)
    – AjaySFDC
    Sep 4 '14 at 10:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.