2

When programming in a scripting language (Python, Perl, Ruby or R), I try to write functions and keep them in separate files to keep my code modular.

For debugging, I often put the arguments of the function inside the function body and un-comment them as soon as I'm done.

Moreover, I often place a call to the function right after its definition, which I also uncomment when I'm done testing.

So in the end, my file looks like this

def add(a, b):
    # a = 1
    # b = 2
    c = a + b
    return c

# add(1, 2)

Is there a more elegant way to achieve this?

Moreover, I think common practice would be to remove these comments when shipping the code to others. Is there a way to achieve this in a JavaDoc like manner, while keeping "real" comments (i.e. not uncommented code)?

I should mention, that I often write wrapper functions around calls to plot functions in Matplotlib or R (ggplot, etc) and since in these cases the output is a graph, it doesn't fit so well into test-driven development frameworks.

NicolasBourbaki
  • 853
  • 1
  • 6
  • 19
  • What exactly is it that you're hoping to achieve? It's not clear to me what part you're asking about. – Dason Jun 04 '18 at 17:14
  • First, putting the argument values in the function seems kind of pointless—you still need to write the code to call the function, with two arguments, so why ignore the parameter values like that? – abarnert Jun 04 '18 at 17:23
  • As for the second option: One option is to turn those calls into permanent unit tests instead of temporary debugging code. For a bit of extra setup cost, you get a lot more flexibility, and you get free regression tests to make sure you don't break the function later. – abarnert Jun 04 '18 at 17:25
  • *"Is there a more elegant way to achieve this?"* Yes it is called test driven development (TDD) and it involves using a testing framework to create assertions (expectations) as to the intended behavior of a method and then running these tests to ensure the functionality is correct. If it does not pass the test you fix the method until it does pass the test. Terms to look into TDD, Red Green Refactor. – engineersmnky Jun 04 '18 at 17:40
  • If you put each function in a separate `.py` file (which makes it a module), you could also put the call to the function at the end of each one inside an `if __name__ == '__main__':` statement because that way it will only be executed if the `.py` file is run directly—but not when the module the function is in is `import`ed by some other script file. – martineau Jun 04 '18 at 18:01
  • Thank you all for your answers. I was not aware of test driven development, until looking it up just now. My problem is, that I often write wrapper functions to generate plots with Matplotlib or R and in that case, it is not so simple to test the output. – NicolasBourbaki Jun 04 '18 at 20:49

1 Answers1

0

The proper way would be to write some unit tests.
If you want to have your tests very close to a source code, you should check out doctest module, it allows you to run tests placed directly in doc comments.
Neat thingy.
Edit: In yours example it would look like this:

def add(a, b): 
    """ Returns sum of a and b

    >>> add(1, 2)
    3

    """
    c = a + b
    return c

And run the tests like this:

python -m doctest -v yourFile.py
scope
  • 1,967
  • 14
  • 15
  • 2
    Nobody should have down-voted this; it's the correct answer. Tests self-document code while helping you keep it modular. – Phlip Jun 04 '18 at 17:40
  • Thanks for pointing to the doctest framework. Its slightly different approach in comparison to other unit tests is very interesting for me. – NicolasBourbaki Jun 04 '18 at 20:54