Lesson 14To go further, some code practices
- Notion 86 - Introduction to eco web design code practices with The Good Manager project
- Notion 87 - Clean code for energy efficiency
- Notion 88 - Asynchronous code: do more, in less time
- Notion 89 - More efficient and less resource-intensive servers
- Notion 90 - Manage your server workload
- Notion 91 - Using external libraries: the example of Bootstrap CDNs
- Notion 92 - Code practices review
Notion 87
Clean code for energy efficiency
Target skills
A clean code for a website is a code easy to modify for developers, and a code that is stable and optimized. We will have a look at some programming practices that can be optimized.
Code optimization
Code style depends on the programmer, just as writing style depends on the author. However, some practices and optimization are welcome in order to reduce the carbon footprint of a program. In the case of a static website (a website composed of HTML/CSS/Javascript elements), the code is executed on the client-side (i.e. the web browser). Delivering a well-coded website will lighten the computational load for all the visitors.
Loops
A loop is a piece of code being executed again and again. Its execution is often linked to conditions, in order to avoid infinite loops. An infinite loop is a piece of code being executed indefinitely with no limit! It often causes the crash of the program and/or freezes the computer running it. It consumes a lot of computing power and is clearly not optimized: there is no reason a piece of code should be executed infinitely and indiscriminately.
External libraries
External librairies are pieces of code imported into a program to add new features. In the case of a website, it can be new Javascript features (jQuery is a famous library), or styling rules (such as Bootstrap). Despite its convenience, a library can increase the carbon footprint of a webpage by importing unused elements. For example, a very popular external library is Google Fonts. It will import fonts from Google servers, and load it on the visitor’s browser, that may not be used by importing several font weights (bold, light) while only one is being used.
External libraries give no or very little control over the imported code. It may import too much, or create compatibility issues. Use them wisely and check your compatibilities!