An Insight Into Green Coding At Simplilearn

Mukul Kashyap
Simplilearn Engineering
6 min readDec 13, 2021
Photo by Nail Gilfanov on Unsplash

Introduction

At Simplilearn, engineering great products remains a firm commitment and the members of the technology team go the extra mile to ensure that the customer experience on the platform is top notch. An important aspect that we both acknowledge and seek to optimize is our environmental footprint in the process of development. This brings to the fore the concept of Green Coding

Green coding refers to a code that is written keeping the intention of minimising the energy consumption demanded by a particular algorithm.

It is a way of coding to put a minimum load on the physical servers and systems and reduce environmental footprints. In order to understand the whole concept, we have to understand some key entities like Energy Capacity, Power Use and Energy Use.

Key Entities

  • Energy Capacity: A fully charged battery has stored energy, of which the unit is watt-hours (Wh). It is calculated by multiplying the watts discharged by the battery by the number of hours of use.
  • Power Use: Any device doing some task like calculations, accessing network etc. are using the energy stored in the system. The rate at which the stored energy is converted into work is power use.
  • Energy Use: The total amount of power used to perform the task over a selected period of time is energy use.

The relation between Capacity, Power and Energy will be:

Relation between Capacity, Power and Energy

Facts

There are some basic facts that we developers know as the “coding best practices”, but we have never quantified them in terms of energy and power.

Let us take an example of a very common feature in desktop browsers and mobile apps — the “Dark Mode”, and understand its relation to the key entities explained above and the environmental footprint.We measure the Mobile dark mode and its effect on the environment with CO2 emission.Mobile Enerlytics is a company that measures the energy use of phones. According to them, dark mode saves 5.6% to 44.7 % on a device at full brightness.So it is obvious that the dark mode improves battery life significantly, but how does it affect the CO2? In the US, for every kWh of energy consumed, the CO2 emitted is 0.4 Kg, so if we can detect how much power is saved by dark mode, we can easily compute the corresponding CO2 emission prevented.

On a rough calculation done on iPhone X, a 20- watt charger takes ~1 hour to charge . If we charge the phone fully once per day the energy consumed will be ( 20 * 1 ) ~20-watt hours in a day and ~7.2 kWh a year.

If we assume that a paltry 5% power is saved by dark mode and consider 1 Million iPhone X in use, we are talking about 144,000 kg of CO2 emission saved, which is equivalent to CO2 emissions by 31 cars. The number 31 looks very small as we have taken a small dataset with a very conservative percentage of 5%.

Did You Know?

We have already understood the relation between the use of the “dark mode” and CO2 emission. As developers, we rarely think about the impact of our best practices on the environmental footprint. We usually assess coding practices on memory management, complexity, time management, and performance parameters. However, in the current context, we will discuss how coding practices can lead to the environmental footprint, especially CO2 emission.

  • The static variable takes 17,700 % more energy than the local variable.
  • The modulus operator consumes up to 277% more energy than the addition operator
  • ‘int’ is the most energy-efficient iteration variable in a for loop.
  • StringBuilder append method consumes minimal energy compared to the String
  • Ternary operator consumes more energy than an if-then-else statement.

This list goes on, but the takeaway from these examples is that a slight change in our coding style will definitely lead to power saving and ultimately reduce CO2 emissions.

Our Path to Green Coding

Having understood a clear relation between energy and CO2 emission, and also seen quantifiable data from good coding standards, we will discuss how Simplilearn as an organisation is ensuring the implementation of best coding practices in development to reduce the environmental footprint.

The below diagram represents the genesis of high CO2 emission in the context of coding.

High CO2 emission in the context of coding

The three main important areas to control the best practices and resource optimisation are below.

  • Code Review
  • Test Cases
  • Code Quality Tool

Our processes like code review, writing test cases, and code quality profiles align with coding practices, leading to less resource utilization, consuming less energy, and ultimately becoming environmentally friendly.

Let us understand with some examples.

Test Cases

Bugs detected in applications more often than not necessitate fix and remediation getting prioritized over other build tasks. Bug fixes always take unplanned bandwidth, and every time a bug is found, it has to hit the environment with a deployment. The later the bug is detected, the higher the number of deployments.

A buggy application means a terrible experience for the customers and moreover results in more CO2 to the environment with repeated deployments to fix the bug. How? We utilize the build servers and deployment tools and hence more energy consumption for these resources.

The best way to control the bugs at the early stage is by writing optimized test cases with a higher vitality (ensuring bugs caught per test case executed is progressively increased). In Simplilearn, we started scrutinizing the test cases closely, ensuring optimal coverage of the positive and negative scenarios. As a result, we have seen a downward graph in defects injected. Below is a screenshot of our defect metrics.

We have eventually reduced the issues raised by QA in pre-prod environment and ultimately resulting in fewer deployments.

Code Review

There is always a requirement of new features in the application which goes through many brainstorming sessions. All these ideas and features comes into reality with some lines of code.

There are many ways to achieve a logic but the complexity and memory management of the code plays a big role.

A non-optimized code can increase your CPU utilization and make the other operation slow increasing the overall energy consumption of the servers.

The code review process is the key to stop such cases. By implementing two layers of code review, this is ensured that the code written by each developer in Simplilearn is optimised and also the correct logic has been implemented.

With the lower percentage of non optimised code we are putting less load on servers and ensuring a less volume of CO2 emission.

Code Quality Tool

Sonarqube is an open-source platform to check the quality of code. Simplilearn uses Sonarqube to perform multiple checks on code coverage, bugs, tech debts, code smell etc. Sonarqube also provides a feature to create profiles to customise the ruleset and focus on the critical areas as per their goals.

In Simplilearn, we are working with a separate profile of Sonarqube to check the code against those rules that play a significant role in the environment. Let us understand this with some examples.

There are green coding guidelines available on the internet. One of these guidelines is “Remove non-essential features”.

It is easy to link these guidelines to Sonarqube rules, like in this case, below are some examples of Sonarqube rules

  • Unused local variable should be removed.
  • Section of code should not be commented out.
  • Unused method parameter should be removed

Checking the code against such rules helps us achieve the green coding guidelines. We can focus more on the green coding best practices and make our code environment friendly.

Take Away

These small changes are going to help us in many ways to improve the performance of the code and reduce the environmental impact, making this a win-win situation. We have always talked about the coding best practices, but seeing it from this angle will help us see the bigger picture.

Hence, concluding the article by wishing everyone Happy and Green Coding.

Credits

--

--

Mukul Kashyap
Simplilearn Engineering

Engineering Manager @ Simplilearn | Full stack developer | Loves to transform feelings into words