Coding for Greener Tomorrow
Who would have thought that lines of code can heat up our planet?
When we write a function, we often prioritize aspects like modularity, readability, and maintainability. We break down complex tasks into smaller, manageable modules, ensuring that our code is clean and easy to understand. We write comprehensive unit tests to verify that each function performs as expected under various conditions.
When was the last time you considered how much electricity your code will consume when writing a function?
Data centers require large amounts of electricity to power servers and maintain cooling systems. Most of the electricity generated still comes from burning coal, leading to a carbon footprint.
The hardware on which the software runs is a proxy for carbon. Every hardware has embodied carbon — carbon emissions that were generated when the hardware was produced.
New software releases and inefficient software can shorten the lifespan of hardware. Frequent hardware replacements contribute to electronic waste (e-waste), which contains toxic materials and difficulties in recycling.
The aviation industry has long been the poster child for significant environmental impact and a substantial carbon footprint.
While software has rarely been directly linked to environmental impact, data centers have now surpassed the aviation industry in carbon emissions. Data centers account for 2.5% to 3.7% of all Global Greenhouse Gas (GHG) and exceed the emissions from the aviation industry (2.4%) and other activities that fuel the global economy (shown in figure below).
What can we do from our keyboards?
Code optimisation, energy efficiency and carbon awareness can help.
1. Code Optimisation
Tere are different techniques to code optimisation. Let’s explore two namely Efficient Algorithms and Efficient Programming Languages.
Bubble sort repeatedly steps through the list, compares adjacent elements, and swaps them if they are in the wrong order.
Quick sort uses a divide-and-conquer approach to sort elements more quickly. It selects a ‘pivot’ element and partitions the array into two sub-arrays according to whether elements are less than or greater than the pivot.
Bubble sort has Big O(n²) while Quick sort is more efficient with O(n log n).
Why does it matter?
Each CPU cycle consumes energy. The more the CPU cycles, the higher the impact on the environment. Hence making an algorithm efficient can reduce the carbon footprint.
Similarly, when it comes to programming languages — C, Rust, C++, Java top the list in efficiency. Unfortunately, Python which is heavily used in machine learning and training models ranks quite low. This will have a significant impact on the environment as the AI industry grows.
Another technique for code optimisation is to use built-in functions that are optimised rather than writing your own. A simple example is the sum() function in Python which is implemented in C and highly optimised for performance, making it faster and more efficient than manually iterating with a for loop.
2. Energy efficiency
Each network call consumes energy. Consider reducing excessive network calls. If your application currently polls the server every 10 seconds, it’s worth exploring alternatives like webhooks and events for more efficient communication.
When creating a mobile application, consider supporting dark mode since it consumes less energy. Empower users by providing them with the option to switch to dark mode.
3. Carbon awareness
Doing more when the energy is clean.
There are 2 concepts here — Demand Shifting and Demand Shaping.
Demand shifting is moving processing to a region or time when energy is clean. For example, Norway and Finland boast green energy sources. Could your scheduled tasks, like billing and invoicing, be set to run in these regions or during off-peak hours when electricity demand is low?
On the other hand, demand shaping is reducing the demand on energy itself by making the application carbon aware. For example, if your website loads high-resolution images and relies on non-clean energy sources, consider offering options to load low-resolution images or none at all. This approach, known as demand shaping, empowers users to make choices that reduce their carbon footprint. Implementing carbon-aware features in your application further enhances user control over environmental impact.
What next?
Small changes can create significant impacts.
The next time you write a function, think green!
When developing your next app, ensure it supports dark mode to reduce energy consumption. If you're deploying an application, strategically consider demand shifting and demand shaping to reduce Carbon footprint. Seek ways to make your code run with maximum efficiency.
The next time you write a function, think green!