Skip to content

Suggestion: cleaner debug code #13

Description

@alphapapa

Hi again,

Just a suggestion you might want to consider. There's a really cool technique for Bash scripts, which I've seen called a "collapsing function." It's especially useful for optional debug output. Here's an example:

function debug {
    # A collapsing function. Cool.
    if [[ $debug ]]
    then
        function debug {
            echo -e "${COLOR_YELLOW}DEBUG: ${@}${COLOR_OFF}" >&2
        }
        debug "$@"
    else
        function debug {
            true
        }
    fi
}

Then to send debug output, you just do:

debug "Something"

instead of having to do:

if [[ $debug ]]
then
  echo something >&2
fi

And by redefining or "collapsing" the function, it avoids the tests when debugging is disabled (though I doubt it matters in a shell script).

Activity

  1. kaushalmodi commented on May 17, 2017

    @kaushalmodi
    Owner

    This is awesome. Thanks! I didn't know this existed. Would you like to work on a PR for this?

  2. alphapapa commented on May 17, 2017

    @alphapapa
    ContributorAuthor

    Ok, I pushed a branch here: https://github.com/alphapapa/eless/tree/collapsing-debug

    I tried to build it according to the instructions, but unfortunately I have several Org-export variables set differently, and this resulted in a lot of whitespace noise and other minor differences in the exported files. It doesn't seem worth the time it would take for me to figure out which variables to change and what to change them to to make the output the same as you get, so I haven't committed the exported changes, just the raw changes to the org file.

  3. kaushalmodi commented on May 17, 2017

    @kaushalmodi
    Owner

    (It's a GitHub "feature".. The commit message had "Fixes Issue#", so it auto-closed this issue.)

    Thanks for that commit! It did get rid of a lot of clutter from eless. I am now using this in all my future bash scripts (and past) :)

    I tried to build it according to the instructions, but unfortunately I have several Org-export variables set differently, and this resulted in a lot of whitespace noise and other minor differences in the exported files.

    Ah, that sucks.. I will figure out how to resolve this so that org based project collaboration becomes easier. May be I just need to have a look at your config for now to see which org-* vars are set differently.

    It doesn't seem worth the time it would take for me to figure out which variables to change and what to change them to to make the output the same as you get, so I haven't committed the exported changes, just the raw changes to the org file.

    No problem. Thanks for that commit!

  4. alphapapa commented on May 17, 2017

    @alphapapa
    ContributorAuthor

    Cool, thanks for fixing it up. :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions