bug-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Inline `ifdef style` debugging


From: Roger
Subject: Re: Inline `ifdef style` debugging
Date: Mon, 8 Aug 2011 10:38:07 -0800
User-agent: Mutt/1.5.21 (2010-09-15)

> On Mon, Aug 08, 2011 at 01:20:25PM -0400, Steven W. Orr wrote:

>Two things:
>
>1. Worrying about lost cycles every time you call _debug because it checks to 
>see if debug is True is pretty miserly. If you *really* have to worry about 
>that kind of economics then it's questionable whether you're even in the right 
>programming language. Having said that, you can certainly create a function 
>that is defined at startup based on the value of debug. Remember that 
>functions are not declared, their creation is executed.
>
>if (( debug ))
>then
>     _debug()
>     {
>         "$@"
>         # I do question whether this is a viable construct, versus
>         # eval "$@"
>     }
>else
>     _debug()
>     {
>         :
>     }
>fi
>
>2. The other thing is that instead of
>
>#!/bin/bash
>debug=true
>
>at the beginning of the file, you can just say:
>
>#! /bin/bash
>: ${debug:=0}  # or false or whatever feels good.
>
>Then when you want to run the program with debug turned on, just say:
>
>debug=1 prog with various args
>
>and the environment variable will override the global variable of the same 
>name in the program, an only set it for the duration of the program.
>
>Is this helpful?

Yes.

I've decided to use Mr. Williamson's suggestion:

_debug()
{
    [[ $DEBUG != 0 ]] && "$@"
}


The reply above was very helpful and might provide people searching for
"ifdef style debugging" for Bash further info on inline debugging techniques.

The above does complete the task of inline ifdef style debugging, however, I
think it might confuse or slight hinder readability for some beginners.

I'll flag email and restudy it later as I think this "ifdef style" is the
better method versus having a function read every time even though it isn't
executed at all!


-- 
Roger
http://rogerx.freeshell.org/

Attachment: signature.asc
Description: Digital signature


reply via email to

[Prev in Thread] Current Thread [Next in Thread]