<?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 Yamilini G A on Medium]]></title>
        <description><![CDATA[Stories by Yamilini G A on Medium]]></description>
        <link>https://medium.com/@yamilini.ga?source=rss-628a5822b220------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*ym_ETbyNhCF4YdYqNQ9hsA.jpeg</url>
            <title>Stories by Yamilini G A on Medium</title>
            <link>https://medium.com/@yamilini.ga?source=rss-628a5822b220------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Thu, 19 Nov 2026 03:11:20 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@yamilini.ga/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[Policy as Code with OPA & Rego]]></title>
            <link>https://medium.com/@yamilini.ga/policy-as-code-opa-rego-basics-a2a06724bfe4?source=rss-628a5822b220------2</link>
            <guid isPermaLink="false">https://medium.com/p/a2a06724bfe4</guid>
            <dc:creator><![CDATA[Yamilini G A]]></dc:creator>
            <pubDate>Wed, 13 May 2026 07:55:34 GMT</pubDate>
            <atom:updated>2026-05-13T07:56:05.727Z</atom:updated>
            <content:encoded><![CDATA[<p>When building modern applications, enforcing rules and access policies consistently across services can quickly become complex. Hardcoding conditional checks inside your codebase often leads to duplication, scattered logic, and reduced maintainability. This is where <strong>Policy as Code</strong> comes into play, and tools like <strong>Open Policy Agent (OPA)</strong> with its policy language <strong>Rego</strong> make it practical and powerful.</p><p>In this blog, I’ll share what I’ve learned about OPA, Rego, and Policy as Code — starting from the basics and covering the essentials that every developer or architect should know.</p><h4>What is Policy as Code (PaC)?</h4><p>Policy as Code (PaC) is the practice of defining, managing, and enforcing policies using code, rather than manual processes or graphical interfaces. This approach allows organisations to automate policy enforcement, adopt software development best practices like version control and automated testing for their policies, and ensure consistency, accuracy, and speed in applying rules for security, compliance, and infrastructure configuration across various environments.</p><p>Key use cases:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*clkhVFZSnhW4U3cnprQZMA.png" /></figure><p>Tools like HashiCorp Sentinel, Conftest, AWS Config, Azure Policy, etc., are also used in similar use cases as OPA &amp; Rego.</p><h4>What is Open Policy Agent (OPA)?</h4><p><strong>Open Policy Agent (OPA)</strong> is an open-source, general-purpose policy engine that allows you to <strong>decouple policy decision-making</strong> from your application logic.</p><p>Instead of hardcoding rules in code, you can define them once in Rego, deploy them centrally or alongside services and use OPA to evaluate decisions at runtime.</p><p>OPA provides:</p><ul><li><strong>One language</strong> for all policies.</li><li>Supporting <strong>fine-grained controls</strong> (down to field level).</li><li>Being <strong>lightweight</strong> (can run as a sidecar, library, or service).</li><li>Enabling <strong>Policy-as-Code</strong>: policies are version-controlled, tested, and reviewed just like application code.</li></ul><h4>How OPA Works in Practice?</h4><p>The diagram below illustrates how <strong>Open Policy Agent (OPA)</strong> fits into your system to make policy decisions without mixing rules into your business logic.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*j76R6wworBu7TYE2N7X-Tg.png" /></figure><h4>1. Incoming Request</h4><ul><li>A user or external system sends a <strong>request</strong> to your service (e.g., API call, resource access attempt).</li><li>The service needs to decide: <em>“Should I allow or deny this request?”</em></li></ul><h4>2. Service Queries OPA</h4><ul><li>Instead of hardcoding rules, the <strong>service delegates the decision</strong> to OPA.</li><li>It sends a <strong>query</strong> to OPA, containing all relevant <strong>input data</strong> (user info, action, resource, context).</li></ul><h4>3. Policy &amp; Data in OPA</h4><ul><li>OPA has two key components to evaluate the query:</li><li><strong>Policy (Rego):</strong> Rules that describe what is allowed or denied.</li><li><strong>Data (JSON):</strong> Contextual information such as roles, groups, or resource ownership.</li></ul><p>OPA combines both to reason about the query.</p><h4>4. OPA Decision</h4><ul><li>OPA evaluates the input against the <strong>Rego policy</strong> and <strong>data</strong>.</li><li>It returns a <strong>decision</strong> (usually allow or deny, but can be more detailed JSON).</li></ul><h4>5. Service Enforces the Response</h4><ul><li>The service receives the decision as a <strong>response</strong>.</li><li>Importantly, <strong>OPA never enforces the decision</strong> — the enforcement remains the responsibility of the service (e.g., block request, allow access, log action).</li></ul><h4>What is Rego?</h4><p>Rego is OPA’s <strong>policy language</strong>. It is declarative, meaning you describe <strong>what you want</strong> (the rule), not <strong>how to compute it</strong>.</p><p>A Rego policy defines <strong>rules</strong> that return a decision:</p><ul><li>allow or deny</li><li>true or false</li><li>Structured JSON responses</li></ul><p>Think of Rego as SQL-like for JSON data.</p><p>Let’s understand more in Rego code. Consider the following snippet.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/4b5ebd9ab80f623577b3712e6790a816/href">https://medium.com/media/4b5ebd9ab80f623577b3712e6790a816/href</a></iframe><p>By default the code always denies. It allows access only when the user is “admin” or “manager”.</p><p>If you pass your input as:</p><pre>{<br>  &quot;user&quot;: { &quot;role&quot;: &quot;admin&quot; }<br>}</pre><p>The output from OPA will be as follows:</p><pre>{ &quot;allow&quot;: true }</pre><p>Following is the snippet to demonstrates policies that has to be denied.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/4ccd55d40825c3ff2efc127ceb8496cc/href">https://medium.com/media/4ccd55d40825c3ff2efc127ceb8496cc/href</a></iframe><p>Allows you unless a non-admin tries to access a confidential resource or from finance department. This time it throws detailed reason why the policy was denied.</p><pre>{<br>  &quot;deny&quot;: [<br>    &quot;Non-admins cannot access confidential resources&quot;<br>  ]<br>}</pre><p>&lt;provide scenario for returning an array — if exists&gt;</p><p>There are many more built-in functions provided by OPA such as aggregates, sort, arrays, etc., to define policies.</p><p>&lt;provide few aggregation snippets&gt;</p><h4>Use cases of OPA + REGO:</h4><p>Apart from RBAC and PBAC use cases, there are many other scenarios where OPA plays a crucial role.</p><p>1. <strong>Kubernetes Admission Control </strong>— OPA validates Kubernetes manifests before they’re admitted.</p><p>Example:</p><ul><li>Ensure every pod has resource limits defined.</li><li>Disallow running privileged containers.</li><li>Enforce naming conventions for namespaces.</li></ul><p>2.<strong> API Gateway &amp; Service Mesh Policies </strong>— Enforce fine-grained API access control in gateways like Envoy or Istio.</p><p>Example:</p><ul><li>Only allow certain users to call specific API endpoints.</li><li>Restrict external traffic from hitting internal services.</li></ul><p>3. <strong>CI/CD &amp; Infrastructure Compliance </strong>— Check infrastructure configs (Terraform, Helm, Dockerfiles) before deployment.</p><p>Example:</p><ul><li>Prevent provisioning unencrypted storage buckets.</li><li>Disallow deploying security groups that expose ports to the internet.</li></ul><p>4. <strong>Cloud &amp; Platform Governance </strong>— Enforce organisation-wide rules in cloud environments.</p><p>Example:</p><ul><li>Allow only certain AWS regions for resource creation.</li><li>Enforce tagging policies for all cloud resources.</li></ul><h4>Limitations of OPA:</h4><p>Despite multiple use cases across different domains, OPA does has limitations where it act as an overkill.</p><p><strong>1. Performance Considerations — </strong>OPA is fast, but evaluating complex Rego policies over large JSON datasets can add latency. If policies are not optimized (e.g., deeply nested loops, huge input data), query performance may degrade. Best suited for real-time decisions with small/medium inputs, not for big data processing.</p><p><strong>2. Learning Curve (Rego Language) — </strong>Policies are written in Rego, a new declarative language. Developers familiar with imperative programming may find it unintuitive at first.</p><p><strong>3. No Native Enforcement — </strong>OPA decides but does not enforce. The burden of integrating enforcement logic stays on your service, API gateway, or platform.</p><p><strong>4. Distributed Policy Management — </strong>Managing and updating policies across many OPA instances can be tricky. Though bundles and central distribution servers help, it adds extra ops overhead.</p><p><strong>5. Limited Context Without Data — </strong>OPA decisions are only as good as the input JSON and data provided. If the service doesn’t supply enough context, OPA cannot make a correct decision. Maintaining external data (like org hierarchies, user attributes) adds syncing challenges.</p><p><strong>6. Not Always the Right Tool — </strong>OPA is not designed for streaming event policies. Not ideal for huge-scale analytics policies. Overkill for simple role checks where a hardcoded RBAC may be enough.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=a2a06724bfe4" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Break Free from If-Else Hell: Embrace the Strategy Pattern]]></title>
            <link>https://medium.com/@yamilini.ga/break-free-from-if-else-hell-embrace-the-strategy-pattern-258ccaa5b65a?source=rss-628a5822b220------2</link>
            <guid isPermaLink="false">https://medium.com/p/258ccaa5b65a</guid>
            <dc:creator><![CDATA[Yamilini G A]]></dc:creator>
            <pubDate>Wed, 05 Feb 2025 07:33:30 GMT</pubDate>
            <atom:updated>2025-02-12T05:04:33.183Z</atom:updated>
            <content:encoded><![CDATA[<p>Many a times we end up or look at if-else ladders and switch-case statements in our code. This code lowers readability and violates the <strong>Open-Closed Principle</strong>, a SOLID principle. Furthermore, it raises cognitive load and necessitates retesting even when one condition is altered or added. We can easily resolve these issues by following the <strong>Strategy Pattern</strong> — one of the <strong>behavioural pattern</strong> mentioned in the <strong>Design Patterns</strong> book introduced by the Gang of Four.</p><p>In this blog, we’ll look at:</p><ul><li>An example for the ladder</li><li>What is <strong>Open-Closed Principle</strong>? and how it is violated?</li><li>What is <strong>Strategy Pattern</strong>, its application<strong> </strong>and how it resolves the — cognitive issue, code maintainability and keeping OCP unviolated.</li><li>Limitations of using Strategy Pattern.</li></ul><h3>An if-else ladder example:</h3><p>Let’s consider the following snippet, which is a python program to add pizza toppings as and when customer requests.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/0d246d1f154a190d1464b391b2091166/href">https://medium.com/media/0d246d1f154a190d1464b391b2091166/href</a></iframe><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/3e0b098af2c15ec75b17ef69fdaa8289/href">https://medium.com/media/3e0b098af2c15ec75b17ef69fdaa8289/href</a></iframe><p>Above snippets are the functions to add toppings, below is the snippet which is the actual program that interacts with customer for the pizza topping specifications.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c5f162b9b705cadba18b115ee4acbdb6/href">https://medium.com/media/c5f162b9b705cadba18b115ee4acbdb6/href</a></iframe><p>Here’s the output:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*KDPQVcX5lR1kmNYZLAZTRA.png" /></figure><p>Here, the function add_topping() has multiple if-elif conditions that determine which function to call. While this approach works, it violates a fundamental design principle: <strong>The Open-Closed Principle </strong>(explained just below).</p><h3>What is Open-Closed Principle? How it is violated?</h3><p>The <strong>Open-Closed Principle (OCP)</strong> states:</p><blockquote><strong><em>A software module should be open for extension but closed for modification.</em></strong></blockquote><p>In our initial implementation, adding a new topping requires modifying the add_topping() function by adding another elif condition. This violates OCP because each new addition of a different condition forces us to change existing code, increasing the risk of <strong>introducing bugs</strong>, making <strong>maintenance harder</strong> and <strong>retesting them</strong>. Any new addition or modification on the existing code, introduces code modification thus violating OCP.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*MlPcAjqde-6UqOXBVd23NA.png" /></figure><p>Instead of having multiple conditional statements (like if-else chains or switch statements), the Strategy Pattern promotes defining a set of algorithms and selecting one dynamically at runtime.</p><h3>Applying the Strategy Pattern</h3><p>To address this, we can replace the if-elif chain with a <strong>dictionary mapping</strong> that associates a topping name with its corresponding function. Here, the Strategy Pattern allows the behaviour to be selected at runtime thereby eliminating the need to modify the existing function when introducing new toppings. In languages like Java, the same can be implemented using interfaces and static maps.</p><p>Here&#39;s the refactored code using the <strong>Strategy Pattern</strong>:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/3e0b098af2c15ec75b17ef69fdaa8289/href">https://medium.com/media/3e0b098af2c15ec75b17ef69fdaa8289/href</a></iframe><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/446d47dfa263880502f49bf7a60172eb/href">https://medium.com/media/446d47dfa263880502f49bf7a60172eb/href</a></iframe><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/dbceb84ce7f82e9a31bfc4cad1ef8bdd/href">https://medium.com/media/dbceb84ce7f82e9a31bfc4cad1ef8bdd/href</a></iframe><p>The output:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*MSqcFHnAYBuKS3jgAIiV4w.png" /></figure><h3>Benefits of Using the Strategy Pattern</h3><ol><li><strong>Extensibility Without Modification — </strong>If we want to add a new topping, we simply define a new function and update the dictionary. We don’t have to modify existing logic, adhering to the <strong>Open-Closed Principle</strong>.</li><li><strong>Improved Readability and Maintainability — </strong>The dictionary-based approach eliminates clutter from multiple if-elif conditions, making the code cleaner and easier to maintain.</li></ol><h3>Limitations:</h3><p>This solution effectively resolves the discussed issues, but <strong>is it always necessary?</strong> When dealing with a large number of conditional statements, the Strategy Pattern significantly improves maintainability and scalability. However, if the decision logic is minimal, such as a simple if-elif-else or just an if-else structure, implementing the Strategy Pattern might be overkill.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Q69TyGJXO7DlyJ24zrFAvg.png" /></figure><p>The key takeaway is to apply design patterns judiciously. Carefully evaluate the trade-offs between extensibility and simplicity before deciding to implement it in your project.</p><h3>Conclusion</h3><p>The <strong>Strategy Pattern</strong> is a powerful way to replace conditional logic with a more maintainable and scalable approach. By using <strong>function mapping (dictionary-based strategy selection)</strong>, we eliminate unnecessary modifications and keep our code <strong>open for extension but closed for modification</strong>. This results in <strong>better design, improved maintainability, and adherence to SOLID principles</strong>.</p><p>Next time you’re dealing with a growing if-elif structure, consider using the <strong>Strategy Pattern</strong> for a cleaner and more scalable solution when necessary!</p><p>I hope this blog helps you understand the importance of the Strategy Pattern and how it improves code maintainability. Happy coding! 🚀</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=258ccaa5b65a" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Breaking the Habit: Why Data Test IDs Shouldn’t Be Your Go-To in Unit Tests]]></title>
            <link>https://medium.com/@yamilini.ga/breaking-the-habit-why-data-test-ids-shouldnt-be-your-go-to-in-unit-tests-a3f3225257fb?source=rss-628a5822b220------2</link>
            <guid isPermaLink="false">https://medium.com/p/a3f3225257fb</guid>
            <dc:creator><![CDATA[Yamilini G A]]></dc:creator>
            <pubDate>Wed, 30 Oct 2024 14:18:15 GMT</pubDate>
            <atom:updated>2024-10-30T14:18:15.103Z</atom:updated>
            <content:encoded><![CDATA[<p>In the world of frontend development, unit testing is crucial to ensure that your components behave as expected. While there are many tools and strategies available for testing, one of the most common practices is using data-testid attributes to target elements in tests. However, while this approach might seem convenient at first glance, it can introduce significant issues in both the robustness and maintainability of your tests.</p><p>In this blog, we’ll explore why relying on data-testid is generally a bad practice for unit testing and discuss better alternatives to help you write more reliable and maintainable tests.</p><h4><strong>Why are they bad?</strong></h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*HoKzFNNtRxmNODk64Zk2Og.png" /></figure><ul><li><strong>Tightly Coupled to UI Implementation</strong>: Real users don’t interact with data-testid attributes—they engage with visible elements like buttons, links, and forms. By focusing on data-testid, your tests become disconnected from actual user behaviour, making them less reliable and harder to maintain.</li><li><strong>Lacks Accessibility Considerations</strong>: data-testid attributes are not part of accessibility tree. It adds no semantic value to your elements, which means screen readers and other assistive technologies can’t benefit from it.</li><li><strong>Increases Developer Cognitive Load</strong>: Introducing data-testid attributes adds extra complexity to your component markup. With these attributes in place, developers now need to keep track of additional information just for the sake of testing, increasing the cognitive load.</li></ul><h4>What should I use then?</h4><p>React Testing Library (RTL) offers several ways to query elements, beyond just using getByTestId(). The three primary types of queries are:</p><ul><li>getBy...()</li><li>findBy...()</li><li>queryBy...()</li></ul><p>Each of these serves a different purpose, with getBy... being the most straightforward for elements that are guaranteed to exist, while findBy... is for elements that might appear asynchronously. queryBy... is used for querying elements that may or may not be present.</p><p>Additionally, for fetching multiple elements, RTL offers corresponding queries:</p><ul><li>getAllBy...()</li><li>findAllBy...()</li><li>queryAllBy...()</li></ul><p>These allow you to query for collections of elements, making it easier to work with lists or groups of components.</p><p>RTL also emphasises a hierarchy in how you should query elements, with data-testid queries being the <strong>least recommended</strong>.</p><p>This is my Home Component</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/78c100bf643017162cd1a1c6ea315e5e/href">https://medium.com/media/78c100bf643017162cd1a1c6ea315e5e/href</a></iframe><p>Following are the queries elaborated <strong>in the order of priority</strong>:</p><ol><li><strong>getByRole(): </strong>Finds by aria role. Using getByRole() on the header “Home page”.</li></ol><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/623d1af0ff6ca6980ecd2b1ade4325cd/href">https://medium.com/media/623d1af0ff6ca6980ecd2b1ade4325cd/href</a></iframe><p>Also, can be called with levels.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f8b235f835da822fd235c25d16dd9f6f/href">https://medium.com/media/f8b235f835da822fd235c25d16dd9f6f/href</a></iframe><p>2. <strong>getByLabelText():</strong> Finds by label or aria-label text content. Using label text on username and password input box.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/dc20e5af5a9f7166fd758d7608860955/href">https://medium.com/media/dc20e5af5a9f7166fd758d7608860955/href</a></iframe><p>3. <strong>getByPlaceholderText():</strong> Finds by an input placeholder value. Retrieving username and password by using getByPlaceholderText.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e66df8b4bf53866aaef141ebbea81ad9/href">https://medium.com/media/e66df8b4bf53866aaef141ebbea81ad9/href</a></iframe><p>4. <strong>getByText():</strong> Finds by an element text content. Using getByText() to get the “Home page” element for demonstration purpose(should use getByRole() instead).</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/722a1616af8ce1b48d6af70ba7f69d64/href">https://medium.com/media/722a1616af8ce1b48d6af70ba7f69d64/href</a></iframe><p>5. <strong>getByDisplayValue():</strong> Finds by form element current value. As age field has a default display value as 18, I can use this query on this field.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/3d3e8f262c238f1ebd731e3865887705/href">https://medium.com/media/3d3e8f262c238f1ebd731e3865887705/href</a></iframe><p>6. <strong>getByAltText():</strong> Finds by an img alt attribute.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/0d99a9e1b13c81bcb2ae737de8cf6ae4/href">https://medium.com/media/0d99a9e1b13c81bcb2ae737de8cf6ae4/href</a></iframe><p>Lines 9 &amp; 10 of the above snippet represent prioritised way of querying the image.</p><p>7. <strong>getByTitle(): </strong>Finds by title attribute or svg title tag.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/126c139823e4b65af528fb3ff8b13579/href">https://medium.com/media/126c139823e4b65af528fb3ff8b13579/href</a></iframe><p>8. <strong>getByTestId():</strong> Finds by data-testid attribute.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/1a3796f8335839e3e805190e3e72720d/href">https://medium.com/media/1a3796f8335839e3e805190e3e72720d/href</a></iframe><p>Moreover, RTL provides Events APIs and Async APIs for firing any user event or simulate any async events. Here’s an example of my test firing a click event on the submit button and using waitFor() for the async() call.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b933c7be95d232bc612e119ca645c62d/href">https://medium.com/media/b933c7be95d232bc612e119ca645c62d/href</a></iframe><p>To conclude, while using data-testid might seem convenient for unit testing, it&#39;s not the best practice when it comes to writing maintainable and robust tests. By leveraging queries like getByRole(), getByLabelText(), and others, you can ensure your tests better reflect actual user interactions and make your components more accessible. React Testing Library encourages focusing on real user experience and behavior, which leads to better testing outcomes. For further details on implementing these best practices and exploring additional query options, you can always refer to the React Testing Library documentation and its handy cheatsheets.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=a3f3225257fb" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Architectural Essentials: What Controllers and Services Should Do?]]></title>
            <link>https://medium.com/@yamilini.ga/architectural-essentials-what-controllers-and-services-should-do-29cca8cf2a61?source=rss-628a5822b220------2</link>
            <guid isPermaLink="false">https://medium.com/p/29cca8cf2a61</guid>
            <dc:creator><![CDATA[Yamilini G A]]></dc:creator>
            <pubDate>Fri, 11 Oct 2024 04:38:31 GMT</pubDate>
            <atom:updated>2024-10-11T04:38:31.211Z</atom:updated>
            <content:encoded><![CDATA[<p>In the realm of software development, clarity in structure is the key to assembling scalable, maintainable solutions. However, as developers, we frequently question ourselves: What exactly ought a controller to be able to do? To what extent does a service need business logic? Where should the data validations happen? Although the answers to these questions may not appear important at first, they shape the design of our systems. We’ll deconstruct the responsibilities of controllers and services in this blog post to make sure that each layer performs just what it’s meant to.</p><p>I’d like to start with an analogy that best explains the controller service role. In a construction site, workers, electricians and carpenters are given particular jobs to accomplish various parts of a building project by the site manager.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/940/1*I9XTKXrJ7kvpVwWd0IzrNQ.png" /><figcaption>Construction site analogy</figcaption></figure><p>Here, the site manager who assigns the assignment serves as an example for the controller, and the labourers, electricians, and carpenters who complete the assigned task are our model for the service.</p><p>Our controllers will be handling all the <strong>web related logic</strong> — handling requests, validating the inputs, routing to appropriate service(the delegation work), session management, handling response and errors, etc.</p><p>Here’s an example of a simple BlogController handling request, response and error.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a17031d5161d3ae8ba50c869a3fb3592/href">https://medium.com/media/a17031d5161d3ae8ba50c869a3fb3592/href</a></iframe><p>Services will be handling all the <strong>core business logic</strong> — business logic, processing the data, other validations if present, coordination of repository calls or external services, etc.</p><p>Here’s an example of simple BlogService handling business logic, coordinating with repository/models.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/97bcd5c87fa0544f2918cac79a1095ea/href">https://medium.com/media/97bcd5c87fa0544f2918cac79a1095ea/href</a></iframe><p>Imagine a controller that handles both its own responsibilities and the logic typically assigned to services — this is often referred to as a “<strong>Fat Controller</strong>.” A Fat Controller tends to contain excessive amounts of business logic, leading to bloated, disorganised code that <strong>reduces readability</strong>, makes it more <strong>difficult to test</strong>, and <strong>hinders code reusability</strong>. Over time, such a controller becomes increasingly <strong>complex to refactor</strong>.</p><p>When errors occur in a Fat Controller, pinpointing the exact source of failure becomes challenging due to the tightly coupled logic. This complexity makes it <strong>harder to write unit tests</strong> or isolate specific issues. To avoid these problems, it’s best to delegate business logic to dedicated service classes, keeping controllers lightweight and focused solely on handling requests and responses. This separation of concerns not only improves maintainability but also makes the code easier to debug and test.</p><p>Key considerations to keep in mind:</p><ul><li>A controller should never be called from within a service. Instead, if the functionality of a controller is needed, you should utilise the service that the controller depends on.</li></ul><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/cf8c5234eea3b2bff20467fd55e48b5f/href">https://medium.com/media/cf8c5234eea3b2bff20467fd55e48b5f/href</a></iframe><ul><li>All interactions with the database should be handled by the repository layer. The service is responsible for invoking the repository to perform database operations (CRUD actions).</li><li>External API calls or any third-party service integrations should fall under the responsibility of the service layer. This ensures a clear separation of concerns, with the service managing business logic and external communication.</li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=29cca8cf2a61" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[From Days to Hours: Unveiling the Threaded Revolution in Process Optimization]]></title>
            <link>https://medium.com/@yamilini.ga/from-days-to-hours-unveiling-the-threaded-revolution-in-process-optimization-3a8fbe506afb?source=rss-628a5822b220------2</link>
            <guid isPermaLink="false">https://medium.com/p/3a8fbe506afb</guid>
            <dc:creator><![CDATA[Yamilini G A]]></dc:creator>
            <pubDate>Fri, 10 May 2024 08:50:54 GMT</pubDate>
            <atom:updated>2024-05-13T06:13:55.557Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*MwHOdPdQH6dzZ8UuPxNV8w.png" /></figure><p>When it comes to getting things done, time is often our biggest challenge. We’ve all experienced the frustration of waiting for tasks to wrap up and the inefficiency of losing entire days to slow processes. I want to share how I tackled this issue head-on.</p><p>Imagine this: a task that used to drag on for days is now finished in just a few hours. Intrigued? Today, I invite you to explore with me as I uncover the remarkable transformation achieved through the deliberate use of threaded processing. It’s a tale of innovation, effectiveness, and reclaiming precious time, all thanks to the power of threading.</p><h4><strong>THE PROBLEM:</strong></h4><p>I was handed a massive csv file (about <strong>5GB</strong>) which includes a list of hotel information from across the world, along with their unique site URLs. The job is to extract each rows from the csv file if the hotel’s web address returns a 200 status code and save them to a new csv file.</p><p>The csv file looks more like the following snippet:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/98baf5c7294389f8636702194c587522/href">https://medium.com/media/98baf5c7294389f8636702194c587522/href</a></iframe><p>The task here is to extract both the urls of each row’s last column and record the url that returns a 200 status code in a new csv file. Post the changes, this is how my output looks:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/9d1f4665e2b6c79c7f6bb26a3b496dd9/href">https://medium.com/media/9d1f4665e2b6c79c7f6bb26a3b496dd9/href</a></iframe><h4><strong>MY FIRST APPROACH:</strong></h4><p>Initially, I utilised the “requests” library in Python, which is a third-party Python library for creating HTTP requests. It makes it easier to perform HTTP queries and handle answers by offering a simple API for things like sending GET and POST requests, adding headers, managing cookies, and so on.</p><p>Here is the code post extracting the url from the csv file:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/8c21360635e399beb7e81c399b59a694/href">https://medium.com/media/8c21360635e399beb7e81c399b59a694/href</a></iframe><p>We need “time.sleep(120)” on line 8 of the preceding snippet because calling “requests.get(url)” on the next line takes time to hit the url and retrieve its information. If we are not putting the process to sleep, threads will bypass this step in a few seconds if line 9 is not returning a value.</p><h4>ADDRESSING THE DRAWBACKS OF MY APPROACH:</h4><p>The difficulty I encountered with the preceding code was that each url took longer to parse. After evaluating the pace at which each line was processed and written to a new file, it appeared that the execution would not be completed even after two days. I believed that the procedure should be optimised more intelligently.</p><p>I went down to investigate the sources causing the slowness and took down the following points:</p><ol><li>Requests package isn’t working effectively for my situation.</li><li>Large file sizes may make it slowing down.</li><li>The procedure is sequential.</li></ol><p>I looked for other options to the requests library, but there were very few, and it appeared that I was already using a solid one. To solve the high file size, I considered dividing it down into smaller portions and running them on my PC. Still, it would take the same amount of time. Running each file chunk on distinct computers is comparable to putting them in threads on the same system, which results in asynchronous execution.</p><h4><strong>FINAL SOLUTION:</strong></h4><p>The final answer was to integrate threading into the process, which checks each URL for a 200 status code. This was the final asynchronous code, which completed the task in <strong>under two hours</strong>.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e85ba205431fab00b4408172c46dab19/href">https://medium.com/media/e85ba205431fab00b4408172c46dab19/href</a></iframe><p>In line 21, max_workers can be increased to expedite the process, but first determine how many cores your system can handle and make appropriate plans.</p><p>Let me know if any other solution could have optimized the process more and thus saving even more time 🙂.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=3a8fbe506afb" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Maximising SQL Performance: Tips for Writing More Optimised & Efficient SQL Queries]]></title>
            <link>https://medium.com/@yamilini.ga/maximising-sql-performance-tips-for-writing-more-optimised-efficient-sql-queries-ad088e0dd71f?source=rss-628a5822b220------2</link>
            <guid isPermaLink="false">https://medium.com/p/ad088e0dd71f</guid>
            <dc:creator><![CDATA[Yamilini G A]]></dc:creator>
            <pubDate>Fri, 21 Apr 2023 05:06:17 GMT</pubDate>
            <atom:updated>2023-04-21T05:06:17.315Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*iWP5pTbL_8JgcHqru17xbQ.png" /></figure><p>SQL query optimisation is the practise of writing carefully thought-out SQL queries in order to boost database performance. The amount of data accessible and tested during development is lesser. So everything runs well and quickly at that moment.</p><p>In reality, the live production database will be your only updated database where the amount of data is massive and should be handled with caution since there is a chance of data loss if the query fails to execute properly.</p><p>Also, we don’t want to spend the rest of our lives waiting for the query to complete and provide the results.</p><p>This brings us to the necessity to optimise your queries. Here are some ideas to assist you increase the speed of your queries.</p><h4><strong>1. </strong>Minimise the usage of DISTINCT while selecting :</h4><p>On using DISTINCT, there is a possibility that the data might be erroneously classified. SELECT DISTINCT groups the column in the query to get unique results, which necessitates massive processing power.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a32c52345dd38ded574cff68a2d155ba/href">https://medium.com/media/a32c52345dd38ded574cff68a2d155ba/href</a></iframe><p>You can choose extra fields to obtain unique results instead of SELECT DISTINCT.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/31328777a36023f94a93604e9b457aca/href">https://medium.com/media/31328777a36023f94a93604e9b457aca/href</a></iframe><h4><strong>2. Make use of SELECT instead of SELECT * in your queries :</strong></h4><p>Picking all columns using SELECT * takes some time to complete execution as it strains database resources by searching for a big number of irrelevant data.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/62143a94554b4b20f73a71ff9608d9de/href">https://medium.com/media/62143a94554b4b20f73a71ff9608d9de/href</a></iframe><p>Selecting only the relevant columns saves time since most of the undesired columns are never considered.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/6a3566422f35426f82790125648d3440/href">https://medium.com/media/6a3566422f35426f82790125648d3440/href</a></iframe><h4><strong>3. LIMITing :</strong></h4><p>We can use LIMIT as a cross check to check whether the sample results of the query is working upto our expectations. This will be helpful in determining if the resultant dataset is usable or not.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/1bc0c60ca1410fd0d1bc91eac03e7b2f/href">https://medium.com/media/1bc0c60ca1410fd0d1bc91eac03e7b2f/href</a></iframe><h4>4. Avoid using HAVING. Use WHERE to apply filters :</h4><p>According to the order of execution of SQL statements, HAVING statements are run after WHERE statements.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*tQgMrvVyUsM3pddT1hnd1Q.png" /><figcaption>SQL Order of Execution</figcaption></figure><p>Applying filters using WHERE statements is a better and more efficient technique since it restricts the amount of records retrieved. When we want to apply a filter to an aggregated field, we may use HAVING.</p><h4><strong>5. </strong>This OR that? OR this? OR that? :</h4><p>Including multiple conditions with OR in your query makes the SQL server to process all of them at once. But the SQL server executes each condition one by one, the processing time increases, causing the query to perform poorly.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f1448e19a63f542cb910e7db68be4a13/href">https://medium.com/media/f1448e19a63f542cb910e7db68be4a13/href</a></iframe><p>This can be prevented by using UNION instead of using OR.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f8bac9cbaf1a122199c949c6496e52d4/href">https://medium.com/media/f8bac9cbaf1a122199c949c6496e52d4/href</a></iframe><h4>6. Merging tables:</h4><p>We never really want to generate billions and trillions of records by performing a cross join on two massive databases. We can replace this CROSS JOIN with INNER JOIN.</p><p>Also, using the WHERE clause to combine tables makes use of a cross join, which we saw before consumes time.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/3a469840002a78ada59c56af60714fa4/href">https://medium.com/media/3a469840002a78ada59c56af60714fa4/href</a></iframe><p>It leads to improper use of database resources. Some DBMS can differentiate between joins and perform them as inner joins.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/6831ce918002b05207de09424c63a8c4/href">https://medium.com/media/6831ce918002b05207de09424c63a8c4/href</a></iframe><p>Also, it is preferable to use INNER JOIN to integrate tables.</p><h4><strong>7. Sub-querying:</strong></h4><p>Sub-queries can be added to the end of a query with the IN or EXISTS clauses. It is best to utilise the EXISTS clause since the IN clause takes twice as long to process as the EXISTS clause.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/cbc81850c037b24625ebf01dd180a698/href">https://medium.com/media/cbc81850c037b24625ebf01dd180a698/href</a></iframe><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/df38ad0469e1783fd2d7f73fcbb90d4f/href">https://medium.com/media/df38ad0469e1783fd2d7f73fcbb90d4f/href</a></iframe><p>Optimising query sometimes might not help you completely as there are many cases where the results may go wrong, for example, the Simpson’s Paradox.</p><p>Simpson’s Paradox is a situation where the analysis made on the grouped data tend to reverse when it is aggregated. The solution for this problem depends on the requirement of the scenario considered. Beware of them!</p><p>This is all what I wanted to share with you all. Let me know if you got to know any more tips for writing optimised and efficient queries in the comment box.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=ad088e0dd71f" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Journey to the centre of Clean Code]]></title>
            <link>https://medium.com/@yamilini.ga/journey-to-the-centre-of-clean-code-f6dfc18d8376?source=rss-628a5822b220------2</link>
            <guid isPermaLink="false">https://medium.com/p/f6dfc18d8376</guid>
            <dc:creator><![CDATA[Yamilini G A]]></dc:creator>
            <pubDate>Mon, 07 Nov 2022 15:57:14 GMT</pubDate>
            <atom:updated>2022-11-07T15:59:51.902Z</atom:updated>
            <content:encoded><![CDATA[<blockquote>“Any fool can write code that a computer can understand. Good programmers write code that humans can understand.” — Martin Fowler</blockquote><p>Clean code is purely not about whether the code works or not. It is about how readable, meaningful and understandable it is to someone else who is reading it. Clean code doesn’t involve clean architecture (how your project is structured). And it is not that difficult to implement!</p><p>The solution to write clean code is to follow the rules &amp; concepts, patterns &amp; principles and TDD(Test Driven Development) to make the code concise to the point. When I started learning clean code, it all started with naming. Just then I realised how naming actually makes the code readable.</p><p>Naming includes how variables, constants, functions and classes have to be named, the casing and instructs to use consistent names throughout the code. For example, if you use getData(), stick with the same name (get) instead of using fetchData(), retrieveData(), etc — everything means the same.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*tDenaEAb9-6ChZ_eMun4IQ.png" /></figure><p>Clean code does not require comments but we can still add good comments like warnings, todo notes, legal information or explanation which can’t be replaced by good naming. Formatting the code makes it to look nice to the eyes. Example: Space between lines and code, grouping of similar code, indentation, etc.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/bef12a0beebc8deb7ef79f91232919ff/href">https://medium.com/media/bef12a0beebc8deb7ef79f91232919ff/href</a></iframe><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/aeab41ec7131ed898ead79416f0e412a/href">https://medium.com/media/aeab41ec7131ed898ead79416f0e412a/href</a></iframe><p>The above snippets 1 and 2 is a simple JavaScript code to check for voting eligibility. We can see that the formatted code (snippet 2) looks good and more readable than the non formatted code (snippet 1).</p><p>Moving onto functions, the general suggestion is to have minimum number of parameters. The order of placing the parameters is considered to be equally important. Consider the below snippets:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/41c556a5829b461473ba28d1771174c7/href">https://medium.com/media/41c556a5829b461473ba28d1771174c7/href</a></iframe><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/3bc244fed534e6de3a92fd93457b25bb/href">https://medium.com/media/3bc244fed534e6de3a92fd93457b25bb/href</a></iframe><p>Snippet A has 2 parameters and snippet B has more than 2 parameters. When the number of parameters is more, it is hard to track the order of parameters on making a function call.</p><p>This problem can be addressed by wrapping all the data in a dictionary or a hash map as shown in snippet C where now the order doesn’t matter.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/06154dcc01b57124f8587c38f15b030e/href">https://medium.com/media/06154dcc01b57124f8587c38f15b030e/href</a></iframe><p>Functions are advised to be short and do only its intended purpose. The power of a function is that it avoids code duplication. Do follow DRY (Don’t Repeat Yourself) principle and write only DRY code.</p><p>Clean classes are also recommended to be short. The SOLID principles are the 5 pillars of Object-Oriented Class Design.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*arNyiCcRulbJIB43b1HlhA.png" /></figure><p>Following SOLID principles, helps in writing clean code easily. The Single Responsibility Principle (SRP) and Open Close Principle (OCP) contributes mostly in writing clean code.</p><p>Implementing SRP restricts class to have one core responsibility which in turn leads to smaller classes and improves readability. Using interfaces achieves OCP by making the class open for extension but closed for modification.</p><p>These were some of the core concepts I have learnt recently. If you have learnt any more useful principles, please feel free to share in the comment section.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=f6dfc18d8376" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>