Green coding - VictoriaMetrics: The efficiency vs complexity trade-off 

This is a guest post written for the Computer Weekly Developer Network by Roman Khavronenko in his position as co-founder at VictoriaMetrics, a company known for its time series database technology and high performance monitoring solutions.

VictoriaMetrics’ openly states that its business objective has always been efficiency – and says that from this efficiency comes consistently sustainable standards and soutions.

Khavronenko writes in full as follows…

Fueled by concerns around climate change, the concept of green coding has become a hot topic in software development. While minimising energy consumption and environmental impact through code is important, achieving this requires a delicate balancing act between efficiency, functionality and simplicity.

One of the biggest culprits behind writing inefficient code is a lack of focus during development. Put simply, code gets bloated if the organisation doesn’t prioritise efficiency. When developers prioritise speed to market over efficiency, intricate and convoluted code structures can emerge. These structures are difficult to understand and modify, making it challenging to optimise later.

Inefficient code is often a symptom of the overuse of external libraries. 

While libraries offer pre-built functionality all in one place, they often come with extra features not required for the specific task in hand. Including unnecessary functionalities increases the code’s size and complexity, ultimately leading to wasted resources.

Library laziness?

Finding the right balance between efficiency and practicality is key. While completely rewriting every function from scratch isn’t ideal, blindly relying on bloated libraries can be equally detrimental. A good approach is to carefully evaluate libraries and incorporate only the necessary functionalities. This can significantly reduce the code size, whilst improving efficiency and simplicity. It can also provide more flexibility, as it’s far easier to modify your own code instead of fighting with the code of the library.

At this point we can remind ourselves of a core home truth i.e. just because it’s easy, doesn’t mean it’s good.

Language choice

Another significant factor impacting coding efficiency is language choice. 

While Python is praised for its readability, it’s not generally considered an efficient language due to obstructions. For example, unlike compiled languages like C++, Python is interpreted. This means the code is translated line by line during runtime, which can be slower than pre-compiled code that runs directly on the machine.

Python is also dynamically typed, so while this makes it flexible and easier to learn, it can lead to inefficiencies. The interpreter needs to perform checks at runtime to ensure data types are compatible, adding an extra step compared to statically typed languages where type mismatches are caught during compilation.

In contrast, C++ is known for its efficiency because the code can be translated very effectively into instructions a computer understands directly. This closeness to the machine translates to faster execution speeds. However, the trade-off between efficiency and ease of use is equally important – and Go, a compiled, statically typed language attempts to bridge this gap. 

Khavronenko: Just because it’s easy, doesn’t mean it’s good.

One of the features Go boasts is that it provides execution performance comparable to C++, but is easier to use for everyday work.

Gen-AI realities

So remember, if you solely rely on generative AI to write code, you’re not a software engineer.

The emergence of generative AI tools has sparked discussions about their potential impact on code efficiency. Technological evolution is imperative, but while these tools can generate code snippets, they lack the deep understanding and logic required to write truly efficient and optimised code.

In the hands of an inexperienced developer, AI-generated code can be riddled with inefficiencies. If an engineer asks generative AI to write code, it means they don’t fully understand what they need or how it works. Ultimately, if you can’t express your thoughts in code and need to ask a Gen AI platform to do it for you, perhaps you shouldn’t write code.

However, this isn’t to say generative AI is inherently bad.

As previously mentioned, technical evolution is positive and generative AI can act like a smart autocomplete, suggesting code snippets or function calls as you type, helping write code faster and with fewer errors. It can also analyse code and suggest improvements, like making it more readable. Nonetheless, human interpretation is still essential to ensure truly efficient code.

Observability is your green friend

Observability acts as a game-changer in the pursuit of efficient code, equipping developers with powerful monitoring tools that shed light on how code behaves in real-world situations. 

These tools delve deep into a program’s inner workings, pinpointing the exact sections that consume the most processing power, memory, or network bandwidth. This granular level of detail allows developers to focus their optimisation efforts on the areas with the biggest potential payoff.

There will always be a trade-off to achieve efficiency. While creating environmentally friendly software is commendable, it shouldn’t come at the expense of core functionality or maintainability. The ideal scenario is to deliver a product that effectively solves the problem it was made for, while minimising its environmental footprint.

Ultimately, if efficiency isn’t the business goal of the organisation, it’s highly likely that their code won’t be efficient… and therefore not green.