Best way to check the null for combination of two object
I wanted to ask this question since long time. After spending some time I feel like not getting a proper way to check two objects for null together, so i am asking it.
For example : I have one function like this
int Function(Object* obj1, Object* obj2)
{
if(obj1 == null && obj2 == null)
{
// Do someting........
}
else if(obj1 == null && obj2 != null)
{
// Do something.....
}
else if(obj1 != null && obj2 == null)
{
// Do something......
}
else
{
// Do something........
}
return 0;
As we can see, we have too many if condition just to check the combination of two object for null.
Is there any other way to do it more effectively so that the readability is good?
Note : Object is a class and no operator is overloaded.
c++ coding-style
add a comment |
I wanted to ask this question since long time. After spending some time I feel like not getting a proper way to check two objects for null together, so i am asking it.
For example : I have one function like this
int Function(Object* obj1, Object* obj2)
{
if(obj1 == null && obj2 == null)
{
// Do someting........
}
else if(obj1 == null && obj2 != null)
{
// Do something.....
}
else if(obj1 != null && obj2 == null)
{
// Do something......
}
else
{
// Do something........
}
return 0;
As we can see, we have too many if condition just to check the combination of two object for null.
Is there any other way to do it more effectively so that the readability is good?
Note : Object is a class and no operator is overloaded.
c++ coding-style
1
Your code is clear and readable to me.
– Jabberwocky
Nov 16 at 10:05
2
Object
being a class object or not isn't relevant here.
– Jabberwocky
Nov 16 at 10:06
1
You can reduce branches by combining them, but this largely dependents on the actual code inside each block. And giving each nullptr test a name (makes them functions) is always a viable solution.
– felix
Nov 16 at 10:21
1
Do you have to accept nulls in your function? Maybe it would be better to forbid passing nullptr to this function (and throw something if that happens).
– Yksisarvinen
Nov 16 at 10:26
1
Your code is clear with detail if condition, it keeps the code easier to maintain and to debug. Put the most passed test in the first if condition to get better performance.
– tunglt
Nov 16 at 10:30
add a comment |
I wanted to ask this question since long time. After spending some time I feel like not getting a proper way to check two objects for null together, so i am asking it.
For example : I have one function like this
int Function(Object* obj1, Object* obj2)
{
if(obj1 == null && obj2 == null)
{
// Do someting........
}
else if(obj1 == null && obj2 != null)
{
// Do something.....
}
else if(obj1 != null && obj2 == null)
{
// Do something......
}
else
{
// Do something........
}
return 0;
As we can see, we have too many if condition just to check the combination of two object for null.
Is there any other way to do it more effectively so that the readability is good?
Note : Object is a class and no operator is overloaded.
c++ coding-style
I wanted to ask this question since long time. After spending some time I feel like not getting a proper way to check two objects for null together, so i am asking it.
For example : I have one function like this
int Function(Object* obj1, Object* obj2)
{
if(obj1 == null && obj2 == null)
{
// Do someting........
}
else if(obj1 == null && obj2 != null)
{
// Do something.....
}
else if(obj1 != null && obj2 == null)
{
// Do something......
}
else
{
// Do something........
}
return 0;
As we can see, we have too many if condition just to check the combination of two object for null.
Is there any other way to do it more effectively so that the readability is good?
Note : Object is a class and no operator is overloaded.
c++ coding-style
c++ coding-style
asked Nov 16 at 10:01
Nihal Kumar
528
528
1
Your code is clear and readable to me.
– Jabberwocky
Nov 16 at 10:05
2
Object
being a class object or not isn't relevant here.
– Jabberwocky
Nov 16 at 10:06
1
You can reduce branches by combining them, but this largely dependents on the actual code inside each block. And giving each nullptr test a name (makes them functions) is always a viable solution.
– felix
Nov 16 at 10:21
1
Do you have to accept nulls in your function? Maybe it would be better to forbid passing nullptr to this function (and throw something if that happens).
– Yksisarvinen
Nov 16 at 10:26
1
Your code is clear with detail if condition, it keeps the code easier to maintain and to debug. Put the most passed test in the first if condition to get better performance.
– tunglt
Nov 16 at 10:30
add a comment |
1
Your code is clear and readable to me.
– Jabberwocky
Nov 16 at 10:05
2
Object
being a class object or not isn't relevant here.
– Jabberwocky
Nov 16 at 10:06
1
You can reduce branches by combining them, but this largely dependents on the actual code inside each block. And giving each nullptr test a name (makes them functions) is always a viable solution.
– felix
Nov 16 at 10:21
1
Do you have to accept nulls in your function? Maybe it would be better to forbid passing nullptr to this function (and throw something if that happens).
– Yksisarvinen
Nov 16 at 10:26
1
Your code is clear with detail if condition, it keeps the code easier to maintain and to debug. Put the most passed test in the first if condition to get better performance.
– tunglt
Nov 16 at 10:30
1
1
Your code is clear and readable to me.
– Jabberwocky
Nov 16 at 10:05
Your code is clear and readable to me.
– Jabberwocky
Nov 16 at 10:05
2
2
Object
being a class object or not isn't relevant here.– Jabberwocky
Nov 16 at 10:06
Object
being a class object or not isn't relevant here.– Jabberwocky
Nov 16 at 10:06
1
1
You can reduce branches by combining them, but this largely dependents on the actual code inside each block. And giving each nullptr test a name (makes them functions) is always a viable solution.
– felix
Nov 16 at 10:21
You can reduce branches by combining them, but this largely dependents on the actual code inside each block. And giving each nullptr test a name (makes them functions) is always a viable solution.
– felix
Nov 16 at 10:21
1
1
Do you have to accept nulls in your function? Maybe it would be better to forbid passing nullptr to this function (and throw something if that happens).
– Yksisarvinen
Nov 16 at 10:26
Do you have to accept nulls in your function? Maybe it would be better to forbid passing nullptr to this function (and throw something if that happens).
– Yksisarvinen
Nov 16 at 10:26
1
1
Your code is clear with detail if condition, it keeps the code easier to maintain and to debug. Put the most passed test in the first if condition to get better performance.
– tunglt
Nov 16 at 10:30
Your code is clear with detail if condition, it keeps the code easier to maintain and to debug. Put the most passed test in the first if condition to get better performance.
– tunglt
Nov 16 at 10:30
add a comment |
2 Answers
2
active
oldest
votes
You need to perform at most two comparisons. This is how I would do it:
if( obj1 )
{
if( obj2 )
{
// obj1 and obj2 are both valid.
}
else
{
// obj1 is valid, obj2 is nullptr.
}
}
else
{
if( obj2 )
{
// obj1 is nullptr, obj2 is valid.
}
else
{
// Both are nullptr.
}
}
add a comment |
The main idea is to reduce the number of if...else branches.
Maybe, some kind of dispatcher, something like that:
int ptr_pair_state(const obj1 *p1, const obj1 *p2)
{
return (p1 ? 1 : 0) | (p2 ? 2 : 0);
}
int Function(Object* obj1, Object* obj2)
{
std::map<int, std::function<void()>> disp{ { 0, f1 }, { 1, f2 }, { 2, f3 }, {3, f4 } };
int state = ptr_pair_state(obj1, obj2);
disp[state]();
}
where f1 - f4 are some functions, for example
void f1()
{
std::cout << "f1";
}
However, this may seem like an unnecessary complication, so another simple alternative - to use switch operator:
int Function(Object* obj1, Object* obj2)
{
int v = (p1 ? 1 : 0) | (p2 ? 2 : 0);
switch(v)
{
case 0: // both nullptr
break;
case 1: // p1 not nullptr
break;
case 2: // p2 not nullptr
break;
case 3: // both not nullptr
break;
}
}
which much more elegant then endless if...else if
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
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: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
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%2fstackoverflow.com%2fquestions%2f53335450%2fbest-way-to-check-the-null-for-combination-of-two-object%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
You need to perform at most two comparisons. This is how I would do it:
if( obj1 )
{
if( obj2 )
{
// obj1 and obj2 are both valid.
}
else
{
// obj1 is valid, obj2 is nullptr.
}
}
else
{
if( obj2 )
{
// obj1 is nullptr, obj2 is valid.
}
else
{
// Both are nullptr.
}
}
add a comment |
You need to perform at most two comparisons. This is how I would do it:
if( obj1 )
{
if( obj2 )
{
// obj1 and obj2 are both valid.
}
else
{
// obj1 is valid, obj2 is nullptr.
}
}
else
{
if( obj2 )
{
// obj1 is nullptr, obj2 is valid.
}
else
{
// Both are nullptr.
}
}
add a comment |
You need to perform at most two comparisons. This is how I would do it:
if( obj1 )
{
if( obj2 )
{
// obj1 and obj2 are both valid.
}
else
{
// obj1 is valid, obj2 is nullptr.
}
}
else
{
if( obj2 )
{
// obj1 is nullptr, obj2 is valid.
}
else
{
// Both are nullptr.
}
}
You need to perform at most two comparisons. This is how I would do it:
if( obj1 )
{
if( obj2 )
{
// obj1 and obj2 are both valid.
}
else
{
// obj1 is valid, obj2 is nullptr.
}
}
else
{
if( obj2 )
{
// obj1 is nullptr, obj2 is valid.
}
else
{
// Both are nullptr.
}
}
answered Nov 16 at 11:38
James
492
492
add a comment |
add a comment |
The main idea is to reduce the number of if...else branches.
Maybe, some kind of dispatcher, something like that:
int ptr_pair_state(const obj1 *p1, const obj1 *p2)
{
return (p1 ? 1 : 0) | (p2 ? 2 : 0);
}
int Function(Object* obj1, Object* obj2)
{
std::map<int, std::function<void()>> disp{ { 0, f1 }, { 1, f2 }, { 2, f3 }, {3, f4 } };
int state = ptr_pair_state(obj1, obj2);
disp[state]();
}
where f1 - f4 are some functions, for example
void f1()
{
std::cout << "f1";
}
However, this may seem like an unnecessary complication, so another simple alternative - to use switch operator:
int Function(Object* obj1, Object* obj2)
{
int v = (p1 ? 1 : 0) | (p2 ? 2 : 0);
switch(v)
{
case 0: // both nullptr
break;
case 1: // p1 not nullptr
break;
case 2: // p2 not nullptr
break;
case 3: // both not nullptr
break;
}
}
which much more elegant then endless if...else if
add a comment |
The main idea is to reduce the number of if...else branches.
Maybe, some kind of dispatcher, something like that:
int ptr_pair_state(const obj1 *p1, const obj1 *p2)
{
return (p1 ? 1 : 0) | (p2 ? 2 : 0);
}
int Function(Object* obj1, Object* obj2)
{
std::map<int, std::function<void()>> disp{ { 0, f1 }, { 1, f2 }, { 2, f3 }, {3, f4 } };
int state = ptr_pair_state(obj1, obj2);
disp[state]();
}
where f1 - f4 are some functions, for example
void f1()
{
std::cout << "f1";
}
However, this may seem like an unnecessary complication, so another simple alternative - to use switch operator:
int Function(Object* obj1, Object* obj2)
{
int v = (p1 ? 1 : 0) | (p2 ? 2 : 0);
switch(v)
{
case 0: // both nullptr
break;
case 1: // p1 not nullptr
break;
case 2: // p2 not nullptr
break;
case 3: // both not nullptr
break;
}
}
which much more elegant then endless if...else if
add a comment |
The main idea is to reduce the number of if...else branches.
Maybe, some kind of dispatcher, something like that:
int ptr_pair_state(const obj1 *p1, const obj1 *p2)
{
return (p1 ? 1 : 0) | (p2 ? 2 : 0);
}
int Function(Object* obj1, Object* obj2)
{
std::map<int, std::function<void()>> disp{ { 0, f1 }, { 1, f2 }, { 2, f3 }, {3, f4 } };
int state = ptr_pair_state(obj1, obj2);
disp[state]();
}
where f1 - f4 are some functions, for example
void f1()
{
std::cout << "f1";
}
However, this may seem like an unnecessary complication, so another simple alternative - to use switch operator:
int Function(Object* obj1, Object* obj2)
{
int v = (p1 ? 1 : 0) | (p2 ? 2 : 0);
switch(v)
{
case 0: // both nullptr
break;
case 1: // p1 not nullptr
break;
case 2: // p2 not nullptr
break;
case 3: // both not nullptr
break;
}
}
which much more elegant then endless if...else if
The main idea is to reduce the number of if...else branches.
Maybe, some kind of dispatcher, something like that:
int ptr_pair_state(const obj1 *p1, const obj1 *p2)
{
return (p1 ? 1 : 0) | (p2 ? 2 : 0);
}
int Function(Object* obj1, Object* obj2)
{
std::map<int, std::function<void()>> disp{ { 0, f1 }, { 1, f2 }, { 2, f3 }, {3, f4 } };
int state = ptr_pair_state(obj1, obj2);
disp[state]();
}
where f1 - f4 are some functions, for example
void f1()
{
std::cout << "f1";
}
However, this may seem like an unnecessary complication, so another simple alternative - to use switch operator:
int Function(Object* obj1, Object* obj2)
{
int v = (p1 ? 1 : 0) | (p2 ? 2 : 0);
switch(v)
{
case 0: // both nullptr
break;
case 1: // p1 not nullptr
break;
case 2: // p2 not nullptr
break;
case 3: // both not nullptr
break;
}
}
which much more elegant then endless if...else if
edited Nov 16 at 11:15
answered Nov 16 at 10:21
snake_style
1,171310
1,171310
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- 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%2fstackoverflow.com%2fquestions%2f53335450%2fbest-way-to-check-the-null-for-combination-of-two-object%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
1
Your code is clear and readable to me.
– Jabberwocky
Nov 16 at 10:05
2
Object
being a class object or not isn't relevant here.– Jabberwocky
Nov 16 at 10:06
1
You can reduce branches by combining them, but this largely dependents on the actual code inside each block. And giving each nullptr test a name (makes them functions) is always a viable solution.
– felix
Nov 16 at 10:21
1
Do you have to accept nulls in your function? Maybe it would be better to forbid passing nullptr to this function (and throw something if that happens).
– Yksisarvinen
Nov 16 at 10:26
1
Your code is clear with detail if condition, it keeps the code easier to maintain and to debug. Put the most passed test in the first if condition to get better performance.
– tunglt
Nov 16 at 10:30