kloia Blog

Generative AI in Action

Written by Orçun Hanay | Dec 16, 2024 1:13:13 PM

In today’s rapidly evolving technological landscape, modernization is no longer optional. For businesses to remain competitive, they must embrace scalable, maintainable systems while transitioning away from outdated frameworks. At Kloia, we undertook a Proof of Concept (PoC) to explore the capabilities of Amazon Q Developer, a Generative AI-powered tool, in transforming a legacy application. The results were both promising and eye-opening, demonstrating the immense potential of AI in software modernization while highlighting areas where improvement is needed.

Modernization: The Bridge to Future-Ready Systems

Legacy systems, often foundational to business operations, pose challenges in agility and scalability. This PoC focused on transitioning an application from the legacy.NET Framework to a modern stack, specifically.NET 8, while improving maintainability, testability, and overall performance.

Amazon Q Developer in Action: The PoC Breakdown

Amazon Q Developer’s features—porting, refactoring, explaining, fixing, and optimizing—were tested on key components of the legacy system. Let’s examine the results.

1. Porting: Bridging the Legacy Gap

One of the primary objectives was to convert a .NET Framework 4.7.2 library to .NET 8. Amazon Q successfully handled the conversion, ensuring the library could be built and function in the new environment.

However, we found that Amazon Q lacked support for.NET Standard—a critical framework for maintaining compatibility with legacy systems. Modernization projects often require such intermediary steps to facilitate a gradual transition.

Amazon Q handled the transition smoothly but would benefit from broader compatibility options.

2. Refactoring: A Leap Toward Cleaner Code

The refactoring capabilities of Amazon Q stood out in this PoC. It successfully decomposed complex, monolithic methods into smaller, maintainable units. For instance, a background job responsible for importing files was rewritten with proper separation of concerns.

Code Snippet: Extracted Error Handling Logic

private void HandleError(FileImportParameters jobParameters, Exception exception, LineCounter counter)
{
    using (UnitOfWork.Start(UnitOfWorkNestingOption.CreateNewOrNestUnitOfWork))
    {
        With.Transaction(() =>
        {
            var header = fileHeaderRepository.Get(jobParameters.FileImportHeaderId);
            UpdateHeaderWithError(header, exception, counter);
            fileHeaderRepository.Update(header);
        });
    }
}
 
private void UpdateHeaderWithError(FileHeader header, Exception exception, LineCounter counter)
{
    header.Status = FileHeaderStatusOptions.Failed;
    header.Message = "Unexpected error importing file";
    header.SystemMessage = FormatErrorMessage(counter.LineNumber, exception);
}
 
private string FormatErrorMessage(int lineNumber, Exception exception)
{
    var message = $"Line {lineNumber}-{exception.Message}-{exception.StackTrace}";
    if (exception.InnerException != null)
    {
        message += $"{Environment.NewLine}{Environment.NewLine}InnerException - {exception.InnerException.Message}-{exception.InnerException.StackTrace}";
    }
    return message;
}
Breaking down large methods into focused units not only improved readability but also enhanced testability, making the code more resilient to future changes.

3. Explaining Complex Logic

Generative AI’s ability to explain intricate code was another area where Amazon Q shined. It detailed the logic of a multi-threaded installation function, showcasing how it preserved important contexts like security and logging.

Code Snippet: Multi-Threaded Installation with Contextual Preservation

Parallel.ForEach(apps, (app, loopstate) =>
{
    Commons.Threading.With.IOThread(data, culture, principal, log4NetProperties, httpContext,
        () => Setup(app, iOAdminEmail, address));
});

Explanation by Amazon Q:

  • Data RetrievalCaptures application data, including host information and email settings.
  • Context CapturePreserves security principles, culture settings, and logging configurations.
  • Parallel ProcessingUtilizes Parallel.ForEach for concurrent execution of application installations.

This level of detail made onboarding developers easier and facilitated a better understanding of legacy code.

4. Fixing Inefficiencies

Amazon Q also demonstrated its capability to identify and resolve inefficiencies. For example, it improved debugging logic by replacing verbose string concatenations with StringBuilder, optimizing resource management.

Code Snippet: Optimized Debugging Logic

catch (Exception ex)
{
    logger.Error(ex.Message, ex);
 
    using (UnitOfWork.Start(UnitOfWorkNestingOption.CreateNewOrNestUnitOfWork))
    {
        With.Transaction(() =>
        {
            var header = fileHeaderRepository.Get(jobParameters.FileHeaderId);
            header.Status = FileHeaderStatusOptions.Failed;
            header.SystemMessage = new StringBuilder()
                .AppendFormat("Line {0}-{1}-{2}", counter.LineNumber, exception.Message, exception.StackTrace)
                .AppendLine()
                .AppendFormat("InnerException - {0}-{1}", exception.InnerException?.Message, exception.InnerException?.StackTrace)
                .ToString();
            fileHeaderRepository.Update(header);
        });
    }
    throw;
}

This reduced memory consumption and improved error reporting.

Optimization: Maximizing Performance

Lastly, Amazon Q’s optimization feature addressed performance bottlenecks. By introducing better resource management techniques, it ensured smoother execution of file processing tasks.

Code Snippet: Enhanced File Processing Logic

private void ProcessFileRows(
    IFileHandler handler,
    FileParameters jobParameters,
    IFileParser parser,
    FileItemBulkInsertRepository itemBulkRepository,
    LineCounter counter,
    FiletHeader header)
{
    var dataTable = itemBulkRepository.CreateEmptyFileItemDataTable();
 
    while (handler.MoveNext())
    {
        ProcessSingleRow(handler, jobParameters, parser, itemBulkRepository, counter, dataTable);
 
        if (counter.IsBatchFull())
        {
            SaveAndResetBatch(itemBulkRepository, ref dataTable, counter);
        }
    }
    HandleRemainingRows(itemBulkRepository, dataTable, counter);
    Finalize(header, counter, jobParameters, parser);
}
This modular approach streamlined row-by-row processing, making the system more efficient and easier to debug.

Challenges Faced

Despite its strengths, the PoC revealed key challenges:

  • Performance LimitationsLarger codebases caused high memory usage and stalled processes.
  • Compatibility GapsLack of support for .NET Standard restricted its use in scenarios requiring legacy compatibility.
  • Regulatory ComplianceSecurity and privacy policies are needed to address industry-specific requirements.

Recommendations for Amazon Q Developer

To make Amazon Q more robust for enterprise use:

  • Expand CompatibilityInclude support for .NET Standard to facilitate gradual modernization.
  • Enhance ScalabilityOptimize memory usage to handle larger projects seamlessly.
  • Transparent PoliciesProvide clear security and privacy guidelines tailored for regulated industries.

Conclusion: Unlocking Modernization with Generative AI

The PoC with Amazon Q Developer showcased the potential of AI in transforming legacy systems. While it excelled in automating repetitive tasks and offering actionable insights, addressing its limitations will unlock even greater value.

At Kloia, we see tools like Amazon Q as the future of software modernization—empowering businesses to innovate faster and stay competitive in an ever-changing world.

What’s your experience with AI-driven tools in modernization? Share your thoughts below!

Learn more about our modernization strategies at kloia.