5 Pro-Tips For Data Scientists To Write Good Code
By: Jason Byrne Phd
Tags: Data Science Skills, Towards Data Science, Data Scientist, Data Science, Coding

Photo by luis gomes from Pexels

5 Pro-Tips For Data Scientists To Write Good Code

The following advice is built from my experience moving from academia to working as a data scientist on a variety of projects across different data & engineering teams. Many data scientists (myself included) do not come from a computer science or software development background, so may not have formal training or good habits in code writing. These tips should help new or aspiring data scientists work collaboratively to write good code and build models in a way that will be easier to productionize.

Use Version Control

This is important for both collaboration and backups. It allows us to track the changes to a project as it undergoes development, useful for coordinating tasks and encouraging due diligence. Git is a powerful version control software, with the ability to branch parts of the development, track and commit changes, push and fetch from remote repositories, and merge code pieces together overcoming conflicts as necessary (https://git-scm.com/).

For additional details see my slideshare “Version Control & Git”

Make it Readable

A key component of collaborative coding is the ability to hand it over to other developers for review and use, meaning it has to be readable. This includes using appropriate variable and function names with explanatory comments where necessary, and regular inclusion of docstrings that introduce the piece of code and its details. It is also important to follow the relevant style guide for the language you’re using, e.g., PEP-8 in Python (https://www.python.org/dev/peps/pep-0008/).

Keep it Modular

When writing code it’s important to keep it modular (https://en.wikipedia.org/wiki/Modular_programming). That is, to break it up into smaller pieces that execute separate tasks as part of the overall algorithm. This level of functionality makes it easy to:

control the scoping of variables, reuse modules of code, refactor code during further development, read, review and test code.

Write Tests

Try to consider what tests can be written alongside your code in order to check the validity of your assumptions and logic. These tests can be anything from a simulation of the expected inputs and outputs, to a series of unit tests to check the code functionality. A unit test generally exercises the functionality of the smallest possible unit of code (which could be a method, class, or component) in a repeatable way. For example, if you are unit testing a class, your test might check that the class is in the right state. Typically, the unit of code is tested in isolation: your test affects and monitors changes to that unit only. Ideally this forms part of a “Test Driven Development” framework for encouraging that all pieces of software are fully reviewed and tested before being integrated or deployed (https://en.wikipedia.org/wiki/Test-driven_development), minimising time spent refactoring and debugging later on.

Code for Production

Try to write your code as if you’re putting it into production. This will form good habits as well as make it easy to scale-up when it inevitably (hopefully) does go into production.

Consider “algorithm efficiency” and try to optimise to reduce runtime and memory use. “Big-O notation” is important here (https://en.wikipedia.org/wiki/Big_O_notation).

Also consider your code environment or ecosystem and avoid dependencies by, e.g., virtualisation either at the code level (Python virtualenv: https://conda.io/docs/user-guide/tasks/manage-environments.html) or at the operating system level (Docker containers: https://www.docker.com/what-container).

Production level code should also employ “logging” to make it easy to review, inspect and diagnose issues when executing the code (https://en.wikipedia.org/wiki/Log_file).