1

The more I advance in coding the more I tend to hang out with people with more advanced knowledge. Some of the things they tell me is that flags should be reduced to a minimum as it can create spaghetti code.

Judging from my own coding style I can only confirm this:

  var active, isWalking, otherState, breathing, flag, flag, flag and so on;

  if (keyPressed) {
    isWalking = true;
  } else {
    isWalking = false;
  }

  if (isWalking && something2) {
    active = true;
  } else {
    active = false;
  }

  if (active && click) {
    otherState = "jump"
  }
  if (otherState == "jump") {
    breakting = "heavy";
  }

It is nice and readable but how do I reduce the number of flags used? What are some best practices?

Asperger
  • 3,064
  • 8
  • 52
  • 100
  • you probably mean flags that are declared in the *global* scope.. – nicholaswmin Dec 31 '15 at 07:50
  • @NicholasKyriakides do they mean global flags or is it flags in general? – Asperger Dec 31 '15 at 07:50
  • I feel there is something wrong with this, be it global scope or not. – Asperger Dec 31 '15 at 07:51
  • Check this: [Are flag variables an absolute evil?](http://programmers.stackexchange.com/questions/173086/are-flag-variables-an-absolute-evil) – Rahul Tripathi Dec 31 '15 at 07:52
  • Directly use isWalking flag. Why are you taking another flag. – Shubham Dec 31 '15 at 07:52
  • @RahulTripathi yes, I saw that and how do I avoid them if possible? – Asperger Dec 31 '15 at 07:53
  • 1
    Flags are only bad if they are used when not really needed. They are not generically bad if required to solve the needs of the application. For us to advise you on how to avoid a specific flag, you would have to describe what you are using the flag for in your application to see if we could develop a better way to do it than by using a flag. This is an issue specific to your particular situation, not a generic issue. You will need to describe the situation for a specific flag. – jfriend00 Dec 31 '15 at 07:53
  • @jfriend00 I will try and edit my code – Asperger Dec 31 '15 at 07:55
  • @Asperger:- As jfriend has just commented, you should avoid it if it is not required. Saying that flags is evil(everytime) will be a debatable topic. You should avoid them if you think that its not required. – Rahul Tripathi Dec 31 '15 at 07:55
  • Also, your `if/else` statement can just be `active = isWalking;` or if `isWalking` isn't necessarily a boolean, you can do `active = !!isWalking;`. – jfriend00 Dec 31 '15 at 07:55
  • @jfriend00 edited my code – Asperger Dec 31 '15 at 08:01
  • @RahulTripathi check out my new code. Sorry if its weird or doesnt make sense. Im not good at creating empty examples – Asperger Dec 31 '15 at 08:02
  • 1
    What you've shown so far looks like a whole bunch of intermediate state flags which might be avoidable. Since they don't all look independent from one another, it looks more like you should figure out which flags truly represent independent state and then just use a boolean logic statement to make a decision based on the real state about what action to carry out. You don't show the actual final action/result you are trying to achieve so we can't really help much more. My guess is that you don't need so many intermediate state flags and can use a compound conditional for much of what you have. – jfriend00 Dec 31 '15 at 08:04

0 Answers0