Coding vs poisonous mushrooms
DALL-E

Coding vs poisonous mushrooms

Today's analogy: "Coding is like eating poisonous mushrooms".

I was recently tasked with taking a parser that had access to the entire buffer that it was processing, and converting it to use a smaller window into the data. The goal was to make it so that when ingesting huge data sets, we didn't need to have a huge buffer for the data, but rather we could process in chunks of, say, 4k at a time.

The code was, of course, recursive, in that certain objects could contain nested objects. In the original code, the outside recursion level would stash a pointer to where it was, and then recurse. The next recursion level would stash a pointer to where it was, and then recurse. When recursion ended, the stashed pointer would be advanced and processing would continue.

That was the initial design.

We came up with two approaches to use a buffer. Both of them involve some action when we need more data. The first approach was to get rid of the data that we processed by moving the remaining data over top of it, and then adding data to the end. The second approach was to use a ring buffer. A Person Having Ordinary Skill In The Art would be able to come up with several other methods, perhaps double buffering, etc.

Of the two approaches, we preferred the first (to get rid of the used data and move the rest of it to make room for the new data) but ... pointers. Always with the pointers. If we moved the data, then the pointers at the top recursion level would become, effectively, junk. That's because the data they were pointing to had moved. There was no "clean" way to magically reach back up the recursion stack and "jiggle" all the pointers so that they'd be correct when we came out of recursion.

So, I used a ring buffer.

Using the ring buffer simplified the code -- I no longer dealt with the raw pointer to the buffer, but used an accessor function (a static inline that just returned the buffer at the head modulo the buffer size, standard fare, nothing to see here). By doing that, and fixing all the resultant bugs, I removed some (but not all) of the pointers.

I presented this in a design review meeting, and, while it worked, it wasn't as elegant as the first approach promised to be. I was strongly encouraged to see if I could get the first approach working after all.

So, taking the simplified code, and revisiting the pointers, I was able to eliminate them! All gone!

Now (and only now) was I able to refactor the code to use the first approach that we really wanted.

And ... that brings us to poisonous mushrooms.

Since I'm too lazy to write my own content, I now turn the platform over to ChatGPT:

Explain how the poison in poisonous mushrooms goes through several changes during digestion before it actually becomes poisonous.

Many toxic compounds found in mushrooms, such as alpha-amanitin, undergo metabolic transformations in the body before they become toxic. The liver plays a crucial role in the biotransformation of these compounds.

When ingested, the toxic compounds are absorbed from the gut into the bloodstream and then transported to the liver, where they are transformed into different compounds by the liver's enzymes. This process is known as biotransformation. The transformed compounds may be more or less toxic than the original compounds, and their toxic effects may be different.

For example, alpha-amanitin is transformed by the liver into a more toxic compound, which then acts on the liver and other organs. The toxic effect of alpha-amanitin is due to its inhibition of RNA polymerase II, a crucial enzyme involved in the transcription of DNA into RNA, leading to cell death.

The time it takes for the toxic effects of a mushroom to become apparent can vary depending on the type of mushroom, the amount consumed, and the individual's liver function. In general, symptoms may not appear for several hours to a day after ingestion and can persist for several days.

It is important to note that not all mushrooms are toxic, and even toxic mushrooms can be safely consumed if prepared properly. If you suspect that you have ingested a poisonous mushroom, it is important to seek medical attention immediately.

So, the analogy is that I couldn't get from the original source code to the preferred solution, but I was able to get there through an intermediate step, days later. Much like how poisonous mushrooms undergo metabolic transformations in the body before they become toxic. Let's see if ChatGPT agrees.

Create an analogy between the way the toxins work and how software is refactored, especially in the case of a refactorization requiring intermediate steps that are not obvious at the outset.

Refactoring software code can be compared to the process of transforming toxins in the body. Just like toxins undergo biotransformation in the liver, software code undergoes a transformation during the refactoring process.

The initial code, like the toxic compound, may contain elements that are not ideal for the desired outcome and may need to be transformed or "purified." The refactoring process involves breaking down the existing code into smaller, more manageable parts and then recombining them into a new, improved structure.

This process can sometimes require intermediate steps that are not immediately obvious, just as the liver's biotransformation of toxins can involve multiple transformations before the final, toxic form is achieved. These intermediate steps are necessary for the eventual success of the refactoring, just as the intermediate toxic forms are necessary for the toxicity of the final compound.

Similarly, just as a toxic reaction can have adverse effects on the body, an unsuccessful refactoring process can cause problems in the software code and lead to unexpected errors or crashes. This highlights the importance of careful planning and testing during both the biotransformation of toxins and the refactoring of software code.

Yup. What it said. Remember, kids -- plan and test during biotransformation of toxins or things may go horribly wrong!

要查看或添加评论,请登录

Robert Krten, HSD的更多文章

  • Sorting Salted + Hashed Data

    Sorting Salted + Hashed Data

    And why you might want to I had an idle thought about sorting hashed data, and then almost immediately discarded it…

    6 条评论
  • Generating Binary Puzzles

    Generating Binary Puzzles

    A few years ago, I got a book called "875 Binary Puzzles". In it, there are various grids with zeros, ones, and empty…

    2 条评论
  • AI as Personal Nirvana: Mass Opiation as an Emergent Property

    AI as Personal Nirvana: Mass Opiation as an Emergent Property

    (Subtitle: Rob, tell us what you really think) Lately, I've been contemplating the future; specifically how AI will…

    1 条评论
  • The naming of cats...

    The naming of cats...

    Lately, I've been using small microcontrollers (like the Raspberry PICO) as a way of offloading (and decoupling)…

  • Where do you see yourself?

    Where do you see yourself?

    I asked ChatGPT4 that very question. In a coy way, of course: ME: I'm creating a graphic that shows how AI and human…

  • Harnessing ChatGPT for Project Development: Lessons from "The Mythical Man Month"

    Harnessing ChatGPT for Project Development: Lessons from "The Mythical Man Month"

    Introduction "The Mythical Man Month" by Fred Brooks is a classic text in the world of software engineering and project…

  • Mythical Man Month & ChatGPT

    Mythical Man Month & ChatGPT

    As technology advances, we are presented with new and innovative tools to aid us in our work. One such tool is ChatGPT,…

    2 条评论
  • Leveraging leverage

    Leveraging leverage

    Leverage is not just a one-dimensional "oh, I can do this thing faster." Here, I'm going to explore how leverage…

  • ChatGPT designs an LED clock

    ChatGPT designs an LED clock

    Here's today's conversation with chatGPT about designing an LED clock. I'm in italics, chatGPT is in bold.

    5 条评论
  • ChatGPT is my copilot, I shall not want.

    ChatGPT is my copilot, I shall not want.

    A lot has been written about ChatGPT, including a lot by people who just don't get it. They say "well, it makes…

    6 条评论

社区洞察

其他会员也浏览了