Take the 2-minute tour ×
Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. It's 100% free, no registration required.

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

share|improve this question

1 Answer 1

up vote 5 down vote accepted

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.

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

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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