<?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 Owor Patrick Ikongha on Medium]]></title>
        <description><![CDATA[Stories by Owor Patrick Ikongha on Medium]]></description>
        <link>https://medium.com/@patrickowor?source=rss-d7035e6d8bd------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*4yJiJoqlZ2Lrb0ko2qFxeQ.jpeg</url>
            <title>Stories by Owor Patrick Ikongha on Medium</title>
            <link>https://medium.com/@patrickowor?source=rss-d7035e6d8bd------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Mon, 23 Nov 2026 19:33:01 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@patrickowor/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 Way We Defer]]></title>
            <link>https://medium.com/@patrickowor/the-way-we-defer-019b60aae3fb?source=rss-d7035e6d8bd------2</link>
            <guid isPermaLink="false">https://medium.com/p/019b60aae3fb</guid>
            <category><![CDATA[software-development]]></category>
            <category><![CDATA[system-design-concepts]]></category>
            <category><![CDATA[memory-management]]></category>
            <category><![CDATA[programming]]></category>
            <dc:creator><![CDATA[Owor Patrick Ikongha]]></dc:creator>
            <pubDate>Tue, 03 Mar 2026 20:22:25 GMT</pubDate>
            <atom:updated>2026-03-03T20:45:50.972Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*2pozgt6-oKdCD1cxv6C8mw.png" /></figure><p>If you ever grew up with a mom like mine, then you would understand that cleaning up after yourself is not an option but a necessity, and it’s the same way in computing, and every good language designer at some point will have to make a decision about this because resources are limited and your program has to manage the little it has.</p><p>Most language designers go the garbage collection route either in an attempt to chase simplicity or because they just don’t trust you to clean up after yourself. <br>Honestly, who would complain about having a nanny just follow them all around and clean up whatever mess they make, but here’s the problem with that approach: imagine playing a football match with your friends and your nanny stops the game every few minutes to clean up the stains on your jersey and the grime on your boots? That will make for a really annoying game, and in the case of a program, a very slow one.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*-ydPRbgQqtQ2HrN2kdVHfg.png" /></figure><p>Other designers decide to get clever and implement something smart enough to do the clean up without having to go the garbage collection route, for instance the ownership model in rust, but this can be a little tricky, and to make sure things work the way they should they have to set up rules and guard rails that sometimes can feel suffocating, think of it like having to listen to a hundred dos and don’t just so you can eat at a friend s house.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*azb6Y7__dx5i2dymy_D3bQ.png" /></figure><p>While other system languages like C and, to some extent, C++ trust you to know what you’re doing, hmm, like anybody ever does <br>The problem with giving you a way to clean up after yourself and not cleaning up for you is, what if you never remember to do that? Then you have a memory leak. What about if you free the same memory twice [a double free]? That couldn’t hurt, right? well sometimes it does because somewhere else in your program something might have snatched up that space when you freed it the first time and now you just corrupted their data or deleted it entirely but there’s also the case where due to a flawed conditional logic you freed up the data and now you’re trying to access it, you know, the regular [use after free] or are we going to assume you don’t make mistakes? yeah right, according to long-term data from Microsoft, Google Chrome, and Mozilla. Approximately 70% of serious software security vulnerabilities are caused by memory safety issues, so if they still make mistakes, then you do too.</p><p>but its not just memory that needs to be cleaned up, some other resources need to be managed and released properly, an example of this is a file, if you do not close the file properly after writing to it you might end up corrupting the file and don’t get me started on system and network resources for instance database connections, i guess that’s why python invented the context managers because of course they don’t trust you</p><p>So what’s the solution for these languages? Well, there isn’t a perfect solution, but we’ve got something to act as a bandage</p><h3><strong>The defer Keyword</strong></h3><p>defer basically says take this block of code and remember to do this for me when I’m done. In more technical terms, it is a feature used to suspend a block of code / or a function call to be executed just before the current calling function or scope exits, regardless of how that exit occurs.</p><p><strong>defer has its advantages</strong>, which are :-</p><p>1. Prevents Forgetfulness<br>2. Improved Code Readability: placing cleanup code immediately after resource acquisition makes it easier to find, rather than hunting for <em>close()</em> and <em>deinit()</em> calls at the end of long functions. <br>3. Simplifies Error and Safe Panic Handling: defer runs even if the code panics, thereby preventing system-wide lockups or corrupted states, and you don’t need to duplicate cleanup logic before every return err statement; hence, one defer to rule them all</p><p>which makes Defer brilliant because it’s easy to remember to clean up data at the moment you allocate new memory, but after a while, you might lose track of all the various pieces of things you allocated, hence forgetting to clean up</p><p><strong>Defer isn’t perfect</strong>; there is a lot to consider on the technical side when you try to implement it in your language. Let’s name a few. <br>1. The runtime overhead needed to track all deferred calls across a function, which could even require a few hidden allocations just to make it work <br>2. There are also lexical scoping issues to consider. Lexical scoping issues occur when nested functions or blocks cannot access expected variables due to how the code is structured or variable shadowing, making outer variables inaccessible <br>3. A lack of closures and anonymous functions in languages like C makes it harder to pass complex cleanup logic to a defer statement without creating separate, named functions <br>4. potential for misuse, that is, if a defer statement is not executed because of a conditional branching, a <em>longjmp </em>or an early exit, resources can still leak, which defies the purpose of the construct. <br>also there are language design philosophy issues like whether nested Defer statements should be allowed, and whether Defer doesn’t feel alien to the language style</p><p><strong>now for the users of a defer keyword, defer comes with its own type of problems</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/625/1*TpfS179zUzd77B8OwhsLtg.png" /><figcaption>defer runs from the bottom up breaking the top down mental model which could result in its own memory bugs especially in language with no closures</figcaption></figure><p>1. It breaks away from the linear way we think about code, as now the code we write does not run the moment we write it <br>2. even during its own execution it executes in a stack like manner I.e. Last in First out meaning what we wrote last runs first and what we wrote first runs last and trust me this trips off a lot of developers during their early encounter with its use because now a proper way to free an item and the children of the item is to first defer the item itself for clean up then defer the children of the item for clean up which looks horrendous to a programmers brain so instead we use a block or closure so we can still maintain our linear mental model.</p><p>In conclusion, there is no perfect way to go about cleaning up; we can only <strong>DEFER </strong>to the language design choice. <br>I am Owor Patrick, the Sourcebird</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=019b60aae3fb" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>