coding format for the blender community

Hey guys, so I have a question to throw out there, when a person is developing an add-on for the community, do you think it is best to write the code in a format that is highly readable or instead in a way that is compact, or rather instead somewhere in the middle and why?

Personally when I work on an addon I tend to lean towards the readable side of things, often keeping a new developer in mind, I prefer to write the code in a way that I feel is highly readable, even when that task can mean that I myself will have to write much more code, sometimes even a few thousand more lines. Rather then using for example list comprehension or the like, but I sometimes feel conflicted as I am aware of the performance boost and effort reduction and I personally find list comprehension highly readable.


simple an readable is the best

happy bl

The KISS principle is more an advice for UI design. But the advice is also valid for code. I am also a big fan of readability and easiness :slight_smile:

The points that jumps into my head:

  • Write a short hint at the top of the file what this code is good for ( dear Blender developers … ^^ )
  • Comment your code! There is no such thing than self explaining code. And there can’t be too much comments. You will thank yourself when you come back after a year or two.
  • Use variable- file- class- etc. names that gives a hint for its useage. Blender has a special formatting for classes and variable names for example.
  • No big single monster class that does everything. Use smaller parts. Makes it easier to understand and to follow.
  • Use proper formatting and indenting. Well, Python forces to do this anyways. But it’s nevertheless worth a hint. Whitespaces between classes increases readability.
  • Just use code that YOU understand and that you are comfortable with. You might be forced by others to write stuff in this and that way. It might be worth the effort when it gives a performance boost. And then it’s worth learning the method. But you have to be able to understand what it does. It’s you who has to modify the code at a later point …

In the end, everybody does the things a bit different. Allowed is what works. And when you work alone at your addon then do it the way that is most comfortable for you.

My two cents :slight_smile:

You could also post a code example. Then we could talk in detail about improvements.

I would go for effectiveness over readability. Sure you can have comments and stuff but your main concern should be how easy it is to write (and update) the code, people asking for feature requests is more common than people taking the code and doing something else with it.

If you want to you could check how the python code is written in blender itself to learn the style, that way it will be consistent and easy to understand even if you don’t focus on readability.

I write in other languages, and generally speaking more compact code is the preferred approach, python to me is strange in this way, to be pythonic is to make your code more readable except when performance is mandatory, and I am currently working on a add-on where performance is something I should be considering but not as crucial, considering the average user of the addon, as one would think.

When first understanding python I am very thankful to those that did in fact write their code in a highly readable way, therefore I intend to do the same in my add-on for those that may one day need to understand its workings, that and I have taken breaks from the code, and it has been very nice to be able to jump right back in easily.

I was really just curious, from the viewpoint of a python developer, what is more appropriate, because in some ways writing readable code, especially to those coming from other languages, can seem bad.

Creating and/or updating a dict:

dict = {}:
for object in context.selected_objects:
  dict.update({[]}) # create and empty list for each object in a dict

Using dict comprehension:

dict = { [] for object in context.selected_objects}

I personally prefer the comprehension, but I use the for loop for readability.

I think the dictionary comprehension is way more readable than that for loop. I wouldn’t avoid core features of the language just because someone with less Python experience might find it harder to read.

Thank you Ryker, this is the sort of reply I was hoping for.

I think that I will adjust my approach to include what I feel to be more readable, to me this composes essentially of a hybrid of the two, where I would use comprehension when it is a simple operation and for loops when there is many scenarios to consider.

Also, please follow pep8.

For the dict I would suggest to use a default dict.