DEV Community

Cover image for 🌎 Can Programming Save the Planet?
Cherlock Code 🔎
Cherlock Code 🔎

Posted on • Originally published at evergrowingdev.substack.com

🌎 Can Programming Save the Planet?

A Guide to Sustainable Coding


Humans are killing our planet.

We hear this all the time. And for most of us, we understand that there are some things we can all do to be more environmentally friendly.

We’ve heard about things like recycling, using green energy, and living more sustainably, but have you heard of sustainable coding?

Yes, that’s right, sustainable coding is a thing.

As a newbie dev or someone who is learning to code, you might not necessarily be thinking about what impact the code you write has on the environment.

Heck, I’d go as far as to say it’s not something many experienced developers think about (including myself until recently).

If we start to think about all the software and applications that we build, there’s one thing at the centre of them all, and that’s data centres.

Data centres are vast, power-hungry complexes that host thousands of servers that power our online lives, from social media to banking to healthcare.

As of now, these data centres consume an astonishing 1% of the world's electricity, and this is set to rise with our increasing reliance on digital services.

While this might not seem much at first when you factor in that global electricity generation is responsible for about a quarter of all greenhouse gas emissions, you begin to grasp the scale of the issue - and it’s quite shocking!

So, what can we do about it?

Well, one answer lies in sustainable coding.

What is Sustainable Coding?

Sustainable coding, also known as green coding, is a term most recently popularised by various organisations seeking to tackle their impact on the environment.

It is a fresh and environmentally-focused perspective on programming, designed to ease the carbon footprint of our digital activities.

At its core, it's about creating code that is not only functional but also energy-efficient, allowing computer algorithms to operate with the least energy possible.

This concept involves two main types of considerations:

1. Structural
Structural considerations refer to the energy metrics associated with the code blocks themselves.

2. Behavioural
Behavioural considerations, focus on the energy usage associated with specific user scenarios, such as scrolling through social media feeds or sending an email.

Green coding doesn't require a radical reinvention of existing coding practices (so no need to go refactoring all your projects!) but rather a rethinking towards energy-efficient design.

Each line of code that a device has to process contributes to its overall carbon emissions, so the aim is to minimise the volume of code without sacrificing functionality.

It’s more about embracing lean coding principles.

Green coding focuses on using the least amount of processing power to achieve the desired results.

This might include optimising high-quality media files to make them smaller or reducing screen image resolutions, all of which can lead to quicker loading times, improved user experience, and, crucially, less energy consumption.

The Benefits of Sustainable Coding

When looking at the long-term benefits of sustainable coding, we quickly realise that green coding extends well beyond the domain of individual coding practices.

Recognising this, tech giants like Amazon and Google have been making strides in adopting renewable energy for their operations.

Amazon Web Services (AWS), for instance, has committed to powering all its operations with 100% renewable energy by 2025. And Google is not far behind with its goal of operating carbon-free data centres by 2030.

So why are they doing this?

Let’s look at some of the long-term benefits of adopting sustainable coding practices:

  • Reduced Energy Consumption
    Sustainable coding practices help reduce the amount of energy consumed by data centres, thereby decreasing carbon emissions.

  • Improved Efficiency
    By prioritising lean code, these practices make software applications more efficient, using fewer resources to deliver the same results.

  • Better User Experience
    Faster loading times and optimised high-quality media result in an improved user experience.

  • Enhanced SEO
    Energy-efficient websites perform better on search engine rankings, increasing visibility and boosting organic traffic.

  • Cost Savings
    By reducing energy usage and streamlining the coding process, companies can save on both operational and overhead costs.

  • Sustainable Future
    These practices play a crucial role in ensuring a sustainable future for the digital world and the planet as a whole.

In short, sustainable coding represents a critical shift towards a more environmentally-conscious approach to software development, which will in the long-term help companies and businesses operate their digital transformation while preserving our planet.

What Does Sustainable Coding Mean for Developers?

We’ve seen how using sustainable coding can benefit organisations in the long term, but what does this mean for us as individual programmers?

This is where the Three Pillars of Green Coding come into play, where we can look at how you can introduce sustainable coding into your everyday programming practices:

Greener Logic

You can think of Greener Logic as the design philosophy of your code.

It encourages you to write energy-efficient, lean code that delivers the required result with minimal resource usage.

Every decision you make - from how to implement functions to choosing libraries - can have a substantial impact on your code's energy consumption.

Here’s what you can do:

  • Write Zero Waste Code
    Strive to write clean, concise code, eliminating unnecessary lines of code and redundancies. Remember, every line of code your computer processes uses energy.

  • Think about Frequency of Use and Proximity
    Consider how often a piece of code will be run and where it will be executed. The more frequently code runs and the closer the execution is to the user, the more energy it consumes.

  • Use Low Footprint Resources and Benefit-driven Visual Content
    Choose smaller, high-quality media files to reduce load times and energy consumption. Remember, less is more when it comes to energy-efficient coding.

Greener Methodology

The Greener Methodology is all about how you develop your software. It's about infusing sustainability into your programming workflow.

Some common methods include:

  • Lean and Agile
    Adopt lean and agile practices, which focus on producing working software with the least waste.

  • Continuous Integration and Delivery (CI/CD)
    Continuous integration and delivery pipelines automate much of the testing and deployment process, reducing the overall energy required for these tasks.

Greener Platform

The Greener Platform is where your software lives and runs.

Even if you have the most energy-efficient code, the platform it runs on can significantly impact its overall energy footprint.

Here are some things to consider:

  • Optimal Utilisation
    Only use the resources you need. Overestimation can lead to unnecessary energy usage.

  • Precise Configuration
    Ensure your server is configured correctly for your needs. An improperly configured server can use more energy than necessary.

  • Holistic Metrics
    Track not only the functional performance of your software but also its energy usage.

  • Cloud Migration
    Consider migrating your platforms and services to the cloud as cloud energy is more efficient for its easy scaling capabilities and cost-effectiveness.

Other Things to Consider

Outside of these main three pillars, there are some other things you could also consider adopting for sustainable coding:

  • Using More Energy-Efficient Tech
    The equipment you use to code can also have an impact on your digital footprint. If you have the means you might want to consider using a more energy-efficient laptop or computer for your programming.

  • Saving Energy
    When you’re not coding, try not to leave your machine plugged in and on standby. Consider turning it off and unplugging it to save energy. Trust me, I know this one is hard, but it all helps!

Incorporating the Three Pillars of Green Coding and as well as the other things into your development process can feel like a big shift, but every small change adds up.

Remember, as a developer, your code doesn't just sit there and does nothing, it has the power to impact the world around us in more ways than we might typically think.

The Most Sustainable Programming Languages

Who would have thought that there are certain programming languages that are more sustainable or greener than others?

When we’re newbies, we’re mostly concerned with which programming language is the best or easiest to learn, rather than the greenest.

However, there are indeed some programming languages that are better than others when it comes to sustainability.

Let's take a moment to understand what makes a programming language "energy-efficient".

Efficiency is based on several factors, including the energy, memory, and time a language demands during execution.

The quality of your virtual machines, compilers, optimised libraries, and of course, your source code, can all contribute towards enhancing this efficiency.

In an interesting study, a team of Portuguese researchers looked at 27 of the most widely used programming languages to assess their energy efficiency. The key question they sought to answer was:

Does a faster programming language necessarily equate to being more energy-efficient, or "greener"?

The researchers conducted extensive tests on these languages, compiling and executing programs using the most advanced virtual machines, compilers, libraries, and interpreters.

They then assessed each programming language's efficiency, factoring in energy consumption, execution time, and memory usage.

Their research ended in identifying the top five most energy-efficient programming languages:

  1. C
  2. Rust
  3. C++
  4. Ada
  5. Java

Interestingly, their findings also debunked the myth that faster programming languages are always the most energy-efficient.

Although Java scored well in both speed and energy efficiency, languages like Python, Perl, Ruby, and others found themselves at the less efficient end of the spectrum.

So, when it comes to green coding, the choice of programming language can indeed make a difference.

However, I’m not saying you should choose one of the languages above, you should feel free to learn and code in whatever language you like.

The good news is that you should now be aware of some of the practices you can use towards greener coding in any language 💚.

Conclusion

To conclude, we’ve seen how code and algorithms, largely hidden away behind the scenes, have a surprising and significant impact on our energy consumption and, by extension, our planet's well-being.

However, the emergence of sustainable or 'green' coding brings a promise of change.

The digital industry, from data centres to individual developers, has begun to acknowledge and address these environmental implications.

Tech giants are now leading the way with significant investments in renewable energy for their data centres.

And the principles of sustainable coding, such as energy-efficient programming languages and lean coding, have begun to reshape the way we approach software development.

Sustainable coding is not merely a trend or a corporate buzzword - it’s essential for our digital future.

It represents the connection between technology and environmental sustainability, providing a pathway for us to progress in harmony with the environment rather than at its expense.

When we look and consider all of the above and think about whether programming can save the planet? The answer is no it can’t - but we can.

And if we can embrace greener coding practices today we can ensure a healthier, more sustainable tomorrow.

Go lean, stay green!

From your fellow ever-growing dev,

Cherlock Code


💙 If you liked this article...

I publish a weekly newsletter to a community of ever-growing developers, seeking to improve programming skills and stay on a journey of continuous self-improvement. Focusing on tips for powering up your programming productivity 🚀.

Get more articles like this straight to your inbox.

Let’s grow together 🌱

And stay in touch on 🐦 @evergrowingdev

Top comments (4)

Collapse
 
lexlohr profile image
Alex Lohr

Interesting approach. I find this works well with the paradigm rising in the front-end world that I call "frugality".

Saving bytes in the end code is great, but you shouldn't do so at the price of maintainability: write good code without unnecessary operations (use assignments for speaking variable names as often as you like) and let tools optimize away the unnecessary parts - and use less and smaller dependencies.

Use caching whenever it's feasible. Avoid too much logging. Support efficient file formats. Attempt to simplify complexity before putting it into code or outsourcing it to tools like lodash.

Collapse
 
evergrowingdev profile image
Cherlock Code 🔎

Hi Alex, thank you for your insight. I find that paradigm you've written about surrounding "frugality" quite interesting and appropriate in relation to this.
I also agree that maintainability is still a key factor here, so definitely something to consider with these approaches.

Collapse
 
rachelfazio profile image
Rachel Fazio

Very cool AND DETAILED article! Thank you for sharing.

Collapse
 
evergrowingdev profile image
Cherlock Code 🔎

Thank you!