8 Coding Practices for Data Scientists

One quality that separates good data scientists from great ones is good coding practices.

Data science is a collaborative field where different teams have to work together on projects, share code, and collaborate on data analysis.

Adopting best coding practices is crucial to ensure the code is reliable, efficient, and easily shared and maintained, especially as projects grow and become more complex.

For example, one common data science practice is using notebooks to share code and analysis with team members. Without proper coding practices, these notebooks can quickly become cluttered, difficult to read, and prone to errors → untitled_18_final_final.ipynb

In this article, we will discuss eight of the most essential coding practices for data scientists, grounded in research, that will help you take your work to the next level.

#1 Write programs for people, not computers

Writing code that is easy to understand and maintain is essential.

Your coworkers are reading your code, so be mindful of whether your code is readable. A good test is if you’re coming back to this code a month, or even better, a year later, will you still be able to understand what the code is doing?

The fundamental principle is to break programs into multiple functions, each of which performs a single task.

This makes the program easier to understand, like how a paper is broken up into sections and paragraphs to make it easier to read.

As for the code itself, writing clean code involves:

  • Naming conventions: Use consistent, distinctive, meaningful names for variables, functions, and classes. This makes your code more readable and helps others understand your work.
  • Code style: Adopt a consistent code style and formatting. For Python, follow the PEP 8 guidelines. Right now, ruff is the best (fastest) tool for this.

#2 Let the computer do the work

Data science workflows can involve complex and time-consuming tasks, but many tools can help automate these tasks.

For instance, Apache Airflow and Prefect can be leveraged to orchestrate and automate complex data workflows.

Frameworks like Dask and Ray enable the distribution and parallelization of computations, which are indispensable when dealing with large-scale data analysis.

Weights & Biases gives data scientists a seamless way to track experiments, log all pertinent files and metrics, and version-control their work, ensuring that it remains transparent and reproducible.

Utilizing AI tools like GitHub Copilot and ChatGPT can provide insightful recommendations and discover open-source packages for your specific problem. These tools can significantly expedite coding, allowing data scientists to craft more effective and efficient code.

Check out more Python packages in our article: 10 Useful Python Libraries Every Data Scientist Should Be Using.

#3 Make incremental changes

For data scientists, making incremental changes is central to robust code development. Using tools like nbdev, which are designed to work with Jupyter Notebooks, data scientists can adopt an agile methodology that allows for iterative improvement of code. This process involves frequent, small updates to code, which is crucial when dealing with the ever-changing landscape of data and analysis requirements.

Version control, with Git being the industry standard, is the underpinning element that makes this iterative approach effective. It enables data scientists to track each change, experiment with different branches of development, and collaborate seamlessly. Sharing code via platforms like GitHub or GitLab becomes streamlined when notebooks are converted into version-controlled modules through nbdev.

Moreover, nbdev integrates continuous integration (CI) tools that automatically run tests with each update, ensuring the integrity and quality of code. This continuous testing, paired with the documentation generated by nbdev, enhances the transparency and reproducibility of your work—essential qualities in data science projects.

#4 Don’t repeat yourself (or others)

Applying the DRY principle to a data scientist’s workflow streamlines processes and enhances efficiency by eliminating redundant tasks and ensuring consistency across data analysis and modeling.

For Data:

  • Centralized Data Store: Instead of having multiple versions of datasets scattered across various files, there should be a single source of truth for datasets. For instance, all cleaned and processed data should be stored in a centralized database or data warehouse. On a related note, check out DVC (Data Version Control).
  • Data Dictionaries: Define each data element once in a data dictionary. For example, if a data scientist is working on geographical data, each place should have a unique identifier used across all analyses. Instead of having latitude and longitude in multiple files, there’s one reference to pull from, minimizing the risk of using outdated or incorrect location data.

For Code:

  • Function Libraries: Instead of writing the same statistical analysis code multiple times for different projects, a data scientist should write a function once and save it in a personal or team library.
  • Open Source Utilization: Reuse code from well-known libraries such as Pandas for data manipulation, NumPy for numerical computations, or Scikit-learn for machine learning instead of writing from scratch.

#5 Plan for mistakes

Defensive programming is essential in data science to prevent errors before they occur. Utilize Python’s assert statements to check for data integrity before steps like model training. For example, assert not df.isnull().any().any(), "Dataframe contains NaN values" helps ensure the dataset is ready for use.

Testing is critical; frameworks like unittest for structured testing or Pytest for a more streamlined syntax ensure your data transformations are accurate. Moreover, transforming bugs into test cases using these frameworks can document and avoid future issues.

Data scenario testing is enhanced with the Hypothesis library, which creates diverse data samples to challenge your models, thus boosting their resilience. For debugging, the ipdb debugger offers a deeper dive into the code’s behavior, helping to pinpoint and understand issues more effectively.

Lastly, the Great Expectations package sets a high bar for data quality, with checks like expect_column_values_to_not_be_null, reinforcing the rigorous standards needed in data science. Integrating these robust tools into your workflow ensures a solid, error-aware foundation for reliable and efficient code.

#6 Optimize software only after it works correctly

Code correctness is the first priority for data scientists, not speed. Confirm algorithm accuracy before considering optimizations. Profiling, such as with Python’s cProfile, should be reserved for later stages, specifically for larger datasets or complex operations where efficiency becomes a bottleneck.

Python’s ease and comprehensive libraries, like Pandas and TensorFlow, satisfy most data science needs, balancing quick development and performance. For performance-critical tasks, integrating Python with C++ or using Cython to translate Python code into C can be powerful for speed enhancements.

When performance tuning is indispensable, ensure the optimized code’s output aligns with the original to maintain accuracy. This check maintains the scientific integrity of the data science work, allowing for a blend of Python’s accessibility and the raw performance of lower-level languages when necessary.

#7 Document design and purpose, not mechanics

Clear and comprehensive documentation is as fundamental to data science as a detailed lab notebook is to traditional research. It elucidates the code’s intent, simplifies the handover process, and eases future updates — essential in the dynamic world of data science teams. Effective documentation focuses on the ‘what’ and ‘why’ over the ‘how’. For instance, Python docstrings should encapsulate the functions and modules’ purpose, expected inputs, and outputs.

Here’s a concise example:

def clean_temperature_data(df):
Adjusts temperature readings for sensor drift observed in quarterly
sensor calibrations. This correction improves the accuracy of trend
analysis by accounting for hardware inconsistencies.

df (DataFrame): Data with 'temp' column for temperature readings.

DataFrame: Data with 'temp' adjusted for sensor drift.
# Implementation of drift adjustment

Avoid superfluous comments that mimic what the code is already expressing. Excessively complex code that necessitates lengthy explanations is often a candidate for refactoring to enhance its intuitiveness and maintainability.

Documentation should evolve with your codebase to prevent mismatches between code functionality and description. Python’s native support for docstrings, coupled with tools like Sphinx, can automatically generate well-organized documentation websites directly from the codebase, ensuring synchronicity.

For narratives that combine code, analytical commentary, and visualizations, Jupyter Notebooks offer a robust solution. They allow data scientists to interlace executable code with rich text and graphics, creating a comprehensive and interactive document. These notebooks document the analysis and serve as an executable guide for replication or extension of the work, promoting clarity and collaborative continuity.

#8 Collaborate

Collaboration in data science is not just about sharing datasets and results; it’s also about ensuring the code that processes and analyzes the data is robust and well-understood by all team members. Much like peer reviews in academic research, code reviews play an essential role in achieving this by identifying bugs, enhancing code readability, and facilitating knowledge transfer within teams.

Effective teams often adopt pre-merge code reviews, where contributions are scrutinized and discussed before being added to the main codebase. This method ensures high standards are maintained and that code quality is addressed consistently.

When direct mentorship or tackling complex challenges is needed, pair programming comes into play. Although sometimes seen as obtrusive by some programmers, this method allows for immediate feedback and shared problem-solving, enhancing code quality and team member skills.

As teams expand, tracking who is working on what and ensuring tasks are not duplicated or dropped requires robust management tools. Issue-tracking systems, often included in platforms like GitHub or available as standalone options like Trac, are indispensable for streamlining the collaborative process. They allow for a structured approach to managing tasks, bugs, and features, ensuring that everyone in the team can work effectively without losing sight of the bigger picture.

All these practices — code reviews, pair programming, and issue tracking — are critical for the collective advancement of a data science team, ensuring that the work is completed efficiently and that every team member’s contribution is valued and built upon.


In conclusion, following best practices for scientific computing is essential for data scientists working in the industry.

By writing programs for people, not computers, letting the computer do the work, making incremental changes, not repeating oneself, planning for mistakes, optimizing software only after it works correctly, documenting design and purpose, and collaborating, data scientists can ensure that their work is reproducible, reliable, and efficient.

Using the latest tools and technologies, data scientists can implement these best practices more easily and effectively.

That’s all I have for you. Thank you for reading!

If you have any suggestions or thoughts, feel free to comment below!

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top

— I develop software that helps you shape the future —

Hello, I’m Diran. Software architect and author.