• About Blog

    What's Blog?

    A blog is a discussion or informational website published on the World Wide Web consisting of discrete, often informal diary-style text entries or posts.

  • About Cauvery Calling

    Cauvery Calling. Action Now!

    Cauvery Calling is a first of its kind campaign, setting the standard for how India’s rivers – the country’s lifelines – can be revitalized.

  • About Quinbay Publications

    Quinbay Publication

    We follow our passion for digital innovation. Our high performing team comprising of talented and committed engineers are building the future of business tech.

Wednesday, July 2, 2025

How to Build an AI Stock Analyst Agent with Python and CrewAI

For Python developers looking to leverage the power of Large Language Models (LLMs) for complex, multi-step tasks, crewAI offers a robust framework for orchestrating autonomous AI agents. This guide provides a strictly technical, step-by-step walkthrough for building a financial analysis "crew" that researches a stock and provides a recommendation.

This system will use two specialized agents:

  1. Fundamental Analyst Agent: Gathers the latest news and essential financial data for a given stock.
  2. Technical Analyst Agent: Consumes the fundamental data, performs a technical analysis, and delivers a final buy, sell, or hold recommendation.

We will use a free, high-speed LLM from Groq and create a custom tool for the technical analysis, providing a practical, real-world example of crewAI's capabilities.

Core CrewAI Concepts

Before writing the code, it's essential to understand the primary components of the framework:

  • Agents: These are the AI workers. Each agent is configured with a role, a goal, and a backstory to define its area of expertise and operational context. They are also equipped with an llm and a set of tools to perform their functions.

  • Tools: These extend an agent's abilities beyond the LLM's inherent knowledge. A tool can be anything from a web search function to a custom Python function that interacts with a database or API.

  • Tasks: A task is a single, well-defined unit of work assigned to an agent. It includes a description of what needs to be done and an expected_output format. Crucially, tasks can be chained together using the context parameter, which passes the output of one or more preceding tasks to the current one.

  • Crew: A crew is the collaborative unit that brings together agents and tasks. It defines the process by which tasks will be executed, such as Process.sequential, where tasks are completed one after another in a defined order.

1. Prerequisites and Environment Setup

First, ensure you have Python installed. Then, install the necessary libraries for this project.

pip install crewai crewai-tools langchain-groq yfinance pandas pandas-ta

  • crewai & crewai-tools: The core framework and its standard tools.
  • langchain-groq: Allows integration with the fast, free-tier LLMs provided by Groq.
  • yfinance: A popular library for fetching historical stock market data from Yahoo Finance.
  • pandas & pandas-ta: For data manipulation and applying technical analysis indicators.

Next, you need to acquire API keys from Groq and Serper for LLM access and web search capabilities, respectively. Create a .env file in your project's root directory to store these keys securely.

GROQ_API_KEY="your-groq-api-key"
SERPER_API_KEY="your-serper-api-key"

2. Defining a Custom Technical Analysis Tool

While crewAI provides built-in tools like web search, its real power is unlocked with custom tools. We will create a tool that fetches historical stock data, calculates key technical indicators, and returns an analysis.

Create a file named stock_tools.py

# stock_tools.py
from crewai_tools import BaseTool
import yfinance as yf
import pandas_ta as ta

class StockTechnicalAnalysisTool(BaseTool):

    name: str = "Stock Technical Analysis Tool"
    description: str = (
        "This tool performs technical analysis on a stock's historical data. "
        "It fetches price data, calculates RSI, MACD, and moving averages, "
        "and provides a summary of these technical indicators."
    )

    def _run(self, ticker: str) -> str:
        try:
            # Fetch historical data for the last 6 months
            stock_data = yf.Ticker(ticker).history(period="6mo")

            if stock_data.empty:
                return f"Error: No data found for ticker {ticker}."

            # Calculate Technical Indicators using pandas_ta
            stock_data.ta.rsi(append=True)
            stock_data.ta.macd(append=True)
            stock_data.ta.sma(length=20, append=True)
            stock_data.ta.sma(length=50, append=True)

            # Get the most recent data
            latest_data = stock_data.iloc[-1]            

            # Create a summary string
            analysis_summary = (
                f"Technical Analysis for {ticker}:\n"
                f"Latest Close Price: {latest_data['Close']:.2f}\n"
                f"RSI (14): {latest_data['RSI_14']:.2f}\n"
                f"SMA (20): {latest_data['SMA_20']:.2f}\n"
                f"SMA (50): {latest_data['SMA_50']:.2f}\n"
                f"MACD: {latest_data['MACD_12_26_9']:.2f} | Signal: {latest_data['MACDs_12_26_9']:.2f}"
            )

            return analysis_summary

        except Exception as e:
            return f"An error occurred: {str(e)}"

This class inherits from BaseTool and implements the _run method, which contains the logic for fetching data and performing calculations.

3. Assembling the crewAI Script

Now, create your main Python file (e.g., main.py) to define and run the crew.

Step 3.1: Imports and Initialization

Load the environment variables and initialize the LLM and tools.

# main.py
import os
from dotenv import load_dotenv
from crewai import Agent, Task, Crew, Process
from crewai_tools import SerperDevTool
from langchain_groq import ChatGroq

# Import our custom tool
from stock_tools import StockTechnicalAnalysisTool

# Load environment variables from .env file
load_dotenv()

# Initialize the LLM (Groq's Llama3)
# Set temperature to 0 for deterministic, fact-based outputs
llm = ChatGroq(
    api_key=os.getenv("GROQ_API_KEY"),
    model="llama3-8b-8192",
    temperature=0.2
)

# Initialize the tools
search_tool = SerperDevTool()
technical_analysis_tool = StockTechnicalAnalysisTool()

Step 3.2: Defining the Agents

Create the two agents, assigning them roles, goals, tools, and the LLM. Setting verbose=True is highly recommended during development to see the agent's reasoning process.

# Agent 1: Fundamental Analyst
fundamental_analyst = Agent(
    role="Fundamental Stock Analyst",
    goal="Gather, analyze, and summarize the latest news and fundamental financial data for a given stock.",
    backstory=(
        "You are an expert in financial markets, skilled at sifting through news articles, "
        "earnings reports, and market announcements to find key information that impacts a stoc,k's value. "
        "Your analysis is purely factual and data-driven."
    ),
    verbose=True,
    allow_delegation=False,
    tools=[search_tool],
    llm=llm
)

# Agent 2: Technical Analyst
technical_analyst = Agent(
    role="Senior Technical Stock Analyst",
    goal="Perform a detailed technical analysis using stock data and indicators, then synthesize all information to provide a clear investment recommendation.",
    backstory=(
        "You are a master of technical analysis, interpreting charts and indicators to predict market movements. "
        "You take fundamental context and combine it with your technical findings to form a holistic view. "
        "Your final output is always a direct and actionable recommendation."
    ),
    verbose=True,
    allow_delegation=False,
    tools=[technical_analysis_tool],
    llm=llm
)

Step 3.3: Defining the Tasks

Create the tasks for each agent. The context in analysis_task is the key to chaining them; it ensures the technical_analyst receives the fundamental_analyst's report.

# Task 1: Fundamental Research

fundamental_research_task = Task(
    description="
        For the stock ticker {stock}, conduct a thorough fundamental analysis.
        Search for the latest news, recent earnings reports, and any major announcements.
        Summarize the key findings in a structured, easy-to-read format.",
    expected_output="A summarized report of the latest news and fundamental data for the stock.",
    agent=fundamental_analyst
)

# Task 2: Technical Analysis and Recommendation

technical_analysis_task = Task(
    description="
        Using the provided fundamental analysis report for {stock}, perform a technical analysis.
        Use your tool to get the latest technical indicators (RSI, MACD, SMAs).
        Synthesize both the fundamental and technical data to provide a final investment recommendation.    ",
    expected_output="A one-paragraph summary of the technical analysis, followed by a final,
        bolded verdict: **BUY**, **SELL**, or **HOLD**.",
    agent=technical_analyst,
    context=[fundamental_research_task] # Pass the output of the first task
)

Step 3.4: Creating and Running the Crew

Finally, assemble the Crew and kick off the process. The process is set to sequential to ensure the research happens before the analysis.

# Assemble the crew

stock_analysis_crew = Crew(
    agents=[fundamental_analyst, technical_analyst],
    tasks=[fundamental_research_task, technical_analysis_task],
    process=Process.sequential,
    verbose=2 # 'verbose=2' provides detailed logs of the crew's execution
)

# Execute the crew for a specific stock
inputs = {'stock': 'RPOWER'} # Example: Reliance Power
result = stock_analysis_crew.kickoff(inputs=inputs)

print("\n\n########################")
print("## Final Stock Analysis Report")
print("########################\n")
print(result)


Conclusion

This guide demonstrates how to construct a multi-agent system using crewAI for a practical, technical task. By defining specialized agents, creating custom tools for specific functionalities (StockTechnicalAnalysisTool), and chaining tasks sequentially, you can automate complex workflows that require both data gathering and analytical reasoning. The modularity of this framework allows for easy extension—you could add a portfolio management agent, a risk assessment agent, or even integrate with trading APIs to create a fully autonomous financial analysis and execution system.

⚠️ Disclaimer:

This AI agent is intended for educational and informational purposes only. Do not use this system to make real-world trading decisions or investments. Always consult with a certified financial professional before making any trades. Use at your own risk.

Wednesday, June 18, 2025

Let's Build a Storyteller with Spring AI

Photo Courtesy DevDocsMaster

Remember those childhood summer holidays? After a long day of cricket in the neighbourhood lane, we’d all gather around, and there was always someone—a grandparent, an uncle, or an older cousin—who was the master storyteller.

I still remember my Mom, she could spin up the most fascinating tales out of thin air. Stories of a clever fox who outsmarted a lion, a tiny sparrow on a big adventure, or a king who learned a lesson from a poor farmer. We’d listen, completely captivated, our imaginations painting vivid pictures. Those simple stories were a magical part of growing up.

Now, as developers, what if we could bring a slice of that magic into our digital world? How about we build our very own storyteller? An application where you give it a tiny spark of an idea—say, "a curious robot who discovers desi chai"—and it instantly writes a wonderful short story for you.

Sounds like a fun project, right? But my mind immediately jumps to the challenges. Figuring out complex AI libraries, handling API calls, all that backend hassle… it seems like it would take all the fun out of it. How can we build something so creative using our solid, reliable Java and Spring Boot?

Well, this is where the story gets really interesting for us. It turns out, the brilliant minds at Spring have already thought about this. And their answer is Spring AI.

So, What’s All This Hungama About Spring AI?

Think of Spring AI as a friendly bridge. On one side, you have your solid, dependable Spring Boot application. On the other, you have the incredible power of AI models like OpenAI's GPT, Google's Gemini, and others. Spring AI connects these two worlds so seamlessly that you'll wonder why you ever thought AI was difficult.

In simple terms, it takes away all the boilerplate code and complex configurations. You don't have to manually handle HTTP requests to AI services or parse messy JSON responses. Spring AI gives you a clean, straightforward way to talk to AI, just like you would talk to any other service in your Spring application.

Let's Build Something! Your First AI-Powered Spring Boot App

Enough talk, let’s get our hands dirty. Let's build our little "Story Generator." You need to give a simple idea, and it cooks up a short story for you.

We'll be building this faster than it takes to get your food delivery on a Friday night.

Step 1: The Foundation - Setting Up Your Project

First things first, we need a basic Spring Boot project. The easiest way is to use the Spring Initializr. It’s our go-to starting point for any new Spring project.

  1. Head over to start.spring.io.
  2. Choose Maven as the project type and Java as the language.
  3. Select a recent stable version of Spring Boot (3.2.x or higher is good).
  4. Give your project a name, something like ai-story-generator.
  5. Now, for the important part – the dependencies. Add the following:
    • Spring Web: Because we want to create a REST endpoint.
    • Spring Boot Actuator: Good practice to monitor our app.
    • OpenAI: This is the Spring AI magic wand we need. Just type "OpenAI" and add the dependency.

Once you’re done, click "Generate". A zip file will be downloaded. Unzip it and open the project in your favourite IDE (IntelliJ or VS Code, your choice!).

Step 2: The Secret Ingredient - Your API Key

To talk to an AI model like OpenAI's, you need an API key. It's like a secret password.

  1. Go to the OpenAI Platform and create an account.
  2. Navigate to the API Keys section and create a new secret key.
  3. Important: Copy this key immediately and save it somewhere safe. You won’t be able to see it again!

Now, open the src/main/resources/application.properties file in your project and add this line:

spring.ai.openai.api-key=YOUR_OPENAI_API_KEY_HERE

Step 3: Writing the Code - Where the Magic Happens

This is the best part. You'll be surprised at how little code we need to write.

Let's create a simple REST controller. Create a new Java class called StoryController.java.

package com.bhargav.ai.storygenerator;

import org.springframework.ai.chat.client.ChatClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class StoryController {

    private final ChatClient chatClient;

    public StoryController(ChatClient.Builder chatClientBuilder) {
        this.chatClient = chatClientBuilder.build();
    }

    @GetMapping("/story")
    public String generateStory(@RequestParam(value = "topic",
            defaultValue = "a curious robot who discovered desi chai") String topic) {
        return this.chatClient.prompt()
                .user("Tell me a short story about " + topic)
                .call()
                .content();
    }
}

Let's break down this simple code, shall we?

  • @RestController: This tells Spring that this class will handle web requests.
  • private final ChatClient chatClient;: This is the hero of our story! The ChatClient is a part of Spring AI that makes talking to the AI model incredibly easy. We inject it using the constructor. Spring Boot automatically configures it for us because we added the OpenAI dependency and the API key. No manual setup needed. Kitna aasan hai! (How easy is that!)
  • @GetMapping("/story"): This creates a web endpoint. You can access it at http://localhost:8080/story.
  • The generateStory method is where the action is.
  • chatClient.prompt(): We start building our request to the AI.
  • .user("Tell me a short story about " + topic): We are telling the AI what to do. This is our "prompt." We take a topic from the user's request.
  • .call(): This sends our request to the AI model.
  • .content(): This gets the text response back from the AI.

And that’s it! We’re done. Seriously.

Step 4: Run the Application!

Now, just run your Spring Boot application from your IDE. Once it starts up, open your web browser and go to:

http://localhost:8080/story

You should see a short story about a curious robot discovering chai.

Want to try another topic? Just add a topic parameter to the URL:

http://localhost:8080/story?topic=a cat who wanted to be a software engineer in Bengaluru

And watch as the AI instantly generates a new story for you.

What Did We Just Do?

Think about it. In just a few minutes, with a handful of dependencies and less than 20 lines of Java code, we built an AI-powered application. We didn't have to wrestle with HTTP clients, authentication headers, or complex JSON.

We just told Spring AI what we wanted, and it did the needful.

This is just the tip of the iceberg. Spring AI allows you to get structured output (like JSON objects), connect to your own data, and much more. It makes AI a first-class citizen in the Spring ecosystem.

So, the next time you feel that spark of a creative idea, don't think it's out of reach for a Java developer. With Spring AI in your toolkit, you're more than ready to build your own magic. Happy coding!


Wednesday, April 26, 2023

Benefits & Best Practices of Code Review

Photo by Bochelly

Code reviews are methodical assessments of code designed to identify bugs, increase code quality, and help developers learn the source code. Developing a strong code review process, or utilizing version control, sets a foundation for continuous improvement and prevents unstable code from shipping to customers.

Software developers should be encouraged to have their code reviewed as soon as they’ve completed coding to get a second opinion on the solution and implementation. The reviewer can also act as a second step in identifying bugs, logic problems, or uncovered edge cases. Reviewers can be from any team or group as long as they’re a domain expert. If the lines of code cover more than one domain, we should have experts from both domains.


Benefits of Code Review

Knowledge Sharing: 

When software developers review code as soon as a team member makes changes, they can learn new techniques and solutions. Code reviews help junior developers learn from more senior team members, similar to how peer programming effectively helps developers share skills and ideas. By spreading knowledge across the organization, code reviews ensure that no person is a single point of failure. Everyone has the ability to review and offer feedback. Shared knowledge also helps team members take vacation, because everyone on the team has background knowledge on a topic.

Discover Bugs: 

Rather than discovering bugs after a feature has been shipped and scrambling to release a patch, developers can immediately find and fix problems before customers ever see them. Moving the review process earlier in the software development lifecycle through unit tests helps developers work on fixes with fresh knowledge. When waiting until the end of the lifecycle to do a review, developers often struggle to remember code, solutions, and reasoning. Static analysis is a cheap, efficient way to meet business and customer value.

Maintain Compliance: 

Developers have various backgrounds and training that influence their coding styles. If teams want to have a standard coding style, code reviews help everyone adhere to the same standards. This is especially important for open source projects that have multiple individuals contributing code. Peer reviews bring in maintainers to assess the code before pushing changes.

Enhance Security: 

Application security is an integral part in software development, and code reviews help ensure compliance. Security team members can review code for vulnerabilities and alert developers to the threat or even setup the quality gates in static code analysis to make sure they are identified well ahead. If your application is dealing with sensitive information then team should be trained on secure coding practices.

Increase Collaboration: 

When team members work together to create a solution, they feel more ownership of their work and a stronger sense of belonging. Authors and reviewers can work together to find the most effective solutions to meet customer needs. It’s important to strengthen collaboration across the software development lifecycle to prevent information silos and maintain a seamless workflow between teams. To successfully conduct code reviews, it’s important that developers build a code review mindset that has a strong foundation in collaborative development.


Best Practices

What to look for during the code review

It’s important to go into reviews knowing what to look for in a code review. Look for key things like code structure, style, logic. performance, test coverage, code readability and maintainability.

You can do automated checks (e.g., static analysis of the code) for some of the things like structure, style, standards and logic. But others areas like design and functionality, requires a human reviewer to evaluate as we don't have any tools for the same.

Reviewing code with certain questions in mind can help you focus on the right things. For instance, you might evaluate code to answer:

  • Do I understand what the code does? 
  • Does the code function as per the requirements? 
  • Does this code has been written as per the company standards requirements?

Build and Test — Before Code Review

In today’s time, we have Continuous Integration setup as part of the process. It’s key to build and test before doing a manual review. Ideally, the code review should be done after tests have passed. This ensures stability and doing automated checks first will cut down on errors and save time in the review process.

Limit Review Time for 45-60 Minutes

Never review for longer than 45 - 60 minutes at a time. Performance and attention-to-detail tend to drop off after that point. It’s best to conduct code reviews often (and in short sessions). Taking a break will give your brain a chance to reset. So, you can review it again with fresh eyes.

Review 300 Lines at a Time

If you try to review too many lines of code at once, you’re less likely to find defects. Try to keep each code review session to 300 lines or less. Setting a line-of-code (LOC) limit is important for the same reasons as setting a time limit. It ensures you are at your best when reviewing the code.

Give Feedback that Helps

Try to be constructive in your feedback, rather than critical. Be kind, explain your reasoning, balance giving explicit directions with just pointing out problems and letting the developer decide and encourage developers to simplify code or add code comments instead of just explaining the complexity to you.

Giving feedback in-person for the new members will help you communicate with the right tone as they will be new to the process.

Communicate Goals and Expectations

You should be clear on what are the goals of the review, as well as the expectations from reviewers. Giving your reviewers a checklist will ensure that the reviews are consistent. Engineers will evaluate each other’s code with the same criteria in mind.

By communicating goals and expectations, everyone saves time. Reviewers will know what to look for — and they’ll be able to use their time wisely in the review process.

Include Everyone in the Code Review Process

No matter how senior the engineer is, everyone needs to review and be reviewed. After all, everyone performs better when they know someone else will be looking at their work. When you’re running reviews, it’s best to include engineer and leads/architect. They’ll spot different issues in the code, in relation to both the broader codebase and the overall design of the product.

Including everyone in the review process improves collaboration and relationships between programmers.

Automate to Save Time

There are some things that reviewers will need to check in manual reviews. But there are some things that can be checked automatically using the right tools. Static code analyzers, for instance, find potential issues in code by checking it against coding rules. Running static analyzers over the code minimizes the number of issues that reach the peer review phase. Using tools for lightweight reviews can help, too.

By using automated tools, you can save time in peer review process. This frees up reviewers to focus on the issues that tools can’t find — like usability.

Conclusion

Code review is a critical process in software development that helps ensure the quality, reliability, and maintainability of the codebase. By following these best practices, your code review process can be an effective tool for ensuring that your codebase is high-quality and maintainable, while also promoting a positive and productive development culture.

I would like to thank my wonderful team members who bought the idea of why code review is important and helped me to build automation to save time for everyone. We automated to identify number of lines of code updated in a pull request. If the lines is above X number then it will reject the pull request by adding an appropriate message for the author. 

Thanks to the latest version of sonar which allows us to have a pull request based analysis that considers only the code that are added/updated. Automated the first round of the code review is done by a bot which pulls the data from the static analysis tool and highlights blockers, critical and major technical debts within the new code added.


Saturday, November 5, 2022

Understanding Spring AOP

Understanding Spring AOP
Photo by Glenn Carstens Peters

What is Spring AOP?

Spring AOP enables Aspect-Oriented Programming in spring applications. It provides the way to dynamically add the cross-cutting concerns(logging, security, transaction management, auditing, i18n etc.) before, after or around the actual logic using simple pluggable configurations. It makes easy to maintain code in the present and future as well. You can add/remove concerns without recompiling complete source code simply by changing configuration files (if you are applying aspects using XML configuration).


What is advice, joinpoint or pointcut?

  • An important term in AOP is advice. It is the action taken by an aspect at a particular join-point. 
  • Joinpoint is a point of execution of the program, such as the execution of a method or the handling of an exception. In Spring AOP, a joinpoint always represents a method execution.
  • Pointcut is a predicate or expression that matches join points.
  • Advice is associated with a pointcut expression and runs at any join point matched by the pointcut.
  • Spring uses the AspectJ pointcut expression language by default.


Pointcut

Pointcut determines the join point of interest and in the code it appears as pointcut expression. It works similarly to regular expressions, using the special syntax it matches methods with advices. Please note, that Spring AOP supports only those classes that are defined as Spring beans/component otherwise they won’t be available. Here is a pointcut expression general syntax (those parts that are in red are mandatory) with some examples:

execution(modifiers-pattern? ret-type-pattern declaring-type-pattern?name-pattern(param-pattern) throws-pattern?)

The following examples show some common pointcut expressions:

The execution of any public method, First * means that it will match any return type, and *(..) means that the expression will match any method, no matter how much arguments it contains.
    execution(public * *(..))

The execution of any method with a name that begins with set:
    execution(* set*(..))

The execution of any method defined by the OrderService interface/class:
    execution(* com.bhargav.service.AccountService.*(..))

The execution of any method defined in the service package or one of its sub-packages:
    execution(* com.bhargav.service..*.*(..))

This will be matched only for those methods in the DemoClass, which has int as a first parameter, return type as int and method should be public:
    execution(public int DemoClass.*(int, ..))

Any join point (method execution only in Spring AOP) within the service package:
    within(com.bhargav.service.*)

Advices

Spring AOP includes the following types of advice:

@Before: 
Advice that runs before a join point but that does not have the ability to prevent execution flow proceeding to the join point (unless it throws an exception).

@After: 
Advice to be run regardless of the means by which a join point exits (normal or exceptional return).

@AfterReturning: 
Advice to be run after a join point completes normally (for example, if a method returns without throwing an exception).

@AfterThrowing: 
Advice to be run if a method exits by throwing an exception.

@Around:
Advice that surrounds a join point such as a method invocation. This is the most powerful kind of advice. Around advice can perform custom behaviour before and after the method invocation. It is also responsible for choosing whether to proceed to the join point or to shortcut the advised method execution by returning its own return value or throwing an exception.


References:

Monday, July 25, 2022

Understand Code Refactoring Techniques to Improve your Code Quality

Code Refactoring Techniques
Photo by Danial Igdery

Now a days, agile teams are under tremendous pressure to write code faster with enhanced functionality in short time. There would be some or the other functionality added at the last moment or just before the release. As engineers are under pressure, the functionality gets implemented in a sloppy manner, which may technically work but may lead to dirty code.

Dirty code usually results from a developer’s inexperience, shortcuts taken to meet increasingly tight deadlines, poor management of the project, several different developers working on a project over time, or some combination of all of the above.

Bad code comes at a price, and writing good code isn’t that complicated. Let's understand what's code refactoring.

Code Refactoring

In computer programming and software design, code refactoring is the process of restructuring existing computer code—changing the factoring—without changing its external behavior. Refactoring is intended to improve the design, structure, and/or implementation of the software (its non-functional attributes), while preserving its functionality. 

Potential advantages of refactoring may include improved code readability and reduced complexity; these can improve the source code's maintainability and create a simpler, cleaner, or more expressive internal architecture or object model to improve extensibility. 

Another potential goal for refactoring is improved performance; software engineers face an ongoing challenge to write programs that perform faster or use less memory.

Code refactoring helps to change this dirty code into clean code and it helps to make it easier to extend the code and add new features easily in the future. Also, helps to improve the more objective attributes of code such as code length, code duplication, and coupling and cohesion, all of which correlate with ease of code maintenance and your code will use less memory and perform better and faster.

How to Perform Code Refactoring?

Now that you know the answer to the question of what is code refactoring, and you know many of its potential benefits, how exactly do you refactor code?

There are many approaches and techniques to refactor the code. Let’s discuss some popular ones.

Red-Green Refactor

Red-Green is the most popular and widely used code refactoring technique in the Agile software development process. This technique follows the test-first approach to design and implementation, this lays the foundation for all forms of refactoring.

Red - The first step starts with writing the failing red-test. You stop and check what needs to be developed.

Green - In the second step, you write the simplest enough code and get the development pass green testing.

Refactor - Find ways to improve the code and implement those improvements, without adding new functionality.

Refactoring by Abstraction

This technique is mostly used by developers when there is a need to do a large amount of refactoring. Mainly we use this technique to reduce the redundancy (duplication) in our code. This involves class inheritances, hierarchy, creating new classes and interfaces, extraction, replacing inheritance with the delegation, and vice versa.

Pull-Up Method - It pulls code parts into a superclass and helps in the elimination of code duplication.

Push-Down Method - It takes the code part from a superclass and moves it down into the subclasses.

Refactoring by abstraction allows you to make big changes to large chunks of code gradually. In this way, you can still release the system regularly, even with the change still in progress.

Composing Method

Code that is too long is difficult to understand and difficult to implement. The composing method is a code refactoring approach that helps to streamline code and remove any code duplications. This is done through extraction and inline techniques.

Extraction - We break the code into smaller chunks to find and extract fragmentation. After that, we create separate methods for these chunks, and then it is replaced with a call to this new method. Extraction involves class, interface, and local variables.

Inline - Refactoring also helps to create simpler, more streamlined code. It helps to remove unnecessary methods within the code and replaces them with the content of the method. After that, we delete the method from our program.

Simplifying Methods

As legacy code gets older and older, it tends to become more polluted and complex. In this sense, simplifying methods help to simplify the logic. These methods include adjusting the interaction between different classes, along with adding a new parameter or removing and replacing certain parameters with explicit methods.

Simplifying Conditional Expressions - Conditional statement in programming becomes more logical and complicated over time. You need to simplify the logic in your code to understand the whole program. There are so many ways to refactor the code and simplify the logic. Some of them are: consolidate conditional expression and duplicate conditional fragments, decompose conditional, replace conditional with polymorphism, remove control flag, replace nested conditional with guard clauses, etc.

Simplifying Method Calls - In this approach, we make method calls simpler and easier to understand. We work on the interaction between classes, and we simplify the interfaces for them. Examples are: adding, removing, and introducing new parameters, replacing the parameter with the explicit method and method call, parameterize method, making a separate query from modifier, preserve the whole object, remove setting method, etc.

Extract Method

The extract method is one of the techniques for code refactoring that helps to decrease complexity, while also increasing the overall readability of the code.

When you find that a class has so many responsibilities and too much thing is going on or when you find that a class is unnecessary and doing nothing in an application, you can move the code from this class to another class and remove it completely from the existing class. It involves the moving of a fragment or block of code from its existing method and into a newly created method, which is clearly named in order to explain its function.

Conclusion

Engineers are the only ones responsible for writing good and quality code. We should all make it a habit to write good code from the very beginning. Writing clean code isn’t complicated and doing so will help both you and your colleagues. A clean and well-organized code is always easy to change, easy to understand, and easy to maintain.

Clean Code: A Handbook of Agile Software Craftsmanship by Robert C. Martin

Even bad code can function. Every year, countless hours and significant resources are lost because of poorly written code but it doesn't have to be that way.

Refactoring: Improving the Design of Existing Code by Martin Fowler

Any fool can write code that a computer can understand. Good programmers write code that humans can understand.


Quick Tips

  • Always perform code refactoring in small chunks by making your code slightly better and leaves the application in a working state. Run jUnit tests after making small changes in the refactoring process. Without running these tests, you create a risk of introducing new bugs.

  • Do not create any new features or functionality during the refactoring process. You should refactor the code before adding any updates or new features into your existing code.

  • Refactoring process always results in complete regression, don't forget to involve your QA team in the process.

Featured Post

How to Build an AI Stock Analyst Agent with Python and CrewAI

For Python developers looking to leverage the power of Large Language Models (LLMs) for complex, multi-step tasks, crewAI offers a robust fr...