H
H
hell0_w0rId2018-12-22 21:14:33
C++ / C#
hell0_w0rId, 2018-12-22 21:14:33

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;
}

The code is exactly the same, only different names of variables and functions.
Is it worth it to shorten the code? And How?
I also dug around the project and saw that there are a lot of macros in the main file. I think they need to be put in a separate file.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
D
devalone, 2018-12-22
@hell0_w0rId

Of course you need it, copy-paste is always bad, you can make mistakes when refactoring, adding new flags and changing the general logic.

A
Adamos, 2018-12-23
@Adamos

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.

V
Victor Bomberow, 2018-12-22
@majstar_Zubr

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 question

Ask a Question

731 491 924 answers to any question