Code Quality Day

The Blender project has existed for more than 20 years. During this time, its codebase has grown organically, with a healthy mix of refactors, brand-new code, and core parts that have survived the journey. There are abandoned parts too, left to their own devices. These need fixing but also function quite well in their current state.

Sometimes technical debts start for the best of reasons. But when left unresolved for too long they can seriously impact the long term sustainability of the project, the cross-module pollination, and the overall stability/quality of the project.

To help mitigate these issues, the code quality day project began earlier this year. The goal was to make Blender more welcoming for new developers, as well as helping the software to scale.

The team will work together on making the Blender code easier to understand, rather than focusing on bugs and other tasks.

Read more details on the wiki.

This event happens on the first Friday of each month and started on February 2020. To celebrate its 10th edition let’s look at some examples.

Clang Tidy

The Clang-Tidy project helps catch bugs before they are even reported by fixing typical programming errors. Its support in the Blender codebase requires a lot of changes to prevent false positives, so this is still an ongoing effort.

However, the benefits of introducing automation are clear, and can be illustrated with the following issues.

Too small loop variable

It was potentially handling data from DNA wrongly.
A bit of a corner case, but no excuse to narrow types used in the DNA.


Misplaced widening cast

Typical errors which cause integer overflow.
User-measurable side effects are, for example, not being able to deal with an image of a huge resolution.


Assert side effect

Different behavior in Debug/Release, worsen performance in debug builds.


Platform specific bugs

Clang-Tidy detected a bug in studio light shading, which was only happening on Windows. The code was (ab)using compiler specific extensions.

Quality of Life

IDTypeInfo

Blender ID datablock types (Object, Collection, Armature, …) used to be hard-coded in Blender, each one with its own function calls and unique parameters.

They now changed to a runtime registration to help with the modularization of the read and write code. Now copy data and make locals are proper callbacks defined for each ID datablock.

Enable Sorted Includes

Blender is using clang-format to help unify its code style across the entire program. A recent addition was the option to sort the included headers alphabetically.

The change is quite small: the commit is a one-line change enabling this option for the .clang-format file. However the codebase had to be cleared so it was less dependent on specific (and error-prone) order of includes.

Cleanups

The most popular changes are cleanups. A lot of the new code is created taking the existing codebase as an example. Cleaning up old functions guarantee that old technical debt doesn’t get replicated.

Descriptive variable names

Descriptive variable names help the code to be better understood. They also work as a way of self-documenting parts of the code, and are aligned with modern code practices.

This commit renamed generic int i variables to view_id in the multi-view code.

Macros

A few macros make accessing the Blender data in a more powerful way. However their adoption is not widely spread across the codebase.

This commit updated more than 200 files to use the LISTBASE_FOREACH macro for datablock iteration.


In the coming months the Clang-Tidy should be wrapped up, and soon enough this will be integrated in the patch review system. More cleanups and comments will happen not only as part of the code quality day, but will be incorporated as a daily practice in the development.

It is important to find a good balance between handling technical debt and pushing development forward. Dedicating one day a month for that helps the most pressing issues from dragging on too long, while also building a culture of high-quality new code.


Blender welcomes any contributor to join its project and help make a positive impact in the world. Get involved today!

This Summer’s Sculpt Mode Refactor
Geometry Nodes Workshop: October 2024
New Brush Thumbnails
Blender 4.3 Beta is here!

5 comments
  1. Remarkable! Its truly remarkable post, I have got much clear idea concerning from this paragraph.

  2. Hi Dalai,
    Thanks for your awesome post detailing everything code quality. clean code practice should be learned from the very beginning. so it becomes natural for programmers to write clean and understandable code. creating descriptive variable names are the most basic and important practice.

    Loved your post as learned many new things here.

  3. Code quality day should be more important.

    A consistent and quality code is a code that make new devs motivated to lean and hack.
    It make the software easier to maintain (a lot)
    It improves stability and performances.

    I think new features should be paused and existing code improved.

    • It is important. The idea of dedicating one day a month is to help build the culture. Which in turn makes code quality the second nature of developers writing new code.

      It also instruments the team to find good targets for longer projects that can be presented with a clear value (as oppose to refactor for the sake of it).

  4. Based, basado, baste!

In order to prevent spam, comments are closed 7 days after the post is published.
Feel free to continue the conversation on the forums.