-
January 27th, 2021, 12:40 PM
#1
Not sure what the best way to handle this task is.
I have a problem I am attempting to handle. What I have is a 6 Boolean's and I need to check the state of each of those against their current value in another method, and if any of them are different then I need to send them all at once to a different method. I was thinking it would look something like this:
Code:
checkTheState()
{
bool hasChanged = false;
if(boolOne != boolOneCurrentState())
{
hasChanged = true;
}
if(boolTwo != boolTwoCurrentState())
{
hasChanged = true;
}
...
...
...
if(boolSix != boolSixCurrentState())
{
hasChanged = true;
}
if(hasChanged == true)
{
acceptNewParams(boolOne, boolTwo, boolThree ... boolSix)()
}
}
which I believe would work but it seems rather elementary, and I am guessing there is probably a better way of doing it. Is this method elementary? Is there a more concise, and "nicer" way of doing this kind of operation?
Last edited by Circuits; January 27th, 2021 at 12:51 PM.
-
January 27th, 2021, 04:28 PM
#2
Re: Not sure what the best way to handle this task is.
Just add "else" to all but the first "if"!
And don't compare bool value with true.
like
Code:
checkTheState()
{
bool hasChanged = false;
else if(boolOne != boolOneCurrentState())
{
hasChanged = true;
}
else if(boolTwo != boolTwoCurrentState())
{
hasChanged = true;
}
...
...
...
else if(boolSix != boolSixCurrentState())
{
hasChanged = true;
}
if(hasChanged)
{
acceptNewParams(boolOne, boolTwo, boolThree ... boolSix)()
}
}
Victor Nijegorodov
-
January 28th, 2021, 03:59 AM
#3
Re: Not sure what the best way to handle this task is.
Originally Posted by Circuits
Is there a more concise, and "nicer" way of doing this kind of operation?
I would use one of these, probably the first,
Code:
void checkTheState() {
const bool hasChanged =
(boolOne != boolOneCurrentState()) ||
(boolTwo != boolTwoCurrentState()) ||
...
(boolSix != boolSixCurrentState());
if (hasChanged) {
acceptNewParams(boolOne, boolTwo, ..., boolSix);
}
}
void checkTheState() {
if ((boolOne != boolOneCurrentState()) ||
(boolTwo != boolTwoCurrentState()) ||
...
(boolSix != boolSixCurrentState()))
{
acceptNewParams(boolOne, boolTwo, ..., boolSix);
}
}
The || is the logical-OR operator.
Last edited by wolle; January 28th, 2021 at 04:27 AM.
-
January 28th, 2021, 04:38 AM
#4
Re: Not sure what the best way to handle this task is.
The first fundamental question is - why 6 individual bool variables? Why not have say an uint8_t variable where each bit represents a specific state. It's easy to check/set a particular bit and to test if two are equal etc.
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|