0
0

Value of writing super simple functions

Last updated at Posted at 2024-05-25

I write python code at work to make my life easier.
I help implement Asset Management Analytics calculation system.
Asset Managers rely on many analytics such as duration, yield to make informed investment decisions.
The system my company sells to other Asset Managers can calculate such analytics.
When new users buy our system, we have to run analytics on their holdings.
This involves running perl script, which is a wrapper around core analytics program written in C++.
My job is to construct commands that will run analytics on portfolio holdings in batches.

Anyways the story I want to tell today is about the value of writing small functions.

The other day I wrote a function that constructs a loader command.
Loader command looks something like loader.pl -filename /file/path -security_resolution SEDOL,ISIN,CI. In reality the loader command is much longer and much complex with dozens of switches.
Initially I wrote the function in below manner

def get_loader_command(jumpsshobj, filepath, force_args, should_post=False, **kwargs):
    post_switch = " -post" if should_post else ""
    cmd = f"loader.pl -filename {filepath} -security_resolution SEDOL,ISIN,CI"
    if len(force_args) > 0:
        for fkey,fval in force_args:
            cmd += f" -force {fkey}={fval}"
    for key,val in kwargs.items():
        cmd += f" -{key} {val}"
    return cmd

Very soon I realized I was writing many more wrappers similar to above function
and all of them were repeating parts like adding force switches, adding adhoc key/value switches and adding post switch to the command.

So I decided to extract them out to their own function like below

def add_kwargs_to_command(cmd, **kwargs):
    for key,val in kwargs.items():
        cmd += f" -{key}  {val}"
    return cmd

def add_force_args_to_command(cmd, force_args):
    if len(force_args)>0:
        for fkey,fval in force_args:
            cmd += f" -force {fkey}={fval}"
    return cmd

def add_post_switch(cmd, should_post):
    if should_post:
        cmd += " -post"
    return cmd

And I changed original get loader command function to use above utility functions

def get_loader_command(jumpsshobj, filepath, force_args, should_post=False, **kwargs):
    cmd = f"loader.pl -filename {filepath} -security_resolution SEDOL,ISIN,CI"
    cmd = add_force_args_to_command(cmd, force_args)
    cmd = add_kwargs_to_command(cmd, **kwargs)
    cmd = add_post_switch(cmd, should_post)
    return cmd

You might ask why to write dedicated function for such a simple operations of adding key/val args to the command string.

The short answer is REUSABILITY, MODULARIZATION.
One of the benefit I felt strong about is when that small piece of code has issues.
When I find an issue with it, I have to fix it in one place only.
If I didn't separate it out to its own dedicated function, I would have to fix the issue in multiple places.

I hope above real example from actual work helped you understand the value of writing dedicated functions for small code.

Now of course in reality it is not so easy to spot what part of the code can be separated out.

Two rules you could use

  1. Judging by number of lines. If it is a piece of code more than 3 lines and is accomplishing one unit of action, then perhaps it is a candidate to separate out
  2. Judging by how frequently that piece of code is used by other parts of your application. Or how frequently do you foresee that other parts of your application will re-use that piece of code

The latter criteria, i. e. how frequently that piece of code will be used in other parts of your application is stronger criteria for separating it out. I would say if you see even one line of code repeating in more than one function or class, that's the candidate for separating into a separate function or module.

Happy Coding!

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0