12 August 2024

Code Green - Sustainable Software Development - Blog Series part 1

Written by: Benja Jansen

Are you a “green” developer?

Have you ever noticed how much electricity your programs uses? Yeah, me neither. It’s not usually something that software engineers think about. If anything I’d say we actively avoid thinking about it. Let’s say you are asked to deploy a website, there is a frontend, a backend and a database. Would you choose the server options that exactly matches the requirements for the system you’ve built? Would you go a little over? If the option was available, would you take double what you believe the system would require?

According to the International Energy Agency, between 2015 and 2022, data centre workloads have increased by more than 340%. In turn, the energy usage from data centres between 2015 and 2022, increased by between 20% and 70%. This is depending on where you look and what exactly you are measuring, this measurement excludes crypto mining, where the energy usage for that same term, is estimated to have increased by between 2300% and 3500% (that’s not a typo, go check the link).

If we lived in a world where software engineers would attempt to write their code in such a way that it uses as few resources as possible while still meeting the minimum requirements in terms of expected performance, would the world be better for it? If we tried to optimize our code with the reduction of energy usage in mind, how much electricity could be saved?

Project Code Green

OpenValue decided to start a project to research if this is possible. We called the project “Code Green - Sustainable Software Development” This blog is part of a series of blogs that will be looking into whether it is possible to optimize programming projects, using AI, with the goal of reducing our energy footprint.

Whether it’s to save the planet or not, code optimization is a critical aspect of any programming project. Not only does it help our code to stay align with the design principals we value, but it also helps us and our fellow developers to grow in our skills. While AI tools have shown promise in assisting developers in this, their effectiveness can vary greatly depending on the specific task and the context provided.

Before we get there though, we have to answer a couple of questions. Firstly, what exactly is meant with “optimization”? Then, if we were to employ some of the AI tools that are out there right now, how would they fair in optimizing code? Would we need to modify them, tweak them to this specific purpose? How exactly are we going to go about this?

What does “optimization” really mean

Asking anyone to optimise your code is a very broad task to give. You’ll likely be met with questions such as: “optimise how” or “in what way”. By definition, code optimization is the process of transforming a piece of code to make it more efficient without changing its output or function. However, as anyone who has ever tried optimizing code would know, it’s more of an art form than an exact science.

Developers loves efficiency, but code needs to be maintainable, it needs to be scalable, this is what the SOLID principals are for. Even if the code is efficient but nobody wants to touch it, then it will eventually buckle under the test of time.

So where does AI fit into all of this? I’ve been working on getting various AI tools (Jetbrains AI Assistant, Tabnine’s Tabnine Pro, Claude 3.5 Sonnet through Anthropic’s API service, locally hosted deepseek-coder-v2) to do code optimizations on some old projects of mine. The results were not as good as I had hoped they would be. In 2 cases, the AI gave me back the exact same code I asked it to optimize, with no changes, just a description of what the code does.

The optimizations I’ve seen can be broken down into three categories.

Readability

I’m not against code that is easier to read, since that usually makes it easier to understand. If it takes a future developer less time to understand what some code does, it will give them more time to implement that change or add that feature in a manner that already falls in line with best practises because they had the time to do it. But the idea of ‘more readable’ brings with it the problem of opinion. Opinions are like noses…everybody has one, and they usually have some holes in them. I didn’t find the readability changes the AI made to my liking, but that’s just my opinion.

Which of the two is more readable?

Original Code AI suggestion

Usage

Some AI tools allows you to add several code-files to the context of the question. This means you can show the AI where the class is defined and how it gets used. From here, the AI can make suggestions to improve the usage of the class like moving a class field’s value assignment into a setter method instead of assigning the value through a constructor. The problem I had with these kind of changes is that they usually felt like a suggestion that could be done, but shouldn’t.

Original Code AI suggestion

Just because you can, doesn't mean you should

Efficiency

For SQL queries that dealt with large volumes of data, the AI was pretty good at suggesting changes that made a difference. Other than that though, most of the suggested changes were negligible in their effect. Moving where an instance of an object gets created, to the outside a loop instead of in it for example. The only times that code changes were on point, was when I first used monitoring and profiling tools to find out where the bottlenecks were, gave that information to the AI and explained exactly what I wanted the results of the change to be.

I feel this really is where having the AI make usable suggestions came in. If you give it broad questions, you’ll get broad answers. But with proper prompting and context about the problem you are trying to solve, you can get some decent results.

Prompt Engineering

Prompt engineering

Prompt engineering is a technique used to optimize the performance of AI models by carefully crafting the input instructions. It involves understanding the limitations of the AI model, as well as the specific requirements of the task at hand.

In essence, it’s somewhat similar to a conversation with a specialist in some field. You want to know where the limits of their knowledge is, and you want to make sure they fully understand exactly what you are saying, in a way that leaves no room for ambiguity. This is not something that is going to occur in a single ask or question, you’ll obtain this information through a conversation.

The steps to this can be broken down as follows:

1. Understand the task

You need to understand what exactly what the task is that you need to perform. If you ask a broad question like: “Can you optimize the code in this project”, you’ll not receive a very precise answer in most cases. This is where you need knowledge of the bottlenecks in your project, it will help you to understand exactly which parts of the project needs to be optimized.

2. Identify the AI’s capabilities

The limitations were a real issue for me at first since the versions of the libraries in my projects are all as recent as I can keep them, which in many cases meant that the answers I was getting, were for what would have worked if I was making use of older versions of the libraries. It’s not uncommon for an AI’s system knowledge to be 2 years out of date. What helped me to figure out the limitations of the AI’s I was using, was by asking questions I knew the answers to.

asked in 07-2024

Asked in June 2024, when the latest LTS version was 22

3. Formulating the prompt

There are many guids to prompt techniques such as Zero-Shot, One-Shot or Few-Shot. I found that these don’t always work for programming problems or code-optimization. What worked very well was providing the necessary context. I did this by providing the class where the change should happen, pointing out which method was causing the delay, providing the AI with information about the delay (data from profiling or monitoring) and then asking if a different approach might be possible that would result in better performance.

4. Testing and refining the prompt

Code optimizing isn’t an “ask once, get all the answers” kind of process. Most of the time, the answers I got would require design changes or implement approaches that makes use of older versions of the libraries I was using. In these cases it was only through a couple of back and forth prompting that I was able to get to an answer that I could actually use. Remember that most of all interfaces with AI allows you to start a new conversation, it’s a good idea to start a new one, copy and paste your original question but just change a few things before sending it. This way you’ll find an approach that works best for you.

5. Incorporating feedback

Sometimes the AI will hallucinate (give you false information), this could be because you asked wrong or because it just doesn’t know. If what was suggested doesn’t work, validate the feedback you got from the AI with other sources or colleagues. While I was in the process, this was very frustrating, but looking back these were the times when I got new insights to how the technologies I was using worked. Those insights were invaluable in the process of code optimizing.

6. Iterating and refining

Using the insights and the previous chats, it felt like I was always able to find another way to improve the code. As long as you have time, keep pushing, keep refining your prompt base. In this manner you’ll find a formula that works for you.

Wrong formula

While AI can provide valuable insights and suggestions, it’s essential to remember that they are not infallible. They should be used as a tool, and not as a replacement for human judgment and expertise. Always validate the suggestions made by an AI before implementing them, be sure to test what you decided to use.

Practice makes perfect, this is true for prompt engineering as well, the more you work with it, the better you’ll be at it.

In my next blog about Project Code Green I’ll be exploring how exactly AI works, for this we’ll be covering topics such as Generative Pretrained Transformer (GPT, what exactly is it made of), Vectors (how AI’s use them to mimic intelligence) and the Transformer architecture (the core of what makes AI as we know it today, work).

The Project “Code Green - Sustainable Software Development” is supported by the SIDN Fonds. Read more about SIDN Fonds and Project Code Green at www.sidnfonds.nl/projecten/project-code-groen


Benja Jansen

Benja Jansen has more than 11 years of experience as a software developer. During this time, he worked in multiple domains and has gained extensive experience in the banking Sector, Fleet Management, Courier services and Warehouse Management. His career started in 2012 when he programmed point-of-sale devices in C. However, driven by his passion for all things programming have seen him working in a large variety of languages such as; Java, Java Spring Boot, React and Redux, Angular, PHP and Xamarin. His hobbies in the last decade have included Skydiving, ToastMasters and more. He is a fun-loving person who is easy to talk to.