<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Anastasia Makhonina on Medium]]></title>
        <description><![CDATA[Stories by Anastasia Makhonina on Medium]]></description>
        <link>https://medium.com/@anastasiamakhonina?source=rss-975308543ba4------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*DnT9xaxx3L5g5iCvp-8T3w.png</url>
            <title>Stories by Anastasia Makhonina on Medium</title>
            <link>https://medium.com/@anastasiamakhonina?source=rss-975308543ba4------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Thu, 19 Nov 2026 15:58:13 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@anastasiamakhonina/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[The Builder Pattern: Write Clean and Maintainable Data Science Code]]></title>
            <link>https://medium.com/@anastasiamakhonina/the-builder-pattern-write-clean-and-maintainable-data-science-code-a2ca2b0dd14e?source=rss-975308543ba4------2</link>
            <guid isPermaLink="false">https://medium.com/p/a2ca2b0dd14e</guid>
            <category><![CDATA[mlops]]></category>
            <category><![CDATA[machine-learning]]></category>
            <category><![CDATA[python-programming]]></category>
            <category><![CDATA[data-science]]></category>
            <category><![CDATA[design-patterns]]></category>
            <dc:creator><![CDATA[Anastasia Makhonina]]></dc:creator>
            <pubDate>Wed, 19 Feb 2025 16:58:14 GMT</pubDate>
            <atom:updated>2025-02-19T16:58:14.877Z</atom:updated>
            <content:encoded><![CDATA[<p>Creating reusable pipelines instead of spaghetti code</p><p>· <a href="#a5bb">The Problem with Data Science Code</a><br>· <a href="#06bf">Introducing Design Patterns</a><br> ∘ <a href="#13f1">Where is the Builder Pattern Useful?</a><br>· <a href="#64fd">Refactoring a Messy Pipeline</a><br> ∘ <a href="#bc65">❌ How not to write code</a><br> ∘ <a href="#df5e">🔍 Why Does This Matter?</a><br> ∘ <a href="#b572">✅ A better way to design custom pipelines</a><br>· <a href="#c47e">Recap &amp; Alternative Ways to Construct Objects</a><br>· <a href="#7bb7">Conclusion</a><br> ∘ <a href="#68b9">References</a></p><h3>The Problem with Data Science Code</h3><p>Data scientists are often guilty of writing spaghetti code — long, unstructured scripts filled with <strong>hardcoded transformations, repetitive steps, and tangled dependencies</strong>.</p><p>This leads to common issues such as:<br>❌ Copy-pasting entire pipeline code to do a minor adjustment <br>❌ Small updates require modifying multiple scripts.<br>❌ No standard way to construct pipelines across projects.</p><p>All of this makes <strong>debugging difficult and prevents code reuse across modules or projects</strong>. Even minor updates to an ML pipeline can require duplicating large sections of code, leading to <strong>redundancy and increasing the risk of errors</strong>.</p><h3>Introducing Design Patterns</h3><p>One way to write <strong>cleaner, more maintainable code</strong> is by applying software design patterns. The idea behind them is pretty simple: <strong>use established solutions for recurring problems</strong>.</p><p>Design patterns are broadly categorized into three types:</p><ul><li><strong>Creational</strong> patterns simplify object creation (classes or instances in Python)</li><li><strong>Structural</strong> patterns define how different components of a system interact.</li><li><strong>Behavioral</strong> patterns manage how objects coordinate their actions.</li></ul><p>In this article, we’ll focus on a creational pattern called the <em>Builder</em>, which helps construct complex objects, such as ML pipelines, in a modular and reusable way.</p><h4>Where is the Builder Pattern Useful?</h4><p>The Builder Pattern is used in:</p><ul><li><strong>ML Pipelines</strong>: combining preprocessing, feature selection, and model training steps dynamically</li><li><strong>Deep Learning</strong> Model Architectures: building neural networks in TensorFlow and PyTorch.</li><li><strong>Data Processing</strong> Pipelines: defining multi-step workflows in PySpark or Dask.</li><li><strong>AutoML</strong>: creating flexible workflows for model selection and hyperparameter tuning.</li></ul><p>Now, let’s explore how the Builder Pattern can improve ML workflows.</p><h3>Refactoring a Messy Pipeline</h3><h4>❌ How not to write code</h4><p>To see the Builder Pattern in action, let’s walk through a simple real-world example: a basic training pipeline.</p><pre># Load dataset<br>data = load_data(&quot;dataset.csv&quot;)<br><br># Preprocessing<br>data = remove_missing_values(data)<br>data = normalize_features(data)<br>data = select_features(data, method=&quot;k_best&quot;, k=10)<br><br># Model training<br>model = RandomForestClassifier(n_estimators=100)<br>model.fit(data.X_train, data.y_train)</pre><p>In the code block above we load data from a CSV file, do some basic preprocessing like handling NULLs and normalizing features, and finally fit a Random Forest model on the subset of data.</p><p>Now, this code works just fine if you want to do a quick one-off experiment — <strong>there is no need to overengineer a problem just to show off your coding skills</strong>. However, in a production setting w<strong>hen your goal is to build a reusable ML pipeline for different projects, this is not the best approach</strong>. Let’s see why.</p><p>1️⃣ <strong>Code Duplication — copy-pasting for small changes</strong><br>Every time we need a slightly different pipeline (e.g., switch feature selection method or change the model), we must copy-paste and modify parts of the script.</p><p>📌 <em>Example: Trying Different Models</em><br>If we want to test Logistic Regression instead of Random Forest, we end up duplicating the entire pipeline:</p><pre># Load dataset<br>data = load_data(&quot;dataset.csv&quot;)<br><br># Preprocessing<br>data = remove_missing_values(data)<br>data = normalize_features(data)<br>data = select_features(data, method=&quot;k_best&quot;, k=10)<br><br># Model training<br># NOTE: Same pipeline, different model<br>model = LogisticRegression()<br>model.fit(data.X_train, data.y_train)</pre><p>❌ <strong>Issue</strong>: <em>Even though we only changed one line, we had to duplicate the entire preprocessing workflow. Any future change in preprocessing would require modifying multiple locations, increasing the risk of errors.</em></p><p>2️⃣ <strong>Hardcoding Steps — No Flexibility in Pipeline Configuration</strong><br>Each step (preprocessing, feature selection, model selection) is explicitly defined in the script.</p><p><em>But what if we want to:</em></p><ul><li>Skip feature selection for some models?</li><li>Use different normalization methods for different datasets?</li></ul><p>📌 <em>Example: No feature selection</em></p><pre># Load dataset<br>data = load_data(&quot;dataset.csv&quot;)<br><br># Preprocessing<br>data = remove_missing_values(data)<br>data = normalize_features(data)<br><br># NOTE: Same pipeline without dropping features<br>#data = select_features(data, method=&quot;k_best&quot;, k=10)<br><br># Model training<br>model = LogisticRegression()<br>model.fit(data.X_train, data.y_train)</pre><p>❌ <strong>Issue</strong>: <em>We have to manually modify or comment out lines in the copy, making the code logic harder to manage.</em></p><p>3️⃣ <strong>Poor Readability — Scattered Code Without Structure</strong><br>Each part of the ML workflow — data loading, preprocessing, model training, and evaluation — is scattered around in the same the script, making it <strong>difficult to understand what is happening upon a quick glance</strong>.</p><p>❌ <strong>Issue</strong>: <em>No clear code structure. Hard to read, debug and maintain.</em></p><h4>🔍 Why Does This Matter?</h4><p>The original approach may work for small experiments, but in real-world ML projects, we often:</p><ul><li>Need to <strong>try multiple models and configurations quickly</strong></li><li><strong>Reuse pipeline components</strong> across different teams and projects <strong>without rewriting code</strong></li><li>Maintain consistency in preprocessing and feature engineering steps across projects, <strong>maintaining a single source of truth for the code</strong></li></ul><p>💡 <em>Instead of copy-pasting and modifying code manually each time, we need a structured way to construct ML pipelines dynamically.</em></p><p>Next, we’ll refactor this into a reusable, modular pipeline using the Builder Pattern.</p><h4><a href="https://emojipedia.org/check-mark-button/">✅</a> A better way to design custom pipelines</h4><p>Now that we’ve identified the problems with our hardcoded ML pipeline, let’s refactor it using the Builder Pattern to make it:</p><p><strong>Step 1</strong>: <em>Define a PipelineBuilder class</em></p><pre>from typing import Callable, List, Optional, Type<br>from sklearn.pipeline import Pipeline<br><br><br>class PipelineBuilder:<br>    def __init__(self) -&gt; None:<br>        self.steps: List[Callable] = []  # List of preprocessing functions<br>        self.model: Optional[object] = None  # Any sklearn-compatible model<br><br>    def add_preprocessing(self, method: Callable) -&gt; &quot;PipelineBuilder&quot;:<br>        &quot;&quot;&quot;Add a preprocessing step.&quot;&quot;&quot;<br>        self.steps.append(method)<br>        return self<br><br>    def add_feature_selection(self, method: str, k: int) -&gt; &quot;PipelineBuilder&quot;:<br>        &quot;&quot;&quot;Add a feature selection step.&quot;&quot;&quot;<br>        self.steps.append(lambda data: select_features(data, method=method, k=k))<br>        return self<br><br>    def set_model(self, model_class: Type[BaseEstimator], **kwargs) -&gt; &quot;PipelineBuilder&quot;:<br>        &quot;&quot;&quot;Set an ML model with chosen parameters.&quot;&quot;&quot;<br>        self.model = model_class(**kwargs)<br>        return self<br><br>    def build(self) -&gt; Pipeline:<br>        &quot;&quot;&quot;Construct and return an instance of ML pipeline.&quot;&quot;&quot;<br>        return Pipeline(self.steps + [(&quot;model&quot;, self.model)])</pre><p>By centralizing <em>Pipeline</em> and <em>PipelineBuilder</em> in a <strong>shared repository</strong>, ML projects can import them without duplication, ensuring a single source of truth. This allows code owners to <strong>enforce best practices like typing hints, unit tests, and code quality standards, so Data Scientists can focus on ML without worrying about implementation details.</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ftwpPic-oapbj-tPEwZptA.png" /></figure><p>Additionally, using design patterns like Builder standardizes code across teams, <strong>making collaboration smoother and reducing onboarding time</strong> by improving readability and consistency.</p><p>Step 2 <em>Build a custom version of the pipeline</em></p><pre># Build a pipeline<br>pipeline = (<br>    PipelineBuilder()<br>    .add_preprocessing(StandardScaler())<br>    # Pass model class and parameters<br>    .set_model(RandomForestClassifier, n_estimators=200, max_depth=5)<br>    .build()<br>)</pre><p>The PipelineBuilder can be improved further to support more complex steps. For instance, we can integrate hyperparameter tuning, or add logging and experiment tracking. <em>By continuously refining the builder, we can create a standardized, reusable ML framework that scales across projects and teams.</em></p><h3>Recap &amp; Alternative Ways to Construct Objects</h3><p>The Builder Pattern is a creational design pattern that <strong>simplifies constructing complex objects by breaking it down to smaller, flexible steps.</strong> With a builder class, users can create objects as complex or as simple as needed, <strong>without worrying about the underlying implementation</strong>.</p><p>1️⃣ <strong>Using Subclasses for Variations</strong><br>An alternative is to define a base class and create subclasses when modifications are needed. This works well when there are few, fixed variations (e.g., a pipeline with a single model type). However, if you need to experiment with many configurations, the n<em>umber of subclasses grows exponentially, making the approach difficult to manage</em>. A Builder Pattern is more scalable in such cases.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*WoC3znewKNGPEom_nnvk4Q.png" /><figcaption>Subclassing instead of using Builder Pattern (RefactoringGuru)</figcaption></figure><p>2️⃣ <strong>Overloading __init__ with Many Parameters</strong><br>This is what I see most often in Data Science code, where many parameters remain unused (set to None), making the logic hard to read and maintain. As the number of optional parameters grows, <em>the class becomes bloated and confusing</em>, making it harder to work with over time.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Ei8zQNvnaE849a6y-rcQnA.png" /><figcaption>Complex __init__ instead of using Builder Pattern (RefactoringGuru)</figcaption></figure><h3>Conclusion</h3><p>This is what we have covered in this article:<br>✔ <strong>Challenges with Data Science code </strong>— hardcoded, repetitive, and difficult to read maintain<br>✔ <strong>The Builder Pattern as a solution</strong> — astep-by-step approach to constructing flexible ML workflows<br>✔ <strong>Hands-on example of refactoring</strong> an ML pipeline using Builder Pattern<br>✔ <strong>Alternative approaches </strong>— comparing Builder with subclassing and overloaded constructors</p><p>The Builder Pattern is just one of many design patterns that can improve code readability, maintainability, and reusability in ML workflows. Other patterns, such as <em>Factory, Strategy or Observer</em> also help with structuring ML code. <strong>By applying the right patterns, we can write cleaner, Data Science and Machine Learning code that is easier to extend to other projects.</strong></p><h4>References</h4><p><a href="https://refactoring.guru/design-patterns/builder">Builder</a></p><p><a href="https://towardsdatascience.com/design-patterns-with-python-for-machine-learning-engineers-builder-45b8e749f134/">https://towardsdatascience.com/design-patterns-with-python-for-machine-learning-engineers-builder-45b8e749f134/</a></p><p>💼 <a href="http://linkedin.com/in/anastasia-makhonina">Linkedin</a> ️| 📝 <a href="https://reliablemlsystems.substack.com/">Substack</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=a2ca2b0dd14e" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>