<?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 Prac&#39;s Journal on Medium]]></title>
        <description><![CDATA[Stories by Prac&#39;s Journal on Medium]]></description>
        <link>https://medium.com/@pranova2508?source=rss-78d72cb29a4f------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*7QCH8q5NB2iHMG35SpXjag.jpeg</url>
            <title>Stories by Prac&amp;#39;s Journal on Medium</title>
            <link>https://medium.com/@pranova2508?source=rss-78d72cb29a4f------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Tue, 17 Nov 2026 21:48:09 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@pranova2508/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[Memory Layout: The Hidden Cost of How We Represent Data]]></title>
            <link>https://medium.com/@pranova2508/memory-layout-the-hidden-cost-of-how-we-represent-data-ee9f98c500f5?source=rss-78d72cb29a4f------2</link>
            <guid isPermaLink="false">https://medium.com/p/ee9f98c500f5</guid>
            <category><![CDATA[data]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[software-engineering]]></category>
            <category><![CDATA[java]]></category>
            <category><![CDATA[software-development]]></category>
            <dc:creator><![CDATA[Prac's Journal]]></dc:creator>
            <pubDate>Tue, 17 Nov 2026 06:31:37 GMT</pubDate>
            <atom:updated>2026-11-17T06:31:37.005Z</atom:updated>
            <content:encoded><![CDATA[<p><em>Computational Foundations</em></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*KZ_n8xqFP_21JOHW8joP5Q.png" /></figure><p>An array looks like a simple thing.</p><p>We declare it, put some values inside it, and retrieve them whenever we need them. We rarely think about what happens beneath that abstraction. An expression such as numbers[3] feels almost trivial. We provide an index, and the program gives us the corresponding value.</p><p>But the computer has to do something more fundamental before that operation can happen. It needs a representation of the data, a way to locate individual elements, and a mechanism for accessing the underlying storage.</p><p>That representation is not an incidental implementation detail. It influences how much memory a program consumes, how efficiently it accesses data, how expensive it is to modify a collection, and how well the program interacts with the processor.</p><p>Consider two data structures that represent the same sequence of integers. One stores its elements in contiguous memory. The other connects individually allocated nodes through references.</p><p>Logically, they represent the same information. Computationally, they are very different.</p><p>This is where memory layout becomes interesting.</p><p>The question is not simply how a computer stores values. It is how the physical organization of those values changes the cost of computation.</p><h3>An Abstract Collection Has to Become Something Physical</h3><p>Consider a sequence:</p><pre>int[] numbers = {10, 20, 30, 40, 50};</pre><p>At the programming-language level, we see five integers arranged in an ordered collection. We can access an element by its index, iterate through the sequence, or modify individual values.</p><p>But what makes indexed access possible?</p><p>For a fixed-size array of primitive values, the elements occupy consecutive storage positions. If the starting location is known, the location of an element can be calculated using its index and the size of each element.</p><p>Conceptually:</p><p>[<br>\text{Address}(i) = \text{Base Address} + i \times \text{Element Size}<br>]</p><p>This is the fundamental idea behind constant-time array indexing.</p><p>The computer does not need to begin at the first element and search for the fourth one. It can calculate where the requested element belongs.</p><p>That small design decision has significant consequences.</p><p>A linked list represents the same sequence differently. Each node contains a value and a reference to another node. To reach a particular position, the program must follow the chain of references.</p><p>The difference is easy to overlook when looking only at the interfaces.</p><p>Both structures might expose operations such as add, get, and remove. Both can represent an ordered collection. But their internal representations determine what those operations actually cost.</p><p>An array trades some flexibility for efficient indexed access and predictable sequential traversal. A linked structure offers different insertion and removal characteristics at the cost of additional references and potentially less efficient memory access.</p><p>The important lesson is that a data structure is not merely a logical organization of information. It is also a decision about how that information will be represented and accessed.</p><h3>Contiguous Storage and the Importance of Locality</h3><p>Contiguous storage provides another advantage beyond calculating element positions.</p><p>It creates an opportunity for efficient sequential access.</p><p>Modern processors do not treat every memory access as an isolated operation. They operate through a hierarchy of registers, caches, and main memory. Accessing data already available in a nearby cache is generally much faster than waiting for it to arrive from main memory.</p><p>Processors transfer data in cache lines. When a program accesses one location, nearby memory may also be brought into the cache.</p><p>This is why the physical arrangement of data matters.</p><p>Consider a simple loop:</p><pre>long sum = 0;</pre><pre>for (int i = 0; i &lt; numbers.length; i++) {<br>    sum += numbers[i];<br>}</pre><p>The program visits elements in their natural order. Because the array is contiguous, nearby elements are likely to be available through the same cache line or subsequent cache-line accesses.</p><p>The processor can take advantage of spatial locality: the tendency of a program to access memory locations near those it has recently accessed.</p><p>Now consider a linked structure. Its nodes may be scattered across memory. Following one reference reveals where the next node is located, and the next access may require retrieving data from a completely different region of memory.</p><p>This creates a phenomenon commonly known as pointer chasing.</p><p>The processor cannot always predict the next memory location as effectively as it can with a sequential array traversal.</p><p>The result is that two algorithms with similar computational complexity can exhibit noticeably different performance on real hardware.</p><p>Big-O notation remains essential, but it does not tell the entire story. It describes how computational costs scale with input size; it does not fully capture cache misses, memory latency, allocation overhead, or the physical cost of moving data.</p><p>Memory layout introduces another dimension into algorithmic analysis.</p><p>It reminds us that the cost of computation is influenced not only by what an algorithm does, but also by where its data lives.</p><h3>The Problem With Growing an Array</h3><p>Contiguous storage is useful, but it introduces a constraint.</p><p>An ordinary array has a fixed length. Once allocated, its capacity cannot be extended.</p><p>This becomes inconvenient when the number of elements is not known in advance.</p><p>Suppose we allocate an array with five positions:</p><pre>int[] elements = new int[5];</pre><p>We insert five values, filling every available position.</p><p>What happens when we need to insert a sixth?</p><p>The existing array has no room.</p><p>One solution is to allocate a larger array, copy the existing elements into it, and continue using the new storage.</p><p>This is the mechanism behind a dynamic array.</p><p>Consider the internal state of a simplified vector:</p><pre>Size:      3<br>Capacity:  5</pre><pre>+----+----+----+----+----+<br>| 10 | 20 | 30 |    |    |<br>+----+----+----+----+----+</pre><p>The collection contains three elements but has room for five.</p><p>Appending another element does not require a new allocation.</p><p>However, once the capacity is exhausted, the vector must grow.</p><pre>Before resizing:</pre><pre>+----+----+----+----+----+<br>| 10 | 20 | 30 | 40 | 50 |<br>+----+----+----+----+----+</pre><pre>After allocating larger storage:</pre><pre>+----+----+----+----+----+----+----+----+----+----+<br>| 10 | 20 | 30 | 40 | 50 |    |    |    |    |    |<br>+----+----+----+----+----+----+----+----+----+----+</pre><p>The existing elements are copied into the new array. The collection then continues operating with the larger capacity.</p><p>A simplified implementation in Java makes the mechanism visible:</p><pre>public class IntVector {</pre><pre>    private int[] elements;<br>    private int size;</pre><pre>    public IntVector() {<br>        elements = new int[4];<br>        size = 0;<br>    }</pre><pre>    public void add(int value) {</pre><pre>        if (size == elements.length) {<br>            resize();<br>        }</pre><pre>        elements[size] = value;<br>        size++;<br>    }</pre><pre>    private void resize() {</pre><pre>        int[] newElements =<br>            new int[elements.length * 2];</pre><pre>        System.arraycopy(<br>            elements,<br>            0,<br>            newElements,<br>            0,<br>            size<br>        );</pre><pre>        elements = newElements;<br>    }</pre><pre>    public int get(int index) {</pre><pre>        if (index &lt; 0 || index &gt;= size) {<br>            throw new IndexOutOfBoundsException(<br>                &quot;Invalid index: &quot; + index<br>            );<br>        }</pre><pre>        return elements[index];<br>    }</pre><pre>    public int size() {<br>        return size;<br>    }<br>}</pre><p>The interesting part is not the code itself. It is the behavior it reveals.</p><p>Most append operations require only writing a value into an available position. Occasionally, however, an append triggers an entirely different sequence of operations: allocate new storage, copy existing values, and update the reference to the underlying array.</p><p>A single logical operation can therefore have different physical costs depending on the current state of the collection.</p><p>If capacity doubles whenever the array becomes full, the expensive resizing operations become increasingly infrequent. Although an individual append can take linear time, repeated appends have amortized constant-time complexity.</p><p>This is a particularly elegant example of how a representation strategy influences algorithmic behavior.</p><p>We are not eliminating the cost of resizing. We are choosing when to pay it.</p><h3>References, Objects, and the Meaning of Copying</h3><p>Memory layout also changes how we reason about objects.</p><p>Consider:</p><pre>IntVector first = new IntVector();<br>IntVector second = first;</pre><p>It is tempting to interpret this as creating two vectors with the same contents.</p><p>But that is not what happens.</p><p>The second assignment copies a reference. Both variables refer to the same object.</p><p>If we modify the collection through second, the modification is visible when accessing it through first.</p><p>This is a consequence of object identity and reference semantics in Java.</p><p>It becomes especially important when designing mutable data structures.</p><p>Consider two different operations:</p><ul><li>Assigning one object reference to another variable.</li><li>Creating a new object and copying the underlying data.</li></ul><p>These operations produce different relationships between the resulting variables.</p><p>In the first case, both variables refer to the same object. In the second, they can refer to independent objects containing equivalent values.</p><p>This distinction becomes important in concurrent systems, API design, defensive copying, and managing mutable state.</p><p>A program can have perfectly correct-looking code while unintentionally sharing mutable data between different parts of the system.</p><p>Understanding references is therefore not simply about learning Java syntax. It is about understanding how objects relate to one another in memory.</p><h3>When the Processor Becomes Part of the Data Structure Problem</h3><p>At some point, reasoning about the logical operations of a collection is no longer sufficient.</p><p>We need to consider how the processor experiences those operations.</p><p>Suppose we have a large array and want to calculate the sum of its elements. We could visit every element sequentially, or visit the same elements in a shuffled order.</p><p>Both approaches perform the same mathematical operation and access the same number of values.</p><p>Yet their execution times may differ.</p><p>Sequential access tends to make better use of spatial locality. Shuffled access can produce more cache misses because consecutive operations may require data from unrelated memory locations.</p><p>This is the basis of an important performance investigation.</p><p>The experiment is not about proving that sequential access is always faster under every condition. It is about understanding why access patterns interact with the memory hierarchy.</p><p>To investigate this, I extended my Java vector laboratory with a memory-layout experiment.</p><p>The experiment examines three questions.</p><p>First, what happens when a dynamic array grows? By observing capacity changes and counting copied elements, we can understand the cost hidden behind append operations.</p><p>Second, what happens when we copy a reference instead of copying the underlying data? By modifying both variables, we can observe the relationship between object identity and shared mutable state.</p><p>Third, how does access order influence performance? By comparing sequential and shuffled traversal over the same dataset, we can investigate the relationship between memory locality and execution time.</p><p>For the third experiment, measurement discipline matters. The JVM uses just-in-time compilation, and early executions may behave differently from later ones. Garbage collection, warm-up, dead-code elimination, and runtime conditions can also influence results.</p><p>A meaningful comparison therefore needs repeated measurements, equivalent workloads, warm-up, and a benchmark framework such as JMH.</p><p>The goal is not to collect impressive numbers. It is to produce evidence that helps explain the behavior of the system.</p><p>A measurement without a controlled experiment can be misleading. An experiment without an explanation is incomplete.</p><h3>The Engineering Trade-off Behind Memory Representation</h3><p>There is no universally optimal way to represent data.</p><p>Contiguous arrays provide efficient indexed access and good sequential locality, but their capacity is fixed. Dynamic arrays address that limitation through resizing, introducing occasional allocation and copying costs.</p><p>Linked structures offer different insertion and removal characteristics, but introduce additional memory overhead and potentially less predictable access patterns.</p><p>The correct choice depends on the workload.</p><p>If a system frequently reads elements by index and processes large sequences, contiguous storage is a natural starting point.</p><p>If the workload involves frequent structural modifications at known positions, another representation may be worth considering.</p><p>If memory consumption, latency, or throughput becomes critical, the decision should be supported by measurements rather than intuition.</p><p>This is where the role of an engineer differs from simply implementing a data structure.</p><p>An implementation can be logically correct and still be inappropriate for the workload it must serve.</p><p>The real question is not whether a particular representation is good or bad. It is whether its characteristics align with the requirements of the system.</p><h3>From Data Structures to Systems Engineering</h3><p>The ideas explored here extend well beyond arrays and linked lists.</p><p>Database engines must organize records and indexes in ways that support their expected access patterns. Search systems must balance the cost of storing large indexes against the cost of retrieving information. In-memory analytics systems must consider the relationship between data representation, memory bandwidth, and processing throughput.</p><p>At larger scales, engineers must think about allocation, cache utilization, data movement, memory pressure, and latency together.</p><p>A design that performs well on a small dataset may behave differently when the workload grows by several orders of magnitude.</p><p>The representation that is easiest to implement may not be the representation that is easiest to operate.</p><p>This is why understanding memory layout is part of understanding systems engineering.</p><p>It connects the abstract world of data structures and algorithms to the physical behavior of processors and memory.</p><h3>The Deeper Lesson</h3><p>An array initially appears to be nothing more than a sequence of values.</p><p>But when we examine it closely, we find a collection of engineering decisions: contiguous storage, indexed addressing, capacity management, allocation, copying, and locality.</p><p>Each decision creates advantages and constraints.</p><p>The same pattern appears throughout computer science. Programming languages hide machine details behind abstractions. Data structures hide representation details behind interfaces. Operating systems hide hardware complexity behind processes and virtual memory.</p><p>These abstractions make software manageable, but they do not eliminate the underlying mechanisms.</p><p>Eventually, performance, reliability, or scale forces us to look beneath them.</p><p>That is what makes memory layout worth studying. It teaches us to move between two ways of thinking: how data appears to a programmer and how it is actually represented and accessed by a machine.</p><p><strong>The deeper we understand that relationship, the better equipped we become to design systems whose behavior we can explain, measure, and improve.</strong></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=ee9f98c500f5" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Testing: How Do We Know Our Software Actually Works?]]></title>
            <link>https://medium.com/@pranova2508/testing-how-do-we-know-our-software-actually-works-43da8edb1880?source=rss-78d72cb29a4f------2</link>
            <guid isPermaLink="false">https://medium.com/p/43da8edb1880</guid>
            <category><![CDATA[python]]></category>
            <category><![CDATA[software-development]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[computer-science]]></category>
            <category><![CDATA[software-engineering]]></category>
            <dc:creator><![CDATA[Prac's Journal]]></dc:creator>
            <pubDate>Fri, 13 Nov 2026 05:40:15 GMT</pubDate>
            <atom:updated>2026-11-13T05:40:15.946Z</atom:updated>
            <content:encoded><![CDATA[<p><em>Day 013 |Computational Foundations · Python Laboratory</em></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*SvgcO3D1N6PTh79jEAyywg.png" /></figure><p>A program runs successfully. It accepts input, processes data, and produces an output. Nothing crashes. The result looks reasonable.</p><p>We run it again with another input. It works.</p><p>At this point, it is tempting to believe that the program is correct.</p><p>But what have we actually established?</p><p>We have observed that the program worked under a small number of conditions. We have not established what happens when the input changes, when an unexpected value arrives, when a dependency fails, or when someone modifies the implementation six months later.</p><p>The distinction between software that works in one situation and software that continues to behave correctly as conditions change is fundamental to engineering.</p><p>This is where testing becomes interesting.</p><p>Testing is often introduced as the practice of finding bugs. That description is incomplete. Testing is also a way of making our expectations explicit, challenging our assumptions, and collecting repeatable evidence about software behavior.</p><p>To understand why this matters, let’s start with a simple function.</p><h3>The Illusion of Working Software</h3><p>Imagine building a data-processing package. One of its responsibilities is calculating the average of a collection of numbers.</p><pre>def calculate_average(numbers):<br>    return sum(numbers) / len(numbers)</pre><p>For ordinary input:</p><pre>calculate_average([10, 20, 30])</pre><p>The function returns:</p><pre>20.0</pre><p>It appears to work.</p><p>Now consider:</p><pre>calculate_average([])</pre><p>The function raises a ZeroDivisionError.</p><p>We have discovered a problem, but the interesting question is not simply why the exception occurred.</p><p>It is why we did not consider this situation in the first place.</p><p>What should an average function do when there are no values? Should it return None? Should it return zero? Should it reject the operation?</p><p>These are different possible behaviors, and each represents a different design decision.</p><p>The original implementation silently assumed that the input collection would never be empty. That assumption was not enforced, documented, or examined.</p><p>This is a common source of software defects: implementations contain assumptions that developers have not made explicit.</p><p>The function may be correct under the conditions we imagined while remaining incorrect under conditions we overlooked.</p><p>And as software grows, the number of assumptions grows with it.</p><p>A function depends on the behavior of other functions. A package depends on libraries. A service depends on databases and networks. Eventually, a system depends on components maintained by different teams and deployed independently.</p><p>We cannot establish reliability by manually checking a few successful executions.</p><p>We need a more systematic way to examine behavior.</p><h3>Turning Expectations into Something Executable</h3><p>Suppose we decide that our average function must satisfy three requirements:</p><ol><li>It calculates the average of valid numerical input.</li><li>It rejects an empty collection.</li><li>It does not silently accept unsupported input.</li></ol><p>We have moved beyond describing what the function happens to do. We are describing what it is supposed to do.</p><p>These expectations form part of the function’s contract.</p><p>A test allows us to express that contract as executable code.</p><p>First, let’s make the implementation more explicit:</p><pre>def calculate_average(numbers):<br>    if not numbers:<br>        raise ValueError(<br>            &quot;Cannot calculate average of empty data&quot;<br>        )</pre><pre>    return sum(numbers) / len(numbers)</pre><p>Now we can write a test:</p><pre>def test_calculate_average():<br>    numbers = [10, 20, 30]</pre><pre>    result = calculate_average(numbers)</pre><pre>    assert result == 20</pre><p>The assertion is simple:</p><pre>assert result == 20</pre><p>But it represents an important engineering decision.</p><p>We are saying that when the function receives this particular input, its output must satisfy this expectation.</p><p>We can express the empty-input requirement too:</p><pre>import pytest<br></pre><pre>def test_empty_input():<br>    with pytest.raises(ValueError):<br>        calculate_average([])</pre><p>This test does not expect a successful result. It expects the function to reject an operation that violates its contract.</p><p>That distinction is important.</p><p>Correct software does not always mean software that produces a result. Sometimes correctness means refusing invalid input, preserving an invariant, or failing in a predictable way.</p><p>A test turns that expectation into something we can execute repeatedly.</p><p>This is the first important idea about testing:</p><p><strong>A test is not merely a check that code produces an output. It is an executable expression of how we expect software to behave under a particular condition.</strong></p><h3>What Actually Happens When a Test Runs?</h3><p>A test may look like an ordinary function, but its structure follows a useful pattern:</p><p><strong>Arrange → Act → Assert.</strong></p><p>Consider the previous example.</p><p>During arrangement, we prepare the input:</p><pre>numbers = [10, 20, 30]</pre><p>During execution, we invoke the behavior:</p><pre>result = calculate_average(numbers)</pre><p>During assertion, we compare the observed result with our expectation:</p><pre>assert result == 20</pre><p>The testing framework executes this sequence and reports whether the assertion holds.</p><p>For Python, I am using pytest.</p><p>Pytest discovers test functions, executes them, evaluates assertions, and reports failures.</p><p>A simple test file might look like this:</p><pre>def add(a, b):<br>    return a + b<br></pre><pre>def test_add():<br>    assert add(2, 3) == 5<br></pre><pre>def test_add_negative_numbers():<br>    assert add(-2, -3) == -5<br></pre><pre>def test_add_zero():<br>    assert add(10, 0) == 10</pre><p>Running:</p><pre>pytest</pre><p>causes the framework to discover and execute the tests.</p><p>When every assertion holds, the tests pass.</p><p>When an assertion fails, pytest reports the discrepancy.</p><p>The mechanism is straightforward, but it changes how we can reason about software.</p><p>Instead of manually repeating the same checks after every modification, we can preserve our expectations and execute them whenever the implementation changes.</p><p>The test suite becomes a repeatable experiment.</p><h3>The Interesting Failures Live at the Edges</h3><p>A function rarely fails in the exact circumstances in which we first wrote it.</p><p>The more interesting failures tend to appear at the boundaries of its expected behavior.</p><p>Consider a function that removes duplicate records while preserving their original order.</p><pre>def remove_duplicates(records):<br>    return list(dict.fromkeys(records))</pre><p>A normal example is:</p><pre>records = [&quot;A&quot;, &quot;B&quot;, &quot;A&quot;, &quot;C&quot;]</pre><pre>remove_duplicates(records)</pre><p>Expected result:</p><pre>[&quot;A&quot;, &quot;B&quot;, &quot;C&quot;]</pre><p>A test could be:</p><pre>def test_remove_duplicates():<br>    records = [&quot;A&quot;, &quot;B&quot;, &quot;A&quot;, &quot;C&quot;]</pre><pre>    result = remove_duplicates(records)</pre><pre>    assert result == [&quot;A&quot;, &quot;B&quot;, &quot;C&quot;]</pre><p>But one example is not enough to understand the function’s behavior.</p><p>What happens with an empty collection?</p><pre>def test_empty_records():<br>    assert remove_duplicates([]) == []</pre><p>What happens when every element is a duplicate?</p><pre>def test_all_records_are_duplicates():<br>    records = [&quot;A&quot;, &quot;A&quot;, &quot;A&quot;]</pre><pre>    assert remove_duplicates(records) == [&quot;A&quot;]</pre><p>Does the implementation preserve order?</p><pre>def test_order_is_preserved():<br>    records = [&quot;C&quot;, &quot;A&quot;, &quot;B&quot;, &quot;A&quot;]</pre><pre>    assert remove_duplicates(records) == [&quot;C&quot;, &quot;A&quot;, &quot;B&quot;]</pre><p>Each test investigates a different property.</p><p>The first examines empty input. The second examines repeated values. The third verifies ordering.</p><p>This reveals something important about test design.</p><p>We should not ask only whether a function works. We should ask which properties of its behavior must remain true.</p><p>Those properties are often called invariants.</p><p>For this function, one invariant is that the relative order of first occurrences must remain unchanged.</p><p>For a data-processing package, other invariants might include:</p><ul><li>Invalid records must never enter the processing pipeline.</li><li>Transformations must preserve required fields.</li><li>Exported data must conform to the documented format.</li><li>A failed operation must not leave partially modified state.</li></ul><p>Once we identify these invariants, we can design tests around them.</p><p>The objective is not to generate as many test cases as possible. It is to select cases that challenge meaningful assumptions.</p><h3>Testing Becomes More Valuable When We Deliberately Break Things</h3><p>There is a difference between writing tests that pass and knowing whether those tests are useful.</p><p>Imagine that our add function currently works:</p><pre>def add(a, b):<br>    return a + b</pre><p>We have a test:</p><pre>def test_add():<br>    assert add(2, 3) == 5</pre><p>Now deliberately introduce a defect:</p><pre>def add(a, b):<br>    return a - b</pre><p>Run the test again.</p><p>It fails.</p><p>This is a small experiment, but it demonstrates the central value of regression testing.</p><p>The test has preserved an expectation about the behavior of the function. When the implementation changes and violates that expectation, the test detects it.</p><p>This becomes increasingly important in cumulative software projects.</p><p>A developer may modify a validation function without realizing that another module depends on its previous behavior. A change to a transformation algorithm may alter exported data. A seemingly harmless refactoring may break an edge case.</p><p>Without tests, these changes may remain unnoticed until someone encounters the problem.</p><p>With tests, we have a repeatable way to check whether previously established behavior still holds.</p><p>However, there is another problem worth considering.</p><p><strong>What if the test itself is wrong?</strong></p><p>Suppose our implementation returns an incorrect result, but our test contains the same mistaken expectation.</p><p>The test passes.</p><p>Nothing in the testing framework automatically tells us that our expectation was incorrect.</p><p>A test can be perfectly executable and still verify the wrong behavior.</p><p>This is why test quality depends on understanding the requirements, examining assumptions, and deliberately challenging the implementation.</p><p>A useful extension of this idea is mutation testing.</p><p>Mutation testing introduces small defects into an implementation and checks whether the existing test suite detects them. If a deliberately incorrect implementation survives the tests, that may reveal a weakness in the suite.</p><p>The important question changes from:</p><p><em>How many tests have I written?</em></p><p>To:</p><p><em>What meaningful defects would my tests actually detect?</em></p><p>That is a much stronger way to evaluate testing quality.</p><h3>From Testing Functions to Testing Systems</h3><p>So far, we have examined individual functions.</p><p>But production software is rarely a collection of completely independent functions.</p><p>Consider a data-processing service that receives records through an API, validates them, stores them in a database, performs transformations, and returns the results.</p><p>We could test each function independently.</p><p>We could verify that validation rejects invalid records, that transformations produce the expected output, and that the export function generates the correct format.</p><p>But would that establish that the entire service works?</p><p>Not necessarily.</p><p>The validation function might work independently while the API sends it data in an unexpected format.</p><p>The database might store records correctly while the transformation layer interprets them incorrectly.</p><p>Every component might pass its individual tests while the complete workflow still fails.</p><p>This is why software systems require different levels of testing.</p><p><strong>Unit testing</strong> examines individual functions or components.</p><p><strong>Integration testing</strong> examines whether multiple components work together correctly.</p><p><strong>End-to-end testing</strong> examines a complete workflow through the system’s external interfaces.</p><p><strong>Performance testing</strong> investigates behavior under different workloads and resource constraints.</p><p><strong>Failure testing</strong> examines what happens when dependencies or components behave unexpectedly.</p><p>These approaches answer different questions.</p><p>A unit test might tell us that a function correctly validates a record.</p><p>An integration test might reveal that the database stores a validated record incorrectly.</p><p>An end-to-end test might reveal that the complete user workflow fails even though the individual components appear healthy.</p><p>No single testing level establishes every property of a system.</p><p>The engineering challenge is to choose the appropriate verification strategy for the behavior we care about.</p><h3>The Problem Changes at Production Scale</h3><p>Now imagine the same data-processing functionality operating as part of a distributed system.</p><p>Instead of one Python process, we have multiple services communicating over a network.</p><p>Instead of a small collection of records, we process millions of requests.</p><p>Instead of a single database operation, we depend on distributed storage, caches, queues, and external services.</p><p>The nature of testing changes.</p><p>We still need to verify individual functions, but correctness now depends on interactions between components and conditions that may not appear in local development.</p><p>What happens when a service becomes unavailable?</p><p>What happens when two requests modify the same record concurrently?</p><p>What happens when a network request times out after the remote system has already processed it?</p><p>What happens when one service deploys a new version while another still expects the old interface?</p><p>These are not merely function-level questions. They involve distributed state, timing, communication, and failure.</p><p>Google’s public engineering literature offers useful material for investigating these challenges.</p><p>In <em>Software Engineering at Google</em>, testing is discussed as part of the broader discipline of maintaining software over time.</p><p>The <a href="https://testing.googleblog.com/">Google Testing Blog</a> also provides engineering discussions about test design, reliability, and testing practices.</p><p>The <a href="https://sre.google/sre-book/table-of-contents/">Google SRE Book</a> extends the investigation toward production reliability, operational behavior, and the relationship between software changes and system stability.</p><p>A few important lessons emerge from this material.</p><p>First, unreliable tests can become an engineering problem themselves. Flaky tests produce inconsistent results, making it harder to distinguish real defects from failures in the test environment.</p><p>Second, component-level verification is not sufficient for complex systems. Interactions, dependencies, and operational conditions introduce additional failure modes.</p><p>Third, testing must be integrated into the software development lifecycle. Verification becomes more useful when it is performed repeatedly as software changes rather than being postponed until the end.</p><p>These are documented engineering principles. They should not be confused with claims about Google’s private testing infrastructure, which cannot be established from public material alone.</p><p>The larger lesson is that testing must evolve with the system.</p><p>A function can often be examined through controlled inputs and outputs. A distributed system requires us to investigate interactions, state transitions, timing, resource limits, and failure recovery.</p><h3>The Cost of Confidence</h3><p>Testing improves our ability to detect defects, but it introduces costs of its own.</p><p>Tests take time to write and maintain. They consume execution resources. They can become fragile when they depend on implementation details rather than externally meaningful behavior.</p><p>A large test suite is not automatically a good test suite.</p><p>Consider unit tests and end-to-end tests.</p><p>Unit tests are generally faster, more focused, and easier to diagnose. End-to-end tests cover broader workflows but usually involve more dependencies and more opportunities for environmental failure.</p><p>Neither is universally better.</p><p>A system that relies exclusively on unit tests may miss integration problems.</p><p>A system that relies exclusively on end-to-end tests may become slow and difficult to maintain.</p><p>The goal is to find a useful balance.</p><p>There are also properties that make a test suite trustworthy.</p><p><strong>Isolation:</strong> Tests should not depend unnecessarily on shared or external state.</p><p><strong>Reproducibility:</strong> Developers should be able to recreate the environment and obtain consistent results.</p><p><strong>Independence:</strong> Tests should not depend on execution order.</p><p><strong>Determinism:</strong> The same controlled conditions should produce consistent outcomes.</p><p><strong>Maintainability:</strong> Tests should express meaningful requirements without becoming tightly coupled to implementation details.</p><p>This is why testing connects naturally to packaging and dependency management.</p><p>A Python test suite that works only because of packages installed globally on one developer’s machine is difficult to reproduce.</p><p>A more professional project uses a virtual environment, declares its dependencies, organizes tests clearly, and documents how to execute them.</p><p>The testing framework is only one part of the larger system required to make verification dependable.</p><h3>What Testing Can and Cannot Tell Us</h3><p>There is a temptation to treat a passing test suite as proof that software is correct.</p><p>But a test only examines the conditions and properties that we have chosen to verify.</p><p>If we test an average function with three ordinary inputs, we have not established that it handles every possible collection.</p><p>If we test a database integration without examining concurrent writes, we have not established that the system behaves correctly under concurrency.</p><p>If we test an API without examining dependency failures, we have not established its behavior during an outage.</p><p>Testing gives us evidence, not unlimited certainty.</p><p>This distinction is important because it changes the way we interpret a passing test.</p><p>A passing test means that a particular expectation held under a particular execution.</p><p>A passing test suite means that the collection of expectations we encoded held under the conditions we examined.</p><p>Neither statement automatically proves the absence of defects.</p><p>The quality of our confidence depends on the quality of our assumptions, the relevance of our test cases, and the conditions under which we execute them.</p><p>For some systems, stronger forms of verification are necessary, including formal methods, static analysis, property-based testing, and extensive production observability.</p><p>Testing is one essential part of a broader engineering discipline concerned with establishing and maintaining correctness.</p><h3>Beyond Green Checkmarks</h3><p>When I began examining testing, it was easy to think of it as another development activity: write some functions, create test cases, run pytest, and check whether everything passes.</p><p>But that description misses the deeper idea.</p><p>Testing is a way to make our assumptions visible.</p><p>When we write a test, we decide what behavior matters. When we execute it, we challenge the implementation against that decision. When we deliberately introduce a defect, we investigate whether our verification mechanism is strong enough to detect it.</p><p>When requirements change, we reconsider which expectations should remain valid and which must evolve.</p><p>This creates a different relationship with software development.</p><p>Instead of treating implementation as the primary activity and testing as something that follows it, we begin thinking about expected behavior alongside the implementation itself.</p><p>We ask what must remain true before deciding how to build something.</p><p>We consider how a system should fail, not just how it should succeed.</p><p>We investigate whether our own verification mechanisms can be trusted.</p><p>And we recognize that correctness is not a permanent property established once at the moment of implementation. It must be examined as software, dependencies, and requirements evolve.</p><p>A program that runs demonstrates that an execution completed.</p><p>A meaningful test suite gives us evidence about whether that execution satisfied the expectations we defined.</p><p><strong>The objective is not simply to write software that works. It is to build software whose behavior we can explain, challenge, verify, and trust as it changes.</strong></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=43da8edb1880" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Why Mathematics Needs a Language of Truth]]></title>
            <link>https://medium.com/@pranova2508/why-mathematics-needs-a-language-of-truth-4d20735e3d8a?source=rss-78d72cb29a4f------2</link>
            <guid isPermaLink="false">https://medium.com/p/4d20735e3d8a</guid>
            <category><![CDATA[computer-science]]></category>
            <category><![CDATA[mathematics]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[discrete-mathematics]]></category>
            <category><![CDATA[logic]]></category>
            <dc:creator><![CDATA[Prac's Journal]]></dc:creator>
            <pubDate>Thu, 12 Nov 2026 06:46:16 GMT</pubDate>
            <atom:updated>2026-11-12T06:46:16.450Z</atom:updated>
            <content:encoded><![CDATA[<p>How propositions, predicates and logical evaluation make mathematical reasoning precise.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*mNC3rhTp8QIw2u2ca0M2mg.png" /></figure><p>We use language to describe the world, make claims, ask questions, and explain relationships. Most of the time, ordinary language is sufficient. We understand what someone means when they say that a number is even, that one object is larger than another, or that every student in a classroom has completed an assignment.</p><p>But mathematical reasoning demands something more precise.</p><p>Consider the following claims:</p><ul><li>Every even integer is divisible by two.</li><li>Some prime numbers are odd.</li><li>If a number is divisible by four, it is even.</li><li>Every computer program eventually terminates.</li></ul><p>They may look similar because they are all expressed as sentences. Yet they differ considerably in what they assert, how they can be evaluated, and what would be required to establish their truth.</p><p>The last statement, for instance, sounds like a mathematical claim, but it cannot be accepted without qualification. Which programs? Under what conditions? What does termination mean? Does the program receive arbitrary input, or only input satisfying certain requirements?</p><p>The difficulty is not merely understanding the words. It is determining exactly what the words commit us to.</p><p>This is one of the fundamental problems addressed by mathematical logic: how can we transform informal claims into precise objects of reasoning?</p><h3>1. Before asking whether something is true, we must know what is being claimed</h3><p>Mathematics frequently begins with statements.</p><p>A statement, or proposition, is a declarative claim that has a definite truth value under a specified interpretation. In classical logic, that value is either true or false.</p><p>For example:</p><p>[<br>P:\quad 7 \text{ is a prime number}<br>]</p><p>This proposition is true.</p><p>Now consider:</p><p>[<br>Q:\quad 9 \text{ is a prime number}<br>]</p><p>This proposition is false.</p><p>Both qualify as statements. Their difference is their truth value, not their eligibility to be considered propositions.</p><p>This distinction is important because mathematical reasoning is not restricted to sentences that happen to be correct. We need to examine false claims just as carefully as true ones. Otherwise, we would have no systematic way to identify errors, construct counterexamples, or determine why an argument fails.</p><p>A proposition makes a complete claim. We can ask whether it is true or false without needing additional information about an unspecified variable.</p><p>But not every mathematical expression has this property.</p><p>Consider:</p><p>[<br>x&gt;5<br>]</p><p>Is it true?</p><p>The answer depends on the value assigned to (x). If (x=8), the expression is true. If (x=3), it is false.</p><p>The expression does not yet make a complete claim about a particular object. It describes a condition that an object may or may not satisfy.</p><p>This brings us to a second mathematical concept.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/558/1*4mIXAoR5cZetJj9L8vzMRg.png" /></figure><h3>2. Predicates: describing properties before evaluating them</h3><p>A predicate is an expression that describes a property or relationship involving one or more variables.</p><p>We can represent the previous expression as:</p><p>[<br>P(x): x&gt;5<br>]</p><p>The variable (x) is an argument. The predicate describes a property: being greater than five.</p><p>When we assign a value to the variable, the expression can be evaluated:</p><p>[<br>P(8)=\text{True}<br>]</p><p>[<br>P(3)=\text{False}<br>]</p><p>In this sense, a predicate can be understood as a function that maps objects from an appropriate domain to truth values.</p><p>For example, if the domain is the integers:</p><p>[<br>P:\mathbb{Z}\rightarrow{\text{True},\text{False}}<br>]</p><p>where:</p><p>[<br>P(x)=<br>\begin{cases}<br>\text{True}, &amp; x&gt;5\<br>\text{False}, &amp; x\leq5<br>\end{cases}<br>]</p><p>This mathematical representation reveals something that ordinary language can easily obscure: a predicate does not describe an object independently of the domain in which it is interpreted.</p><p>Consider another predicate:</p><p>[<br>Q(x)²=2<br>]</p><p>If the domain is the integers, no object satisfies it.</p><p>If the domain is the real numbers, two objects satisfy it:</p><p>[<br>x=\sqrt{2},\quad x=-\sqrt{2}<br>]</p><p>The expression has not changed. What has changed is the domain of interpretation.</p><p>This is a small but important lesson. Mathematical expressions do not operate in isolation. Their meaning depends on the objects they are permitted to describe and the rules under which they are interpreted.</p><p>A predicate therefore gives us a way to describe a property without immediately committing to whether every object, some objects, or no objects possess that property.</p><p>That distinction becomes particularly useful when we want to reason about entire collections rather than individual examples.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/561/1*0reCi7AinOZxh5ermfhAqg.png" /></figure><h3>3. Truth values turn mathematical claims into objects we can evaluate</h3><p>A truth value represents whether a proposition is true or false under a particular interpretation.</p><p>In classical logic, we work with:</p><p>[<br>\mathbb{B}={\text{True},\text{False}}<br>]</p><p>This simple distinction provides the basis for constructing more complicated logical expressions.</p><p>Suppose we have two propositions:</p><p>[<br>P:\quad 8 \text{ is divisible by }4<br>]</p><p>[<br>Q:\quad 8 \text{ is even}<br>]</p><p>Both propositions are true.</p><p>We can now construct a compound statement:</p><p>[<br>P\Rightarrow Q<br>]</p><p>Read this as:</p><p>“If (P), then (Q).”</p><p>The expression is called a logical implication.</p><p>Its meaning can be examined using a truth table:</p><p>PQP ⇒ QTrueTrueTrueTrueFalseFalseFalseTrueTrueFalseFalseTrue</p><p>The third and fourth rows can initially seem counterintuitive.</p><p>Why should an implication be true when its initial condition is false?</p><p>Because an implication in classical logic makes a specific promise: whenever its antecedent is true, its consequent must also be true. It makes no additional claim about situations in which the antecedent is false.</p><p>This is called material implication.</p><p>Consider the statement:</p><p>“If an integer is divisible by four, it is even.”</p><p>The integer 7 does not contradict this statement. It is not divisible by four, so it does not satisfy the condition under which the implication makes its claim.</p><p>The distinction matters because informal reasoning often confuses a conditional statement with a relationship that works in both directions.</p><p>For example, we can establish:</p><p>[<br>(4\mid n)\Rightarrow(2\mid n)<br>]</p><p>But we cannot conclude:</p><p>[<br>(2\mid n)\Rightarrow(4\mid n)<br>]</p><p>The first implication is true. The second is false.</p><p>The number 6 is enough to demonstrate why:</p><p>[<br>2\mid6<br>]</p><p>but:</p><p>[<br>4\nmid6<br>]</p><p>A single counterexample defeats a universal claim.</p><p>Formal truth evaluation therefore does something more useful than assigning labels to sentences. It allows us to investigate the exact logical relationship between claims.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/561/1*W8og2fe-rnvbnPFMFDzsnQ.png" /></figure><h3>4. From individual objects to universal claims</h3><p>Predicates become considerably more powerful when combined with quantifiers.</p><p>Suppose we define:</p><p>[<br>P(x)²\geq0<br>]</p><p>We could evaluate this predicate for individual integers:</p><p>[<br>P(2)=\text{True}<br>]</p><p>[<br>P(-5)=\text{True}<br>]</p><p>[<br>P(0)=\text{True}<br>]</p><p>But checking individual examples does not establish that the property holds for every integer.</p><p>To express such a claim, we use the universal quantifier:</p><p>[<br>\forall x\in\mathbb{Z},\quad x²\geq0<br>]</p><p>The symbol (\forall) means “for every.”</p><p>A universal statement requires the predicate to hold for every object in the specified domain.</p><p>We can also express the opposite kind of claim: that at least one object satisfies a property.</p><p>This uses the existential quantifier:</p><p>[<br>\exists x\in\mathbb{Z},\quad x²=25<br>]</p><p>The symbol (\exists) means “there exists.”</p><p>This statement is true because both 5 and -5 satisfy the condition.</p><p>The difference between these two quantifiers is fundamental.</p><p>A universal claim makes a demand about an entire domain. An existential claim makes a demand about at least one member of that domain.</p><p>Their proof requirements differ accordingly.</p><p>To establish an existential statement, one valid example may be sufficient.</p><p>To establish a universal statement, checking several examples is not enough. We need an argument that applies to an arbitrary member of the domain.</p><p>Likewise, disproving a universal claim requires only one counterexample, whereas disproving an existential claim requires showing that no object in the domain satisfies the predicate.</p><p>This changes how we approach mathematical reasoning. Instead of treating examples as isolated observations, we can distinguish between evidence for an individual case and an argument about an entire collection.</p><h3>5. What mathematical proof makes possible</h3><p>Consider the following statement:</p><p><strong>Every integer divisible by four is even.</strong></p><p>We can write it formally:</p><p>[<br>\forall n\in\mathbb{Z},\quad<br>(4\mid n)\Rightarrow(2\mid n)<br>]</p><p>We want to establish that it is true.</p><p>Rather than checking integers one by one, we begin with an arbitrary integer (n) and assume that it is divisible by four.</p><p>By the definition of divisibility, there must exist an integer (k) such that:</p><p>[<br>n=4k<br>]</p><p>Rearranging:</p><p>[<br>n=2(2k)<br>]</p><p>Since (k) is an integer, (2k) is also an integer.</p><p>Therefore, (n) can be expressed as twice an integer. By definition, it is even.</p><p>Hence:</p><p>[<br>\boxed{<br>\forall n\in\mathbb{Z},<br>\quad 4\mid n\Rightarrow2\mid n<br>}<br>]</p><p>The significance of this proof is not the particular relationship between four and two.</p><p>It is the method.</p><p>We began with an arbitrary object satisfying a condition, used the definition of that condition, and demonstrated that another property necessarily followed.</p><p>We did not need to enumerate every integer. We established a general result through reasoning that applies to all integers in the specified domain.</p><p>This is one of the central purposes of mathematical proof: to establish why a conclusion must follow, rather than merely observe that it appears to hold in the examples we have examined.</p><p>The difference between checking and proving is therefore not simply a difference in the number of examples. It is a difference in the kind of justification being offered.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/556/1*kyUEC0yJ_f4iZ95RooGSnQ.png" /></figure><h3>6. Why this language matters to computer science</h3><p>These ideas become particularly concrete when we move from mathematical reasoning to programming.</p><p>Consider a simple function:</p><pre>def is_even(n):<br>    return n % 2 == 0</pre><p>The function evaluates a condition:</p><p>[<br>P(n)\bmod2=0<br>]</p><p>For an integer input, the result is a Boolean value.</p><pre>is_even(8)  # True<br>is_even(7)  # False</pre><p>The connection is direct:</p><p>Mathematical conceptProgramming counterpartPropositionBoolean expressionPredicateBoolean-valued functionTruth valueBoolean valueNegationNOTConjunctionANDDisjunctionORImplicationConditional logical relationship</p><p>This is more than a convenient analogy. Boolean expressions and predicates are fundamental to how programs make decisions.</p><p>An if statement evaluates a condition and selects a path based on its result. A loop repeatedly evaluates a condition. A database query uses conditions to select records satisfying specified properties.</p><p>But there is a deeper connection.</p><p>Consider the function:</p><pre>def double(n):<br>    return 2 * n</pre><p>We might want to establish that this function correctly doubles every integer.</p><p>The claim can be expressed as:</p><p>[<br>\forall n\in\mathbb{Z},<br>\quad double(n)=2n<br>]</p><p>This is a statement about program correctness.</p><p>In more advanced computer science, formal verification uses mathematical specifications and logical reasoning to establish whether programs satisfy their intended requirements.</p><p>Preconditions describe what must be true before an operation. Postconditions describe what must be true after it. Invariants describe properties that must remain true throughout a computation.</p><p>Predicates provide a language for expressing these requirements.</p><p>This is why the foundations of logic matter. Before we can rigorously establish whether a program behaves correctly, we need a precise way to describe what correct behavior means.</p><p>A program that produces the expected result for five examples has not necessarily been shown to work for every valid input.</p><p>The same distinction between examples and proof that appears in mathematics reappears in software engineering.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/556/1*ySk9cocK4Pe6U0z94T_Big.png" /></figure><h3>7. Where does this model stop being sufficient?</h3><p>There is a temptation to treat classical logic as a complete description of every possible form of reasoning.</p><p>That would be a mistake.</p><p>The framework we have examined uses two truth values: true and false. This is known as classical bivalent logic.</p><p>However, not every situation is adequately represented by immediately assigning one of these two values.</p><p>Consider a database containing a field for a person’s age. If the field is empty, does that mean the person’s age is false? Does it mean the person has no age? Or does it mean that the information is unknown?</p><p>These are different situations.</p><p>Database systems such as SQL distinguish unknown or missing information through their treatment of NULL, which introduces three-valued logic into certain operations.</p><p>Other logical systems investigate additional truth values, inconsistent information, degrees of truth, or different rules for inference.</p><p>There is also a distinction between truth and proof that deserves attention.</p><p>A statement can be true under a particular interpretation, but that does not mean we have established it through a formal derivation.</p><p>Semantics investigates truth and interpretation. Proof theory investigates formal derivations and the rules by which conclusions can be established.</p><p>These perspectives are closely related, but they are not interchangeable.</p><p>The lesson is not that classical logic is inadequate. It is that a formal system must be understood within its assumptions and intended domain of application.</p><p>A model becomes useful not because it explains everything, but because we know precisely what it explains and what it leaves outside its boundaries.</p><h3>8. The larger significance: making reasoning inspectable</h3><p>We began with a relatively simple question: what does mathematics gain from distinguishing statements, predicates and truth values?</p><p>The answer is that these concepts allow us to make reasoning explicit.</p><p>A proposition gives us a complete claim that can be evaluated.</p><p>A predicate describes a property or relationship that can be evaluated when its variables receive appropriate values.</p><p>Truth values allow us to examine propositions and construct logical relationships between them.</p><p>Quantifiers let us express claims about entire domains.</p><p>Together, these ideas provide the language through which mathematical statements can be constructed, evaluated, compared and proved.</p><p>But perhaps their most important contribution is not that they make reasoning more complicated or more formal.</p><p>They make reasoning inspectable.</p><p>When someone claims that something is true, we can ask what exactly is being asserted. We can identify the domain, examine the conditions, distinguish the statement from its converse, construct counterexamples and investigate whether the conclusion actually follows from the assumptions.</p><p>The same discipline applies to mathematics, algorithms, software systems and scientific models.</p><p>It replaces the vague impression that an argument makes sense with a more demanding question: can we show precisely why it must make sense?</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=4d20735e3d8a" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Packaging and Typing: What Makes Code Reusable Software?]]></title>
            <link>https://medium.com/@pranova2508/packaging-and-typing-what-makes-code-reusable-software-e26f0c138821?source=rss-78d72cb29a4f------2</link>
            <guid isPermaLink="false">https://medium.com/p/e26f0c138821</guid>
            <category><![CDATA[software-engineering]]></category>
            <category><![CDATA[software-development]]></category>
            <category><![CDATA[python]]></category>
            <category><![CDATA[code]]></category>
            <dc:creator><![CDATA[Prac's Journal]]></dc:creator>
            <pubDate>Thu, 12 Nov 2026 05:36:08 GMT</pubDate>
            <atom:updated>2026-11-12T05:36:08.741Z</atom:updated>
            <content:encoded><![CDATA[<p><em>Day 012|Computational Foundations · Python Laboratory</em></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*rQjo8thzK_2ro1c5_-oLVg.png" /></figure><p>A function can work perfectly and still be a poor piece of software.</p><p>Imagine writing a small Python library that cleans, validates, and transforms data. You test it with different inputs, verify its output, and move on. A few days later, another application needs the same functionality.</p><p>You copy the files. It works.</p><p>Then another developer tries to install it in a different environment. Dependencies are missing. Imports behave differently. Nobody knows exactly what the functions expect, and invalid values travel through the system until something breaks.</p><p>Nothing was necessarily wrong with the original transformation logic.</p><p>The problem was that we had solved the problem of computation without solving the problem of software distribution and interface reliability.</p><p>This distinction is more important than it initially appears.</p><p>Software does not exist only where it is written. It exists in the environments where it is installed, the systems that depend on it, and the interfaces through which other developers interact with it.</p><p>Packaging and typing address two different parts of this problem.</p><p>Packaging determines how software is organized, built, distributed, and installed. Typing makes the intended relationships between inputs, outputs, and components more explicit.</p><p>Together, they help transform a collection of working functions into a reusable software component.</p><p>But understanding these mechanisms requires looking beyond their syntax. We need to understand why they exist, what problems they remove, and what happens when the assumptions behind them stop holding.</p><h3>1. The problem with code that only works locally</h3><p>Consider a simple Python function:</p><pre>def clean_names(names):<br>    return [name.strip().lower() for name in names]</pre><p>It takes a collection of names, removes unnecessary whitespace, converts the text to lowercase, and returns the result.</p><p>There is nothing complicated about its implementation.</p><p>But imagine that this function becomes useful to several applications.</p><p>The first application imports it from a local file. The second copies the implementation. The third introduces additional processing functions. Eventually, all three applications contain slightly different versions of the same logic.</p><p>Now we have a new problem.</p><p>When the cleaning algorithm changes, which version should be updated? How do the applications know which implementation they are using? What happens if one version introduces a dependency that the others do not have?</p><p>We have created multiple copies of the same capability without establishing a reliable way to share and maintain it.</p><p>This is one reason software needs structure.</p><p>A reusable component should have a recognizable identity, an explicit interface, and a defined way of being distributed.</p><p>The question is no longer simply how to write a function.</p><p>It is how to make that function available to other systems without requiring them to understand its original development environment.</p><h3>2. From individual files to software components</h3><p>Python gives us modules and packages to organize related functionality.</p><p>A module is generally a Python file containing functions, classes, variables, and other definitions.</p><p>A package organizes related modules into a larger unit.</p><p>For example, a data processing system might initially contain several unrelated functions. As the system grows, we can organize them by responsibility:</p><pre>data_processing/<br>│<br>├── __init__.py<br>├── cleaning.py<br>├── validation.py<br>└── transformation.py</pre><p>The cleaning module handles normalization and cleanup. The validation module checks incoming data. The transformation module converts valid data into the desired representation.</p><p>Instead of copying individual functions between applications, another application can import the functionality through the package:</p><pre>from data_processing.cleaning import clean_names</pre><p>This creates a clearer organizational boundary.</p><p>However, there is an important distinction here: organizing files into a package does not automatically make the package distributable.</p><p>We still need to describe the project, its dependencies, its supported Python versions, and the mechanism through which it should be built.</p><p>A directory structure explains how we have organized the source code.</p><p>It does not fully explain how someone else should obtain and install the resulting software.</p><p>That is the next problem packaging addresses.</p><h3>3. How does software become distributable?</h3><p>A reusable Python project needs information that exists outside its implementation.</p><p>Consider a developer receiving a directory containing Python files. The developer needs to know the project’s name, version, dependencies, build requirements, and other metadata.</p><p>Without this information, installation becomes dependent on assumptions.</p><p>Modern Python projects commonly use pyproject.toml to describe these requirements.</p><p>A simplified configuration might look like this:</p><pre>[build-system]<br>requires = [&quot;setuptools&gt;=75&quot;]<br>build-backend = &quot;setuptools.build_meta&quot;</pre><pre>[project]<br>name = &quot;data-processing&quot;<br>version = &quot;0.1.0&quot;<br>description = &quot;A reusable data processing package&quot;<br>requires-python = &quot;&gt;=3.11&quot;</pre><pre>[tool.setuptools.packages.find]<br>where = [&quot;src&quot;]</pre><p>There are two important responsibilities represented here.</p><p>The [build-system] section identifies the build requirements and the backend responsible for producing distribution artifacts.</p><p>The [project] section describes the project itself, including its identity, version, and Python compatibility requirements.</p><p>The package discovery configuration tells the build backend where to find the source package.</p><p>Notice the separation.</p><p>The Python source code describes the behavior of the software. The project configuration describes how that software is identified and built.</p><p>A build backend such as Setuptools takes this configuration and the source tree and produces distribution artifacts.</p><p>Two important artifacts are wheels and source distributions.</p><h3>The difference between a wheel and a source distribution</h3><p>A wheel (.whl) is a built distribution designed for installation by tools such as pip.</p><p>A source distribution (.tar.gz) contains the source material and build-related information needed to produce an installable distribution.</p><p>Why do we need both?</p><p>Because distributing source material and distributing a built artifact are different operations.</p><p>A wheel allows an installer to install an already-built distribution. A source distribution provides the source from which a distribution can be built.</p><p>The distinction becomes particularly important when projects have native extensions, platform-specific dependencies, or more complicated build requirements.</p><p>The important idea is that a package needs a defined path from source code to a distributable artifact.</p><p>A successful build gives us evidence that this process works under the conditions we tested. It does not automatically prove that the artifact will work everywhere.</p><h3>4. Installation changes the relationship between software and its environment</h3><p>There is a simple experiment that reveals whether a package is actually independent of its development directory.</p><p>First, execute the code from its original project directory.</p><p>Then build the package, install the resulting wheel into a clean virtual environment, change to a different directory, and import it.</p><p>These are two different tests.</p><p>The first demonstrates that the source code can execute in its development context.</p><p>The second tests whether the distribution contains what another environment needs to install and use it.</p><p>A virtual environment provides an isolated place to perform this experiment:</p><pre>python -m venv .venv</pre><p>After activating the environment, a development installation can be performed using:</p><pre>python -m pip install -e .</pre><p>This is convenient during development because changes to the source are generally reflected without reinstalling the package.</p><p>However, an editable installation is not sufficient evidence that a built distribution is correct.</p><p>For a stronger experiment, build the wheel:</p><pre>python -m pip install build<br>python -m build</pre><p>Then install the resulting wheel into a clean environment:</p><pre>python -m pip install dist/data_processing-0.1.0-py3-none-any.whl</pre><p>Finally, run Python from outside the source directory and attempt to import the package.</p><p>If the package works only inside its original project directory, we have discovered a problem in its structure, packaging configuration, or distribution process.</p><p>This experiment exposes an important principle:</p><p>A software component should not depend accidentally on the environment in which it was originally developed.</p><p>The more independent the installation process becomes, the easier it is to distribute and reuse the software.</p><p>But installation alone does not tell us whether the package has a clear interface.</p><p>For that, we need to examine typing.</p><h3>5. The hidden problem inside function interfaces</h3><p>Consider another function:</p><pre>def calculate_average(values):<br>    return sum(values) / len(values)</pre><p>The implementation is short, but its interface is ambiguous.</p><p>Does it accept a list of integers? A list of floating-point values? A tuple? An arbitrary iterable?</p><p>What should happen when the collection is empty?</p><p>What happens if someone passes strings?</p><p>The implementation provides clues, but the intended contract is not explicit.</p><p>As software grows, these ambiguities become increasingly expensive.</p><p>A developer reading the function must infer its intended behavior. Another developer using it may make assumptions that the implementation does not support.</p><p>Type annotations give us a way to make some of these expectations visible.</p><pre>def calculate_average(values: list[float]) -&gt; float:<br>    if not values:<br>        raise ValueError(&quot;Values cannot be empty&quot;)</pre><pre>    return sum(values) / len(values)</pre><p>Now the function communicates that it expects a list of floating-point values and returns a floating-point result.</p><p>This makes the interface easier to understand.</p><p>But the more interesting question is what tools can do with this information.</p><p>A static type checker can inspect the program and identify certain inconsistencies between declared types and how values are used.</p><p>For example:</p><pre>average: float = calculate_average([&quot;10&quot;, &quot;20&quot;, &quot;30&quot;])</pre><p>A type checker can report that strings do not satisfy the declared input contract.</p><p>This is useful because certain mistakes can be discovered during development rather than waiting for them to appear during execution.</p><p>Typing therefore introduces another layer of reasoning about software.</p><p>Instead of understanding a function only through its implementation, we can also examine the relationships between its inputs, outputs, and other components.</p><h3>6. Why Python’s type annotations do not guarantee correctness</h3><p>There is an important distinction between declaring a contract and enforcing one.</p><p>Python is dynamically typed. Type annotations generally do not impose automatic runtime restrictions on values.</p><p>Consider:</p><pre>def greet(name: str) -&gt; str:<br>    return &quot;Hello, &quot; + name</pre><p>The annotation communicates that name is expected to be a string.</p><p>However, Python does not automatically reject every value that violates this expectation.</p><p>A static type checker can identify certain violations when the relevant code is visible to it.</p><p>But consider data arriving from an external API, a JSON document, or a database.</p><p>The static type checker cannot automatically guarantee that the actual incoming data satisfies the expected contract.</p><p>The application must validate that data at runtime when necessary.</p><p>This gives us three distinct responsibilities:</p><p><strong>Type annotations</strong> communicate the intended interface.</p><p><strong>Static type checking</strong> identifies certain contract violations before execution.</p><p><strong>Runtime validation</strong> checks actual values while the program is running.</p><p>These mechanisms complement each other, but they are not interchangeable.</p><p>There is another subtle point worth understanding.</p><p>A collection can satisfy its declared type and still violate the conditions required for a particular operation.</p><p>An empty list of floating-point values is still a valid list[float], but it cannot produce an average using ordinary division by the number of elements.</p><p>The problem is not the type.</p><p>It is a condition that the function must handle explicitly.</p><p>This is why types cannot replace program logic, validation, or testing.</p><p>They make some assumptions visible, but they do not eliminate the need to reason about correctness.</p><h3>7. When the contract changes, the implementation must change too</h3><p>Suppose our original average function accepts a list:</p><pre>def calculate_average(values: list[float]) -&gt; float:<br>    if not values:<br>        raise ValueError(&quot;Values cannot be empty&quot;)</pre><pre>    return sum(values) / len(values)</pre><p>Later, another developer asks for support for arbitrary iterables.</p><p>This sounds like a small interface change.</p><p>We could write:</p><pre>from collections.abc import Iterable</pre><pre>def calculate_average(values: Iterable[float]) -&gt; float:<br>    numbers = list(values)</pre><pre>    if not numbers:<br>        raise ValueError(&quot;Values cannot be empty&quot;)</pre><pre>    return sum(numbers) / len(numbers)</pre><p>The new function accepts a broader category of inputs.</p><p>But look at what changed internally.</p><p>An iterable may be a generator that produces values only once. It may not support indexing or direct access to its length.</p><p>Converting it to a list allows us to calculate the sum and count, but introduces additional memory consumption.</p><p>For a small collection, this is reasonable.</p><p>For a stream containing millions of values, holding every element in memory may be undesirable.</p><p>We could instead calculate the running sum and count:</p><pre>from collections.abc import Iterable</pre><pre>def calculate_average(values: Iterable[float]) -&gt; float:<br>    total = 0.0<br>    count = 0</pre><pre>    for value in values:<br>        total += value<br>        count += 1</pre><pre>    if count == 0:<br>        raise ValueError(&quot;Values cannot be empty&quot;)</pre><pre>    return total / count</pre><p>Now we can process a generator without materializing the entire collection.</p><p>The trade-off has changed.</p><p>The interface remains flexible, but the implementation has different memory and execution characteristics.</p><p>This is a useful example of why engineering decisions cannot be made by looking at type annotations alone.</p><p>A type contract affects the acceptable input domain. The implementation must still satisfy the behavioral requirements efficiently and correctly.</p><h3>8. The laboratory: Turning a collection of functions into a package</h3><p>For this investigation, the objective is to extend the cumulative data processing laboratory into a typed, installable Python package.</p><p>The important part is not simply creating a directory called data_processing.</p><p>It is establishing a complete workflow through which another project can obtain and use the functionality.</p><p>The target architecture separates the implementation into responsibilities:</p><pre>data-processing/<br>│<br>├── pyproject.toml<br>│<br>├── src/<br>│   └── data_processing/<br>│       ├── __init__.py<br>│       ├── cleaning.py<br>│       ├── validation.py<br>│       ├── transformation.py<br>│       └── py.typed<br>│<br>├── tests/<br>│   ├── test_cleaning.py<br>│   ├── test_validation.py<br>│   └── test_transformation.py<br>│<br>└── dist/</pre><p>The src directory contains the actual importable package.</p><p>The modules separate different processing responsibilities.</p><p>The tests verify expected behavior.</p><p>The py.typed marker indicates that the package distributes inline type information for consumers using static type checkers.</p><p>The pyproject.toml file describes the project and build configuration.</p><p>The investigation follows a sequence:</p><ol><li>Define the public API.</li><li>Establish input and output contracts.</li><li>Implement the package structure.</li><li>Write tests for normal, boundary, and invalid inputs.</li><li>Run static type checking.</li><li>Build wheel and source distribution artifacts.</li><li>Install the wheel into a clean environment.</li><li>Import and execute the package outside its source directory.</li></ol><p>Each step establishes a different kind of evidence.</p><p>A passing unit test tells us something about behavior.</p><p>A passing type check tells us something about the consistency of declared interfaces.</p><p>A successful build tells us something about the distribution process.</p><p>A successful installation outside the source tree tells us something about the independence of the resulting artifact.</p><p>No single result proves the entire package is correct.</p><p>The strength comes from combining these forms of verification.</p><h3>9. What breaks when assumptions change?</h3><p>A useful engineering investigation should not stop when the normal case works.</p><p>We need to deliberately change conditions and observe what happens.</p><p>Consider four experiments.</p><p><strong>Experiment 1: A dependency disappears.</strong></p><p>The package imports a third-party library that is not installed in the target environment.</p><p>The implementation might be perfectly correct, but importing the package can fail.</p><p>This exposes the importance of declaring dependencies correctly and controlling the installation environment.</p><p><strong>Experiment 2: An invalid value enters the system.</strong></p><p>A function expects numeric values but receives strings from an external data source.</p><p>Static checking can detect certain mistakes in code, but runtime validation must protect the application against unexpected external data.</p><p><strong>Experiment 3: The source directory is unavailable.</strong></p><p>The package is installed into a clean environment and executed elsewhere.</p><p>If imports fail, we need to investigate package discovery, included files, dependencies, or assumptions about the working directory.</p><p><strong>Experiment 4: A new requirement changes the input contract.</strong></p><p>The original implementation accepts lists, but the new requirement introduces generators and streaming input.</p><p>The function’s interface, memory consumption, and testing strategy must all be reconsidered.</p><p>These experiments reveal something important.</p><p>A system is not defined only by what it does under expected conditions.</p><p>It is also defined by how it responds when its assumptions are violated.</p><h3>10. The connection to large-scale engineering</h3><p>The same problems appear in larger software systems.</p><p>A small Python package has dependencies, interfaces, versioning, build processes, and consumers.</p><p>A distributed service has dependencies, interfaces, versioning, deployment processes, and consumers.</p><p>The scale changes, but the underlying engineering concerns remain recognizable.</p><p>Consider Google’s public engineering ecosystem.</p><p>Bazel provides a useful example of explicit build configuration and dependency relationships. Large-scale codebases also demonstrate why interface stability, dependency management, and reproducible tooling become increasingly important as the number of developers and components grows.</p><p>We should not assume that a small Python package represents Google’s internal architecture. Instead, we can compare the engineering principles and investigate publicly documented mechanisms.</p><p>The connections are useful:</p><p>Python packageLarger engineering systemPublic package APIService or library contractDependency metadataDependency managementWheel distributionSoftware artifact distributionPackage versionRelease and compatibility managementStatic type checkingStatic analysis and contract verificationIsolated installationReproducible deployment environmentUnit testsComponent and integration verificationBuild artifactDeployment artifact</p><p>The deeper question is how organizations maintain correctness when software is no longer developed or consumed by a single person.</p><p>At that point, consistency becomes an engineering requirement rather than a matter of individual preference.</p><p>Tools, explicit contracts, automated checks, and reproducible processes help reduce the number of assumptions that must be communicated informally between developers.</p><h3>11. What do these mechanisms actually guarantee?</h3><p>One mistake engineers can make is treating a successful tool execution as proof of overall correctness.</p><p>Each mechanism provides a different level of confidence.</p><p>MechanismWhat it providesWhat it does not proveType annotationsDeclared interfacesRuntime correctnessStatic type checkerDetection of certain type errorsAbsence of all bugsUnit testsEvidence for tested behaviorCorrectness for every inputDependency metadataDeclared dependenciesPermanent dependency availabilityWheel buildA distribution artifactCorrect behavior in every environmentIsolated installationInstallation and import evidenceProduction reliabilityVersion pinningMore controlled dependency selectionComplete reproducibility by itself</p><p>Even dependency pinning has limits.</p><p>A reproducible environment may require controlling transitive dependencies, the Python interpreter version, the build environment, platform-specific dependencies, and other environmental variables.</p><p>Likewise, a successful build does not establish that the package will behave correctly under production workloads.</p><p>The engineering task is to understand the boundary of every guarantee.</p><p>A good engineer should be able to explain not only why a mechanism is useful, but also what it cannot establish.</p><h3>12. Beyond working code</h3><p>Packaging and typing initially appear to be separate technical concerns.</p><p>One is about distribution. The other is about interfaces.</p><p>But together, they reveal a broader idea about software engineering.</p><p>A function is an implementation of behavior.</p><p>A type contract communicates expectations about that behavior.</p><p>A package establishes an organizational and distribution boundary.</p><p>A build system transforms the source into a distributable artifact.</p><p>Tests provide evidence that selected behaviors are correct.</p><p>Installation experiments establish whether the resulting software can be used independently of its original development environment.</p><p>These mechanisms answer different questions, but they work toward a common objective: reducing ambiguity between the software we build and the systems that depend on it.</p><p>This changes how I think about a software project.</p><p>Previously, it was easy to consider a project complete when its functions worked and its tests passed.</p><p>Now there are additional questions.</p><p>Can another developer install it without knowing my directory structure? Can a different application understand its public interface? Are its dependencies declared? Can its artifact be built again? What happens when an input contract changes? Which assumptions are enforced, and which are merely documented?</p><p>These questions move the investigation beyond implementation.</p><p><strong>The goal is not simply to write code that works in one environment. It is to create software that can be understood, distributed, verified, and maintained by systems and people beyond its original author.</strong></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=e26f0c138821" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Modules: The Architecture of Growing Software]]></title>
            <link>https://medium.com/@pranova2508/modules-the-architecture-of-growing-software-c57dd082705f?source=rss-78d72cb29a4f------2</link>
            <guid isPermaLink="false">https://medium.com/p/c57dd082705f</guid>
            <category><![CDATA[software-engineering]]></category>
            <category><![CDATA[java]]></category>
            <category><![CDATA[computer-science]]></category>
            <dc:creator><![CDATA[Prac's Journal]]></dc:creator>
            <pubDate>Wed, 11 Nov 2026 11:03:58 GMT</pubDate>
            <atom:updated>2026-11-11T11:03:58.904Z</atom:updated>
            <content:encoded><![CDATA[<p>D 011 <em>Why separating code is not enough, and how modules help us control complexity.</em></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*_bXSNOmr8ihiUZ84U4rYhQ.png" /></figure><p>A program can begin with a single file.</p><p>A few functions, some variables, perhaps a little input processing. Everything is close together, easy to understand, and relatively easy to change.</p><p>At this stage, there is little reason to introduce a complicated structure. The program is small enough that we can hold most of it in our heads.</p><p>But software has a tendency to grow.</p><p>A simple data-processing script might eventually need to read CSV and JSON files, validate records, handle missing values, calculate statistics, generate reports, and expose its functionality to other applications.</p><p>Each new requirement introduces more code. More importantly, it introduces more relationships between pieces of code.</p><p>A function begins depending on another function. Two different responsibilities start sharing implementation details. A change intended for one part of the application unexpectedly affects another.</p><p>The difficulty is no longer writing the next function.</p><p>It is understanding how everything fits together.</p><p>This is where an important question emerges:</p><p><strong>How do we divide a growing program into independent parts without losing the ability to make those parts work together?</strong></p><p>Python’s module system provides one answer.</p><p>But modules are more interesting than a way of organizing files. They introduce a mechanism for creating namespaces, exposing interfaces, and controlling how different parts of a program depend on one another.</p><p>To understand why that matters, we need to look beyond the syntax of import.</p><h3>1. When a single file stops being a good idea</h3><p>Imagine building a small application that processes student performance records.</p><p>Initially, everything fits into one Python file:</p><pre>def read_records(path):<br>    pass<br></pre><pre>def validate_records(records):<br>    pass<br></pre><pre>def clean_records(records):<br>    pass<br></pre><pre>def calculate_statistics(records):<br>    pass<br></pre><pre>def generate_report(results):<br>    pass</pre><p>There is nothing inherently wrong with this design.</p><p>In fact, for a small script, it might be the simplest and most appropriate solution.</p><p>The problem appears when the application starts evolving.</p><p>Suppose we introduce a requirement to support JSON files alongside CSV files. Then we add different validation rules, more transformation operations, and several report formats.</p><p>The original file becomes responsible for too many things.</p><p>Now consider a change to the reporting functionality. Ideally, it should not require us to understand how CSV parsing works. Similarly, replacing the input format should not force us to rewrite the statistical calculations.</p><p>These responsibilities have different reasons to change.</p><p>Keeping them together creates unnecessary relationships between otherwise independent parts of the application.</p><p>One solution is to separate them:</p><pre>data_processing/<br>│<br>├── readers.py<br>├── validators.py<br>├── transformations.py<br>├── statistics.py<br>└── exporters.py</pre><p>Each file becomes a module.</p><p>The reader handles input. The validator checks records. The transformation module changes data. The statistics module calculates results. The exporter produces the final output.</p><p>The important change is not that we now have five files instead of one.</p><p>It is that we have started expressing the structure of our application through boundaries.</p><p>A well-designed boundary allows one part of the system to use another part without needing to understand its entire implementation.</p><p>That distinction is fundamental to software engineering.</p><h3>2. A module is more than a file</h3><p>In Python, a module is an importable unit of code. A regular Python source module is commonly represented by a .py file.</p><p>Consider:</p><pre># statistics.py</pre><pre>def calculate_average(values):<br>    return sum(values) / len(values)<br></pre><pre>def calculate_maximum(values):<br>    return max(values)</pre><p>Another file can use its functionality:</p><pre>import statistics</pre><pre>scores = [80, 90, 70]</pre><pre>average = statistics.calculate_average(scores)</pre><pre>print(average)</pre><p>We could also write:</p><pre>from statistics import calculate_average</pre><pre>scores = [80, 90, 70]</pre><pre>print(calculate_average(scores))</pre><p>The syntax is straightforward.</p><p>What matters is the relationship it establishes.</p><p>The calling code does not need to know how the average is calculated. It only needs to know that the module exposes a function with a particular name and expected behaviour.</p><p>This is an interface.</p><p>The implementation can change internally while the interface remains stable.</p><p>For example, perhaps the original function uses a simple loop. Later, we optimize it using a different algorithm. If its contract remains unchanged, the rest of the application may not need to change.</p><p>This is one of the reasons modules are useful: they allow us to separate what a component provides from how it provides it.</p><p>However, Python does not automatically enforce strict interface boundaries. We still have to design them deliberately.</p><h3>3. What actually happens when we import something?</h3><p>The interesting part begins when we ask what Python does behind the familiar import statement.</p><p>Consider:</p><pre>import math</pre><pre>result = math.sqrt(25)</pre><pre>print(result)</pre><p>It looks as though Python simply makes another file available to us.</p><p>But an import involves several operations.</p><p>When Python encounters an import request, its import machinery first checks whether the module is already present in sys.modules.</p><p>This dictionary acts as a cache of modules loaded in the current interpreter.</p><p>If the module is not already available, Python uses its import system to locate it. This involves finders and loaders that determine where the module exists and how it should be loaded.</p><p>Once found, the module is loaded and initialized. For an ordinary Python source module, its top-level code is executed during initialization.</p><p>Python then makes the resulting module object available through the importing name.</p><p>Let’s observe this directly:</p><pre>import math<br>import sys</pre><pre>print(math.sqrt(25))</pre><pre>print(&quot;math&quot; in sys.modules)</pre><pre>print(math.__name__)<br>print(math.__file__)</pre><p>The last two attributes are particularly interesting.</p><p>__name__ identifies the module by name.</p><p>__file__, when available, identifies the location from which it was loaded.</p><p>We can also investigate what happens when a module is imported more than once.</p><p>Create a file called module_a.py:</p><pre>print(&quot;Initializing module A&quot;)</pre><pre>value = 100</pre><p>Now create main.py:</p><pre>import module_a<br>import module_a</pre><pre>print(module_a.value)</pre><p>In a normal execution of this program, the initialization message appears once.</p><p>Why?</p><p>Because after the first successful import, Python caches the module object in sys.modules. The second import generally reuses that object instead of executing the module again.</p><p>This is an important design detail.</p><p>A module is not simply a collection of functions that Python copies into another file every time we import it.</p><p>It is an object managed by the interpreter, with its own namespace and initialization lifecycle.</p><p>Understanding this mechanism helps explain behaviour that otherwise appears surprising.</p><h3>4. Namespaces: keeping different parts of a program independent</h3><p>Consider two modules:</p><pre># pricing.py</pre><pre>value = 100</pre><pre># statistics.py</pre><pre>value = 200</pre><p>Both define a variable called value.</p><p>In a single shared namespace, this could create a naming conflict.</p><p>But Python modules provide separate namespaces.</p><pre>import pricing<br>import statistics</pre><pre>print(pricing.value)<br>print(statistics.value)</pre><p>The output is:</p><pre>100<br>200</pre><p>The two variables coexist because they belong to different module namespaces.</p><p>This is a deceptively powerful idea.</p><p>As applications grow, different components naturally develop their own names, functions, classes, and internal state.</p><p>A module gives those names a context.</p><p>Instead of treating the entire application as one enormous collection of globally accessible names, we can organize names according to their responsibilities.</p><p>But namespace isolation should not be confused with complete independence.</p><p>Two modules can still share mutable objects, depend on each other’s internal details, or create circular dependencies.</p><p>The namespace solves naming problems. Good architecture requires us to think about the relationships between modules as well.</p><h3>5. When modules become packages</h3><p>A single module is useful for separating one responsibility.</p><p>But what happens when an application contains dozens or hundreds of related modules?</p><p>We need another level of organization.</p><p>Python provides packages.</p><p>Consider the following structure:</p><pre>data_processing/<br>│<br>├── __init__.py<br>│<br>├── readers.py<br>├── validators.py<br>├── statistics.py<br>│<br>└── transformations/<br>    │<br>    ├── __init__.py<br>    ├── cleaning.py<br>    └── normalization.py</pre><p>Here, readers.py, validators.py, and statistics.py are modules.</p><p>The data_processing directory is a regular package, and transformations is a subpackage.</p><p>We can import functionality using its package path:</p><pre>from data_processing.readers import read_csv</pre><pre>from data_processing.validators import validate_records</pre><pre>from data_processing.transformations.cleaning import clean_records</pre><p>Notice how the structure communicates something about the application.</p><p>The import path tells us where a responsibility belongs.</p><p>We are no longer just collecting files. We are creating a hierarchy of related functionality.</p><p>There is an important distinction here: Python also supports namespace packages that do not require an __init__.py file. The structure above uses regular packages because they provide a straightforward starting point.</p><p>Another distinction is equally important.</p><p>Organizing modules into a package does not automatically make that package installable or distributable.</p><p>If we want another project to use it, we need to consider package metadata, build configuration, dependencies, and installation.</p><p>That takes us from code organization to software distribution.</p><h3>6. Building a data-processing package</h3><p>To make these ideas concrete, let’s consider a small data-processing system.</p><p>Its job is to accept records, validate them, calculate statistics, and produce a summary.</p><p>Our input looks like this:</p><pre>records = [<br>    {&quot;name&quot;: &quot;Alice&quot;, &quot;score&quot;: 80},<br>    {&quot;name&quot;: &quot;Bob&quot;, &quot;score&quot;: 90},<br>    {&quot;name&quot;: &quot;Charlie&quot;, &quot;score&quot;: 70}<br>]</pre><p>We want to produce:</p><pre>{<br>    &quot;count&quot;: 3,<br>    &quot;average_score&quot;: 80.0,<br>    &quot;highest_score&quot;: 90<br>}</pre><p>A first implementation might place all the logic in one file.</p><p>But let’s organize the system around its responsibilities:</p><pre>data_processing/<br>│<br>├── __init__.py<br>├── readers.py<br>├── validators.py<br>├── transformations.py<br>├── statistics.py<br>└── exporters.py</pre><p>The reader is responsible for understanding input formats.</p><p>The validator checks whether the records satisfy the application’s requirements.</p><p>The transformation module handles operations that modify the data.</p><p>The statistics module calculates meaningful measurements.</p><p>The exporter converts the results into an output format.</p><p>Each module should expose only the functionality that other parts of the application genuinely need.</p><p>For example, the statistics module might expose:</p><pre>def calculate_average(scores):<br>    if not scores:<br>        raise ValueError(&quot;Scores cannot be empty&quot;)</pre><pre>    return sum(scores) / len(scores)</pre><p>Another module can use this function without needing to know how the calculation works.</p><p>This also creates a useful testing boundary.</p><p>We can test calculate_average() independently with normal inputs, empty lists, and other relevant boundary cases.</p><p>Then we can test the complete processing pipeline separately.</p><p>The separation gives us more control over what we test and where failures originate.</p><p>However, this structure is only a starting point.</p><p>The real test of the architecture comes when requirements change.</p><p>Suppose we now need to support a new input format.</p><p>Can we add another reader without modifying the statistical calculations?</p><p>Suppose we need to change how missing values are handled.</p><p>Can we update the transformation logic without rewriting the exporters?</p><p>If the answer is yes, our boundaries are doing useful work.</p><p>If every change requires modifications across the entire package, we may have separated files without actually separating responsibilities.</p><h3>7. The problem that appears between modules: dependencies</h3><p>Once we divide an application into modules, they need to communicate.</p><p>This introduces another architectural problem: dependency management.</p><p>Imagine a simplified dependency structure:</p><pre>readers<br>   |<br>   v<br>validators<br>   |<br>   v<br>transformations<br>   |<br>   v<br>statistics<br>   |<br>   v<br>exporters</pre><p>Each stage uses functionality provided by another stage.</p><p>This is manageable when dependencies are clear and predictable.</p><p>But consider a different structure:</p><pre>module_a → module_b<br>module_b → module_c<br>module_c → module_a</pre><p>Now the dependencies form a cycle.</p><p>Circular imports are particularly interesting because they expose an important detail of Python’s import mechanism.</p><p>Python places a module in sys.modules during initialization, before its execution has necessarily completed.</p><p>This allows certain circular import patterns to work, but it can also mean that another module encounters a partially initialized module and cannot access the name it expects.</p><p>The result may be an import error.</p><p>The underlying architectural problem is often that responsibilities are not clearly separated.</p><p>Perhaps two modules know too much about one another. Perhaps shared functionality belongs in a third module. Perhaps one component should receive a dependency rather than importing it directly.</p><p>The solution is not always to eliminate every cycle mechanically.</p><p>It is to understand why the cycle exists and whether the dependency structure reflects the actual responsibilities of the system.</p><p>This leads to a broader principle:</p><p><strong>The quality of a modular system depends not only on how its components are separated, but also on how their dependencies are controlled.</strong></p><h3>8. An import can execute more than we expect</h3><p>There is another subtle problem worth investigating.</p><p>Consider this module:</p><pre># database.py</pre><pre>print(&quot;Connecting to database...&quot;)</pre><pre>def get_records():<br>    return []</pre><p>Now imagine that five different modules import database.py.</p><p>Even though each module only wants to use get_records(), the top-level code runs during the first import.</p><p>This is an import-time side effect.</p><p>In a real application, the consequences could be more significant than printing a message.</p><p>A module might load a large dataset, create a network connection, read environment variables, or initialize an external service.</p><p>That means an import can unexpectedly become an expensive operation.</p><p>A better approach is often to make initialization explicit:</p><pre>def connect_to_database():<br>    print(&quot;Connecting to database...&quot;)</pre><pre>    # Establish the connection here.</pre><p>Now importing the module makes the function available without automatically connecting to the database.</p><p>The application decides when to perform that operation.</p><p>This distinction becomes increasingly important in larger applications, test environments, command-line tools, and systems where startup time matters.</p><p>It also illustrates something important about module design.</p><p>A good module should not surprise the code that imports it.</p><h3>9. What changes when we move beyond the development machine?</h3><p>A package may work perfectly when executed from the directory in which it was created.</p><p>But that does not prove it is ready to be used elsewhere.</p><p>Imagine another developer cloning the repository and trying to install the package.</p><p>Will the imports still work?</p><p>Are all dependencies available?</p><p>Does the application rely on the current working directory?</p><p>Can the package be tested independently?</p><p>These questions introduce several additional engineering concerns.</p><p><strong>Environment isolation</strong></p><p>Different projects may require different versions of the same dependency. Python virtual environments allow us to isolate those dependencies.</p><p><strong>Packaging and distribution</strong></p><p>A pyproject.toml file provides a standard place to configure project metadata and build requirements.</p><p>We can build and install the package rather than relying on its original directory structure.</p><p><strong>Testing</strong></p><p>We should test individual modules as well as the complete application.</p><p>A successful execution is not enough. We need evidence that normal inputs, invalid inputs, empty datasets, and boundary conditions behave correctly.</p><p><strong>Typing</strong></p><p>Type annotations communicate the expected structure of an interface:</p><pre>def calculate_average(scores: list[float]) -&gt; float:<br>    return sum(scores) / len(scores)</pre><p>This communicates the intended input and output types.</p><p>Python does not enforce these annotations automatically at runtime, but static type checkers can use them to detect certain classes of errors.</p><p><strong>Import resolution</strong></p><p>We should verify that the package works in a clean environment after installation.</p><p>A successful import from the repository root may simply be a consequence of how Python configures its module search path.</p><p>It is not necessarily proof that the package has been installed correctly.</p><p>These details reveal that modular programming is connected to a much larger engineering discipline.</p><p>Organizing code is only the beginning. We must also make the resulting structure reproducible, testable, installable, and maintainable.</p><h3>10. The same problem at a much larger scale</h3><p>The challenge of managing dependencies does not disappear when we move from a small Python package to a large software organization.</p><p>It becomes more important.</p><p>Consider a codebase containing thousands of components maintained by hundreds of developers.</p><p>A small change to a shared library may affect dozens of other components.</p><p>A dependency update may require rebuilding multiple targets.</p><p>An unclear dependency graph can make it difficult to determine what needs to be tested after a change.</p><p>This is one reason build systems become important.</p><p>Google’s publicly documented Bazel build system provides a useful example of how large codebases can represent build targets and their dependencies.</p><p>A build system needs to understand relationships between components, determine what must be built, and execute those operations according to dependency constraints.</p><p>Bazel targets are not the same thing as Python modules, but they address a related organizational challenge: making dependencies explicit and managing changes across a large codebase.</p><p>The scale is different, but the underlying question is recognizable.</p><p>How do we divide a system into meaningful units while preserving predictable relationships between them?</p><p>The same principle appears in libraries, build systems, services, databases, and distributed architectures.</p><p>The mechanisms differ, but the need for boundaries remains.</p><h3>11. The engineering trade-off: How modular should a system become?</h3><p>There is a temptation to conclude that smaller modules are always better.</p><p>They are not.</p><p>Imagine creating a separate module for every function in a small application.</p><p>We might end up with dozens of tiny files, each requiring imports and navigation.</p><p>The application would have more boundaries, but not necessarily more clarity.</p><p>On the other hand, placing every responsibility inside one enormous module creates the opposite problem.</p><p>There is no universal number of modules that makes an application well designed.</p><p>The right boundaries depend on the responsibilities of the system, the way those responsibilities change, and the relationships between them.</p><p>A small script may be the best solution for a temporary task.</p><p>A structured package may be more appropriate when functionality needs to be reused, tested independently, or distributed.</p><p>A larger application may need additional architectural boundaries beyond Python packages.</p><p>The goal is not to maximize modularity.</p><p>It is to minimize unnecessary coupling while keeping the system understandable.</p><p>This is a judgment that cannot be made by syntax alone.</p><p>It requires understanding how the software is expected to evolve.</p><h3>12. What I understand differently now</h3><p>I started this investigation thinking about modules as a way to divide a large Python program into multiple files.</p><p>That explanation is correct, but incomplete.</p><p>The deeper idea is that software becomes difficult to maintain when its responsibilities and dependencies become difficult to understand.</p><p>Modules provide a mechanism for introducing boundaries.</p><p>Namespaces help organize names. Imports make functionality available across those boundaries. Packages organize related modules. Packaging tools make those components installable and reusable.</p><p>But the mechanisms do not automatically create good architecture.</p><p>We still have to decide what belongs together, what should remain independent, which functionality should be exposed, and how components should communicate.</p><p>A poorly designed package can be just as difficult to maintain as a poorly designed script.</p><p>The difference is that a well-designed modular system gives us control over how complexity grows.</p><p>Perhaps that is the most useful way to think about modules:</p><p>They are not primarily about organizing files.</p><p>They are about organizing change.</p><p>As software grows, different parts of it acquire different responsibilities and different reasons to evolve. Good module boundaries allow those parts to change without unnecessarily disturbing the rest of the system.</p><p>That is what makes modularity valuable.</p><p>And that is why understanding modules is not merely learning a Python feature. It is learning one of the foundations of software architecture.</p><p><em>Day 011 of my Computational Foundations engineering apprenticeship.</em></p><p><em>This investigation is part of a longer effort to understand computer science through implementation, experimentation, failure analysis, and engineering judgment.</em></p><p><strong>Build → Measure → Break → Explain → Publish → Improve.</strong></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=c57dd082705f" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Iterators and Smart Pointers: The Hidden Responsibilities Behind Objects]]></title>
            <link>https://medium.com/@pranova2508/iterators-and-smart-pointers-the-hidden-responsibilities-behind-objects-906a6cf87e1f?source=rss-78d72cb29a4f------2</link>
            <guid isPermaLink="false">https://medium.com/p/906a6cf87e1f</guid>
            <category><![CDATA[computer-science]]></category>
            <category><![CDATA[software-engineering]]></category>
            <category><![CDATA[java]]></category>
            <category><![CDATA[software-development]]></category>
            <dc:creator><![CDATA[Prac's Journal]]></dc:creator>
            <pubDate>Sun, 08 Nov 2026 10:51:01 GMT</pubDate>
            <atom:updated>2026-11-08T10:51:01.582Z</atom:updated>
            <content:encoded><![CDATA[<p>Day 010 · Computational Foundations</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*9JSPIg_-OOhFqUVGfTNf6g.png" /></figure><p>Imagine building a collection that stores a million objects.</p><p>You want other parts of your program to process those objects. But you don’t want every caller to know whether the collection uses an array, a linked list, or some entirely different data structure.</p><p>Now consider another problem.</p><p>Several components in your program use the same objects. Who decides when an object should stop existing? Who is responsible for releasing the memory or resources associated with it?</p><p>These questions appear in different parts of programming, but they point towards a common engineering challenge: how do we separate responsibilities without losing control over what happens underneath?</p><p>Two ideas address these problems: iterators and smart pointers.</p><p>An iterator separates the process of accessing elements from the way they are stored. A smart pointer makes ownership and object lifetime more explicit.</p><p>Understanding them is not just about learning two programming constructs. It is about understanding how software manages complexity as systems grow.</p><h3>The first problem: Why should traversal depend on storage?</h3><p>Let’s start with something familiar.</p><p>Suppose we have a collection:</p><pre>[10, 20, 30, 40, 50]</pre><p>We want to process every element.</p><p>The obvious solution is to use an index:</p><pre>for (int i = 0; i &lt; collection.size(); i++) {<br>    System.out.println(collection.get(i));<br>}</pre><p>There is nothing inherently wrong with this code.</p><p>But it introduces an assumption: the collection supports efficient indexed access.</p><p>That assumption matters when we change the underlying data structure.</p><p>An array allows us to access an element directly using its index. A linked list works differently. To reach an element, we generally have to follow links from one node to another.</p><p>The logical collection may remain exactly the same, but the mechanism used to traverse it changes.</p><p>This creates an unnecessary dependency between two things:</p><ul><li>How data is stored.</li><li>How data is consumed.</li></ul><p>Ideally, code that processes a collection should not need to understand its internal representation.</p><p>It should only need a reliable way to obtain the next element.</p><p>That is where iterators become useful.</p><h3>An iterator is a boundary between two responsibilities</h3><p>An iterator provides a way to traverse a collection without requiring the caller to understand its storage implementation.</p><p>In Java:</p><pre>Iterator&lt;Integer&gt; iterator = collection.iterator();</pre><pre>while (iterator.hasNext()) {<br>    Integer value = iterator.next();<br>    System.out.println(value);<br>}</pre><p>Compare this with the earlier index-based loop.</p><p>The caller no longer asks for an element at a particular position. Instead, it asks whether another element exists and then requests that element.</p><p>The collection provides an iterator. The iterator manages the traversal process.</p><p>Java expresses this relationship through two interfaces:</p><ul><li>Iterable&lt;T&gt; represents something that can provide an iterator.</li><li>Iterator&lt;T&gt; represents the mechanism used to traverse it.</li></ul><p>Th distinction is important.</p><p>A collection is responsible for providing access to its elements. An iterator is responsible for maintaining the state of traversal.</p><p>This separation allows different collection implementations to expose a common way of being consumed.</p><p>An array-backed collection and a linked collection can have completely different internal structures while supporting the same iteration interface.</p><p>The calling code does not need to change simply because the storage representation changes.</p><p>That is the real power of the abstraction.</p><h3>What is actually happening when an iterator moves?</h3><p>An iterator needs to remember where it is.</p><p>Consider our original collection:</p><pre>[10, 20, 30, 40, 50]</pre><p>When an iterator is created, it begins with an initial traversal position.</p><p>Its internal state might look like this:</p><pre>Collection: [10, 20, 30, 40, 50]<br>Cursor:      ^</pre><p>When hasNext() is called, the iterator checks whether another element is available.</p><p>When next() is called, it returns the current element and advances its internal position.</p><p>The process repeats until the collection has no more elements.</p><pre>hasNext() → true<br>next()    → 10</pre><pre>hasNext() → true<br>next()    → 20</pre><pre>hasNext() → true<br>next()    → 30</pre><pre>hasNext() → true<br>next()    → 40</pre><pre>hasNext() → true<br>next()    → 50</pre><pre>hasNext() → false</pre><p>The iterator is effectively maintaining a small state machine.</p><p>Its state changes after every successful call to next().</p><p>In Java, calling next() after the iterator is exhausted must throw NoSuchElementException. This is part of the iterator contract.</p><p>A small mechanism establishes a useful boundary: the caller controls when to request another element, while the iterator controls how traversal progresses.</p><p>There is another benefit.</p><p>The iterator can be implemented differently depending on the underlying collection.</p><p>For an array-backed collection, it might maintain an integer cursor.</p><p>For a linked structure, it might maintain a reference to the current node.</p><p>The external interface can remain unchanged even though the internal traversal mechanism is different.</p><p>This is an example of representation independence: the implementation can change without forcing every consumer to understand that change.</p><h3>Building the abstraction ourselves</h3><p>To understand this mechanism properly, I am extending a generic collection library in Java.</p><p>The objective is not simply to make a loop work. It is to create a collection that exposes traversal without exposing its storage representation.</p><p>A simplified implementation might look like this:</p><pre>public class GenericCollection&lt;T&gt; implements Iterable&lt;T&gt; {</pre><pre>    private final T[] elements;<br>    private int size;</pre><pre>    @Override<br>    public Iterator&lt;T&gt; iterator() {</pre><pre>        return new Iterator&lt;&gt;() {</pre><pre>            private int cursor = 0;</pre><pre>            @Override<br>            public boolean hasNext() {<br>                return cursor &lt; size;<br>            }</pre><pre>            @Override<br>            public T next() {</pre><pre>                if (!hasNext()) {<br>                    throw new NoSuchElementException();<br>                }</pre><pre>                return elements[cursor++];<br>            }<br>        };<br>    }<br>}</pre><p>Here, the iterator maintains its own cursor.</p><p>The collection provides the data, while the iterator manages traversal.</p><p>The caller can simply write:</p><pre>for (Integer number : collection) {<br>    System.out.println(number);<br>}</pre><p>It does not need to know how the elements are stored.</p><p>This example is intentionally simple. A production-quality implementation must also consider mutation behavior, iterator validity, independent iterators, and the collection’s modification contract.</p><p>For example, what happens if the collection changes while an iterator is traversing it?</p><p>Many Java collection implementations use fail-fast iterators. They track structural modifications and may throw ConcurrentModificationException when unexpected changes are detected.</p><p>This is useful for exposing certain programming errors, but it is not a thread-safety guarantee.</p><p>The important lesson is that an abstraction needs a contract not only for normal operation, but also for situations in which its assumptions are violated.</p><h3>The second problem: Who is responsible for an object’s lifetime?</h3><p>Now let’s move away from collections.</p><p>Consider a C++ program that allocates memory dynamically:</p><pre>int* value = new int(42);</pre><p>The program has allocated memory and received a pointer to it.</p><p>But allocation creates an additional responsibility.</p><p>At some point, that memory must be released:</p><pre>delete value;</pre><p>This seems straightforward when the program is small.</p><p>The difficulty appears when objects move between functions, components share references, or execution takes unexpected paths.</p><p>What if the programmer forgets to call delete?</p><p>The memory remains allocated even though the program no longer needs it.</p><p>What if the same memory is deleted twice?</p><p>The program encounters undefined behavior.</p><p>What if an exception interrupts execution before the cleanup operation?</p><p>The resource may never be released.</p><p>The underlying problem is not simply memory allocation.</p><p>It is ownership.</p><p>When an object is created, someone must be responsible for determining how long it remains alive and when its resources should be released.</p><p>In simple programs, this responsibility can be managed manually.</p><p>In complex systems, relying entirely on manual discipline becomes increasingly difficult.</p><p>C++ smart pointers were designed to make these ownership relationships more explicit.</p><h3>Smart pointers: expressing ownership through types</h3><p>C++ provides three important smart pointer types: unique_ptr, shared_ptr, and weak_ptr.</p><p>They represent different relationships between an object and the code that refers to it.</p><h3>Exclusive ownership: unique_ptr</h3><p>Suppose an object should have exactly one owner.</p><p>C++ provides std::unique_ptr:</p><pre>auto value = std::make_unique&lt;int&gt;(42);</pre><p>The pointer exclusively owns the dynamically allocated object.</p><p>When the owning unique_ptr is destroyed, the managed object is automatically destroyed.</p><p>Ownership can be transferred, but the pointer cannot be copied.</p><p>This makes the ownership relationship clear.</p><p>If one component owns an object and transfers ownership to another component, the responsibility moves with it.</p><p>The programmer does not need to remember a separate cleanup instruction for every possible execution path.</p><h3>Shared ownership: shared_ptr</h3><p>Now consider a different situation.</p><p>Two components genuinely need to share ownership of the same object.</p><p>C++ provides std::shared_ptr:</p><pre>auto first = std::make_shared&lt;int&gt;(42);</pre><pre>auto second = first;</pre><p>Both pointers now participate in owning the same object.</p><p>A reference count tracks the number of strong owning references.</p><p>When the last strong owner disappears, the managed object is destroyed.</p><p>This makes shared ownership convenient, but it also introduces costs.</p><p>Reference counting requires bookkeeping, and the lifetime of an object becomes dependent on the relationships between all its owners.</p><p>An object may remain alive longer than expected because another component still holds a reference.</p><p>The abstraction solves an ownership problem, but it does not remove the need to reason about ownership.</p><h3>Non-owning references: weak_ptr</h3><p>Shared ownership creates another interesting problem.</p><p>Imagine two objects that strongly own each other.</p><p>Object A holds a shared_ptr to B, and B holds a shared_ptr to A.</p><p>Even when the rest of the application stops using them, their reference counts may remain nonzero.</p><p>Neither object gets destroyed.</p><p>This is a circular ownership problem.</p><p>C++ provides std::weak_ptr for situations where a component needs to observe an object without participating in its ownership.</p><p>A weak pointer does not increase the strong reference count.</p><p>It can be checked and converted into a temporary owning reference when the object is still alive.</p><p>This distinction between owning and merely observing an object is important.</p><p>Not every reference should extend an object’s lifetime.</p><h3>Java approaches the problem differently</h3><p>The comparison becomes particularly interesting when we move from C++ to Java.</p><p>Java does not provide direct equivalents of unique_ptr and shared_ptr.</p><p>Instead, ordinary Java objects are managed through references and garbage collection.</p><p>Consider:</p><pre>Person person = new Person();</pre><p>The variable holds a reference to an object.</p><p>Java’s garbage collector determines when objects are no longer reachable and reclaims their memory.</p><p>This removes the need for programmers to manually deallocate ordinary objects.</p><p>But garbage collection does not mean that Java has solved every resource-management problem.</p><p>Consider opening a file:</p><pre>try (BufferedReader reader =<br>         Files.newBufferedReader(path)) {</pre><pre>    String line = reader.readLine();<br>}</pre><p>The reader implements AutoCloseable.</p><p>The try-with-resources statement ensures that it is closed when execution leaves the block, including when an exception occurs.</p><p>This is a different mechanism from garbage collection.</p><p>Garbage collection reclaims memory.</p><p>Try-with-resources provides deterministic cleanup for resources that need explicit release.</p><p>A file, socket, or database connection may require cleanup even while the Java object representing it is still reachable.</p><p>This distinction is easy to miss when learning Java.</p><p>An object can be managed automatically by the garbage collector, while the external resource associated with that object still requires explicit lifecycle management.</p><p>Java also provides WeakReference&lt;T&gt;, which has a relationship to the idea behind C++ weak_ptr, but the two mechanisms are not identical. In particular, Java weak references do not participate in shared ownership through a reference count.</p><p>The languages solve related problems through different runtime and type-system mechanisms.</p><h3>What changes when the system grows?</h3><p>These abstractions are easy to understand in small programs.</p><p>Their real importance becomes visible when systems grow.</p><p>Imagine traversing a collection of five elements.</p><p>Almost any reasonable implementation will work.</p><p>Now imagine millions of elements, multiple consumers, frequent modifications, and strict latency requirements.</p><p>The questions become more demanding.</p><p>Does the collection support efficient traversal?</p><p>Does the iterator introduce unnecessary allocations?</p><p>What happens when the underlying collection changes?</p><p>Should the consumer see a live view or a stable snapshot?</p><p>Can multiple threads safely access the data?</p><p>The same is true of object lifetime.</p><p>A small program may have a handful of objects with obvious ownership relationships.</p><p>A larger system may have objects shared across services, caches, background tasks, and asynchronous operations.</p><p>An object that remains alive longer than necessary can increase memory pressure.</p><p>An unclear ownership relationship can make cleanup unpredictable.</p><p>In Java, high allocation rates and long-lived references can influence garbage collection behavior. In C++, excessive shared ownership can make lifetime reasoning and reference-counting overhead more complicated.</p><p>The point is not that one language or mechanism is universally better.</p><p>The point is that every abstraction introduces a set of assumptions, costs, and guarantees.</p><p>Those assumptions need to be understood before the abstraction is used at scale.</p><h3>An experiment: abstraction versus performance</h3><p>For my generic collection library, I am investigating iterator traversal alongside index-based traversal.</p><p>The experiment uses the same collection and the same processing operation.</p><p>The questions are straightforward:</p><ul><li>Do both approaches produce identical results?</li><li>How does traversal behave as the dataset grows?</li><li>Does the implementation depend on the storage representation?</li><li>What happens when the collection is modified during iteration?</li><li>Does either approach introduce measurable performance differences?</li></ul><p>There is no reason to assume that iterator traversal is automatically faster.</p><p>For an array-backed collection, index-based access can be efficient.</p><p>For a linked collection, repeatedly accessing elements by index can become expensive because reaching an element may require traversing preceding nodes.</p><p>An iterator can traverse the linked structure directly.</p><p>The result depends on the data structure and implementation.</p><p>That is why measurement matters.</p><p>I want to understand not only whether an abstraction works, but also what it costs and what flexibility it provides.</p><p>The goal is not to prove that one loop is superior to another.</p><p>It is to understand which approach creates the better boundary between the collection and its consumers.</p><h3>What these two abstractions teach us</h3><p>Iterators and smart pointers solve different problems, but they reveal a similar design principle.</p><p>An iterator separates the responsibility of storing data from the responsibility of traversing it.</p><p>A smart pointer separates the use of an object from the responsibility of managing its lifetime.</p><p>Both reduce the amount of internal knowledge that other parts of a program need.</p><p>But neither abstraction eliminates complexity.</p><p>An iterator still has to deal with invalidation, mutation, exhaustion, and traversal costs.</p><p>A smart pointer still requires careful ownership decisions, and Java still requires explicit management of external resources.</p><p>Good abstractions do not make complexity disappear.</p><p>They put complexity in the right place, establish clear contracts, and allow other parts of the system to depend on those contracts rather than on implementation details.</p><p>That is what makes them valuable.</p><p>When building software, I increasingly find that the important question is not simply how to make something work.</p><p>It is how to make it work without forcing every other part of the system to understand how it works internally.</p><p>That is the engineering value of iterators.</p><p>That is the engineering value of explicit ownership.</p><p>And that is a recurring idea throughout computer science: <strong>separate responsibilities, make assumptions visible, and preserve the freedom to change implementations without breaking the systems that depend on them.</strong></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=906a6cf87e1f" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[One Implementation, Many Types: The Engineering Behind Templates and Generics]]></title>
            <link>https://medium.com/@pranova2508/one-implementation-many-types-the-engineering-behind-templates-and-generics-043484ce2e21?source=rss-78d72cb29a4f------2</link>
            <guid isPermaLink="false">https://medium.com/p/043484ce2e21</guid>
            <category><![CDATA[computer-science]]></category>
            <category><![CDATA[java]]></category>
            <category><![CDATA[software-development]]></category>
            <category><![CDATA[software-engineering]]></category>
            <dc:creator><![CDATA[Prac's Journal]]></dc:creator>
            <pubDate>Sat, 07 Nov 2026 10:41:01 GMT</pubDate>
            <atom:updated>2026-11-07T10:41:01.694Z</atom:updated>
            <content:encoded><![CDATA[<p>Day 009 · Computational Foundations · Engineering Apprenticeship</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*OcBh6R5SPs13FvolEXdVnQ.png" /></figure><p>Imagine building a collection library.</p><p>You start with a collection of integers. It can store values, retrieve elements, remove them, and report how many elements it contains.</p><p>Everything works.</p><p>Then someone asks for a collection of strings.</p><p>You create another implementation. The storage logic is almost identical. The operations are almost identical. Only the type of the elements has changed.</p><p>Next comes a collection of employees, followed by products, orders, and other application objects.</p><p>Each new requirement introduces another implementation of essentially the same behavior.</p><p>At some point, a reasonable question emerges:</p><p><strong>If the behavior does not change when the data type changes, why are we rewriting the implementation?</strong></p><p>This is more than a code duplication problem. It is a question about how we design abstractions.</p><p>Can we describe an algorithm or data structure independently of the particular types it operates on, while still allowing the compiler to verify that those types are used correctly?</p><p>C++ templates and Java generics approach this problem from different directions.</p><p>Understanding both reveals an important idea in programming language design: reusable code does not have to mean weaker type safety.</p><h3>The problem is not the data. It is the repeated behavior.</h3><p>Consider a simple container that stores one integer.</p><pre>class IntegerBox {<br>    private Integer value;</pre><pre>    public void set(Integer value) {<br>        this.value = value;<br>    }</pre><pre>    public Integer get() {<br>        return value;<br>    }<br>}</pre><p>Now imagine implementing the same container for strings.</p><pre>class StringBox {<br>    private String value;</pre><pre>    public void set(String value) {<br>        this.value = value;<br>    }</pre><pre>    public String get() {<br>        return value;<br>    }<br>}</pre><p>There is almost nothing new in the second implementation.</p><p>The storage mechanism is the same. The operations are the same. The relationship between the container and its stored value is the same.</p><p>Only the type has changed.</p><p>We could continue creating separate classes for every type our application needs, but this would be a poor abstraction. Every improvement to the underlying behavior would have to be repeated across multiple implementations.</p><p>The more types we support, the more duplicated code we maintain.</p><p>The deeper problem is that we have tied two independent things together:</p><ul><li>The behavior of the container.</li><li>The type of data stored inside it.</li></ul><p>These should not necessarily be coupled.</p><p>The container should describe how values are stored and retrieved. The type should describe what kind of values are allowed.</p><p>This separation leads us to generic programming.</p><h3>Give the type a parameter</h3><p>Instead of writing a different class for every data type, we can make the type itself a parameter.</p><p>In Java, this looks like:</p><pre>class Box&lt;T&gt; {</pre><pre>    private T value;</pre><pre>    public void set(T value) {<br>        this.value = value;<br>    }</pre><pre>    public T get() {<br>        return value;<br>    }<br>}</pre><p>The important change is not the letter T.</p><p>It is the relationship that T represents.</p><p>The class no longer assumes that it stores integers or strings. It describes a container whose stored value has a type determined when the class is used.</p><p>We can now create:</p><pre>Box&lt;Integer&gt; integerBox = new Box&lt;&gt;();<br>integerBox.set(100);</pre><pre>Box&lt;String&gt; stringBox = new Box&lt;&gt;();<br>stringBox.set(&quot;Hello&quot;);</pre><p>Both objects use the same class implementation.</p><p>Yet they preserve different type relationships.</p><p>The integer container accepts integers. The string container accepts strings.</p><p>Try introducing an invalid value:</p><pre>integerBox.set(&quot;Hello&quot;);</pre><p>The compiler rejects the operation.</p><p>This is where generic programming becomes interesting.</p><p>We have achieved reuse without abandoning the type system.</p><p>The compiler still understands what each container is supposed to store, and it can reject incompatible operations before the program executes.</p><p>Generics are therefore not simply a way to write less code. They allow us to express constraints about how different parts of a program relate to one another.</p><h3>What happens when we remove those constraints?</h3><p>To understand the value of generics, it helps to look at what happens without them.</p><p>Java allows us to use collections without specifying their element types:</p><pre>List values = new ArrayList();</pre><pre>values.add(100);<br>values.add(&quot;Hello&quot;);<br>values.add(true);</pre><p>This is a raw collection.</p><p>It accepts different kinds of objects because the compiler does not have a specific element type to enforce.</p><p>The problem appears when we retrieve a value.</p><pre>Integer number = (Integer) values.get(0);</pre><p>We must perform a cast.</p><p>The compiler cannot guarantee that the object we retrieve is actually an integer.</p><p>If the collection contains a string and we attempt to retrieve it as an integer, the program can fail at runtime with a ClassCastException.</p><p>Now compare that with:</p><pre>List&lt;Integer&gt; values = new ArrayList&lt;&gt;();</pre><pre>values.add(100);<br>values.add(200);</pre><pre>// Compilation error<br>values.add(&quot;Hello&quot;);</pre><p>The collection now communicates its intended type relationship.</p><p>When we retrieve an element:</p><pre>Integer number = values.get(0);</pre><p>No explicit cast is necessary.</p><p>The compiler already knows the type.</p><p>This moves an entire category of errors from runtime to compile time.</p><p>The difference may seem small in a simple example, but in a large codebase, it becomes a significant design advantage.</p><p>A collection can be passed between methods, stored inside other objects, and used across application boundaries while maintaining its declared type relationship.</p><h3>C++ templates and Java generics: similar ideas, different mechanisms</h3><p>The idea of parameterizing code by type is not exclusive to Java.</p><p>C++ provides a powerful mechanism called templates.</p><p>Consider a function that returns the larger of two values:</p><pre>template &lt;typename T&gt;<br>T maximum(T a, T b) {<br>    return (a &gt; b) ? a : b;<br>}</pre><p>We can use it with integers:</p><pre>int result = maximum(10, 20);</pre><p>Or strings:</p><pre>std::string result =<br>    maximum(<br>        std::string(&quot;Apple&quot;),<br>        std::string(&quot;Orange&quot;)<br>    );</pre><p>The function does not need a separate implementation for every supported type.</p><p>The compiler instantiates the template for the types used.</p><p>Conceptually, it generates type-specific implementations during compilation.</p><p>This approach gives C++ templates considerable flexibility. They support compile-time specialization and powerful forms of compile-time computation.</p><p>However, there is an important requirement hidden inside our example.</p><p>The function uses the &gt; operator.</p><p>Therefore, the supplied type must support that operation in a meaningful way.</p><p>A template parameter does not automatically make every operation valid for every type.</p><p>The operations required by the implementation must be supported by the type being supplied.</p><p>Java generics approach the problem differently.</p><p>Consider our earlier class:</p><pre>class Box&lt;T&gt; {</pre><pre>    private T value;</pre><pre>    public void set(T value) {<br>        this.value = value;<br>    }</pre><pre>    public T get() {<br>        return value;<br>    }<br>}</pre><p>Java generally implements generics through a mechanism called type erasure.</p><p>The compiler checks the generic type relationships, then erases most type arguments when generating bytecode.</p><p>For an unbounded parameter such as T, the erased representation is generally Object.</p><p>For a bounded parameter, erasure uses its leftmost bound.</p><p>The compiler also inserts casts where necessary and may generate bridge methods to preserve polymorphic behavior.</p><p>This means Java does not generally generate a separate runtime class for every parameterized use of Box.</p><p>Box&lt;String&gt; and Box&lt;Integer&gt; are different parameterized types at the source-code level, but they share the same generic class at runtime.</p><p>This is a fundamental difference between the two languages.</p><p>DimensionC++ TemplatesJava GenericsMain mechanismTemplate instantiationType erasureType checkingCompile timeCompile timeType-specific specializationSupportedNo general equivalentPrimitive typesSupported directlyWrapper types requiredRuntime type argumentsDepend on instantiated typesGenerally erasedCompile-time programmingExtensive template metaprogrammingMore limited</p><p>Neither approach should be understood simply as a better or worse version of the other.</p><p>They represent different engineering decisions about compilation, runtime representation, compatibility, and abstraction.</p><h3>The hidden cost of Java’s design: type erasure</h3><p>Type erasure solves an important compatibility problem, but it also introduces limitations.</p><p>Consider:</p><pre>Box&lt;String&gt; strings = new Box&lt;&gt;();<br>Box&lt;Integer&gt; numbers = new Box&lt;&gt;();</pre><p>At the source-code level, these objects have different parameterized types.</p><p>At runtime, however, Java generally does not retain the generic type arguments in a form that allows us to distinguish them directly.</p><p>For example, this is not permitted:</p><pre>if (value instanceof Box&lt;String&gt;) {<br>    // ...<br>}</pre><p>The runtime cannot generally perform this parameterized type check because the String argument has been erased.</p><p>There are other consequences.</p><p>We cannot directly instantiate a type parameter:</p><pre>class Box&lt;T&gt; {</pre><pre>    // Not allowed:<br>    // T value = new T();<br>}</pre><p>We cannot create a generic array in the ordinary way:</p><pre>// Not allowed:<br>T[] values = new T[10];</pre><p>And primitive types cannot be used directly as generic arguments:</p><pre>// Not allowed:<br>List&lt;int&gt; numbers;</pre><p>Instead, we use wrapper types:</p><pre>List&lt;Integer&gt; numbers;</pre><p>Java’s autoboxing and unboxing make this convenient, but convenience does not mean there is no cost. Boxing may introduce additional object allocations and memory overhead.</p><p>These restrictions are easier to understand once we know how the language implements generics.</p><p>They are not arbitrary limitations. They follow from Java’s type-erasure design and its compatibility goals.</p><p>The important engineering lesson is that an abstraction’s implementation strategy determines what the abstraction can and cannot do.</p><h3>A generic type does not mean every operation is valid</h3><p>There is another subtlety worth investigating.</p><p>Suppose we want to write a method that adds numbers from a list.</p><p>A first attempt might look like this:</p><pre>public static &lt;T&gt; T sum(List&lt;T&gt; values) {<br>    // How do we add two arbitrary T values?<br>}</pre><p>The problem is that T could represent almost any reference type.</p><p>It could be an integer, a string, an employee, or a custom object.</p><p>Java has no universal addition operation for arbitrary reference types.</p><p>The compiler cannot assume that every possible T supports arithmetic.</p><p>We must express the constraint our operation requires.</p><p>For example:</p><pre>public static double sum(<br>        List&lt;? extends Number&gt; values) {</pre><pre>    double total = 0;</pre><pre>    for (Number value : values) {<br>        total += value.doubleValue();<br>    }</pre><pre>    return total;<br>}</pre><p>Now the method accepts lists containing subclasses of Number, such as Integer, Double, and Long.</p><p>The type constraint tells the compiler what operations are available.</p><p>This becomes even more interesting when designing APIs that move values between collections.</p><p>Consider:</p><pre>public static &lt;T&gt; void copy(<br>        List&lt;? super T&gt; destination,<br>        List&lt;? extends T&gt; source) {</pre><pre>    for (T value : source) {<br>        destination.add(value);<br>    }<br>}</pre><p>Why does the source use extends while the destination uses super?</p><p>Because the source produces values that we want to read as T, while the destination consumes values of type T.</p><p>This is commonly summarized as PECS:</p><p><strong>Producer Extends, Consumer Super.</strong></p><p>The principle is useful when designing flexible generic APIs.</p><p>It also demonstrates that generic programming is not merely about replacing a concrete type with a placeholder.</p><p>It is about expressing the relationships and constraints that make an abstraction correct.</p><h3>From theory to implementation: building a collection library</h3><p>To understand the idea beyond syntax, I wanted to implement a reusable collection abstraction.</p><p>The objective was straightforward:</p><p>Build one collection implementation that can work with unrelated element types without duplicating its storage logic.</p><p>I began with an interface:</p><pre>public interface Collection&lt;T&gt; {</pre><pre>    void add(T element);</pre><pre>    T get(int index);</pre><pre>    int size();</pre><pre>    boolean isEmpty();<br>}</pre><p>The interface describes the operations a collection must provide.</p><p>It does not specify how elements are stored.</p><p>Next, I introduced an array-backed implementation using Java’s existing ArrayList as the internal storage mechanism:</p><pre>import java.util.ArrayList;<br>import java.util.List;</pre><pre>public class ArrayCollection&lt;T&gt;<br>        implements Collection&lt;T&gt; {</pre><pre>    private final List&lt;T&gt; elements =<br>            new ArrayList&lt;&gt;();</pre><pre>    @Override<br>    public void add(T element) {<br>        elements.add(element);<br>    }</pre><pre>    @Override<br>    public T get(int index) {<br>        return elements.get(index);<br>    }</pre><pre>    @Override<br>    public int size() {<br>        return elements.size();<br>    }</pre><pre>    @Override<br>    public boolean isEmpty() {<br>        return elements.isEmpty();<br>    }<br>}</pre><p>Now the same implementation can be used with unrelated types.</p><pre>Collection&lt;Integer&gt; numbers =<br>        new ArrayCollection&lt;&gt;();</pre><pre>numbers.add(10);<br>numbers.add(20);<br>numbers.add(30);</pre><p>And:</p><pre>Collection&lt;String&gt; languages =<br>        new ArrayCollection&lt;&gt;();</pre><pre>languages.add(&quot;Java&quot;);<br>languages.add(&quot;C++&quot;);<br>languages.add(&quot;Python&quot;);</pre><p>The collection does not need to understand what an integer or a programming language represents.</p><p>It only needs to maintain its contract: store elements of type T and return elements of type T.</p><p>This separation is the real benefit.</p><p>The implementation handles storage behavior.</p><p>The type parameter establishes the relationship between the collection and its elements.</p><p>The compiler checks that relationship.</p><p>But I also wanted to understand what generics do not guarantee.</p><p>For example, they do not prevent an invalid index:</p><pre>numbers.get(1000);</pre><p>They do not automatically prevent null-related problems.</p><p>They do not guarantee good memory usage or efficient algorithms.</p><p>They do not prove that the collection’s internal logic is correct.</p><p>Generics protect type relationships, not every aspect of program correctness.</p><p>That distinction is important.</p><p>A type-safe implementation can still be poorly designed, inefficient, or logically incorrect.</p><h3>What happens when the requirements change?</h3><p>An abstraction should not be judged only by whether it works for a small example.</p><p>It should also be evaluated when its environment or requirements change.</p><p>Consider four situations.</p><p><strong>The collection becomes very large.</strong></p><p>The generic type parameter does not determine the performance of the underlying storage structure.</p><p>An array-backed collection can provide efficient indexed access, but resizing and insertion behavior still depend on its implementation.</p><p>Generics do not make a slow algorithm fast.</p><p><strong>We need to support primitive values.</strong></p><p>Java requires wrapper types for generic parameters.</p><p>This introduces potential boxing and unboxing costs, which may matter in performance-sensitive workloads.</p><p><strong>We need runtime type information.</strong></p><p>Type erasure becomes a limitation.</p><p>If the application needs to construct objects or inspect their classes dynamically, we may need to pass explicit type information, such as a Class&lt;T&gt; token.</p><p><strong>We need stronger constraints.</strong></p><p>A collection that supports arbitrary types may not be sufficient for every problem.</p><p>For example, a sorted collection needs a way to compare its elements.</p><p>We can express that requirement:</p><pre>public class SortedCollection&lt;T extends Comparable&lt;T&gt;&gt; {</pre><pre>    // Operations can rely on comparison.<br>}</pre><p>The type parameter now communicates an additional contract.</p><p>The collection is not intended for every possible type. It is intended for types that provide the comparison behavior it requires.</p><p>This reveals an important principle:</p><p><strong>A good abstraction is reusable, but it is not unrestricted.</strong></p><p>Its constraints should describe the operations it genuinely needs.</p><p>Removing every restriction does not make an abstraction more powerful. Sometimes it simply makes its behavior less precise.</p><h3>The engineering significance</h3><p>Generic programming appears throughout modern software libraries.</p><p>Java’s collections, maps, comparators, optional values, and streams all use generics to express relationships between data types.</p><p>Consider:</p><pre>Map&lt;String, Integer&gt; scores =<br>        new HashMap&lt;&gt;();</pre><p>This declaration communicates that keys are strings and values are integers.</p><p>Or:</p><pre>Optional&lt;User&gt; user;</pre><p>This communicates that the optional value, when present, contains a User.</p><p>The compiler can use these declarations to check how different components interact.</p><p>This becomes especially valuable when designing libraries.</p><p>A library author does not know every type that future applications will introduce. But the library can still define reusable operations and clearly communicate the constraints those operations require.</p><p>Applications then supply their own types while relying on the library’s established behavior.</p><p>That is the larger significance of generic programming.</p><p>It allows software components to be designed around reusable mechanisms rather than a fixed collection of concrete types.</p><h3>What this investigation changed in my understanding</h3><p>Before investigating templates and generics, it would have been easy to describe them as language features that reduce code duplication.</p><p>That explanation is correct, but incomplete.</p><p>The more interesting idea is the separation between an implementation and the types it operates on.</p><p>C++ templates and Java generics both explore this idea, but their mechanisms produce different trade-offs.</p><p>C++ template instantiation enables type-specific compile-time specialization.</p><p>Java type erasure provides generic type checking while maintaining a largely shared runtime representation.</p><p>Neither mechanism removes the need to think carefully about constraints, performance, or correctness.</p><p>The collection experiment made this distinction clearer.</p><p>Writing one implementation for integers and strings was straightforward. The more interesting questions appeared when I considered what the implementation could assume, what the compiler could guarantee, and what would happen when the requirements changed.</p><p>That is where abstraction becomes an engineering decision rather than a syntax feature.</p><p>The real achievement of generic programming is not that we can write one implementation for many types.</p><p>It is that we can describe what an implementation requires from a type, preserve those relationships through the compiler, and reuse the resulting abstraction without knowing every type that will eventually use it.</p><p><strong>The goal of a good abstraction is not to hide complexity. It is to make the right constraints explicit so that complexity becomes easier to reason about.</strong></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=043484ce2e21" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[The Data Structure Is Not the Interface]]></title>
            <link>https://medium.com/@pranova2508/the-data-structure-is-not-the-interface-315f8b54abe2?source=rss-78d72cb29a4f------2</link>
            <guid isPermaLink="false">https://medium.com/p/315f8b54abe2</guid>
            <category><![CDATA[java]]></category>
            <category><![CDATA[computer-science]]></category>
            <category><![CDATA[software-engineering]]></category>
            <category><![CDATA[software-development]]></category>
            <dc:creator><![CDATA[Prac's Journal]]></dc:creator>
            <pubDate>Fri, 06 Nov 2026 10:31:01 GMT</pubDate>
            <atom:updated>2026-11-06T10:31:01.619Z</atom:updated>
            <content:encoded><![CDATA[<p>What STL and Java Collections Reveal About Memory, Abstraction, and Engineering Decisions</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*bJJgZt6TajFH0j4rID6mcw.png" /></figure><p>There is something interesting about the way we write software today.</p><p>We can create a collection, insert thousands of elements, retrieve them, sort them, and process them without ever thinking about how those elements are actually organized in memory.</p><p>A few lines of code are enough.</p><pre>List&lt;String&gt; records = new ArrayList&lt;&gt;();</pre><pre>records.add(&quot;record-001&quot;);<br>records.add(&quot;record-002&quot;);<br>records.add(&quot;record-003&quot;);</pre><pre>for (String record : records) {<br>    System.out.println(record);<br>}</pre><p>The code is simple. The operations are familiar. We know what the program is supposed to do.</p><p>But simplicity at the interface does not necessarily mean simplicity underneath it.</p><p>Where are these records stored? What happens when the collection grows? How does the program find an element? What happens when we insert something in the middle? And why would replacing ArrayList with LinkedList change the performance of a program even though both implement the same interface?</p><p>These questions are easy to overlook because modern programming languages and libraries do so much work for us.</p><p>But understanding software requires occasionally stepping behind these abstractions and examining the decisions they hide.</p><p>Collections are a good place to begin.</p><h3>The Problem Was Never Just Storing Data</h3><p>Imagine building a file-processing utility.</p><p>It reads records from a file, stores them temporarily, searches for particular entries, processes them, and eventually produces an output.</p><p>Initially, the file might contain ten records. Later, it might contain ten thousand. Eventually, the application might need to process millions.</p><p>We need to organize this information in a way that makes common operations manageable.</p><p>One approach would be to implement everything ourselves.</p><p>We could create an array, manage its capacity, implement insertion and deletion, write searching logic, and handle traversal. We could also implement different structures for different requirements.</p><p>There is nothing fundamentally wrong with doing this. In fact, implementing data structures ourselves is an excellent way to understand them.</p><p>The problem appears when every application must repeatedly solve the same underlying problems.</p><p>A software engineer should not need to rebuild a reliable dynamic array every time an application needs to store a growing sequence of elements.</p><p>We need reusable implementations of common data structures, along with consistent ways to interact with them.</p><p>This is the problem that collection libraries help solve.</p><p>But there is a second problem hidden inside the first one.</p><p>Different applications do not use data in the same way.</p><p>One application might frequently access elements by index. Another might frequently insert and remove elements. A third might primarily search for values using keys.</p><p>A single data structure cannot be equally efficient for every possible workload.</p><p>We therefore need more than reusable containers. We need a way to choose between different implementations without unnecessarily coupling the entire application to their internal details.</p><p>This is where abstraction becomes important.</p><h3>An Interface Can Stay the Same While the Mechanism Changes</h3><p>C++ addresses collection-related problems through the Standard Template Library, commonly known as STL.</p><p>Its design brings together containers, algorithms, iterators, and generic programming.</p><p>Containers organize data. Algorithms perform operations on data. Iterators provide a common mechanism for accessing and traversing elements.</p><p>Consider some familiar STL containers:</p><ul><li>std::vector</li><li>std::list</li><li>std::map</li><li>std::unordered_map</li></ul><p>Java approaches similar problems through the Java Collections Framework.</p><p>It provides interfaces such as List, Set, and Map, together with implementations such as ArrayList, LinkedList, HashSet, and HashMap.</p><p>These libraries are not identical, but they express an important shared idea: separate the operations a program needs from the particular data structure that performs them.</p><p>Consider this Java declaration:</p><pre>List&lt;String&gt; records = new ArrayList&lt;&gt;();</pre><p>The reference is declared using List, while the object is an ArrayList.</p><p>The interface tells us what operations are available. The implementation determines how those operations are carried out.</p><p>This separation gives us flexibility.</p><p>We can potentially replace the implementation without rewriting every part of the application that interacts with the collection.</p><p>For example:</p><pre>List&lt;String&gt; records = new LinkedList&lt;&gt;();</pre><p>The declaration still uses List.</p><p>But something important has changed.</p><p>The program is now using a different mechanism to organize its elements.</p><p>The available operations may look familiar, but their costs and performance characteristics are different.</p><p>That distinction is easy to miss when we work primarily through APIs.</p><p>An abstraction hides implementation details so that we can work at a higher level. It does not eliminate the consequences of those implementation details.</p><p>To understand those consequences, we need to examine what happens inside the collections themselves.</p><h3>Two Ways of Organizing the Same Information</h3><p>Let’s consider ArrayList and LinkedList.</p><p>Both implement Java’s List interface. Both can store elements in an ordered sequence. Both support insertion, deletion, retrieval, and iteration.</p><p>Yet their internal organization is fundamentally different.</p><h3>ArrayList: The Advantage of Contiguous Storage</h3><p>An ArrayList uses a dynamically resizable array internally.</p><p>Imagine that we store four records:</p><pre>Index:    0          1          2          3</pre><pre>       +----------+----------+----------+----------+<br>       | Record A | Record B | Record C | Record D |<br>       +----------+----------+----------+----------+</pre><p>The elements occupy positions in an underlying array.</p><p>This arrangement provides an important advantage: direct indexed access.</p><p>When we write:</p><pre>records.get(2);</pre><p>The implementation can calculate the position of the requested element and access the corresponding array slot directly.</p><p>The operation takes O(1) time.</p><p>But this organization introduces another problem.</p><p>An array has a fixed capacity. An ArrayList provides the appearance of a dynamically growing structure by managing that capacity internally.</p><p>When the backing array becomes full, the implementation allocates a larger array and transfers the existing element references into it.</p><p>The individual resizing operation can take O(n) time.</p><p>However, because the capacity grows geometrically, appending elements repeatedly has amortized O(1) time complexity.</p><p>This is an interesting property of dynamic arrays.</p><p>An operation can occasionally be expensive without making the overall sequence of operations expensive.</p><p>The design trades occasional resizing work for efficient indexed access and generally efficient iteration.</p><h3>LinkedList: The Advantage of Connections</h3><p>A LinkedList organizes its elements differently.</p><p>Instead of maintaining all element references in a backing array, it stores elements in nodes connected to their neighbors.</p><p>Conceptually, the structure looks like this:</p><pre>HEAD</pre><pre>  |<br>  v</pre><pre>+--------+     +--------+     +--------+<br>|   A    | &lt;-&gt; |   B    | &lt;-&gt; |   C    |<br>+--------+     +--------+     +--------+<br>   prev           prev           prev<br>   next           next           next</pre><pre>                                  |<br>                                 TAIL</pre><p>Each node maintains a reference to its previous node and its next node.</p><p>The structure is therefore a doubly linked list.</p><p>This changes how elements are accessed.</p><p>When we request the third element, the implementation cannot simply calculate an array position.</p><p>It must traverse the links until it reaches the required node.</p><p>Indexed access therefore takes O(n) time.</p><p>But insertion and removal at a position where the relevant node is already known can be performed in O(1) time because the implementation mainly needs to update neighboring links.</p><p>The qualification matters: finding the position still takes time.</p><p>If we need to traverse the list to locate an element before inserting something, the complete operation is not O(1).</p><p>This illustrates why complexity analysis should describe the entire operation rather than only the final modification.</p><h3>The Interesting Part Is What Happens Between the API and the Hardware</h3><p>At the level of Big-O notation, we can already see important differences.</p><p>OperationArrayListLinkedListIndexed accessO(1)O(n)AppendAmortized O(1)O(1)Insert at beginningO(n)O(1)Remove at beginningO(n)O(1)Full iterationO(n)O(n)</p><p>But these numbers do not tell the entire story.</p><p>Consider iteration.</p><p>Both collections require O(n) time to visit n elements.</p><p>It would be easy to conclude that their iteration performance should be approximately the same.</p><p>In practice, it often isn’t.</p><p>An array-backed structure stores its element references together in a contiguous backing array.</p><p>A linked structure requires following references from one node to another. Its nodes also introduce additional memory overhead and allocation activity.</p><p>Modern processors benefit from spatial locality: accessing nearby memory locations can make it more likely that useful data is already available in the processor’s cache.</p><p>An array-backed structure often benefits from this property.</p><p>A linked structure may involve less predictable memory access because its nodes can be distributed across memory.</p><p>The result is that two operations with identical asymptotic complexity can have substantially different execution times.</p><p>This is one reason why theoretical complexity and practical performance must be considered together.</p><p>Big-O describes how resource requirements scale as input grows. It does not fully describe the cost of accessing memory, allocating objects, or interacting with the hardware.</p><p>Understanding a data structure means understanding both its algorithmic properties and its physical consequences.</p><h3>When the Workload Changes, the Decision Changes</h3><p>Now imagine that our file-processing utility grows.</p><p>It initially handles a few hundred records, but eventually needs to process millions.</p><p>The question is no longer simply whether a collection can store the records.</p><p>We must understand how the application uses them.</p><p>Suppose most operations involve retrieving elements by index.</p><p>An ArrayList is a natural choice. Its constant-time indexed access matches the workload.</p><p>A LinkedList would introduce unnecessary traversal.</p><p>Now suppose the application frequently inserts and removes elements at the beginning.</p><p>A linked list can perform these operations efficiently.</p><p>However, this does not automatically make LinkedList the best choice.</p><p>Java provides another structure: ArrayDeque.</p><p>For ordinary queue and deque operations, ArrayDeque is generally a better starting point because it provides efficient operations at both ends without the per-node overhead of a linked list.</p><p>This is an important part of engineering judgment.</p><p>We should not assume that the best choice must come from the two implementations we initially considered.</p><p>Sometimes the investigation reveals that we were comparing the wrong alternatives.</p><p>Now consider memory.</p><p>As the collection grows, the cost of storing the elements becomes increasingly important.</p><p>An ArrayList maintains a backing array, which may contain unused capacity. A LinkedList maintains additional references for every node and generally requires separate node allocations.</p><p>Both have memory costs, but those costs arise from different design decisions.</p><p>At sufficiently large scales, allocation pressure, garbage collection, memory locality, and the total memory footprint can become more important than the apparent simplicity of the API.</p><p>The correct choice therefore depends on the workload and the constraints of the system.</p><p>There is no universally best collection.</p><p>There is only a collection whose characteristics are appropriate for a particular set of requirements.</p><h3>Turning the Explanation Into an Experiment</h3><p>Understanding the mechanism is useful, but I don’t want to stop at theoretical comparisons.</p><p>For this investigation, I am using Java 21 and the Java Collections Framework to compare ArrayList and LinkedList.</p><p>The laboratory focuses on three operations:</p><ol><li>Insertion</li><li>Lookup</li><li>Iteration</li></ol><p>The collection sizes will increase progressively:</p><pre>1,000 elements<br>10,000 elements<br>100,000 elements<br>1,000,000 elements</pre><p>The important part is that both implementations must perform equivalent workloads.</p><p>For example, measuring indexed access on both structures reveals the difference between direct array access and sequential traversal.</p><p>Measuring iteration reveals something different: how the structures behave when every element must be visited.</p><p>Insertion needs similar care.</p><p>Appending elements is not the same workload as repeatedly inserting at the beginning. A benchmark that does not specify its workload clearly can produce results that are technically valid but practically misleading.</p><p>I also want to avoid treating one timing measurement as reliable evidence.</p><p>The JVM introduces factors such as JIT compilation, warm-up, garbage collection, and runtime optimization.</p><p>A useful benchmark must account for these effects.</p><p>For serious performance measurement, Java’s JMH (Java Microbenchmark Harness) is a much better tool than relying on a single call to System.nanoTime().</p><p>The experiment will therefore focus on more than recording execution time.</p><p>I want to understand why the results differ, which assumptions explain them, and what changes when the workload changes.</p><p>The goal is not to prove that one collection is always faster.</p><p>The goal is to understand the relationship between a data structure and the work it is expected to perform.</p><h3>The Principle Extends Beyond Collections</h3><p>Once we understand the distinction between an interface and its implementation, we begin to recognize the same design principle in other areas of software engineering.</p><p>A database application can interact with an abstraction while different storage engines provide different underlying mechanisms.</p><p>A caching system can expose a common API while using different eviction policies.</p><p>A network service can maintain a stable application-level interface while changing the transport or communication strategy underneath it.</p><p>In each case, the abstraction allows us to reason about a system without constantly depending on its internal implementation.</p><p>But the implementation still determines performance, resource consumption, failure characteristics, and operational complexity.</p><p>The same principle appears repeatedly throughout computer science.</p><p>Abstraction makes complexity manageable. Understanding mechanisms makes engineering decisions possible.</p><h3>What I Want to Take Away From This Investigation</h3><p>Before examining collections closely, it is easy to think of them as ordinary programming conveniences.</p><p>We create a list, add elements, retrieve values, and continue building the application.</p><p>But underneath those familiar operations are decisions about memory organization, access patterns, allocation, traversal, and computational complexity.</p><p>An ArrayList and a LinkedList can expose a similar interface while making very different trade-offs.</p><p>The interface tells us what we can do.</p><p>The implementation determines how the operation is performed.</p><p>The workload determines whether that implementation is appropriate.</p><p>This changes how I want to approach software development.</p><p>I don’t want to stop at knowing which collection to use. I want to understand why it exists, how it works, what it costs, and what happens when the assumptions behind its design no longer hold.</p><p>Because writing code is only one part of engineering.</p><p>Understanding the consequences of the mechanisms we choose is another.</p><p>And that is where a programming abstraction becomes an engineering decision.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=315f8b54abe2" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[References and Classes: When Objects Become Shared, Responsibility Becomes an Engineering Problem]]></title>
            <link>https://medium.com/@pranova2508/references-and-classes-when-objects-become-shared-responsibility-becomes-an-engineering-problem-51cca1e36621?source=rss-78d72cb29a4f------2</link>
            <guid isPermaLink="false">https://medium.com/p/51cca1e36621</guid>
            <category><![CDATA[software-engineering]]></category>
            <category><![CDATA[software-development]]></category>
            <category><![CDATA[computer-science]]></category>
            <category><![CDATA[java]]></category>
            <dc:creator><![CDATA[Prac's Journal]]></dc:creator>
            <pubDate>Thu, 05 Nov 2026 10:16:01 GMT</pubDate>
            <atom:updated>2026-11-05T10:16:01.579Z</atom:updated>
            <content:encoded><![CDATA[<p>Day 007 · Computational Foundations</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*MvN_MHp4C2KBx1We4UyzFw.png" /></figure><p>Imagine building a file-processing system.</p><p>One component opens a file. Another reads its contents. A third records information about the operation. Eventually, someone needs to close the file and release its resources.</p><p>At first, the design seems straightforward. Create an object representing the file and pass it to whichever component needs it.</p><p>But consider a small change.</p><p>The reading component decides to close the file when it finishes its operation. Meanwhile, another component still expects to use it.</p><p>The object exists. The reference is valid. The method call is legal. Yet the system is now in an unexpected state.</p><p>Nothing particularly complicated happened. We simply allowed multiple parts of a program to access the same object without making their responsibilities sufficiently clear.</p><p>This is where a deeper problem in software engineering begins.</p><p><strong>How should a program organize objects, allow them to be shared, and maintain control over the state and resources those objects represent?</strong></p><p>To understand this, we need to examine three closely related ideas: classes, references, and ownership.</p><h3>From scattered data to objects</h3><p>Consider a simple file utility.</p><p>We need to remember a filename, whether the file is open, and how many bytes have been read.</p><p>We might begin with separate variables:</p><pre>String fileName = &quot;data.txt&quot;;<br>boolean isOpen = true;<br>int bytesRead = 0;</pre><p>Then we introduce functions to operate on them:</p><pre>openFile();<br>readFile();<br>closeFile();</pre><p>This works for a small example. But as the program grows, the relationship between the data and the operations becomes harder to maintain.</p><p>Which file does isOpen describe? Does bytesRead belong to the current operation? What prevents one function from changing state that another function depends on?</p><p>The problem is not simply the number of variables. It is that the program has no strong boundary around the state and behavior associated with a particular resource.</p><p>A class gives us a way to establish that boundary.</p><pre>class FileResource {</pre><pre>    private final String fileName;<br>    private boolean open;</pre><pre>    public FileResource(String fileName) {<br>        this.fileName = fileName;<br>        this.open = true;<br>    }</pre><pre>    public void close() {<br>        open = false;<br>    }</pre><pre>    public boolean isOpen() {<br>        return open;<br>    }<br>}</pre><p>The filename and lifecycle state now belong to the object. Operations that modify that state are defined alongside it.</p><p>Instead of maintaining unrelated variables throughout the program, we create an instance that represents a particular entity.</p><pre>FileResource file =<br>    new FileResource(&quot;data.txt&quot;);</pre><p>The class describes the structure and behavior. The object is the actual instance carrying state.</p><p>This distinction matters because a class is not the resource itself. It is the definition from which resource-representing objects can be created.</p><p>We can create several instances from the same class:</p><pre>FileResource first =<br>    new FileResource(&quot;first.txt&quot;);</pre><pre>FileResource second =<br>    new FileResource(&quot;second.txt&quot;);</pre><p>They share the same class definition, but their states belong to separate objects.</p><p>Classes therefore give us more than a convenient way to group variables and functions. They provide a structure in which related state, behavior, and rules can live together.</p><p>But organizing state inside objects creates another question.</p><p>How do different parts of the program access those objects?</p><h3>One object, multiple references</h3><p>Suppose we create a file object and assign it to another variable.</p><pre>FileResource first =<br>    new FileResource(&quot;data.txt&quot;);</pre><pre>FileResource second = first;</pre><p>We now have two variables, but we have not created two file objects.</p><p>The assignment copies the reference value held by first into second. Both variables refer to the same object.</p><p>This is one of the most important distinctions in understanding Java.</p><p>A reference provides a way to access an object. It does not represent an independent copy of that object.</p><p>We can see the consequence through a simple experiment:</p><pre>second.close();</pre><pre>System.out.println(first.isOpen());</pre><p>The output is:</p><pre>false</pre><p>We invoked close() through second, but the change is visible through first.</p><p>Why?</p><p>Because there was only one object to begin with.</p><p>Both variables provide access to the same mutable state.</p><p>This relationship is called <em>aliasing</em>: multiple references refer to the same object.</p><p>Aliasing is not inherently a problem. In fact, it is essential to how many software systems operate.</p><p>A service may pass an object to another component. A collection may store references to objects. Several components may work with the same configuration or shared resource.</p><p>Without references, coordinating work around objects would be much more difficult.</p><p>The problem begins when we forget that sharing access also means sharing the consequences of mutation.</p><h3>The hidden cost of sharing</h3><p>Consider two components that receive references to the same object.</p><p>Component A expects the object to remain open while it performs an operation.</p><p>Component B decides to close it.</p><p>Neither component necessarily knows what the other is doing.</p><p>The result is a dependency that exists in the program but is not obvious from either component’s local implementation.</p><p>This is one reason shared mutable state becomes difficult to manage.</p><p>A mutation can occur far away from the code where its consequences become visible.</p><p>The same problem appears in many forms:</p><ul><li>A method changes an object that another method assumes is unchanged.</li><li>A component closes a resource that another component still needs.</li><li>An object is passed through several layers and its state becomes difficult to trace.</li><li>Concurrent operations access and modify the same mutable object.</li></ul><p>The difficulty is not simply that multiple references exist.</p><p>It is that the program allows multiple parts to influence state without making those relationships sufficiently explicit.</p><p>We need to distinguish two questions:</p><ol><li>Who can access this object?</li><li>Who is responsible for managing it?</li></ol><p>These questions lead to a concept that becomes increasingly important as software grows.</p><p>Ownership.</p><h3>Access is not ownership</h3><p>A reference tells us how to reach an object.</p><p>Ownership tells us which part of the system is responsible for managing its lifecycle.</p><p>Imagine that a file-processing component receives a file resource from another component.</p><p>It needs to read the file. That does not automatically mean it should close the file.</p><p>The component may have access without having ownership.</p><p>This distinction is especially important when building resource-safe software.</p><p>A reasonable architecture for our laboratory might look like this:</p><pre>ResourceSafeFileUtility<br>          |<br>          | creates<br>          v<br>      FileSession<br>          |<br>          | manages<br>          v<br>      ManagedFile<br>          |<br>          | supports<br>          v<br>     File Operations</pre><p>The utility coordinates the overall operation.</p><p>The session manages the resource lifecycle.</p><p>The managed file encapsulates its state and operations.</p><p>Other components receive only the access they require.</p><p>This is a design model, not a special ownership feature enforced by Java. We must implement the responsibility boundaries ourselves.</p><p>Java provides useful mechanisms for this.</p><p>For example, AutoCloseable allows an object to expose a deterministic cleanup operation. Try-with-resources ensures that the resource’s close() method is invoked when execution leaves the associated block.</p><pre>try (FileSession session = openSession()) {</pre><pre>    session.process();</pre><pre>}</pre><p>Even if process() throws an exception, Java attempts to close the session as the block exits.</p><p>This is the connection to the previous investigation into RAII and resource lifecycle management.</p><p>The underlying engineering problem is closely related, although Java and C++ implement resource management differently.</p><p>In C++, RAII commonly ties resource lifetime to object lifetime through deterministic destruction.</p><p>In Java, garbage collection manages memory reclamation, but it does not guarantee timely release of external resources. Explicit lifecycle management through AutoCloseable and try-with-resources remains important.</p><p>The important design decision is not merely which method closes a file.</p><p>It is deciding which object is responsible for closing it and preventing that responsibility from becoming ambiguous.</p><h3>Protecting the object’s rules</h3><p>Once an object contains state, we need to decide which changes are valid.</p><p>Consider a resource that has two possible lifecycle states:</p><pre>OPEN<br>  |<br>  | close()<br>  v<br>CLOSED</pre><p>A closed resource should not accept further read operations.</p><p>We can express that as an invariant:</p><p><strong>A resource that has been closed must not perform further file operations.</strong></p><p>An invariant is a condition that must remain true throughout the valid operation of an object.</p><p>We can protect this condition inside the class:</p><pre>class ManagedFile implements AutoCloseable {</pre><pre>    private boolean closed;</pre><pre>    public void read() {</pre><pre>        if (closed) {<br>            throw new IllegalStateException(<br>                &quot;Cannot read a closed file&quot;<br>            );<br>        }</pre><pre>        // Perform the read operation.<br>    }</pre><pre>    @Override<br>    public void close() {<br>        if (!closed) {<br>            closed = true;<br>        }<br>    }<br>}</pre><p>This is a simplified lifecycle example rather than a complete implementation of file handling.</p><p>Notice what changed.</p><p>External components cannot directly modify the closed field. They must interact through methods that enforce the object’s rules.</p><p>This is encapsulation.</p><p>Encapsulation is often explained as hiding data behind private fields. But its deeper purpose is to control how an object’s state changes.</p><p>A class becomes useful when it protects meaningful conditions, not merely when it contains variables and methods.</p><p>There is another important consequence.</p><p>If we allow every component to manipulate internal state directly, the class loses much of its ability to guarantee that its objects remain valid.</p><p>Good object-oriented design therefore involves deciding not only what an object knows and does, but also what other objects are allowed to change.</p><h3>What changes when the system grows?</h3><p>Our example contains one resource and a handful of objects.</p><p>A production system may process thousands of requests, each involving multiple resources and components.</p><p>Now the same design questions become more difficult.</p><p>Can two requests access the same mutable object? What happens if one request fails midway? Can a component retain access to a resource after it has been closed? What happens when many resources remain open simultaneously?</p><p>At this point, ownership and references become reliability concerns.</p><p>For example, a file-processing service that does not release resources correctly may eventually exhaust available file descriptors.</p><p>A service that shares mutable state without appropriate synchronization may experience race conditions.</p><p>A system that allows objects to escape beyond their intended lifecycle may encounter failures that are difficult to reproduce.</p><p>The scale changes, but the fundamental relationships remain:</p><ul><li>Objects maintain state.</li><li>References provide access.</li><li>Aliasing creates shared access.</li><li>Mutations affect the shared object.</li><li>Ownership establishes lifecycle responsibility.</li><li>Encapsulation protects valid state transitions.</li></ul><p>This is why object-oriented design should not be measured by how many classes a program contains.</p><p>A system with hundreds of classes can still have poorly defined responsibilities. A system with a small number of carefully designed objects can be much easier to understand and maintain.</p><p>The goal is not to create more objects.</p><p>The goal is to create meaningful boundaries between them.</p><h3>Investigating the design through Java</h3><p>For this investigation, I extended my cumulative resource-safe file utility laboratory.</p><p>Rather than creating an unrelated demonstration, I used the existing resource-management problem as the environment in which to examine object relationships.</p><p>The investigation focused on five questions:</p><p><strong>First: What happens when two variables refer to the same object?</strong></p><p>I assigned one object reference to another and examined object identity.</p><p><strong>Second: Can a mutation through one reference be observed through another?</strong></p><p>I changed the object’s state through one variable and inspected it through the other.</p><p><strong>Third: Who is responsible for closing the resource?</strong></p><p>I examined the relationship between the utility, the session, and the managed resource.</p><p><strong>Fourth: What happens when an operation is attempted after closure?</strong></p><p>I examined the lifecycle boundary and the behavior expected from an invalid operation.</p><p><strong>Fifth: How does primitive assignment differ from object-reference assignment?</strong></p><p>I compared independent primitive values with multiple references to a shared mutable object.</p><p>The objective was to make these relationships observable through code and tests.</p><p>A successful compilation would not be sufficient evidence. The tests needed to demonstrate the behavior we were investigating.</p><p>In particular, I wanted the implementation to make three questions answerable:</p><ul><li>Who created the resource?</li><li>Which components are allowed to use it?</li><li>Who is responsible for closing it?</li></ul><p>If the architecture cannot answer these questions clearly, the ownership model deserves further investigation.</p><h3>The deeper engineering principle</h3><p>Classes, references, and ownership are often introduced as separate programming concepts.</p><p>But their significance becomes clearer when we consider them together.</p><p>Classes organize state and behavior.</p><p>References allow objects to be accessed and shared.</p><p>Aliasing explains why changes made through one reference can affect other parts of a program.</p><p>Encapsulation protects the conditions under which an object remains valid.</p><p>Ownership establishes responsibility for managing resources and their lifecycle.</p><p>Together, these ideas help us reason about the internal relationships of a software system.</p><p>The distinction I find most important is this:</p><p><strong>Access to an object does not automatically imply responsibility for its lifecycle.</strong></p><p>A component may need to use a resource without owning it. Another component may be responsible for creating and closing it. The design should make that relationship explicit.</p><p>This principle extends far beyond file utilities.</p><p>It appears in database connections, network sockets, caches, shared configuration, application services, and distributed systems.</p><p>As software grows, the difficulty is not determined only by how many objects exist.</p><p>It is also determined by how those objects are connected, how their state can change, and whether their responsibilities remain understandable.</p><p>That is the real reason references and classes matter.</p><p>They are not simply mechanisms for writing object-oriented code.</p><p>They are tools for structuring relationships inside software.</p><p>And once objects begin to share state and resources, designing those relationships becomes an engineering responsibility.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=51cca1e36621" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[The Hidden Cost of Acquiring a Resource]]></title>
            <link>https://medium.com/@pranova2508/the-hidden-cost-of-acquiring-a-resource-8beff51b048b?source=rss-78d72cb29a4f------2</link>
            <guid isPermaLink="false">https://medium.com/p/8beff51b048b</guid>
            <category><![CDATA[java]]></category>
            <category><![CDATA[computer-science]]></category>
            <category><![CDATA[software-engineering]]></category>
            <category><![CDATA[software-development]]></category>
            <dc:creator><![CDATA[Prac's Journal]]></dc:creator>
            <pubDate>Wed, 04 Nov 2026 10:06:01 GMT</pubDate>
            <atom:updated>2026-11-04T10:06:01.538Z</atom:updated>
            <content:encoded><![CDATA[<h3><em>Day 006 |</em>Understanding RAII, Ownership, and Deterministic Cleanup</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*EYUA6DQdPMDdPIIjD_e_rg.png" /></figure><p>Opening a file is one of the simplest operations a program can perform.</p><p>A program requests access to a file, reads its contents, processes the data, and closes it. The sequence is familiar enough that we rarely stop to examine it.</p><p>But there is something hidden inside that operation.</p><p>The moment a program opens a file, it acquires more than access to data. It acquires a responsibility. The file must remain available while the program needs it, and it must eventually be released.</p><p>That responsibility becomes interesting when execution does not follow the expected path.</p><p>What happens if an exception interrupts processing? What if a function returns early? What if the program acquires several resources and fails while initializing the last one? What if the operation itself fails, and cleanup fails immediately afterward?</p><p>The difficulty is no longer reading the file. It is ensuring that every resource acquired by the program has a well-defined lifetime and a reliable cleanup mechanism.</p><p>This is the problem behind Resource Acquisition Is Initialization, commonly known as RAII.</p><p>And although RAII originates in C++, understanding it reveals a much broader engineering principle: <strong>resource ownership should be part of a program’s structure, not an afterthought left to individual cleanup instructions.</strong></p><h3>1. The responsibility hidden inside a resource</h3><p>Consider a simple Java program that opens a file.</p><pre>BufferedReader reader =<br>    Files.newBufferedReader(Path.of(&quot;data.txt&quot;));</pre><pre>String line = reader.readLine();</pre><pre>System.out.println(line);</pre><pre>reader.close();</pre><p>The code is easy to understand. A reader is created, data is retrieved, and the reader is closed.</p><p>But the correctness of this implementation depends on reaching the final instruction.</p><p>Suppose readLine() throws an exception. The final close() might never execute.</p><p>The problem becomes more visible when the operation contains several steps:</p><pre>Acquire file<br>    |<br>Read data<br>    |<br>Transform data<br>    |<br>Write result<br>    |<br>Release resources</pre><p>An exception can interrupt execution at any point.</p><p>If cleanup is not structurally guaranteed, the program may leave resources occupied beyond their intended lifetime.</p><p>This is not merely a file-handling problem.</p><p>A database connection acquired by a request must eventually be returned or closed. A lock acquired by a thread must eventually be released. A network socket must not remain open indefinitely because an operation failed halfway through.</p><p>Each acquisition introduces an obligation.</p><p>The more resources a program manages, the more important it becomes to make those obligations explicit.</p><p>Manual cleanup can work, but it makes correctness depend on the programmer remembering to handle every possible execution path.</p><p>A more reliable design would connect the responsibility for releasing a resource directly to the mechanism that manages its lifetime.</p><p>That is the central idea of RAII.</p><h3>2. RAII: Making ownership responsible for cleanup</h3><p>Resource Acquisition Is Initialization is a C++ idiom in which acquiring a resource is tied to constructing an object, while releasing that resource is tied to the object’s destruction.</p><p>The idea is deceptively simple.</p><p>An object acquires a resource when it is initialized. That object becomes responsible for the resource. When its lifetime ends, its destructor releases what it owns.</p><p>Instead of treating acquisition and cleanup as two unrelated operations, RAII connects them through object lifetime.</p><p>Consider a C++ example:</p><pre>#include &lt;fstream&gt;<br>#include &lt;iostream&gt;<br>#include &lt;string&gt;</pre><pre>void readFile() {<br>    std::ifstream file(&quot;data.txt&quot;);</pre><pre>    std::string line;</pre><pre>    while (std::getline(file, line)) {<br>        std::cout &lt;&lt; line &lt;&lt; &#39;\n&#39;;<br>    }<br>}</pre><p>There is no explicit file.close() instruction.</p><p>The std::ifstream object manages the file resource. When the function finishes, the local object is destroyed, and its destructor closes the associated file.</p><p>The important behavior is not simply that the file eventually closes. It is that the cleanup responsibility is attached to the lifetime of the object that owns the resource.</p><p>This changes how we reason about the program.</p><p>The programmer no longer needs to ask whether every execution path remembers to close the file. Instead, the programmer establishes an ownership relationship and lets the language’s object-lifetime rules govern cleanup.</p><p>The lifecycle becomes:</p><p><strong>Acquisition → Ownership → Usage → Destruction → Release</strong></p><p>The object is more than a container for data. It represents responsibility for an external resource.</p><h3>3. Why object lifetime is such a powerful mechanism</h3><p>The deeper insight behind RAII becomes clearer when we examine what happens during exceptional execution.</p><p>Consider:</p><pre>void processFile() {<br>    std::ifstream file(&quot;data.txt&quot;);</pre><pre>    performOperationThatMayThrow();</pre><pre>}</pre><p>Suppose performOperationThatMayThrow() throws an exception.</p><p>In C++, exception propagation can trigger stack unwinding. As execution leaves the function, local objects whose lifetimes are ending are destroyed.</p><p>The file object is therefore destroyed, and its destructor releases the associated file resource.</p><p>The cleanup mechanism does not depend on reaching a particular instruction after the operation.</p><p>It depends on the lifetime of the owning object.</p><p>This is a significant shift in program design.</p><p>A conventional approach says:</p><p>“After using the resource, remember to release it.”</p><p>RAII says:</p><p>“Design ownership so that releasing the resource follows naturally from the end of its lifetime.”</p><p>This is why RAII is closely associated with deterministic resource management and exception safety.</p><p>However, the guarantee depends on correct ownership.</p><p>If multiple objects incorrectly believe they own the same resource, cleanup can become dangerous. If ownership is transferred incorrectly, a resource may be released too early or not at all.</p><p>RAII does not eliminate the need to think about resource ownership. It makes ownership the central part of the design.</p><h3>4. Java: The same problem, a different mechanism</h3><p>Java introduces an interesting comparison.</p><p>Java has garbage collection, but garbage collection is not a reliable mechanism for releasing external resources at a specific point in time.</p><p>An object becoming unreachable does not mean its file descriptor, socket or database connection should remain open until the garbage collector eventually processes it.</p><p>Memory reclamation and resource cleanup are different responsibilities.</p><p>Java addresses deterministic resource cleanup through AutoCloseable and try-with-resources.</p><p>Consider:</p><pre>try (BufferedReader reader =<br>         Files.newBufferedReader(Path.of(&quot;data.txt&quot;))) {</pre><pre>    String line;</pre><pre>    while ((line = reader.readLine()) != null) {<br>        System.out.println(line);<br>    }</pre><pre>}</pre><p>The resource is initialized inside the try-with-resources statement.</p><p>When execution leaves the statement, Java automatically invokes close() on the resource.</p><p>This happens after normal completion, when an exception occurs, and when control leaves the block through an early return.</p><p>The compiler generates the necessary cleanup behavior.</p><p>The important interface is:</p><pre>public interface AutoCloseable {<br>    void close() throws Exception;<br>}</pre><p>A class implementing AutoCloseable establishes that it supports an explicit cleanup operation.</p><p>Java’s Closeable interface extends AutoCloseable and is commonly used for I/O resources.</p><p>This gives Java a structured way to manage resource lifetimes without relying on garbage collection.</p><p>There is, however, an important distinction between Java and C++.</p><p>C++ RAII ties cleanup to object destruction. Java try-with-resources ties cleanup to the lexical scope of a resource-management statement.</p><p>Both address the problem of deterministic cleanup, but their underlying lifetime semantics are different.</p><p>That distinction matters because understanding a programming concept requires understanding the mechanism that actually implements it, not simply assigning the same name to superficially similar features.</p><h3>5. The interesting failure: When cleanup also fails</h3><p>Resource management becomes more complicated when the cleanup operation itself encounters an error.</p><p>Imagine that file processing fails because of an IOException.</p><p>Java then attempts to close the file, but close() also throws an exception.</p><p>Now there are two failures.</p><p>Which one should the program report?</p><p>The original processing failure explains why the operation did not complete. The cleanup failure provides additional information about what happened while releasing the resource.</p><p>Replacing the original exception with the cleanup exception would destroy valuable diagnostic information.</p><p>Java handles this through suppressed exceptions.</p><p>With try-with-resources, the original exception remains primary, while exceptions raised during automatic cleanup are attached as suppressed exceptions.</p><p>They can be inspected using:</p><pre>exception.getSuppressed();</pre><p>This is a small language feature with an important engineering consequence.</p><p>Reliable cleanup is not simply about executing close(). It is also about preserving the information necessary to understand failures when multiple operations go wrong.</p><p>A resource-management mechanism must consider both lifecycle correctness and failure reporting.</p><h3>6. Ownership becomes more interesting with multiple resources</h3><p>Now consider a program that reads from one file and writes to another.</p><pre>try (<br>    BufferedReader reader =<br>        Files.newBufferedReader(Path.of(&quot;input.txt&quot;));</pre><pre>    BufferedWriter writer =<br>        Files.newBufferedWriter(Path.of(&quot;output.txt&quot;))<br>) {</pre><pre>    String line;</pre><pre>    while ((line = reader.readLine()) != null) {<br>        writer.write(line);<br>        writer.newLine();<br>    }</pre><pre>}</pre><p>Two resources are acquired.</p><p>The reader is initialized first, followed by the writer.</p><p>When execution leaves the try-with-resources statement, Java closes the resources in reverse initialization order: the writer first, then the reader.</p><p>This behavior is useful because resources can have dependencies.</p><p>A resource acquired later may depend on one acquired earlier. Releasing resources in reverse order helps preserve the intended lifetime relationship.</p><p>The same general idea appears in systems involving nested ownership, locks, transactions and dependent objects.</p><p>The acquisition order establishes a relationship. The cleanup order must respect it.</p><p>This is one reason structured resource management becomes increasingly valuable as operations become more complicated.</p><p>Instead of manually coordinating multiple cleanup paths, the program expresses the resources together and delegates their orderly release to the language mechanism.</p><h3>7. Reconstructing the guarantee through an experiment</h3><p>Understanding the definition of RAII is relatively easy.</p><p>Understanding whether a resource-management mechanism actually provides the expected guarantee requires experimentation.</p><p>For my Day 006 Java laboratory, I approached the problem through a resource-safe file-processing utility.</p><p>The objective was to move beyond a successful file read and investigate what happens when execution is interrupted.</p><p>I wanted to compare two implementations:</p><p><strong>Manual cleanup:</strong> The program explicitly manages resource release using a finally block.</p><p><strong>Structured cleanup:</strong> The program uses try-with-resources and delegates resource release to Java.</p><p>Both approaches can be correct when implemented carefully. The interesting question is whether their structure makes correctness easier to maintain as failure scenarios become more complicated.</p><p>The experiment introduces a controlled exception halfway through processing.</p><p>Rather than relying on an unpredictable operating-system failure, an instrumented AutoCloseable resource can record whether its close() method was invoked.</p><p>This gives the experiment an observable result.</p><p>The investigation should cover several conditions:</p><p>ExperimentQuestionNormal completionIs the resource closed after successful processing?Empty fileDoes an empty input change the cleanup behavior?Injected exceptionDoes cleanup still happen after processing fails?Multiple resourcesAre resources closed in reverse initialization order?Cleanup exceptionIs the original failure preserved?Large fileCan processing remain resource-safe without loading the entire file into memory?</p><p>The final experiment is particularly important.</p><p>Correct resource management does not automatically imply efficient resource usage.</p><p>A program could close its file correctly and still consume excessive memory by loading an entire large file into a collection.</p><p>A streaming implementation processes data incrementally, keeping memory usage bounded by the processing strategy rather than the total input size.</p><p>This reveals another engineering distinction: resource safety and resource efficiency are related, but they are not interchangeable.</p><p>A successful experiment should therefore record not just whether the program ran, but what happened to the resource, how execution changed under failure, and what limitations remained.</p><h3>8. From a local file to a production system</h3><p>A file-processing utility manages a relatively small number of resources.</p><p>Production systems operate under very different conditions.</p><p>A backend service might simultaneously manage hundreds of database connections, thousands of concurrent requests, network sockets, locks, temporary files and background workers.</p><p>Each resource has a lifecycle, and mistakes can accumulate.</p><p>Consider a database connection pool.</p><p>A request acquires a connection, executes a query and should return the connection to the pool when its work is complete.</p><p>Now imagine that an exception interrupts the request before the connection is returned.</p><p>The database connection may remain unavailable to other requests.</p><p>If this happens repeatedly, the connection pool becomes exhausted. New requests begin waiting. Latency increases. Eventually, requests may time out even though the database itself is still functioning.</p><p>The original problem might not be query performance or database capacity.</p><p>It might be a failure to manage resource lifetimes correctly.</p><p>This is why resource management belongs to the broader discipline of reliability engineering.</p><p>However, the production environment introduces a more difficult boundary.</p><p>A local resource and a distributed resource are not the same thing.</p><p>Closing a local network socket does not guarantee that a remote operation has been cancelled. Releasing a connection object does not necessarily reverse a transaction that has already committed remotely.</p><p>A local cleanup mechanism cannot independently guarantee the recovery of a distributed operation.</p><p>Distributed systems require additional mechanisms: timeouts, cancellation protocols, transaction boundaries, retries, idempotency and recovery procedures.</p><p>RAII and structured cleanup establish useful local ownership guarantees. They do not solve every problem involving remote state.</p><p>The important engineering question is therefore not simply whether a resource is automatically closed.</p><p>It is whether the resource’s lifetime and ownership model match the actual guarantees required by the system.</p><h3>9. Resource management is really responsibility management</h3><p>When I began examining RAII, the obvious interpretation was that it was a technique for closing files automatically.</p><p>But that interpretation misses the more important idea.</p><p>RAII connects ownership, lifetime and cleanup.</p><p>Ownership establishes who is responsible for a resource. Lifetime determines how long that responsibility exists. Cleanup fulfils the responsibility when the resource is no longer needed.</p><p>When these concepts are disconnected, correctness depends heavily on programmers remembering to perform the right operations at the right time.</p><p>When they are connected through an appropriate abstraction, the structure of the program helps enforce the intended behavior.</p><p>This does not make every failure impossible.</p><p>Destructors have limitations. Cleanup operations can fail. Ownership can be designed incorrectly. Distributed resources can outlive local operations.</p><p>But it provides a much stronger foundation for reasoning about correctness.</p><p>The lesson extends beyond C++ and Java.</p><p>Whenever software acquires something that must eventually be released, ownership and lifetime become architectural concerns.</p><p>They should be designed deliberately, tested under failure and made observable wherever possible.</p><h3>Conclusion: Every acquisition creates an obligation</h3><p>Opening a file looks like one instruction.</p><p>But behind that instruction is a lifecycle that the program must manage.</p><p>The same is true of database connections, network sockets, locks and many other resources.</p><p>RAII demonstrates how ownership and object lifetime can make resource cleanup a structural property of a program. Java’s try-with-resources applies a related principle through explicit resource scopes and the AutoCloseable contract.</p><p>The two mechanisms are different, but they address the same underlying engineering concern.</p><p>I find the most useful way to think about resource management is not as remembering to close things.</p><p>It is as designing a system in which responsibility has a clear owner, a defined lifetime and a reliable end.</p><p>Because acquiring a resource is never just an operation.</p><p>It is accepting responsibility for everything that happens to that resource until it is released.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=8beff51b048b" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>