www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Debug arguments?

reply "bearophile" <bearophileHUGS lycos.com> writes:
Sometimes you want to give arguments to a function that are only 
used for debug builds. So is it a good idea to introduce debug 
arguments (only allowed as trailing arguments, like the arguments 
with a default value)?


import std.stdio;
void foo(ref int x, debug int y) {
     x++;
     debug writeln(y);
}
void main() {
     int a, b;
     foo(a, debug b);
}


That is equivalent to code like:

import std.stdio;
debug {
     void foo(ref int x, int y) {
         x++;
         writeln(y);
     }
} else {
     void foo(ref int x) {
         x++;
     }
}
void main() {
     int a, b;
     debug {
         foo(a, b);
     } else {
         foo(a);
     }
}


This avoids to pass useless arguments, and it documents (in the 
code) that certain arguments are not used in a function in 
non-debug builds.

(The "debug" at the calling point is not necessary, but it 
documents better the meaning of the code).

Bye,
bearophile
May 13 2014
next sibling parent "Daniel Murphy" <yebbliesnospam gmail.com> writes:
"bearophile"  wrote in message news:dmpcwdctnuenxdlpfysl forum.dlang.org...

 Sometimes you want to give arguments to a function that are only used for 
 debug builds. So is it a good idea to introduce debug arguments (only 
 allowed as trailing arguments, like the arguments with a default value)?
I don't think this is a good idea. I've never liked code that did this using macros, and it wasn't just because it was using macros.
May 13 2014
prev sibling next sibling parent reply "Gary Willoughby" <dev nomad.so> writes:
On Tuesday, 13 May 2014 at 09:38:51 UTC, bearophile wrote:
 Sometimes you want to give arguments to a function that are 
 only used for debug builds. So is it a good idea to introduce 
 debug arguments (only allowed as trailing arguments, like the 
 arguments with a default value)?
I think i have to agree with Daniel. Without trying to sound condescending, if you need to pass certain arguments only in debug builds something has gone terribly wrong with the design of your program. IMHO good OOP design would remove the need.
May 13 2014
parent reply "bearophile" <bearophileHUGS lycos.com> writes:
Gary Willoughby:

 Without trying to sound condescending, if you need to pass 
 certain arguments only in debug builds something has gone 
 terribly wrong with the design of your program. IMHO good OOP 
 design would remove the need.
I think often OOP obfuscates the actual flow of data between functions (methods). So I even suggested to give D a kind of its opposite: https://d.puremagic.com/issues/show_bug.cgi?id=5007 In the debug build inside some functions I print some of the current state, as modified by a function. The extra arguments for the debug build are needed to produce a better or more complete print. But in release builds I'd like to avoid passing extra arguments that are useless. Bye, bearophile
May 13 2014
parent reply Ary Borenszweig <ary esperanto.org.ar> writes:
On 5/13/14, 2:35 PM, bearophile wrote:
 Gary Willoughby:

 Without trying to sound condescending, if you need to pass certain
 arguments only in debug builds something has gone terribly wrong with
 the design of your program. IMHO good OOP design would remove the need.
I think often OOP obfuscates the actual flow of data between functions (methods). So I even suggested to give D a kind of its opposite: https://d.puremagic.com/issues/show_bug.cgi?id=5007 In the debug build inside some functions I print some of the current state, as modified by a function. The extra arguments for the debug build are needed to produce a better or more complete print. But in release builds I'd like to avoid passing extra arguments that are useless. Bye, bearophile
IMHO the compiler should be able to remove the unused arguments for you.
May 13 2014
parent "bearophile" <bearophileHUGS lycos.com> writes:
Ary Borenszweig:

 IMHO the compiler should be able to remove the unused arguments 
 for you.
That doesn't happen even for global module-private functions. Thank you for all the answers. Bye, bearophile
May 13 2014
prev sibling next sibling parent Dmitry Olshansky <dmitry.olsh gmail.com> writes:
13-May-2014 13:38, bearophile пишет:
 Sometimes you want to give arguments to a function that are only used
 for debug builds. So is it a good idea to introduce debug arguments
NO. -- Dmitry Olshansky
May 13 2014
prev sibling parent Walter Bright <newshound2 digitalmars.com> writes:
On 5/13/2014 2:38 AM, bearophile wrote:
 Sometimes you want to give arguments to a function that are only used for debug
 builds. So is it a good idea to introduce debug arguments (only allowed as
 trailing arguments, like the arguments with a default value)?
Optional arguments interact badly with overloading. They make overloads very hard to reason about, and overloading is complex enough already.
May 13 2014