After reading a blog post by Matthias Noback on keeping an eye on code churn, I was motivated to run the churn php library over some modules in core to gauge the level of churn.
Is this something you might like to do on your modules? Read on for more information.
What is churn
As Matthias details in his blog post - churn is a measure of the number of times a piece of code has been changed over time. The red flags start to crop up when you have high complexity and high churn.
Enter churn-php
Churn php is a library that analyses PHP code that has its history in git to identify high churn/complexity scores.
You can either install it with composer require bmitch/churn-php --dev or run it using docker docker run --rm -ti -v $PWD:/app dockerizedphp/churn run /path/to/code
Some results from core
So I ran it for some modules I look after in core, as well as the Drupal\Core\Entity namespace.
Block Content
| File | Times Changed | Complexity | Score |
|---|---|---|---|
| core/modules/block_content/src/Entity/BlockContent.php | 41 | 6 | 1 |
| core/modules/block_content/src/BlockContentForm.php | 32 | 6 | 0.78 |
| core/modules/block_content/src/Plugin/Block/BlockContentBlock.php | 20 | 6 | 0.488 |
| core/modules/block_content/src/Tests/BlockContentTestBase.php | 16 | 6 | 0.39 |
| core/modules/block_content/src/BlockContentTypeForm.php | 18 | 4 | 0.347 |
| core/modules/block_content/src/Controller/BlockContentController.php | 8 | 6 | 0.195 |
Comment
| File | Times Changed | Complexity | Score |
|---|---|---|---|
| core/modules/comment/src/CommentForm.php | 60 | 45 | 1 |
| core/modules/comment/src/Entity/Comment.php | 55 | 25 | 0.548 |
| core/modules/comment/src/Tests/CommentTestBase.php | 33 | 29 | 0.426 |
| core/modules/comment/src/Controller/CommentController.php | 32 | 20 | 0.274 |
| core/modules/comment/src/CommentViewBuilder.php | 37 | 16 | 0.25 |
| core/modules/comment/src/Plugin/Field/FieldFormatter/CommentDefaultFormatter.php | 32 | 18 | 0.24 |
| core/modules/comment/src/Form/CommentAdminOverview.php | 29 | 17 | 0.191 |
| core/modules/comment/src/CommentAccessControlHandler.php | 17 | 28 | 0.19 |
| core/modules/comment/src/CommentLinkBuilder.php | 15 | 29 | 0.17 |
| core/modules/comment/src/CommentManager.php | 29 | 15 | 0.157 |
Drupal\Core\Entity
| File | Times Changed | Complexity | Score |
|---|---|---|---|
| core/lib/Drupal/Core/Entity/ContentEntityBase.php | 115 | 173 | 0.808 |
| core/lib/Drupal/Core/Entity/Sql/SqlContentEntityStorage.php | 61 | 196 | 0.465 |
| core/lib/Drupal/Core/Entity/Sql/SqlContentEntityStorageSchema.php | 56 | 203 | 0.427 |
| core/lib/Drupal/Core/Entity/Entity.php | 131 | 43 | 0.212 |
| core/lib/Drupal/Core/Entity/ContentEntityStorageBase.php | 41 | 105 | 0.16 |
Conclusion
So, what to do with these results?
Well I think if you're looking to simplify your code-base and identify places that would warrant refactoring, those with a high 'churn' score would be a good place to start.
What do you think? Let us know in the comments.
Comments
It would be interesting to feed this data back as a risk rating for change requests.
"They found that simply ranking files by the number of times they've been changed with a bug-fixing commit (i.e. a commit which fixes a bug) will find the hot spots in a code base."
http://google-engtools.blogspot.com/2011/12/bug-prediction-at-google.ht…
Comment by cilefen
Dated