Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

First of all I want to point out my concern with some pseudocode (I think you'll understand better)

Assume you have a global debug flag, or class variable named "debug",

class a :
   var debug = FALSE

and you use it to enable debug methods. There are two types of usage it as I know:

first in a method :

 method a :
      if debug then call method b;

 method b :

second in the method itself:

 method a : 
       call method b;

 method b :
      if not debug exit

And I want to know, is there any File IO or stack pointer wise difference between these two approaches. Which usage is better, safer and why?

share|improve this question
Not sure "execution preventer" is a good term here. I think the term often has the specific meaning of data execution prevention, and I think your case here is one of simple flow control. – Sonia Mar 26 '12 at 18:38
Are you concerned about the API trade-offs as well? – Jeremy Heiler Mar 26 '12 at 18:57
@Sonia: Yes but I couldn't remember "flow control". Sorry. I'm fixing it. – tpaksu Mar 26 '12 at 19:15
@JeremyHeiler: API trade-offs, pointers, I know they are small stuff to worry about because the compiler optimisations etc. But still which one would be fast? – tpaksu Mar 26 '12 at 19:15
up vote 1 down vote accepted

First, I don't think you should be concerned about speed here. My primary concern would be designing a proper API, otherwise speed wont be an issue because nobody would actually use it. Even if this is just for an internal application, I personally like to design things with the intent that someday they can be factored out into their own library.

So what are the API trade-offs? If you should be able to call method b with out having it check the state of the debug variable, then the second example is not an option. You may also be concerned with whether or not the debug variable is part of the public API. If it is, then exposing an isDebug method and allowing the users to decide when method b should be called might make sense. However, if method b has no other reason to exist other than to be called when debugging is on, then it does makes sense to encapsulate the conditional inside the method.

share|improve this answer
Ok, but it would be like this too: if(!cart_is_empty){ checkout(); }else{error} or checkout() { if (cart_is_empty) return false; } and you would be checking if something went wrong with using if(!checkout()){error} instead of using else. Which is right? – tpaksu Mar 26 '12 at 19:35
@tpaksu: Either can be correct. It really depends on how you expect your API to be used. Naively, I would expect a checkout() function to error or explicitly say there is nothing in the cart, but in that case I am assuming that it is a high-level API. But if your API is meant to be more low-level, where error checking is done before the method, then that is a valid use case. – Jeremy Heiler Mar 26 '12 at 19:44
Now I got that. "If you have no money, why did you enter the market?" right? – tpaksu Mar 26 '12 at 19:49
Basically. It's all about scope. – Jeremy Heiler Mar 26 '12 at 19:54

Ignoring compiler optimizations, of course testing to avoid a function call (your first solution) can avoid whatever overhead is associated with the function call. This is not specific to global debug flags though, and is certainly small stuff in the bigger picture. If you have any reason at all to prefer the second solution, that's fine. Worry about other stuff.

share|improve this answer
Got it. So you say avoiding calls is better if your method b doesn't execute at all if you call with that flag set. and if you have something still running inside method b you can use the second option. So in my case you say first option. – tpaksu Mar 26 '12 at 19:42
Yes. In the case you present in the original question, I would use the first option. In general though there are cases where it might read better the other way. It's good to consider other ways of doing things, as in the checkout discussion. – Sonia Mar 26 '12 at 20:13

First method: O(n) in methods * O(m) in call sites.

Second method: O(n) methods.

It's clear that the first method requires way too much code and violates DRY all over the place.

share|improve this answer
Ok, but it's a variable control instead of method call at first method. At second, both method call and variable control is executed. – tpaksu Mar 26 '12 at 22:07

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.