Reusability

Code reusability

  • Less code written, more work done

  • Writing a tool while doing your analysis

  • Stop reinventing the wheel!

Code reusability: some guidelines

  • Separate code and data: data is specific, code need not be

    • consider using a config file for project-specific (meta)data
    • but DO hard-code unchanging variables, e.g. gravity = 9.80665, once.

. . .

  • Do One Thing (and do it well)

    • One function for one purpose
    • One class for one purpose
    • One script for one purpose (no copy-pasting to recycle it!)

. . .

  • Don’t Repeat Yourself: use functions

  • Write routines in functions, i.e., code you reuse often

  • Identify potential functions by action: functions perform tasks (e.g. sorting, plotting, saving a file, transform data…)

Code reusability through functions

Functions are smaller code units reponsible of one task.

  • Functions are meant to be reused

  • Functions accept arguments (though they may also be empty!)

  • What arguments a function accept is defined by its parameters

Functions do not necessarily make code shorter (at first)!

Compare:

indexATG = [n for n,i in enumerate(myList) if i == 'ATG']
indexAAG = [n for n,i in enumerate(myList) if i == 'AAG']
def indexString(inputList,z):
    zIndex = [n for n,i in enumerate(li) if i == z]
    return zIndex
    
indexATG = indexString(myList,'ATG')
indexAAG = indexString(myList,'AAG')

Think in building blocks!

Small, cohesive units are much better than…

image/svg+xml

… a customized behemoth!

image/svg+xml

Your turn: visualize your code!

Choose:

  • Make a screenshot, process it in paint, powerpoint, or your favorite editor;
  • Copy paste your code to a text editor, and use markers.

The objective is for you to ‘see’ your code!

  • Yellow denotes scripted, unstructured code (basic, sequential lines of instructions)

  • Purple denotes functions or other structured code (e.g. for-loops, conditionals, etc.)

  • Green denotes comments (or comment blocks) (consider combining this with yellow for heavily commented code)

Again, make notes in your code (#TODO!) if you see:

  • Scripted code: this can be a function

What can you learn from your colleagues today?

Your turn: make a function

You have visualized your code. Use your findings to improve it!

  • Preferably: take scripted code and turn it into a function, or split an existing function into two or more functions.

  • If there is no function to work on: try and address the readability of your code.

However: for future exercises you will need at least one function, preferably with parameters, in your code! For example:

def my_function(param_a, param_b):
  if param_b == 99:
    return None
  
  if param_a == 100:
    do_something(param_a)
  else:
    do_something_else(param_a)