-2
public boolean healthy()
   {

      if(organic)
      {

         if(toppings<=3)
         {
            if((size=="small"))
            {
               return true;

            }
            else
            {
               return false;
            }
         }
      }

      else
      {
      return false;
      }

   }

The program is saying that I am missing a return statement before the last bracket. But if I were to put one there, there would always be one answer to the boolean which would be true and false. This puts all the other 'if' statements to no use which is a problem. Any help?

Jervis
  • 19
  • 4
ali_h
  • 31
  • 1
  • 3

1 Answers1

4

Help put an end to bad code

public boolean healthy() {
  return (organic && toppings <= 3 && size === "small");
}

Your code had no else branch on toppigs <= 3 which could result in no value being returned.

Also, never write stuff like this

if (something < 2) { 
  return true;
}
else {
  return false;
}

Instead, just write

return something < 2;
Mulan
  • 129,518
  • 31
  • 228
  • 259
  • Like a breath of fresh air. Well... besides for the parentheses, but I'm not one to nitpick *(who am I kidding?)* – shmosel May 17 '16 at 03:56
  • The parentheses are optional yes, but I think they could help just a bit with the readability of the long logic chain. Completely optional, take 'em or leave 'em ^_^. – Mulan May 17 '16 at 03:57
  • 2
    Oh and this is `java`, not `javascript`, so `size.equals("small")`. – shmosel May 17 '16 at 03:58
  • I already fixed it based on how I did it. Everything worked, but there was one small thing I had to add. – ali_h May 17 '16 at 04:09