Tip

6 green coding best practices and how to get started

Software developers can support sustainability efforts via a range of green coding tactics. These best practices range from shrinking artifacts to maximizing efficiency.

From server closets within businesses to massive server farms underpinning cloud service providers, data centers run applications all over the world. All that processing, writing to memory and other activities use a lot of power, as well as generate a lot of waste heat. While data center operators can tackle green initiatives at the physical hardware and facilities level, developers and testers can contribute with green coding.

Learn green coding techniques to program software with sustainability in mind, from reduced artifacts to slimmed-down CI/CD pipelines to solar-powered coding. Use the various benefits of a sustainable approach to development to get teams involved and start making change.

Green coding best practices

1. Set up minimal artifacts

Anyone who has used a package manager, such as npm or HomeBrew, knows a major package installation often requires multiple minor packages as well. And those minor packages have dependencies. As best you can, limit the number of artifacts involved.

Operating systems vary widely in requirements. For example, Linux Mint, a popular Linux distribution based on Ubuntu and Debian, recommends 100 GB of disk space for comfortable operation. Alpine Linux, by contrast, is a 5 MB image built around musl libc and BusyBox for a Docker and Kubernetes containerized environment.

IT organizations can reduce disk, memory and processing demands by considering the software for a given purpose. Curate a list of dependencies to do just what is needed. Compile binaries with just the needed dependencies. Consider cloud-based and virtualized deployments, as many virtual servers can run on shared physical hardware.

2. Follow lean coding practices

How much processing power should software use? In one school of thought, programmers build software to use all the available processing power. This approach assumes that advances in computing hardware will enable the software to run faster when it comes to market or enters production. Joel Spolsky, founder of Trello, Stack Overflow and others, promoted the idea that bloatware is good as far back as 2001.

Computer science schools, on the contrary, teach topics like memory consumption and Big O notation. Big O notation is a method to calculate the amount of processing power used by an algorithm. Big O optimization aims for processing demand to grow more slowly as the number of elements in a list, or leaves in a tree structure, increases. It might be time to give Big O notation another look.

Other Lean coding methods include using lower-resolution photos on the internet and moving lookups from databases to in-memory caches. Open source NoSQL databases such as MongoDB, Couchbase and Redis store and retrieve common information with less processing power than a relational database.

3. Minimize resources used by the dev and test pipeline

While opting for SaaS dev and test tools may be generally more efficient than installing them to run on servers, cloud apps can still suffer from bloat. Modern DevSecOps tools often create full test environments and run all automated checks on every commit. They can also run full security scans, code linters and complexity analyzers, and stand up entire databases in the cloud. When the team merges the code, they do it all over again. Some systems run on a delay and automatically restart, perhaps on dozens of monitored branches.

Observability tools to monitor everything can lead to processing bloat and network saturation. For example, imagine a scenario where the team activates an observability system for all testing. Each time network traffic occurs, the observability system messages a server about what information goes where -- essentially doubling the test traffic. The energy consumed is essentially wasted. At best, the test servers run slowly for little benefit. At worst, the production servers are also affected, and outages occur.

An audit of exactly how much activity takes place in the pipeline could yield both processing savings and lower consumption. For example, the team could have the build process produce a cloud-native image, yet do initial testing locally on a laptop.

4. Isolate code updates, testing and deploys

Monolithic systems can require an expensive regression testing process on every build. Even if entirely automated, the build and automated check process will be largely redundant, redoing what has been done before, for every build. Microservices offer the opportunity to change, test, deploy and monitor just one section of the code at a time.

Other modern approaches include languages such as PHP where the code can be deployed one web page at a time. There are also tools that take interpreted languages like PHP and compile them, resulting in smaller files that run with less CPU and memory use.

5. Simplify the rollout process

Commonly, teams that subscribe to the continuous delivery approach actually have a three- to four-day cycle time between code commit and deployment. That's likely due to human delay and processing that may be redundant. An analysis of the CI/CD pipeline could yield both time and processing savings.

The ideas above focus on coding, development and test practices, but there are also similar changes operations teams can make.

6. Adopt solar and sustainable energy

While green coding largely focuses on software design and operation, developers and testers can make an individual difference for the environment, as can their employers. Short or nonexistent commutes reduce emissions from vehicles, as does choosing a green vehicle. Solar, wind and other renewable energy sources can power homes and businesses. Companies can provide incentives for their employees to work in this way. Many governments offer tax benefits and other rewards for sustainability efforts at the corporate and individual levels.

Get started with green coding

One great advantage of green coding practices is that you can start both globally and locally. Any individual worker can brainstorm ways to reduce the footprint of the systems and code. The team can commit to reviewing the CI/CD pipeline or to look into architecture best practices.

All it takes is someone to get started. Changes to a CI/CD pipeline, analyzing code practices, adding containers and refactoring microservices are all in the hands of a software development team.

Additionally, send larger, architectural green ideas up to leadership. To get buy-in for these initiatives, highlight the benefits to the business, which include the following:

  • Reduced costs. Organizations that invest in green coding decrease their energy consumption. The focus on lower consumption can also simplify and shrink networks, and keep test environments in check. These changes reduce computing expenses.
  • Better discipline. Programmers can focus on working quickly and creatively during prototypes and brainstorming. However, once the team selects languages and code design, pay attention to memory, CPU and disk use. It saves energy and can lead to better software engineering.
  • Boosted morale. Green practices can also benefit employees. Developers want to build elegant systems with little bloat, and embrace cloud technology smartly. They also might see remote work as a perk, regardless of their support for green coding practices.

Matt Heusser is managing director at Excelon Development, where he recruits, trains and conducts software testing and development. The initial lead organizer of the Great Lakes Software Excellence Conference and lead editor of "How to Reduce the Cost of Software Testing," Matt served a term on the board of directors for the Association for Software Testing.

Dig Deeper on Software development lifecycle

Cloud Computing
App Architecture
ITOperations
TheServerSide.com
SearchAWS
Close