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?
Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. It only takes a minute to sign up.
Sign up to join this communityTernary 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';
}
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!
Jun 5, 2017 at 16:36
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.
Jun 5, 2017 at 22:26
It should be like this, removing if
condition.
public Boolean validateName(String name)
{
return ( name == null || name =='' || name.length() < 3);
}
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);
}
String.isBlank
instead of == null
. Just Saying. :)
Jun 5, 2017 at 22:43
String.isBlank
checks for null, empty, and whitespace strings. In this case we only want to check for null or length < 3
Jun 5, 2017 at 23:11
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.
Jun 5, 2017 at 23:45
if
from your return statement. Ternary operators don't need theif
part.