3

I am trying to debug this error.

unexpected syntax: 'missing SEMICOLON at 'if''

public Boolean validateName(String name){
    return if ( name == null || name =='' || name.length() < 3) ? true : false;           
}

Any idea what I am missing?

  • 2
    Try removing if from your return statement. Ternary operators don't need the if part. – Dan Jones Jun 5 '17 at 16:20
  • I'll add it as an answer. :) – Dan Jones Jun 5 '17 at 16:23
10

Ternary operators don't need if defined. So your return statement should just read:

return (name == null || name =='' || name.length() < 3) ? true : false;

Or, as @AdrianLarson pointed out in the comments you could just use:

return (name == null || name =='' || name.length() < 3);

Which saves a few bytes of code whilst still delivering the same result.

Edit

There's a few ways to achieve what you want here, based off of the comments on this post. For instance, from IllusiveBrian:

return String.isEmpty(name);

This returns a boolean value depending on whether or not the String is empty. Pretty self explanitory!

Additionally, Sebastian Kessel mentions you can use:

return String.isBlank(name);

Would also return a boolean value whether or not the String is empty.

The difference between these functions are as follows:

isBlank(inputString)

Returns true if the specified String is white space, empty (''), or null; otherwise, returns false.

isEmpty(inputString)

Returns true if the specified String is empty ('') or null; otherwise, returns false.

These are from the docs.

The long story short is that there's a number of ways to handle your specific use case. You can use many functions that will return either true or false.

But, with respect to ternary operators, you just didn't need the if part meaning if you, for instance, wanted a ternary operator that returned a String, you'd do so as follows:

public static String HelloWorld(Boolean x) {
  return (x) ? 'Hello' : 'World';
}
  • 5
    You don't need the ternary either... – Adrian Larson Jun 5 '17 at 16:34
  • @AdrianLarson Ah, so return (name == null || name =='' || name.length() < 3) would do the same job? That's interesting actually I'd probably not have thought of that. Good to know to golf your code! – Dan Jones Jun 5 '17 at 16:36
  • Wow. it is good to know from pros like you guys. – Java Jun 5 '17 at 20:10
  • 2
    I prefer String.isEmpty(name) to name == null || name == '', myself. You can also use String.isBlank() if you want to also check that the string is not just white space. – IllusiveBrian Jun 5 '17 at 22:26
2

It should be like this, removing if condition.

public Boolean validateName(String name)
{
    return ( name == null || name =='' || name.length() < 3);           
}
  • You can always replace x ? true : false with just x. – Adrian Larson Jun 5 '17 at 16:34
1

As others have stated, you don't use if in a ternary operator. There is also no point in checking both name=='' or name.length() < 3. Instead you can use:

public Boolean validateName(String name) {
    return (name == null || name.length() < 3);
}
  • I would use String.isBlank instead of == null. Just Saying. :) – Sebastian Kessel Jun 5 '17 at 22:43
  • @SebastianKessel nice. I'm a Java programmer who is not familiar at all with the SalesForce Apex language. However, String.isBlank checks for null, empty, and whitespace strings. In this case we only want to check for null or length < 3 – Zachafer Jun 5 '17 at 23:11
  • Sorry I was unclear. I was saying that String.isBlank is overkill in this case. We don't need to check for empty or whitespace strings, we simply need to check if for null or less than 3 characters. String.isBlank will check for null, empty, and whitespace however we will still need to check for length less than 3. – Zachafer Jun 5 '17 at 23:45

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.