Answer the question
In order to leave comments, you need to log in
Should the code be shortened?
I was looking at someone's source code and came across this:
struct
{
bool btn_state: 1;
bool btn_flag: 1;
bool hold_flag: 1;
bool counter_flag: 1;
bool isHolded_f: 1;
bool isRelease_f: 1;
bool isPress_f: 1;
bool step_flag: 1;
bool oneClick_f: 1;
bool isOne_f: 1;
} fl;
fl flags;
// ........
boolean GButton::isPress() {
if (_tickMode) GButton::tick();
if (flags.isPress_f) {
flags.isPress_f = false;
return true;
} else return false;
}
boolean GButton::isRelease() {
if (_tickMode) GButton::tick();
if (flags.isRelease_f) {
flags.isRelease_f = false;
return true;
} else return false;
}
boolean GButton::isClick() {
if (_tickMode) GButton::tick();
if (flags.isOne_f) {
flags.isOne_f = false;
return true;
} else return false;
}
boolean GButton::isHolded() {
if (_tickMode) GButton::tick();
if (flags.isHolded_f) {
flags.isHolded_f = false;
return true;
} else return false;
}
Answer the question
In order to leave comments, you need to log in
Of course you need it, copy-paste is always bad, you can make mistakes when refactoring, adding new flags and changing the general logic.
The code can be shortened if there is such a desire and there are no more pressing matters.
You don't have to shorten the code - it works, it doesn't contain any pitfalls - and its performance, as analysts have already found out, is completely uncritical.
If you have to constantly read it and this copy-paste wastes the time of the reader, it is better to add a comment with explanations to stop this.
No, everything is simple, understandable, and most importantly - it works quickly. If reduced, performance may suffer.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question