3

Can anyone describe, what the colon and question mark syntax between the two methods is for?

public PageReference initialize() {
        String profileId = ApexPages.currentPage().getParameters().get('profileId');
        return profileId == null ? initializeView() : initializeUpdate();
 }

Thanks a lot

1 Answer 1

6

This is just another syntax for creating an IF() statement. It is known as a ternary conditional operation. Take a look here for some more info on boolean and conditional statements.

THIS

return (profileId == null) ? initializeView() : initializeUpdate();

Says the same thing as THIS

if(profileId == null){
   return initializeView();
} else {
   return initializeUpdate();
} 

Just helps save some code statements and makes the code a bit more readable.

3
  • Personaly, I like to put the condition into parenthesis for more readability like return (profileId == null) ? initializeView() : initializeUpdate();
    – SF_user
    Commented Aug 8, 2014 at 14:16
  • Good point. That might help segment the parts a bit better to help improve readability. Commented Aug 8, 2014 at 14:17
  • @sfdc_ninja sorry, I deleted my comment after you updated your question but before you replied to my comment
    – BarCotter
    Commented Aug 8, 2014 at 14:18

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .