Simplicity is the Ultimate Sophistication

One of the great things about Python is its built-in support for functional programming where you can pass anonymous functions to other functions at runtime. This is done through python’s lambda function, which is often used in conjunction with functional concepts like filter()map(), and reduce().

For those who are still just starting out with python, I highly encourage you to take the time to learn about these functions along with Python’s concept of list comprehension. I myself am still rather new to Python but the discovery to these functions made my life a lot easier.

Now this is where the big issue comes in, these functions are made to simplify your code, however, overusing these functions will simplify your code to the point that your code is extremely complicated and hard to manage. I know what you might be thinking, how on earth does a function that is meant to simplify your code actually make it more complex.

Well during my internship here as an Infrastructure Engineer, one of my projects was to create a command-line tool that describes ec2 instances from Amazon Web Services in a table format based on a set of filters from the user. While working on this project, I wrote this interesting line of code:

for name in vpc_name_filters:  
    vpc_id_list +=map(lambda v: v.id, filter(lambda vpc: vpc.tags and filter(lambda t: vpc.tags and t['Key']=='Name' and re.search(name,t['Value']), vpc.tags), ec2.vpcs.all()))

If you understood that code, gold star for you, cause I guarantee you that I won’t even understand what it does a couple months from now. That’s why at the very least it is extremely important to help the future you understand the code and write a simple comment to explain what it does:

#Loops through all vpcs > checks if it has tags > finds the tag with the key value of 'Name' > loops through supplied name regex expressions > adds to the list if regex matched > converts the list of vpcs to just the vpc ids

Now my argument is, even though that is the proudest line of code I have ever written in my life, I am 100% prepared for it to be rejected upon code review just because how difficult it will be to debug and manage in future iterations of the project. A more readable version of that code is as follows:

for vpc in ec2.vpcs.all():  
    if vpc.tags:        
        for tag in vpc.tags:    
            if tag['Key']=='Name':
                for name in vpc_name_filters:
                    if re.search(name, tag['Value']):
                        vpc_id_list.append(vpc)
                        break
vpc_id_list = map(lambda v: v.id, vpc_id_list)  

That brings up the question – which code is more efficient? After performing multiple time tests, the difference is unnoticeable. Some programmers prefer the first version, some prefer the second. Either way, please, for the sake of your own sanity or your company’s sanity, explain your code if you do choose the first version.

Regardless of which version you prefer, I still think that as a developer, it is crucial to challenge yourself. Initially, I didn’t believe that I could transform this algorithm into the two liner that I devised, as I had initially started off with the more readable version. I was amazed by my own abilities and through this experience, I personally feel a lot more comfortable with using these great options offered by Python. Now I had the luxury at working at Vena which allowed me to spend the time to complete this challenge, but regardless, it is important to continue to develop yourself as a developer (pun intended).

I began this post with a quote Leonardo da Vinci? Clare Boothe Luce? Leonard Thiessen? Elizabeth Hillyer? William Gaddis? Eleanor All? Apple Computer Company? Anonymous?, and I’ll leave you today with a quote probably from Martin Fowler,

” Any fool can write code that a computer can understand. Good programmers write code that humans can understand.”

Posted by Roger Lai

Infrastructure Engineering Intern at Vena