<?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 Romulo Freires on Medium]]></title>
        <description><![CDATA[Stories by Romulo Freires on Medium]]></description>
        <link>https://medium.com/@romulofreires?source=rss-2f872eb4ee7d------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*jpfVuMb-DHXC4FDs.jpg</url>
            <title>Stories by Romulo Freires on Medium</title>
            <link>https://medium.com/@romulofreires?source=rss-2f872eb4ee7d------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Wed, 18 Nov 2026 15:57:08 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@romulofreires/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[Code Analysis and Refactoring — A Bottom-Up Approach]]></title>
            <link>https://medium.com/@romulofreires/code-analysis-and-refactoring-a-bottom-up-approach-8bbf96bfb776?source=rss-2f872eb4ee7d------2</link>
            <guid isPermaLink="false">https://medium.com/p/8bbf96bfb776</guid>
            <category><![CDATA[design-patterns]]></category>
            <category><![CDATA[clean-code]]></category>
            <category><![CDATA[clean-architecture]]></category>
            <category><![CDATA[metrics]]></category>
            <category><![CDATA[refactoring]]></category>
            <dc:creator><![CDATA[Romulo Freires]]></dc:creator>
            <pubDate>Tue, 17 Sep 2024 22:32:43 GMT</pubDate>
            <atom:updated>2024-09-17T22:44:39.394Z</atom:updated>
            <content:encoded><![CDATA[<h3>Code Analysis and Refactoring: A Bottom-Up Approach</h3><p>Refactoring code is an essential skill for developers, both in the workplace and during technical interviews. Identifying problems and proposing improvements makes code more readable, maintainable, and scalable, while also preparing the system for new requirements.</p><p>In this article, we will follow a <strong>bottom-up</strong> approach to refactor a class responsible for processing weather alerts, addressing common issues that undermine code quality and maintainability, such as:</p><ul><li><strong>Poorly descriptive variable names</strong>, making the code harder to understand.</li><li><strong>Rigid logic</strong>, which prevents easy adaptation to new scenarios and requirements.</li><li><strong>Accumulation of responsibilities</strong> in a single class, violating the single responsibility principle.</li><li><strong>Lack of abstractions</strong>, limiting the flexibility and scalability of the code.</li><li><strong>Absence of logging</strong>, <strong>exception handling</strong>, and <strong>metrics</strong>, making it difficult to identify and fix errors in production.</li></ul><p>By applying best practices from <strong>Clean Code</strong>, <strong>Clean Architecture</strong>, and <strong>Design Patterns</strong>, we will refactor this code to make it more flexible, scalable, and ready for future demands.</p><p>Check out the initial version of the code below.</p><pre>@Service<br>public class DisasterAlertProcessor {<br>    private final WeatherDataGateway wd;<br>    private final AlertGateway ag;<br>    private final NotificationService ns;<br><br>    @Autowired<br>    public DisasterAlertProcessor(WeatherDataGateway wd, AlertGateway ag, NotificationService ns) {<br>        this.wd = wd;<br>        this.ag = ag;<br>        this.ns = ns;<br>    }<br><br>    public void process() {<br>        WeatherData[] wList = wd.findRecentEvents();<br>        List&lt;Alert&gt; aList = new ArrayList&lt;&gt;();<br><br>        for (int i = 0; i &lt; wList.length; i++) {<br>            WeatherData d = wList[i];<br>            if (d.getSeverity() &gt; 5 &amp;&amp; d.getType().equals(&quot;storm&quot;)) {<br>                Alert a = new Alert(d.getRegion(), d.getType(), d.getSeverity());<br>                aList.add(a);<br>                ns.send(a);<br>            }<br>        }<br><br>        ag.saveAll(aList);<br>    }<br>}</pre><p>The code presented is part of a disaster alert system that processes weather data and generates alerts based on specific conditions. The system fetches recent weather data, evaluates the severity and type of weather events (such as storms or heavy rain), and triggers alerts when certain thresholds are met.</p><h3>Step 1: Low-Level Improvements - Variable Names, Loops, and Magic Numbers/Strings</h3><p>In the original version, several issues make the code harder to maintain and prone to errors. These include:</p><ul><li><strong>Poorly descriptive variable names</strong>: Names like wList and aList are vague and do not communicate their purpose clearly. <strong>Solution</strong>: Rename wList to weatherDataList and aList to alertList to improve clarity and make the variable names more descriptive.</li><li><strong>Unnecessary indexed </strong><strong>for loop</strong>: The use of an indexed for loop is not necessary, as we are iterating over the entire list and don’t need to access or manipulate individual indices. <strong>Solution</strong>: Replace the indexed for loop with a for-each loop to simplify iteration and avoid index-related errors.</li><li><strong>Magic numbers and string comparisons</strong>: The hardcoded value 5 for severity and the use of string comparisons like equals(&quot;storm&quot;) make the code unclear and prone to errors. These magic numbers and strings can easily be replaced with incorrect values, leading to maintenance challenges and potential bugs. <strong>Solution</strong>: Introduce an enum for severity levels and event types. This centralizes valid values and reduces the risk of types or incorrect usage, while improving the clarity and maintainability of the code.</li></ul><pre>public enum WeatherEventType {<br>    STORM, RAIN, SNOW, WIND;<br>}<br><br>public enum SeverityLevel {<br>    LOW(1), MEDIUM(3), HIGH(5);<br><br>    private final int level;<br><br>    SeverityLevel(int level) {<br>        this.level = level;<br>    }<br><br>    public int getLevel() {<br>        return level;<br>    }<br>}<br><br>@Service<br>public class DisasterAlertProcessor {<br>    private static final SeverityLevel SEVERITY_THRESHOLD = SeverityLevel.MEDIUM;<br><br>    private final WeatherDataGateway weatherDataGateway;<br>    private final AlertGateway alertGateway;<br>    private final NotificationService notificationService;<br><br>    @Autowired<br>    public DisasterAlertProcessor(WeatherDataGateway weatherDataGateway, AlertGateway alertGateway, NotificationService notificationService) {<br>        this.weatherDataGateway = weatherDataGateway;<br>        this.alertGateway = alertGateway;<br>        this.notificationService = notificationService;<br>    }<br><br>    public void process() {<br>        WeatherData[] weatherDataList = weatherDataGateway.findRecentEvents();<br>        List&lt;Alert&gt; alertList = new ArrayList&lt;&gt;();<br><br>        for (WeatherData weatherData : weatherDataList) {<br>            if (weatherData.getSeverity() &gt; SEVERITY_THRESHOLD.getLevel() &amp;&amp; weatherData.getType().equals(WeatherEventType.STORM.name())) {<br>                Alert alert = new Alert(weatherData.getRegion(), weatherData.getType(), weatherData.getSeverity());<br>                alertList.add(alert);<br>                notificationService.send(alert);<br>            }<br>        }<br><br>        alertGateway.saveAll(alertList);<br>    }<br>}</pre><h3>Step 2: Design Patterns — Introducing the Strategy Pattern for Alert Conditions</h3><p>In the initial version of the code, the logic for checking if an alert should be triggered is embedded directly in the process() method. This approach works, but it is rigid and can become difficult to maintain or extend when new types of alerts or more complex conditions are added.</p><h4>Problem:</h4><ul><li><strong>Rigid logic</strong>: The current implementation is hardcoded to handle only a specific type of alert (storm with severity greater than a threshold). Adding new alert conditions or supporting other types of weather events would require modifying this method, leading to duplicated or overly complex code.</li></ul><h4>Solution:</h4><p>To solve this, we can apply the <strong>Strategy Pattern</strong>, which allows us to encapsulate different alert conditions into separate classes. This pattern will make it easy to add or modify alert conditions without changing the process() method. Each strategy can define its own rules for when an alert should be triggered.</p><pre>public interface AlertStrategy {<br>    boolean shouldTriggerAlert(WeatherData weatherData);<br>}<br><br>public class StormAlertStrategy implements AlertStrategy {<br>    private final SeverityLevel severityThreshold;<br><br>    public StormAlertStrategy(SeverityLevel severityThreshold) {<br>        this.severityThreshold = severityThreshold;<br>    }<br><br>    @Override<br>    public boolean shouldTriggerAlert(WeatherData weatherData) {<br>        return weatherData.getSeverity() &gt; severityThreshold.getLevel() <br>               &amp;&amp; weatherData.getType().equals(WeatherEventType.STORM.name());<br>    }<br>}<br><br>// Additional strategies can be added as needed, e.g.:<br>public class RainAlertStrategy implements AlertStrategy {<br>    private final SeverityLevel severityThreshold;<br><br>    public RainAlertStrategy(SeverityLevel severityThreshold) {<br>        this.severityThreshold = severityThreshold;<br>    }<br><br>    @Override<br>    public boolean shouldTriggerAlert(WeatherData weatherData) {<br>        return weatherData.getSeverity() &gt; severityThreshold.getLevel() <br>               &amp;&amp; weatherData.getType().equals(WeatherEventType.RAIN.name());<br>    }<br>}<br><br>@Service<br>public class DisasterAlertProcessor {<br>    private final WeatherDataGateway weatherDataGateway;<br>    private final AlertGateway alertGateway;<br>    private final NotificationService notificationService;<br>    private final List&lt;AlertStrategy&gt; alertStrategies;<br><br>    @Autowired<br>    public DisasterAlertProcessor(WeatherDataGateway weatherDataGateway, AlertGateway alertGateway, NotificationService notificationService, List&lt;AlertStrategy&gt; alertStrategies) {<br>        this.weatherDataGateway = weatherDataGateway;<br>        this.alertGateway = alertGateway;<br>        this.notificationService = notificationService;<br>        this.alertStrategies = alertStrategies;<br>    }<br><br>    public void process() {<br>        WeatherData[] weatherDataList = weatherDataGateway.findRecentEvents();<br>        List&lt;Alert&gt; alertList = new ArrayList&lt;&gt;();<br><br>        for (WeatherData weatherData : weatherDataList) {<br>            for (AlertStrategy strategy : alertStrategies) {<br>                if (strategy.shouldTriggerAlert(weatherData)) {<br>                    Alert alert = new Alert(weatherData.getRegion(), weatherData.getType(), weatherData.getSeverity());<br>                    alertList.add(alert);<br>                    notificationService.send(alert);<br>                }<br>            }<br>        }<br><br>        alertGateway.saveAll(alertList);<br>    }<br>}</pre><h3>Step 3: Refactoring Functions and Removing Responsibilities</h3><p>In the current version of the code, the process() method is doing too much, which violates the <strong>Single Responsibility Principle</strong> (SRP). Additionally, when you first look at the process() method, it’s not immediately clear what it does because it handles multiple tasks at once. This lack of clarity means you need to spend extra time reading through the entire method to understand its purpose.</p><h4>Problem:</h4><ul><li><strong>Multiple Responsibilities</strong>: The process() method handles too many tasks at once (fetching data, checking alert conditions, creating alerts, sending notifications, and saving alerts). This makes the code difficult to understand at a glance, and any changes to one of these responsibilities may introduce bugs in other parts of the method.</li><li><strong>Unclear Purpose</strong>: When you first look at the process() method, it’s hard to immediately understand what the method is responsible for. You need to spend extra time reading and breaking down the logic to figure out its purpose.</li></ul><h4>Solution:</h4><p>We can refactor the process() method by breaking it down into smaller, more focused methods. This makes it immediately clear what each part of the method does, improving both readability and maintainability. Each method will handle a single responsibility, adhering to the <strong>Single Responsibility Principle</strong>.</p><pre>@Service<br>public class DisasterAlertProcessor {<br>    private final WeatherDataGateway weatherDataGateway;<br>    private final AlertGateway alertGateway;<br>    private final NotificationService notificationService;<br>    private final List&lt;AlertStrategy&gt; alertStrategies;<br><br>    @Autowired<br>    public DisasterAlertProcessor(WeatherDataGateway weatherDataGateway, AlertGateway alertGateway, NotificationService notificationService, List&lt;AlertStrategy&gt; alertStrategies) {<br>        this.weatherDataGateway = weatherDataGateway;<br>        this.alertGateway = alertGateway;<br>        this.notificationService = notificationService;<br>        this.alertStrategies = alertStrategies;<br>    }<br><br>    public void process() {<br>        WeatherData[] weatherDataList = this.weatherDataGateway.findRecentEvents();<br>        List&lt;Alert&gt; alertList = generateAlerts(weatherDataList);<br>        sendAlerts(alertList);<br>        saveAlerts(alertList);<br>    }<br><br>    private List&lt;Alert&gt; generateAlerts(WeatherData[] weatherDataList) {<br>        List&lt;Alert&gt; alertList = new ArrayList&lt;&gt;();<br>        for (WeatherData weatherData : weatherDataList) {<br>            for (AlertStrategy strategy : alertStrategies) {<br>                if (strategy.shouldTriggerAlert(weatherData)) {<br>                    alertList.add(new Alert(weatherData.getRegion(), weatherData.getType(), weatherData.getSeverity()));<br>                }<br>            }<br>        }<br>        return alertList;<br>    }<br><br>    private void sendAlerts(List&lt;Alert&gt; alertList) {<br>        for (Alert alert : alertList) {<br>            notificationService.send(alert);<br>        }<br>    }<br><br>    private void saveAlerts(List&lt;Alert&gt; alertList) {<br>        alertGateway.saveAll(alertList);<br>    }<br>}</pre><p>Here’s the portion of the code that involves the <strong>Strategy Pattern</strong> in context:</p><pre>for (AlertStrategy strategy : alertStrategies) {<br>    if (strategy.shouldTriggerAlert(weatherData)) {<br>        alertList.add(new Alert(weatherData.getRegion(), weatherData.getType(), weatherData.getSeverity()));<br>    }<br>}</pre><p>This loop iterates through a list of AlertStrategy objects, applying each strategy to check if an alert should be triggered for the current WeatherData. While this code could be refactored into a separate function to improve modularity, it remains in-line to highlight the flexibility of code analysis. Depending on the developer’s perspective, one might decide to refactor this for better readability, while others might prioritize keeping it inline for simplicity. Both approaches can be correct, depending on the context, goals, and style preferences.</p><h3>Step 4: Delegating Responsibilities to Dedicated Classes</h3><p>In previous steps, the DisasterAlertProcessor handled both the generation and saving of alerts, as well as orchestrating the overall flow of data. To better adhere to the <strong>Single Responsibility Principle</strong>, we will need to make additional changes.</p><h4>Problem:</h4><p><strong>Accumulation of Responsibilities</strong>: The DisasterAlertProcessor is still responsible for generating alerts, saving them, and coordinating the overall process. This violates the <strong>Single Responsibility Principle</strong>, as the class has multiple reasons to change.</p><h4>Solution:</h4><p>We will introduce a new AlertService, responsible for both generating and saving alerts. This service will manage all alert-related logic, further simplifying the DisasterAlertProcessor, which will now only handle orchestrating tasks between the weather data, alert, and notification services.</p><pre>@Service<br>public class AlertService {<br>    private final List&lt;AlertStrategy&gt; alertStrategies;<br>    private final AlertGateway alertGateway;<br><br>    @Autowired<br>    public AlertService(List&lt;AlertStrategy&gt; alertStrategies, AlertGateway alertGateway) {<br>        this.alertStrategies = alertStrategies;<br>        this.alertGateway = alertGateway;<br>    }<br><br>    public List&lt;Alert&gt; generateAlerts(WeatherData[] weatherDataList) {<br>        List&lt;Alert&gt; alertList = new ArrayList&lt;&gt;();<br>        for (WeatherData weatherData : weatherDataList) {<br>            for (AlertStrategy strategy : alertStrategies) {<br>                if (strategy.shouldTriggerAlert(weatherData)) {<br>                    alertList.add(new Alert(weatherData.getRegion(), weatherData.getType(), weatherData.getSeverity()));<br>                }<br>            }<br>        }<br>        return alertList;<br>    }<br><br>    public void saveAlerts(List&lt;Alert&gt; alertList) {<br>        alertGateway.saveAll(alertList);<br>    }<br>}<br><br>@Service<br>public class NotificationService {<br>    public void send(Alert alert) {<br>        // Logic for sending notifications<br>    }<br>}<br><br>@Service<br>public class DisasterAlertProcessor {<br>    private final WeatherDataGateway weatherDataGateway;<br>    private final AlertService alertService;<br>    private final NotificationService notificationService;<br><br>    @Autowired<br>    public DisasterAlertProcessor(WeatherDataGateway weatherDataGateway, AlertService alertService, NotificationService notificationService) {<br>        this.weatherDataGateway = weatherDataGateway;<br>        this.alertService = alertService;<br>        this.notificationService = notificationService;<br>    }<br><br>    public void process() {<br>        WeatherData[] weatherDataList = weatherDataGateway.findRecentEvents();<br>        List&lt;Alert&gt; alertList = alertService.generateAlerts(weatherDataList);<br>        sendAlerts(alertList);<br>        alertService.saveAlerts(alertList);<br>    }<br><br>    private void sendAlerts(List&lt;Alert&gt; alertList) {<br>        for (Alert alert : alertList) {<br>            notificationService.send(alert);<br>        }<br>    }<br>}</pre><h3>Step 5: Introducing Abstractions — Creating an Interface for AlertGateway</h3><p>To ensure flexibility and improve testability, we need to introduce an abstraction layer by creating an interface for AlertGateway. This step is important to follow the <strong>Dependency Inversion Principle (DIP)</strong>, which states that high-level modules should not depend on low-level modules. Instead, both should depend on abstractions.</p><h4>Problem:</h4><ul><li><strong>Tight Coupling</strong>: The current implementation tightly couples the AlertService to the concrete AlertGateway class. This makes it harder to extend the system, swap out implementations, or mock the gateway during testing.</li></ul><h4>Solution:</h4><p>We will introduce an interface IAlertGateway to abstract the functionality of saving alerts. The AlertService will depend on this interface instead of a concrete class, making the system more modular and easier to maintain or modify.</p><pre>public interface IAlertGateway {<br>    void saveAll(List&lt;Alert&gt; alertList);<br>}<br><br>public class AlertGateway implements IAlertGateway {<br>    @Override<br>    public void saveAll(List&lt;Alert&gt; alertList) {<br>        // Concrete logic to save alerts<br>    }<br>}<br><br>@Service<br>public class AlertService {<br>    private final List&lt;AlertStrategy&gt; alertStrategies;<br>    private final IAlertGateway alertGateway;<br><br>    @Autowired<br>    public AlertService(List&lt;AlertStrategy&gt; alertStrategies, IAlertGateway alertGateway) {<br>        this.alertStrategies = alertStrategies;<br>        this.alertGateway = alertGateway;<br>    }<br><br>    public List&lt;Alert&gt; generateAlerts(WeatherData[] weatherDataList) {<br>        List&lt;Alert&gt; alertList = new ArrayList&lt;&gt;();<br>        for (WeatherData weatherData : weatherDataList) {<br>            for (AlertStrategy strategy : alertStrategies) {<br>                if (strategy.shouldTriggerAlert(weatherData)) {<br>                    alertList.add(new Alert(weatherData.getRegion(), weatherData.getType(), weatherData.getSeverity()));<br>                }<br>            }<br>        }<br>        return alertList;<br>    }<br><br>    public void saveAlerts(List&lt;Alert&gt; alertList) {<br>        alertGateway.saveAll(alertList);<br>    }<br>}</pre><h3>Step 6: Handling Exception Management and Logging</h3><p>In this step, we’ll focus on improving the error handling and logging capabilities of the system. Proper error handling and logging are essential for identifying and diagnosing issues in production, as well as ensuring that the system remains robust in the face of unexpected events.</p><h4>Problem:</h4><ul><li><strong>Lack of Exception Handling</strong>: The current implementation does not handle exceptions. If something goes wrong (e.g., a network failure when fetching weather data), the system could fail without logging useful information.</li><li><strong>No Logging</strong>: There is no logging implemented in the system. Logging is crucial for tracking the flow of the program and identifying where failures or issues occur.</li></ul><h4>Solution:</h4><p>We will implement proper exception handling with meaningful error messages and logging. Each method will catch potential exceptions and log the necessary details, allowing for better monitoring and easier debugging.</p><pre>import org.slf4j.Logger;<br>import org.slf4j.LoggerFactory;<br><br>@Service<br>public class AlertService {<br>    private static final Logger logger = LoggerFactory.getLogger(AlertService.class);<br>    <br>    private final List&lt;AlertStrategy&gt; alertStrategies;<br>    private final IAlertGateway alertGateway;<br><br>    @Autowired<br>    public AlertService(List&lt;AlertStrategy&gt; alertStrategies, IAlertGateway alertGateway) {<br>        this.alertStrategies = alertStrategies;<br>        this.alertGateway = alertGateway;<br>    }<br><br>    public List&lt;Alert&gt; generateAlerts(WeatherData[] weatherDataList) {<br>        List&lt;Alert&gt; alertList = new ArrayList&lt;&gt;();<br>        for (WeatherData weatherData : weatherDataList) {<br>            for (AlertStrategy strategy : alertStrategies) {<br>                try {<br>                    if (strategy.shouldTriggerAlert(weatherData)) {<br>                        alertList.add(new Alert(weatherData.getRegion(), weatherData.getType(), weatherData.getSeverity()));<br>                        logger.info(&quot;Alert generated for region: {}&quot;, weatherData.getRegion());<br>                    }<br>                } catch (Exception e) {<br>                    logger.error(&quot;Error occurred while processing weather data for region: {}&quot;, weatherData.getRegion(), e);<br>                }<br>            }<br>        }<br>        return alertList;<br>    }<br><br>    public void saveAlerts(List&lt;Alert&gt; alertList) {<br>        try {<br>            alertGateway.saveAll(alertList);<br>            logger.info(&quot;Successfully saved {} alerts.&quot;, alertList.size());<br>        } catch (Exception e) {<br>            logger.error(&quot;Failed to save alerts&quot;, e);<br>            throw e;<br>        }<br>    }<br>}<br><br>import org.slf4j.Logger;<br>import org.slf4j.LoggerFactory;<br><br>@Service<br>public class DisasterAlertProcessor {<br>    private static final Logger logger = LoggerFactory.getLogger(DisasterAlertProcessor.class);<br>    <br>    private final WeatherDataGateway weatherDataGateway;<br>    private final AlertService alertService;<br>    private final NotificationService notificationService;<br><br>    @Autowired<br>    public DisasterAlertProcessor(WeatherDataGateway weatherDataGateway, AlertService alertService, NotificationService notificationService) {<br>        this.weatherDataGateway = weatherDataGateway;<br>        this.alertService = alertService;<br>        this.notificationService = notificationService;<br>    }<br><br>    public void process() {<br>        try {<br>            WeatherData[] weatherDataList = fetchWeatherData();<br>            List&lt;Alert&gt; alertList = alertService.generateAlerts(weatherDataList);<br>            sendAlerts(alertList);<br>            alertService.saveAlerts(alertList);<br>        } catch (Exception e) {<br>            logger.error(&quot;An error occurred during the alert processing&quot;, e);<br>        }<br>    }<br><br>    private WeatherData[] fetchWeatherData() {<br>        try {<br>            return weatherDataGateway.findRecentEvents();<br>        } catch (Exception e) {<br>            logger.error(&quot;Failed to fetch weather data&quot;, e);<br>            throw e;<br>        }<br>    }<br><br>    private void sendAlerts(List&lt;Alert&gt; alertList) {<br>        for (Alert alert : alertList) {<br>            try {<br>                notificationService.send(alert);<br>                logger.info(&quot;Alert sent for region: {}&quot;, alert.getRegion());<br>            } catch (Exception e) {<br>                logger.error(&quot;Failed to send alert for region: {}&quot;, alert.getRegion(), e);<br>            }<br>        }<br>    }<br>}</pre><p>By introducing proper exception handling and logging, we ensured the system is more robust and traceable. Errors are now captured and logged, allowing developers to identify and resolve issues more easily. While there are various design patterns and approaches for handling exceptions and logs — such as implementing a centralized <strong>Error Handler</strong> or using <strong>AOP (Aspect-Oriented Programming)</strong> for logging — the goal here was to demonstrate the importance of having basic exception handling and logging in place. This ensures that errors are caught and can be tracked effectively, laying the groundwork for further refinement in future iterations.</p><h3>Step 7: Metrics Collection — Monitoring Performance and Request Counts</h3><p>In this final step, we introduced metrics collection to monitor the performance and reliability of the system.</p><h4>Problem:</h4><ul><li><strong>No Performance Monitoring</strong>: The original implementation lacked performance monitoring, providing no visibility into how long critical operations were taking or how frequently they occurred.</li><li><strong>No Request Counting</strong>: There was no way to track how many alerts were being processed, how many notifications were sent, or how often errors occurred during key operations like weather data fetching and alert processing.</li></ul><h4>Solution:</h4><p>We integrated a metrics library (e.g., <strong>Micrometer</strong>) to collect essential metrics, including:</p><ul><li><strong>Request Counts</strong>: We now track how many alerts are generated and sent, how often alerts are saved, and how many errors occur during these operations.</li><li><strong>Response Time</strong>: We measure the time taken for critical operations.</li></ul><pre>import io.micrometer.core.instrument.MeterRegistry;<br>import io.micrometer.core.instrument.Counter;<br>import io.micrometer.core.instrument.Timer;<br>import org.slf4j.Logger;<br>import org.slf4j.LoggerFactory;<br><br>@Service<br>public class AlertService {<br>    private static final Logger logger = LoggerFactory.getLogger(AlertService.class);<br><br>    private final List&lt;AlertStrategy&gt; alertStrategies;<br>    private final IAlertGateway alertGateway;<br>    private final MeterRegistry meterRegistry;<br>    private final Counter alertCounter;<br>    private final Counter alertErrorCounter;<br>    private final Timer alertGenerationTimer;<br>    private final Timer alertSaveTimer;<br><br>    @Autowired<br>    public AlertService(List&lt;AlertStrategy&gt; alertStrategies, IAlertGateway alertGateway, MeterRegistry meterRegistry) {<br>        this.alertStrategies = alertStrategies;<br>        this.alertGateway = alertGateway;<br>        this.meterRegistry = meterRegistry;<br><br>        this.alertCounter = meterRegistry.counter(&quot;alerts.generated&quot;);<br>        this.alertErrorCounter = meterRegistry.counter(&quot;alerts.errors&quot;);<br>        this.alertGenerationTimer = meterRegistry.timer(&quot;alerts.generation.time&quot;);<br>        this.alertSaveTimer = meterRegistry.timer(&quot;alerts.save.time&quot;);<br>    }<br><br>    public List&lt;Alert&gt; generateAlerts(WeatherData[] weatherDataList) {<br>        return alertGenerationTimer.record(() -&gt; {<br>            List&lt;Alert&gt; alertList = new ArrayList&lt;&gt;();<br>            for (WeatherData weatherData : weatherDataList) {<br>                for (AlertStrategy strategy : alertStrategies) {<br>                    try {<br>                        if (strategy.shouldTriggerAlert(weatherData)) {<br>                            alertList.add(new Alert(weatherData.getRegion(), weatherData.getType(), weatherData.getSeverity()));<br>                            logger.info(&quot;Alert generated for region: {}&quot;, weatherData.getRegion());<br>                            alertCounter.increment();<br>                        }<br>                    } catch (Exception e) {<br>                        logger.error(&quot;Error occurred while processing weather data for region: {}&quot;, weatherData.getRegion(), e);<br>                        alertErrorCounter.increment();<br>                    }<br>                }<br>            }<br>            return alertList;<br>        });<br>    }<br><br>    public void saveAlerts(List&lt;Alert&gt; alertList) {<br>        alertSaveTimer.record(() -&gt; {<br>            try {<br>                alertGateway.saveAll(alertList);<br>                logger.info(&quot;Successfully saved {} alerts.&quot;, alertList.size());<br>            } catch (Exception e) {<br>                logger.error(&quot;Failed to save alerts&quot;, e);<br>                alertErrorCounter.increment();<br>                throw e;<br>            }<br>        });<br>    }<br>}<br><br>@Service<br>public class NotificationService {<br>    private static final Logger logger = LoggerFactory.getLogger(NotificationService.class);<br><br>    private final MeterRegistry meterRegistry;<br>    private final Timer notificationSendTimer;<br>    private final Counter notificationErrorCounter;<br><br>    @Autowired<br>    public NotificationService(MeterRegistry meterRegistry) {<br>        this.meterRegistry = meterRegistry;<br>        this.notificationSendTimer = meterRegistry.timer(&quot;notifications.send.time&quot;);<br>        this.notificationErrorCounter = meterRegistry.counter(&quot;notifications.errors&quot;);<br>    }<br><br>    public void send(Alert alert) {<br>        notificationSendTimer.record(() -&gt; {<br>            try {<br>                logger.info(&quot;Sending alert for region: {}&quot;, alert.getRegion());<br>                System.out.println(&quot;Alert sent: &quot; + alert.getRegion() + &quot; - &quot; + alert.getType());<br>            } catch (Exception e) {<br>                logger.error(&quot;Failed to send alert for region: {}&quot;, alert.getRegion(), e);<br>                notificationErrorCounter.increment();<br>            }<br>        });<br>    }<br>}<br><br>@Service<br>public class DisasterAlertProcessor {<br>    private static final Logger logger = LoggerFactory.getLogger(DisasterAlertProcessor.class);<br><br>    private final WeatherDataGateway weatherDataGateway;<br>    private final AlertService alertService;<br>    private final NotificationService notificationService;<br>    private final MeterRegistry meterRegistry;<br>    private final Timer weatherDataFetchTimer;<br><br>    @Autowired<br>    public DisasterAlertProcessor(WeatherDataGateway weatherDataGateway, AlertService alertService, NotificationService notificationService, MeterRegistry meterRegistry) {<br>        this.weatherDataGateway = weatherDataGateway;<br>        this.alertService = alertService;<br>        this.notificationService = notificationService;<br>        this.meterRegistry = meterRegistry;<br>        this.weatherDataFetchTimer = meterRegistry.timer(&quot;weatherdata.fetch.time&quot;);<br>    }<br><br>    public void process() {<br>        weatherDataFetchTimer.record(() -&gt; {<br>            try {<br>                WeatherData[] weatherDataList = fetchWeatherData();<br>                List&lt;Alert&gt; alertList = alertService.generateAlerts(weatherDataList);<br>                sendAlerts(alertList);<br>                alertService.saveAlerts(alertList);<br>            } catch (Exception e) {<br>                logger.error(&quot;An error occurred during alert processing&quot;, e);<br>            }<br>        });<br>    }<br><br>    private void sendAlerts(List&lt;Alert&gt; alertList) {<br>        for (Alert alert : alertList) {<br>            notificationService.send(alert);<br>        }<br>    }<br>}</pre><h3>Final Thoughts</h3><p>By following this <strong>bottom-up</strong> refactoring approach, we were able to address low-level issues first and gradually improve the system at every layer.</p><p>In this refactoring process, we systematically improved the design and maintainability of the system by adhering to <strong>clean code</strong> practices, <strong>clean architecture</strong> principles, and <strong>design patterns</strong>. We tackled specific issues step by step, ensuring each component follows best practices, remains modular, testable, and scalable.</p><p>This approach can serve as a valuable model for handling similar refactoring tasks, whether in day-to-day work or in technical interviews, highlighting the importance of thoughtful, incremental improvements to achieve a cleaner and more effective architecture.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=8bbf96bfb776" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Meet Fetchin: The Lightweight Python Library for Resilient HTTP Requests]]></title>
            <link>https://medium.com/@romulofreires/meet-fetchin-the-lightweight-python-library-for-resilient-http-requests-f6d31c78b2d5?source=rss-2f872eb4ee7d------2</link>
            <guid isPermaLink="false">https://medium.com/p/f6d31c78b2d5</guid>
            <category><![CDATA[api-development]]></category>
            <category><![CDATA[circuit-breaker]]></category>
            <category><![CDATA[http-request]]></category>
            <category><![CDATA[python]]></category>
            <category><![CDATA[monitoring]]></category>
            <dc:creator><![CDATA[Romulo Freires]]></dc:creator>
            <pubDate>Sat, 07 Sep 2024 16:17:34 GMT</pubDate>
            <atom:updated>2024-09-08T02:34:19.152Z</atom:updated>
            <content:encoded><![CDATA[<h4>Why Are HTTP Requests Such a Pain?</h4><p>Let’s be real — dealing with HTTP requests in Python can quickly become a hassle. Between handling failed requests, retries, and figuring out why your API calls aren’t going through, things can get messy. Add to that the complexity of tracking and monitoring these calls, and you’ve got yourself a recipe for overcomplicated code.</p><p>Sure, there are expensive APM tools that can help you keep track of things, but they can be overkill when all you really need is to manage some API calls. This is exactly where <strong>Fetchin</strong> comes into play — a Python library that takes care of retries, error handling, logging, and metrics, without the need for a massive APM setup.</p><h4>The Fetchin Solution: Simple, Resilient, and Flexible</h4><p><strong>Fetchin</strong> was created to make your life as a developer easier by simplifying HTTP requests. It’s built to handle the complexities that come with calling external APIs or internal microservices. With features like <strong>circuit breakers</strong>, <strong>custom logging</strong>, and <strong>metrics collection</strong>, Fetchin ensures that your API calls are both resilient and observable.</p><p>Fetchin is also a great alternative when you don’t need robust monitoring tools like <strong>Datadog</strong> or <strong>New Relic</strong> but still want visibility into your API calls. It provides just enough observability with custom logging and metrics to keep you in the loop without the overhead of heavy APM solutions.</p><p>And even if you’re already using <strong>Datadog</strong> or <strong>New Relic</strong>, Fetchin is still useful! You can <strong>disable the metrics</strong> functionality and continue to benefit from its <strong>context-aware logging</strong> and <strong>circuit breaker</strong> features. This means you can manage your HTTP requests efficiently without worrying about duplicating monitoring efforts.</p><h4>Why Fetchin Rocks: Key Features</h4><p><strong>Automatic Retry and Failure Management with Custom Backoff Strategies</strong></p><p>Fetchin handles retries automatically using the <strong>circuit breaker pattern</strong>, so if an API goes down or experiences timeouts, Fetchin will manage the retries for you. But here’s the best part: you can <strong>implement your own custom backoff strategy</strong>. Whether you need exponential backoff, linear backoff, or something unique, Fetchin gives you the flexibility to make it happen. No more hardcoded retry logic scattered across your codebase!</p><p><strong>Context-Aware Circuit Breakers and Logging</strong></p><p>One of Fetchin’s most powerful features is its <strong>context-aware logging</strong>. While the <strong>circuit breaker</strong> is shared when you pass the same <strong>label</strong> across multiple calls, you can easily maintain contextual logs by creating a new <strong>logger</strong> for each incoming request. This means that if your API receives an incoming request and makes calls to other services using Fetchin, you pass along the same <strong>label</strong> to ensure shared circuit breaker behavior, but you generate a <strong>new logger</strong> that includes the specific context of that incoming request (such as request IDs or user-specific details). Fetchin will log everything consistently with the new logger, allowing you to trace the flow of each incoming request through to any downstream API calls it makes. This ensures both resilience and full traceability of every request’s journey across services.</p><p><strong>Flexible, JSON-based Logging</strong></p><p>Fetchin’s logging system outputs everything in <strong>JSON format</strong>, making it easy to send logs to <strong>ElasticSearch</strong>, <strong>Logstash</strong>, or any other logging platform you’re using. It’s fully customizable, letting you add <strong>extra fields</strong> like request IDs or user information, so you can easily track requests and responses in detail.</p><p><strong>Metrics Collection Beyond Prometheus</strong></p><p>While Fetchin offers <strong>Prometheus</strong> integration by default, it doesn’t stop there. Fetchin is built on an extensible metrics interface, allowing you to create your own metrics system if you prefer something else.</p><p>If you’re already using tools like <strong>Datadog</strong> or <strong>New Relic</strong>, Fetchin lets you <strong>disable its internal metrics</strong>. This way, you won’t duplicate monitoring efforts but still get the benefits of Fetchin’s logging, context tracking, and circuit breaker features.</p><h4>Proposing New Features: What Could Make Fetchin Even Better?</h4><p>Fetchin is already packed with powerful features, but there’s always room for more. Here are a few ideas for how Fetchin could grow in the future:</p><p><strong>Built-in Rate Limiting</strong></p><p>API rate limits can be tricky to manage, and Fetchin could easily extend its capabilities by adding <strong>rate limiting</strong> out of the box. This would allow Fetchin to automatically throttle requests to stay within API limits, preventing your app from overloading external services and getting hit with “429 Too Many Requests” errors.</p><p><strong>Distributed Circuit Breaker Support</strong></p><p>In large distributed systems, a <strong>distributed circuit breaker</strong> could provide better resilience by allowing multiple instances of Fetchin to share circuit breaker states. This could be useful when you have a microservice architecture with multiple replicas calling the same external API.</p><p><strong>Preconfigured Monitoring Templates</strong></p><p>Fetchin could offer <strong>prebuilt templates</strong> for integrating with popular observability platforms. This would make it even easier to get up and running with whatever monitoring stack you’re already using.</p><h4>Fetchin in Action: A Quick Example</h4><p>Let’s see Fetchin in action with a real-world example. This snippet shows how you can set up a <strong>custom backoff strategy</strong> while logging requests, preserving context, and sending metrics to Prometheus:</p><pre>from fetchin import Fetcher, CustomLogger, PrometheusMetrics<br><br># Define a custom backoff strategy (linear backoff in this case)<br>def linear_backoff(attempt):<br>    return attempt * 2  # Wait 2 seconds more for each retry<br><br># Initialize a logger with JSON format for ElasticSearch<br>logger = CustomLogger(format=&#39;json&#39;, extra_fields={&#39;service&#39;: &#39;api-service&#39;})<br><br># metrics = PrometheusMetrics(registry=your_registry)<br>metrics = PrometheusMetrics()<br><br># Configure the circuit breaker with retries and custom backoff<br>circuit_config = {<br>    &quot;fail_max&quot;: 3,<br>    &quot;reset_timeout&quot;: 60,<br>    &quot;backoff_strategy&quot;: linear_backoff<br>}<br><br># Create the fetcher instance<br>fetcher = Fetcher(<br>    label=&quot;api-service&quot;, <br>    logger=logger, <br>    metrics=metrics, <br>    circuit_config=circuit_config, <br>    max_retries=5<br>)<br><br># Make a GET request with retries, logging, and context preservation<br>try:<br>    response = fetcher.get(&quot;http://localhost:8080/api/example&quot;, timeout=10, headers={&quot;Authorization&quot;: &quot;Bearer token&quot;})<br>    print(response.json())<br>except Exception as e:<br>    logger.error(f&quot;Error during fetch: {e}&quot;)</pre><p>In this example:</p><ul><li>We’ve set up a <strong>linear backoff strategy</strong>, meaning each retry waits 2 seconds longer than the last.</li><li>The request is logged in <strong>JSON format</strong>, making it easy to integrate with ElasticSearch or similar systems.</li><li>The same <strong>circuit breaker</strong> is shared across API calls by reusing the same <strong>label</strong>.</li><li>The <strong>context of the incoming request</strong> is preserved across all outgoing requests and logged.</li></ul><h4>The Fetchin Edge: Why You Should Use It</h4><p>Fetchin isn’t just another HTTP client library. It’s a powerful tool that makes dealing with API calls easy and efficient, all while giving you the visibility and resilience your system needs. Here’s why Fetchin stands out:</p><ul><li><strong>Context-Aware Logging</strong>: Fetchin logs the context of the incoming request across all downstream API calls, making it easy to trace requests through your system.</li><li><strong>Shared Circuit Breakers</strong>: By using the same label, Fetchin guarantees that all API calls to the same service share the same circuit breaker, preventing overwhelming failing services with duplicate calls.</li><li><strong>No Lock-In</strong>: Whether you use Prometheus, ElasticSearch, or something else, Fetchin integrates seamlessly without locking you into any one tool.</li><li><strong>Customizable and Extensible</strong>: From backoff strategies to metrics systems, Fetchin gives you the flexibility to tailor its behavior to your needs.</li><li><strong>Lightweight Monitoring</strong>: Fetchin brings the essential observability features you need for monitoring HTTP requests — without the complexity or cost of full-blown APM tools.</li><li><strong>No Duplication of Metrics</strong>: Already using <strong>Datadog</strong> or <strong>New Relic</strong>? No problem! Just disable Fetchin’s metrics and enjoy the benefits of resilient API calls with context-aware logging and circuit breakers.</li></ul><h4>Final Thoughts: Get Started with Fetchin Today</h4><p>Fetchin is a great choice for developers who want an easy, flexible, and powerful way to manage HTTP requests without all the baggage of traditional APM solutions. It gives you the control you need, the visibility you want, and the reliability you can count on — all in a lightweight package.</p><p>You can find <strong>Fetchin</strong> on:</p><ul><li><strong>GitHub</strong>: <a href="https://github.com/romulofreires1/fetchin">https://github.com/romulofreires1/fetchin</a></li><li><strong>PyPI</strong>: <a href="https://pypi.org/project/fetchin/">https://pypi.org/project/fetchin/</a></li></ul><p>Ready to make your API calls resilient and visible? Give Fetchin a try today and start building better APIs!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=f6d31c78b2d5" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>