Prototyping research code in a notebook, then rewriting it as a tested package
Research code starts before anyone knows what the answer looks like. I try several approaches, most of them fail, and the code that survives carries the marks of every dead end. In a June 2024 LinkedIn article I described how I handle this: prototype in a Jupyter notebook until something works, then rewrite the code with the final solution in mind and write down the design decisions while I still remember them. I also wrote that the rewrite is the step teams skip under deadline pressure.
This post tests that advice against one of my own projects over 30 months. My phased-array
pattern code started as a Colab notebook in April 2024 and became the phased-array-modeling
package on the Python Package Index (PyPI) in January 2026, with an AI coding assistant doing most
of the conversion. The rewrite fixed one decibel (dB) error in the notebook and carried a second
error forward, and a unit test written during the rewrite asserted the wrong value. An outside
contributor found the second error eight months after release.
Throwaway code is an old idea
Fred Brooks made the case for a planned rewrite in 1975. In chapter 11 of The Mythical Man-Month he argued that the first system built for a new problem is usually barely usable, and concluded: “Hence plan to throw one away; you will, anyhow.” In the 1995 anniversary edition he revised that chapter’s advice. He wrote that it assumed a sequential waterfall process, and he recommended growing software through incremental builds instead.
Agile practice kept a smaller version of the idea. Extreme Programming (XP) calls exploratory code a spike, and Don Wells’s description of spike solutions says “Most spikes are not good enough to keep, so expect to throw it away.” Ward Cunningham’s 1992 experience report on the WyCash portfolio system, the origin of the technical debt metaphor, describes the repayment in the same terms: “A little debt speeds development so long as it is paid back promptly with a rewrite” (Cunningham, OOPSLA 1992).
Machine learning (ML) systems add their own version of the risk. Sculley and colleagues list “Prototype Smell” among the anti-patterns in Hidden Technical Debt in Machine Learning Systems (NeurIPS 2015) and warn that “time pressures may encourage a prototyping system to be used as a production solution.”
What notebook code looks like at scale
Two large studies of public notebooks measure how often notebook code runs and how it reads. Pimentel, Murta, Braganholo, and Freire collected 1,159,166 notebooks from GitHub and attempted to run the 863,878 that had a defined Python version and execution order. Only 24.11% executed without errors, and only 4.03% produced the same results as the saved outputs (Mining Software Repositories (MSR) conference, 2019). Grotov and colleagues compared 847,881 notebooks with 465,776 Python scripts and found that the notebooks had 1.4 times more stylistic issues (MSR 2022).
Neither study measures research notebooks specifically, and many public notebooks are tutorials or class exercises. The obstacles Pimentel and colleagues name still apply to research code: hidden state left by cells that were edited or re-run, cells executed out of order, and hardcoded paths.
The phased-array notebook and the package
The notebook computed radiation patterns for a planar phased array: element positions, a steered array factor, a cosine element pattern, and gain cuts in decibels relative to an isotropic radiator (dBi). I committed it to GitHub on April 14 and 15, 2024, as 30 code cells holding 370 lines of Python. It had one test function, and that function printed the element pattern’s shape and plotted two polar cuts. It asserted nothing.
The notebook sat mostly unchanged until January 2026, when I converted it into a package with Claude Code. The repository history shows the guidance file for the assistant at 22:17 on January 24, the package at 00:18 on January 25, unit tests and continuous integration (CI) at 00:35, and the PyPI release of version 1.0.0 at 16:49 the same day. Work can start before its first commit, so the 19-hour span between the first and last of these commits is a rough measure of elapsed time. Version 1.0.0 had 4,398 lines of Python, counting docstrings and comments, and 72 test functions. The current release, 1.5.0, has 9,830 lines and 294 test functions.
In 2024 I wrote that the approach may seem inefficient because it means writing the code twice. For this project, with an assistant doing most of the typing, the second writing fit inside about a day of commits. Reviewing the rewritten code stayed my job, and the next section describes what that review missed.
The error the rewrite carried forward
The notebook had two related errors in its decibel conversions. It converted the total field, the element pattern multiplied by the array factor, with 10·log₁₀|E| instead of 20·log₁₀|E|. That halved every dB value. For the notebook’s 16 × 8 array steered to θ = 30°, φ = 45°, the θ cut showed the peak sidelobe (the strongest lobe outside the main beam) at about −10 dB instead of about −20 dB. It also applied the element’s peak gain as a power ratio, 10^(dBi/10), to a pattern that the code then treated as a field amplitude.
The package fixed the first error, because its pattern functions square the field before taking
10·log₁₀. It kept the second. element_pattern() still returned a power-valued pattern, and every
pattern function multiplied it into the field array factor and squared the product, so the
element pattern was squared twice. A 6 dBi element added 12 dB, and the default cosine element
rolled off as cos² in power, −6 dB at 60°, instead of cos, −3 dB at 60°
(CHANGELOG, v1.5.0).
Version 1.0.0 had a test for the element gain, and it passed. It set max_gain_dBi=6.0 and
asserted that the pattern at broadside equaled 10 ** (6.0 / 10), the same power-ratio
convention as the prototype. The test confirmed that the code matched itself. The other two
element tests checked that the pattern peaked at broadside and fell to zero at 90°, and cos and cos²
both pass those checks. No test compared the pattern at an intermediate angle with the closed-form
value.
On September 25, 2026, an outside contributor opened a pull request that changed the gain conversion to 10^(dBi/20) and halved the cosine exponent applied to the field. I released the fix as version 1.5.0 on September 26, and the same day I corrected the tutorial notebook, which still had the 2024 versions of both errors.
A procedure for research code
The 2024 advice holds up with two changes. The rewrite still has to happen, and writing down the design decisions still matters: the field-versus-power convention is the kind of decision that belongs in a docstring and a glossary. The changes are about what the tests compare against and how cheap the rewrite has become.
- Prototype in a notebook until the approach works. Expect to throw the code away.
- Before extracting a package, write down the conventions the code depends on: units, field or power, angle definitions, array indexing order.
- Extract the package and write tests against independent references: closed-form results, textbook values, or published measurements. For an antenna pattern, check the value at an angle where the expected answer is known, such as −3 dB at 60° for a cosine element in power. A test that copies the prototype’s output checks consistency and inherits its errors.
- Keep the notebook runnable after the package exists. Jupytext
pairs a notebook with a plain
.pyfile so changes show up in code review, and Papermill parameterizes and executes notebooks so CI can run them. - Publish the code where other engineers can read it. In this case my own tests and review missed the error for eight months, and an outside reader found it.
Summary
My phased-array notebook went from 370 lines with no assertions in April 2024 to a package with 4,398 lines and 72 tests in January 2026, and to 9,830 lines and 294 tests by version 1.5.0. The AI-assisted rewrite took about a day of commits, and it fixed a 10·log₁₀ error that had halved every decibel value in the notebook. It kept the element-gain error, which added 12 dB for a 6 dBi element and put the cosine element at −6 dB at 60° instead of −3 dB, because the test written during the rewrite asserted the prototype’s convention. The rewrite step is cheaper than it was in 2024. The tests still need an independent reference.
This post grew out of a LinkedIn article I published on June 9, 2024.
This is an independent analysis I did on my own time, using my own open-source repository and the public sources cited above. The views are my own and do not represent any current or former employer.
Frequently asked questions
How often do Jupyter notebooks on GitHub reproduce their results?
Rarely, in the largest study I know of. Pimentel and colleagues collected 1,159,166 notebooks from GitHub for MSR 2019 and attempted to run the 863,878 with a defined Python version and execution order. Only 24.11% executed without errors and only 4.03% produced the same results.
What did Fred Brooks mean by plan to throw one away?
In chapter 11 of The Mythical Man-Month (1975), Brooks argued that the first system built for a new problem will be too slow, too big, or too awkward to use, so managers should plan for its replacement: 'Hence plan to throw one away; you will, anyhow.' In the 1995 anniversary edition he revised the advice, writing that it assumed a sequential waterfall process, and recommended growing a system by incremental builds.
Why did a unit test miss the field-versus-power error in the phased-array package?
The test checked the code against the convention the code already used. It asserted that a 6 dBi element scales the pattern by 10^(6/10), which is the power ratio the prototype applied to a field quantity. A test against the closed-form cosine element pattern, which is 3 dB down at 60 degrees, would have failed.