2

I have the following Boolean assignemnt:

Contact currentContact = new Contact();

currentContact.Is_Manager__c = UserWrapper.isManager() != null ? UserWrapper.isManager() : false;

UserWrapper class has isManager() method that can return either True, False or Null. CurrentContact.Is_Manager__c should either be true or false, not null.

I'm getting "Remove the unnecessary Boolean literal" warning in Sonarqube due to the 'false' in the ternary operator. Is this expected?

1

1 Answer 1

2

I think you meant UserWrapper.isManager() in both instances, rather than User.isManager(). Maybe that's causing the confusing error?

That said, there's a more modern way to write your code, assuming SonarQube supports it:

currentContact.Is_Manager__c = UserWrapper.isManager() ?? false;

The new null coalescing operator greatly improves code readability.

In any case, if the code was just a typo (we all do that from time to time), consider submitting a bug to SonarQube regarding the behavior.

2
  • You’re right, I meant UserWrapper in both instances. In the code that is ok though, the typo happened when writing the question here. It’s great to know about the new coalescing operator but I think the sonar warning will remain the same, since the Boolean literal “false” it’s still there.
    – Teo
    Commented May 22 at 5:45
  • @Teo If so, I would also expect you to get a SonarQube warning: "null" should not be returned from a "Boolean" method on the isManager function. If so, then the ternary/null coalesce is actually redundant. Otherwise, it's a bug in the rule engine to report this as redundant yet not report isManager for misbehaving.
    – sfdcfox
    Commented May 22 at 22:18

You must log in to answer this question.

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