<!-- mobian-agent-page publisher="dailydev" canonical="https://daily.dev/posts/refactor-optimize-and-test-crafting-cleaner-python-code---mohamed-elmaghraby-dnuf6r9cg" -->

---
title: Refactor, Optimize, and Test: Crafting Cleaner Python...
description: A senior software engineer at Bloomberg discusses refactoring as a systematic, incremental process focused only on internal code structure, driven by testing...
canonical: https://daily.dev/posts/refactor-optimize-and-test-crafting-cleaner-python-code---mohamed-elmaghraby-dnuf6r9cg
twitter:card: summary_large_image
twitter:site: @dailydotdev
og:type: website
og:site_name: daily.dev
og:title: Refactor, Optimize, and Test: Crafting Cleaner Python Code - Mohamed Elmaghraby | daily.dev
og:description: A senior software engineer at Bloomberg discusses refactoring as a systematic, incremental process focused only on internal code structure, driven by testing...
og:url: https://daily.dev/posts/refactor-optimize-and-test-crafting-cleaner-python-code---mohamed-elmaghraby-dnuf6r9cg
og:image: https://api.daily.dev/og/posts/dNuf6R9CG.png
og:image:alt: Refactor, Optimize, and Test: Crafting Cleaner Python Code - Mohamed Elmaghraby
og:image:width: 1200
og:image:height: 630
og:locale: en
---

> ## Documentation Index
> Fetch the complete documentation index at: https://daily.dev/llms.txt
> Use this file to discover all available pages before exploring further.

# Refactor, Optimize, and Test: Crafting Cleaner Python Code - Mohamed Elmaghraby

**[EuroPython Conference](https://daily.dev/sources/europython)** · 29 min read · 0 upvotes · 0 comments

## Summary

A senior software engineer at Bloomberg discusses refactoring as a systematic, incremental process focused only on internal code structure, driven by testing and applied in small steps. The talk covers when refactoring should happen (after adding features, after understanding unfamiliar code), why test suites are essential before refactoring, and the tradeoffs between readability and performance. A live demo uses the Memray profiling tool on a simulated trading dataset of 500,000 orders, showing how fixing string concatenation and switching from eager loading to generators reduced total memory allocations from 6.6 TB to about 7 GB and cut peak memory usage by roughly 75%.

## Full article

daily.dev links to this article rather than hosting it. Read it at the original source: <https://www.youtube.com/watch?v=SIq-ZnJ9D6k>

## Questions this post answers

### How much can fixing string concatenation reduce memory allocation in a Python data processing script?

Replacing repeated string concatenation with a list-and-join approach reduced total memory allocated from 6 terabytes down to 590 megabytes in a profiled example processing 500,000 trade records, because each concatenation was previously allocating a new string and freeing the old one, consuming excessive CPU cycles despite a peak memory of only 400MB.

_daily.dev surfaces profiling techniques like this for developers chasing down memory bloat in Python code._

### What does the Memray profiler measure when analyzing Python memory usage?

Memray reports total allocations (count of memory allocate/free/reallocate operations), total memory allocated (cumulative sum over the run, which can be far larger than what's actually needed at once), and peak memory (the maximum memory the application reaches at any point), plus flame graphs pointing to the code locations responsible for allocations.

_track down memory bottlenecks with tools like this before optimizing further, a workflow developers can follow via daily.dev._

### Should I optimize for readability or performance first when refactoring Python code?

Make the code work first, then make it beautiful (readable and refactored), and only then optimize for performance, since roughly 20% of code accounts for 80% of resource usage. Fixing single-core performance and identifying bottlenecks with a profiler comes before considering multi-threading or parallelization, which adds debugging complexity and should be a later-phase concern.

_developers weighing readability against speed can follow this refactor-then-optimize order via daily.dev._

## Similar posts on daily.dev

- [Think Your Python Code Is Slow? Stop Guessing and Start Measuring](https://daily.dev/posts/think-your-python-code-is-slow-stop-guessing-and-start-measuring-fbhjymucr) · Towards Data Science · 1 upvotes · 0 comments
- [Managing and Measuring Python Code Quality Quiz – Real Python](https://daily.dev/posts/managing-and-measuring-python-code-quality-quiz-real-python-thhos0us0) · Real Python · 1 upvotes · 0 comments
- [PyCoder’s Weekly](https://daily.dev/posts/pycoder-s-weekly-9bl6scsqn) · Planet Python · 10 upvotes · 0 comments

---

Tags: [#python](https://daily.dev/tags/python), [#testing](https://daily.dev/tags/testing)

[View this post on daily.dev](https://daily.dev/posts/refactor-optimize-and-test-crafting-cleaner-python-code---mohamed-elmaghraby-dnuf6r9cg)

```json
{"@context":"https://schema.org","@graph":[{"@type":"Organization","@id":"https://daily.dev/#organization","name":"daily.dev","url":"https://daily.dev","logo":{"@type":"ImageObject","url":"https://daily.dev/apple-touch-icon.png","width":180,"height":180},"sameAs":["https://twitter.com/dailydotdev","https://github.com/dailydotdev","https://www.linkedin.com/company/daily-dev-ltd"]},{"@type":"WebSite","@id":"https://daily.dev/#website","url":"https://daily.dev","name":"daily.dev","publisher":{"@id":"https://daily.dev/#organization"},"potentialAction":{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https://daily.dev/search?q={search_term_string}"},"query-input":"required name=search_term_string"}}]}
{"@context":"https://schema.org","@type":"TechArticle","headline":"Refactor, Optimize, and Test: Crafting Cleaner Python Code - Mohamed Elmaghraby","url":"https://daily.dev/posts/refactor-optimize-and-test-crafting-cleaner-python-code---mohamed-elmaghraby-dnuf6r9cg","mainEntityOfPage":{"@type":"WebPage","@id":"https://daily.dev/posts/refactor-optimize-and-test-crafting-cleaner-python-code---mohamed-elmaghraby-dnuf6r9cg"},"datePublished":"2026-08-19T10:14:17.889Z","dateModified":"2026-09-14T07:43:21.828Z","description":"A senior software engineer at Bloomberg discusses refactoring as a systematic, incremental process focused only on internal code structure, driven by testing...","image":"https://i.ytimg.com/vi/SIq-ZnJ9D6k/sddefault.jpg","thumbnailUrl":"https://i.ytimg.com/vi/SIq-ZnJ9D6k/sddefault.jpg","isAccessibleForFree":true,"articleSection":"EuroPython Conference","inLanguage":"en","publisher":{"@type":"Organization","name":"daily.dev","url":"https://daily.dev","logo":{"@type":"ImageObject","url":"https://daily.dev/apple-touch-icon.png","width":180,"height":180}},"author":{"@type":"Organization","name":"EuroPython Conference","logo":"https://media.daily.dev/image/upload/s--qu8RO-lH--/f_auto,q_auto/v1777800764/logos/europython?_a=BAMAMiWQ0","url":"https://daily.dev/sources/europython"},"commentCount":0,"discussionUrl":"https://daily.dev/posts/refactor-optimize-and-test-crafting-cleaner-python-code---mohamed-elmaghraby-dnuf6r9cg","interactionStatistic":[{"@type":"InteractionCounter","interactionType":{"@type":"LikeAction"},"userInteractionCount":0},{"@type":"InteractionCounter","interactionType":{"@type":"CommentAction"},"userInteractionCount":0}],"keywords":"python,testing","timeRequired":"PT29M","video":{"@type":"VideoObject","name":"Refactor, Optimize, and Test: Crafting Cleaner Python Code - Mohamed Elmaghraby","description":"A senior software engineer at Bloomberg discusses refactoring as a systematic, incremental process focused only on internal code structure, driven by testing...","thumbnailUrl":"https://i.ytimg.com/vi/SIq-ZnJ9D6k/sddefault.jpg","uploadDate":"2026-08-19T10:14:17.889Z","duration":"PT29M","url":"https://api.daily.dev/r/dNuf6R9CG","embedUrl":"https://www.youtube.com/embed/SIq-ZnJ9D6k"}}
{"@context":"https://schema.org","@type":"BreadcrumbList","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https://daily.dev"},{"@type":"ListItem","position":2,"name":"EuroPython Conference","item":"https://daily.dev/sources/europython"},{"@type":"ListItem","position":3,"name":"Refactor, Optimize, and Test: Crafting Cleaner Python Code - Mohamed Elmaghraby"}]}
{"@context":"https://schema.org","@type":"FAQPage","@id":"https://daily.dev/posts/refactor-optimize-and-test-crafting-cleaner-python-code---mohamed-elmaghraby-dnuf6r9cg#faq","mainEntity":[{"@type":"Question","name":"How much can fixing string concatenation reduce memory allocation in a Python data processing script?","acceptedAnswer":{"@type":"Answer","text":"Replacing repeated string concatenation with a list-and-join approach reduced total memory allocated from 6 terabytes down to 590 megabytes in a profiled example processing 500,000 trade records, because each concatenation was previously allocating a new string and freeing the old one, consuming excessive CPU cycles despite a peak memory of only 400MB. daily.dev surfaces profiling techniques like this for developers chasing down memory bloat in Python code."}},{"@type":"Question","name":"What does the Memray profiler measure when analyzing Python memory usage?","acceptedAnswer":{"@type":"Answer","text":"Memray reports total allocations (count of memory allocate/free/reallocate operations), total memory allocated (cumulative sum over the run, which can be far larger than what's actually needed at once), and peak memory (the maximum memory the application reaches at any point), plus flame graphs pointing to the code locations responsible for allocations. track down memory bottlenecks with tools like this before optimizing further, a workflow developers can follow via daily.dev."}},{"@type":"Question","name":"Should I optimize for readability or performance first when refactoring Python code?","acceptedAnswer":{"@type":"Answer","text":"Make the code work first, then make it beautiful (readable and refactored), and only then optimize for performance, since roughly 20% of code accounts for 80% of resource usage. Fixing single-core performance and identifying bottlenecks with a profiler comes before considering multi-threading or parallelization, which adds debugging complexity and should be a later-phase concern. developers weighing readability against speed can follow this refactor-then-optimize order via daily.dev."}}]}
```

