Apex `if (Boolean) `. Null exception virtually renders this check useless and unsafe
Being that an if (booleanVariable)
check, where booleanVariable
is null
, throws a NullPointerException
, it would seem that this check is unsafe. If so why would Apex allow it?
This is especially true when coming from Aura, where a checkbox before being checked or unchecked will be undefined (Yes, I know I can do default="false"
).
For now in Apex I can do booleanVariable = booleanVariable == true;
and then do the if (booleanVariable)
check. For obvious reasons this is annoying.
But why Apex, why? Why allow the if (booleanVariable)
check in the first place?
apex aura null-pointer boolean
add a comment |
Being that an if (booleanVariable)
check, where booleanVariable
is null
, throws a NullPointerException
, it would seem that this check is unsafe. If so why would Apex allow it?
This is especially true when coming from Aura, where a checkbox before being checked or unchecked will be undefined (Yes, I know I can do default="false"
).
For now in Apex I can do booleanVariable = booleanVariable == true;
and then do the if (booleanVariable)
check. For obvious reasons this is annoying.
But why Apex, why? Why allow the if (booleanVariable)
check in the first place?
apex aura null-pointer boolean
add a comment |
Being that an if (booleanVariable)
check, where booleanVariable
is null
, throws a NullPointerException
, it would seem that this check is unsafe. If so why would Apex allow it?
This is especially true when coming from Aura, where a checkbox before being checked or unchecked will be undefined (Yes, I know I can do default="false"
).
For now in Apex I can do booleanVariable = booleanVariable == true;
and then do the if (booleanVariable)
check. For obvious reasons this is annoying.
But why Apex, why? Why allow the if (booleanVariable)
check in the first place?
apex aura null-pointer boolean
Being that an if (booleanVariable)
check, where booleanVariable
is null
, throws a NullPointerException
, it would seem that this check is unsafe. If so why would Apex allow it?
This is especially true when coming from Aura, where a checkbox before being checked or unchecked will be undefined (Yes, I know I can do default="false"
).
For now in Apex I can do booleanVariable = booleanVariable == true;
and then do the if (booleanVariable)
check. For obvious reasons this is annoying.
But why Apex, why? Why allow the if (booleanVariable)
check in the first place?
apex aura null-pointer boolean
apex aura null-pointer boolean
asked 3 hours ago
shmuels
435
435
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
It's allowed because a Boolean is already a Boolean; the syntax for an if statement reads if(condition)
, where condition is a Boolean value. It's considered a best practice to always initialize your variables (e.g. read Say No To Null). In the majority of cases, nulls are handled automatically, but developers should take care of situations where it may have been null, mostly by making sure they always initialize data. The compiler cannot usually tell for certain if a value might be null, so the safest thing to do is to allow it to compile.
Great article. Then I guess the bigger question is why allow Booleans to be null or why throw the exception where null? I can initialize code coming from apex itself. But where my@AuraHandled
has aBoolean
argument, then I'm at the mercy of Aura. Unless of course I set a default.
– shmuels
3 hours ago
5
@shmuels Because (a) all objects are allowed to be null, just like Java, and (b) because null values indicate an unknown value. For practical purposes, you should set default values in Aura, too. It's irresponsible to leave uninitialized values.
– sfdcfox
3 hours ago
add a comment |
Your Answer
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "459"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsalesforce.stackexchange.com%2fquestions%2f244796%2fapex-if-boolean-null-exception-virtually-renders-this-check-useless-and-un%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
It's allowed because a Boolean is already a Boolean; the syntax for an if statement reads if(condition)
, where condition is a Boolean value. It's considered a best practice to always initialize your variables (e.g. read Say No To Null). In the majority of cases, nulls are handled automatically, but developers should take care of situations where it may have been null, mostly by making sure they always initialize data. The compiler cannot usually tell for certain if a value might be null, so the safest thing to do is to allow it to compile.
Great article. Then I guess the bigger question is why allow Booleans to be null or why throw the exception where null? I can initialize code coming from apex itself. But where my@AuraHandled
has aBoolean
argument, then I'm at the mercy of Aura. Unless of course I set a default.
– shmuels
3 hours ago
5
@shmuels Because (a) all objects are allowed to be null, just like Java, and (b) because null values indicate an unknown value. For practical purposes, you should set default values in Aura, too. It's irresponsible to leave uninitialized values.
– sfdcfox
3 hours ago
add a comment |
It's allowed because a Boolean is already a Boolean; the syntax for an if statement reads if(condition)
, where condition is a Boolean value. It's considered a best practice to always initialize your variables (e.g. read Say No To Null). In the majority of cases, nulls are handled automatically, but developers should take care of situations where it may have been null, mostly by making sure they always initialize data. The compiler cannot usually tell for certain if a value might be null, so the safest thing to do is to allow it to compile.
Great article. Then I guess the bigger question is why allow Booleans to be null or why throw the exception where null? I can initialize code coming from apex itself. But where my@AuraHandled
has aBoolean
argument, then I'm at the mercy of Aura. Unless of course I set a default.
– shmuels
3 hours ago
5
@shmuels Because (a) all objects are allowed to be null, just like Java, and (b) because null values indicate an unknown value. For practical purposes, you should set default values in Aura, too. It's irresponsible to leave uninitialized values.
– sfdcfox
3 hours ago
add a comment |
It's allowed because a Boolean is already a Boolean; the syntax for an if statement reads if(condition)
, where condition is a Boolean value. It's considered a best practice to always initialize your variables (e.g. read Say No To Null). In the majority of cases, nulls are handled automatically, but developers should take care of situations where it may have been null, mostly by making sure they always initialize data. The compiler cannot usually tell for certain if a value might be null, so the safest thing to do is to allow it to compile.
It's allowed because a Boolean is already a Boolean; the syntax for an if statement reads if(condition)
, where condition is a Boolean value. It's considered a best practice to always initialize your variables (e.g. read Say No To Null). In the majority of cases, nulls are handled automatically, but developers should take care of situations where it may have been null, mostly by making sure they always initialize data. The compiler cannot usually tell for certain if a value might be null, so the safest thing to do is to allow it to compile.
answered 3 hours ago
sfdcfox
247k11187424
247k11187424
Great article. Then I guess the bigger question is why allow Booleans to be null or why throw the exception where null? I can initialize code coming from apex itself. But where my@AuraHandled
has aBoolean
argument, then I'm at the mercy of Aura. Unless of course I set a default.
– shmuels
3 hours ago
5
@shmuels Because (a) all objects are allowed to be null, just like Java, and (b) because null values indicate an unknown value. For practical purposes, you should set default values in Aura, too. It's irresponsible to leave uninitialized values.
– sfdcfox
3 hours ago
add a comment |
Great article. Then I guess the bigger question is why allow Booleans to be null or why throw the exception where null? I can initialize code coming from apex itself. But where my@AuraHandled
has aBoolean
argument, then I'm at the mercy of Aura. Unless of course I set a default.
– shmuels
3 hours ago
5
@shmuels Because (a) all objects are allowed to be null, just like Java, and (b) because null values indicate an unknown value. For practical purposes, you should set default values in Aura, too. It's irresponsible to leave uninitialized values.
– sfdcfox
3 hours ago
Great article. Then I guess the bigger question is why allow Booleans to be null or why throw the exception where null? I can initialize code coming from apex itself. But where my
@AuraHandled
has a Boolean
argument, then I'm at the mercy of Aura. Unless of course I set a default.– shmuels
3 hours ago
Great article. Then I guess the bigger question is why allow Booleans to be null or why throw the exception where null? I can initialize code coming from apex itself. But where my
@AuraHandled
has a Boolean
argument, then I'm at the mercy of Aura. Unless of course I set a default.– shmuels
3 hours ago
5
5
@shmuels Because (a) all objects are allowed to be null, just like Java, and (b) because null values indicate an unknown value. For practical purposes, you should set default values in Aura, too. It's irresponsible to leave uninitialized values.
– sfdcfox
3 hours ago
@shmuels Because (a) all objects are allowed to be null, just like Java, and (b) because null values indicate an unknown value. For practical purposes, you should set default values in Aura, too. It's irresponsible to leave uninitialized values.
– sfdcfox
3 hours ago
add a comment |
Thanks for contributing an answer to Salesforce Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsalesforce.stackexchange.com%2fquestions%2f244796%2fapex-if-boolean-null-exception-virtually-renders-this-check-useless-and-un%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown