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 Dec 27 '18 at 16:24
shmuelsshmuels
796
796
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@AuraHandledhas aBooleanargument, then I'm at the mercy of Aura. Unless of course I set a default.
– shmuels
Dec 27 '18 at 16:41
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
Dec 27 '18 at 16:45
Note also Apex REST use case wherein deserialized Json into custom Apex types can easily yield null values for Booleans
– cropredy
Dec 28 '18 at 2:40
@cropredy Yes, there's places where it can happen. Developers should be aware of this and plan accordingly. Still, if you have full control over the moving parts, one should prefer to avoid nulls as much as possible. A good portion of null checks can be avoided with a little extra care.
– sfdcfox
Dec 28 '18 at 3:44
I wish my colleagues would read 'Say No to Null' - I've seen code littered with null checks that make figuring out what the method does nigh impossible (forest from trees).
– cropredy
Dec 28 '18 at 3:53
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@AuraHandledhas aBooleanargument, then I'm at the mercy of Aura. Unless of course I set a default.
– shmuels
Dec 27 '18 at 16:41
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
Dec 27 '18 at 16:45
Note also Apex REST use case wherein deserialized Json into custom Apex types can easily yield null values for Booleans
– cropredy
Dec 28 '18 at 2:40
@cropredy Yes, there's places where it can happen. Developers should be aware of this and plan accordingly. Still, if you have full control over the moving parts, one should prefer to avoid nulls as much as possible. A good portion of null checks can be avoided with a little extra care.
– sfdcfox
Dec 28 '18 at 3:44
I wish my colleagues would read 'Say No to Null' - I've seen code littered with null checks that make figuring out what the method does nigh impossible (forest from trees).
– cropredy
Dec 28 '18 at 3:53
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@AuraHandledhas aBooleanargument, then I'm at the mercy of Aura. Unless of course I set a default.
– shmuels
Dec 27 '18 at 16:41
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
Dec 27 '18 at 16:45
Note also Apex REST use case wherein deserialized Json into custom Apex types can easily yield null values for Booleans
– cropredy
Dec 28 '18 at 2:40
@cropredy Yes, there's places where it can happen. Developers should be aware of this and plan accordingly. Still, if you have full control over the moving parts, one should prefer to avoid nulls as much as possible. A good portion of null checks can be avoided with a little extra care.
– sfdcfox
Dec 28 '18 at 3:44
I wish my colleagues would read 'Say No to Null' - I've seen code littered with null checks that make figuring out what the method does nigh impossible (forest from trees).
– cropredy
Dec 28 '18 at 3:53
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 Dec 27 '18 at 16:29
sfdcfoxsfdcfox
249k11191426
249k11191426
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@AuraHandledhas aBooleanargument, then I'm at the mercy of Aura. Unless of course I set a default.
– shmuels
Dec 27 '18 at 16:41
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
Dec 27 '18 at 16:45
Note also Apex REST use case wherein deserialized Json into custom Apex types can easily yield null values for Booleans
– cropredy
Dec 28 '18 at 2:40
@cropredy Yes, there's places where it can happen. Developers should be aware of this and plan accordingly. Still, if you have full control over the moving parts, one should prefer to avoid nulls as much as possible. A good portion of null checks can be avoided with a little extra care.
– sfdcfox
Dec 28 '18 at 3:44
I wish my colleagues would read 'Say No to Null' - I've seen code littered with null checks that make figuring out what the method does nigh impossible (forest from trees).
– cropredy
Dec 28 '18 at 3:53
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@AuraHandledhas aBooleanargument, then I'm at the mercy of Aura. Unless of course I set a default.
– shmuels
Dec 27 '18 at 16:41
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
Dec 27 '18 at 16:45
Note also Apex REST use case wherein deserialized Json into custom Apex types can easily yield null values for Booleans
– cropredy
Dec 28 '18 at 2:40
@cropredy Yes, there's places where it can happen. Developers should be aware of this and plan accordingly. Still, if you have full control over the moving parts, one should prefer to avoid nulls as much as possible. A good portion of null checks can be avoided with a little extra care.
– sfdcfox
Dec 28 '18 at 3:44
I wish my colleagues would read 'Say No to Null' - I've seen code littered with null checks that make figuring out what the method does nigh impossible (forest from trees).
– cropredy
Dec 28 '18 at 3:53
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
Dec 27 '18 at 16:41
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
Dec 27 '18 at 16:41
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
Dec 27 '18 at 16:45
@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
Dec 27 '18 at 16:45
Note also Apex REST use case wherein deserialized Json into custom Apex types can easily yield null values for Booleans
– cropredy
Dec 28 '18 at 2:40
Note also Apex REST use case wherein deserialized Json into custom Apex types can easily yield null values for Booleans
– cropredy
Dec 28 '18 at 2:40
@cropredy Yes, there's places where it can happen. Developers should be aware of this and plan accordingly. Still, if you have full control over the moving parts, one should prefer to avoid nulls as much as possible. A good portion of null checks can be avoided with a little extra care.
– sfdcfox
Dec 28 '18 at 3:44
@cropredy Yes, there's places where it can happen. Developers should be aware of this and plan accordingly. Still, if you have full control over the moving parts, one should prefer to avoid nulls as much as possible. A good portion of null checks can be avoided with a little extra care.
– sfdcfox
Dec 28 '18 at 3:44
I wish my colleagues would read 'Say No to Null' - I've seen code littered with null checks that make figuring out what the method does nigh impossible (forest from trees).
– cropredy
Dec 28 '18 at 3:53
I wish my colleagues would read 'Say No to Null' - I've seen code littered with null checks that make figuring out what the method does nigh impossible (forest from trees).
– cropredy
Dec 28 '18 at 3:53
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.
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