Uploaded February 2023 | Updated November 2026, 3 hours ago
Overall the metrics on our codebase are pretty favorable. We have a Code Climate Maintainability rating of A. We have 100% code coverage. Our test suite and builds run fast.
But, we do have one code smell identified in Code Climate. In the grand scheme of things, it's not that big of a deal, but my "inner Monica" wants perfection. Also, it can serve as a teaching opportunity for how to solve for issues like this.
If you're not familiar with the term "code smell," it refers to something that works but could be improved. The particular type of code smell identified by Code Climate is cognitive complexity. The amount of assignment/branch/condition activity that goes on in a method adds exponentially to the cognitive load of the person reading and trying to understand the code. The fact that we're using the Ruby ternary operator a couple of times in the method masks the level of complexity in the current method.
The solution for almost all cognitive complexity problems is to refactor larger methods into smaller methods that do less. Even though Ruby has the reputation of being an object-oriented language, you can still use the functional programming paradigm well with it. Have a method that does one thing well and returns a consistent output for a given input.
After we refactor out the methods, we run afoul of RuboCop's Metrics/ModuleLength cop. The refactoring pushed us up to 106 lines out of an allowable 100. In this case, the value of breaking the module down into smaller files isn't that great, so we just add a magic comment to disable that cop for this module.
After we push and merge the pull request to master, our code smell is cleared on Code Climate.
This video covers:
00:00:12 Introduction
00:01:12 What is a code smell, and why should we fix it?
00:03:01 Review the particular method
00:05:12 Plug for the metaprogramming epic
00:08:07 Applying principles of the functional programming paradigm to Ruby code
00:09:40 Refactor get_default_from_number_of_dice to its own method
00:11:51 The importance of having a robust test suite that covers everything you care about
00:15:58 Refactor get pattern_match_or_default into its own method
00:19:01 Ensure adequate commenting
00:20:06 Run RuboCop and add an exclusion about Metrics/ModuleLength
00:24:47 Commit, push, pull request
00:27:51 Verify code smell cleared in Code Climate and close issue
#ruby #rubygems #codecast #screencast #NerdDice #DnD #roleplaying #softwaredevelopment #github #opensource #dice #TDD #rubocop #codeclimate #codesmell #cognitivecomplexity #metaprogramming #functionalprogramming #refactoring
See other related StatelessCode videos:
- NerdDice RubyGem Playlist (with emphasis on videos 66-86) youtube.com/playlist?list=PL9kkbu1kLUeOnUtMpAnJOCtHdThx1Efkt
- Codecast: Create a RubyGem 66: Overview of Metaprogramming using method_missing youtu.be/O6DwzgIlVXY
- Codecast: Create a RubyGem 86: Retro on Version 0.4.0 (Now with Mediocre Karaoke) youtu.be/yqms6h0cQAE
This video is CC0 - No rights reserved. (YouTube doesn't allow this option when publishing.) All code is released under the UNLICENSE. Stateless Code denies the concept of "intellectual property". Copying is not stealing.
Overall the metrics on our codebase are pretty favorable. We have a Code Climate Maintainability rating of A. We have 100% code coverage. Our test suite and builds run fast.
But, we do have one code smell identified in Code Climate. In the grand scheme of things, it's not that big of a deal, but my "inner Monica" wants perfection. Also, it can serve as a teaching opportunity for how to solve for issues like this.
If you're not familiar with the term "code smell," it refers to something that works but could be improved. The particular type of code smell identified by Code Climate is cognitive complexity. The amount of assignment/branch/condition activity that goes on in a method adds exponentially to the cognitive load of the person reading and trying to understand the code. The fact that we're using the Ruby ternary operator a couple of times in the method masks the level of complexity in the current method.
The solution for almost all cognitive complexity problems is to refactor larger methods into smaller methods that do less. Even though Ruby has the reputation of being an object-oriented language, you can still use the functional programming paradigm well with it. Have a method that does one thing well and returns a consistent output for a given input.
After we refactor out the methods, we run afoul of RuboCop's Metrics/ModuleLength cop. The refactoring pushed us up to 106 lines out of an allowable 100. In this case, the value of breaking the module down into smaller files isn't that great, so we just add a magic comment to disable that cop for this module.
After we push and merge the pull request to master, our code smell is cleared on Code Climate.
This video covers:
00:00:12 Introduction
00:01:12 What is a code smell, and why should we fix it?
00:03:01 Review the particular method
00:05:12 Plug for the metaprogramming epic
00:08:07 Applying principles of the functional programming paradigm to Ruby code
00:09:40 Refactor get_default_from_number_of_dice to its own method
00:11:51 The importance of having a robust test suite that covers everything you care about
00:15:58 Refactor get pattern_match_or_default into its own method
00:19:01 Ensure adequate commenting
00:20:06 Run RuboCop and add an exclusion about Metrics/ModuleLength
00:24:47 Commit, push, pull request
00:27:51 Verify code smell cleared in Code Climate and close issue
#ruby #rubygems #codecast #screencast #NerdDice #DnD #roleplaying #softwaredevelopment #github #opensource #dice #TDD #rubocop #codeclimate #codesmell #cognitivecomplexity #metaprogramming #functionalprogramming #refactoring
See other related StatelessCode videos:
- NerdDice RubyGem Playlist (with emphasis on videos 66-86) youtube.com/playlist?list=PL9kkbu1kLUeOnUtMpAnJOCtHdThx1Efkt
- Codecast: Create a RubyGem 66: Overview of Metaprogramming using method_missing youtu.be/O6DwzgIlVXY
- Codecast: Create a RubyGem 86: Retro on Version 0.4.0 (Now with Mediocre Karaoke) youtu.be/yqms6h0cQAE
This video is CC0 - No rights reserved. (YouTube doesn't allow this option when publishing.) All code is released under the UNLICENSE. Stateless Code denies the concept of "intellectual property". Copying is not stealing.




![Upgrade to Devise 4.9 on Rails 7 and Decommission the 4.8 Hacks
Some exciting developments have taken place while we were working on our RubyGem! Devise released a new minor version 4.9 that works with Rails 7 and Turbo without requiring a custom responder class or TurboDeviseController.
We intentionally held off on doing the upgrade in our video and commit where we upgraded to Ruby 3.2.1 and Rails 7.0.4.3 to isolate the Devise change. (We even needed to modify our Gemfile.lock to explicitly hold the Responders gem back at 3.0.1 because it was breaking our system tests.)
Situations like this illustrate why its so important to have a robust automated test suite. In the devise epic, we created system integration tests for all of our Devise scenarios so that we could discover whether the upgrade attempts would cause a breaking change or not.
To upgrade our app, we refer to the How To: Upgrade to Devise 4.9.0 [Hotwire Turbo integration] guide on the Devise Wiki. We start by removing the hacks we introduced to get Devise 4.8 working with Turbo and Rails 7. We delete our custom TurboDeviseController file altogether and remove the custom failure app code from our initializer.
We already modified the views to work with Turbo, so we can skip that part of the upgrade guide. The actual code change to upgrade to Devise 4.9 once we roll back our hacks is fairly simple. We just need to add configuration options for config.responder.error_status (:unprocessable_entity) and config.responder.redirect_status (:see_other).
When we run our full test suite, there is one failure. We investigate the screenshot and development environment of our application. It looks like the flash message for a blank email when Devise is configured to paranoid mode has changed. Were okay with the change, and modify our LogInLogOutTest for blank email to expect the new behavior.
This video covers:
00:00:12 Introduction
00:01:59 Check out the Devise pull request that fixes the Rails issues and the commit to our project where we introduced the hacks to get it working with 4.8
00:04:17 Update the Gemfile to use Devise 4.9.0 and run `bundle update`
00:05:15 Debug gem dependencies were blown away by the general `bundle update`. Reset to Gemfile.lock to HEAD and just run `bundle update devise`
00:07:34 Git Remove on the TurboDeviseController (git rm app/controllers/turbo_devise_controller.rb)
00:07:55 Remove the hacks from the Devise initializer configuration file (config/initializers/devise.rb)
00:10:04 Add in the new configuration options needed for config.responder.error_status and config.responder.redirect_status
00:11:36 Run the full test suite and investigate the one failure
00:13:56 Pull up the scenario in localhost development environment and see what happens. Determine that the change in behavior is acceptable
00:15:26 Modify the test to use the new flash message and refactor out the flash error text into an instance variable
00:16:52 Re-run tests to ensure everything is passing consistently
00:17:55 Review the git diff, run RuboCop, and commit
00:20:25 Remove missed TECH DEBT comment and amend the commit
00:21:52 Push to the branch, pull request, close issue
#ruby #rails7 #rails #rubyonrails #rubygems #codecast #screencast #github #opensource #minitest #TDD #agile #NerdDice #DnD #roleplaying #softwaredevelopment #learntocode #tailwind #postgresql #rubocop #devise #turbo #controllers #views #selenium #capybara #systemtest #debug #responders
See other related StatelessCode videos:
- Playlist: Using Devise 4.8 on Ruby on Rails 7 with Turbo and Tailwind https://www.youtube.com/playlist?list=PL9kkbu1kLUeMnvkrX7EOLSv6glmQzGMXL
- Get Devise Working with Turbo in Rails 7 https://youtu.be/xnuA2K2mohs
- Upgrade to Ruby 3.2.1 and Rails 7.0.4.3 https://youtu.be/XvRzpoWfM9Q
Resources that we relied upon for this solution:
- Devise pull request to fix Turbo https://github.com/heartcombo/devise/pull/5548
- How To: Upgrade to Devise 4.9.0 [Hotwire Turbo integration] https://github.com/heartcombo/devise/wiki/How-To:-Upgrade-to-Devise-4.9.0-%5BHotwire-Turbo-integration%5D
This video is CC0 - No rights reserved. (YouTube doesnt allow this option when publishing.) All code is released under the UNLICENSE. Stateless Code denies the concept of intellectual property. Copying is not stealing. Upgrade to Devise 4.9 on Rails 7 and Decommission the 4.8 Hacks](https://i.ytimg.com/vi/niNuwTu0URs/mqdefault.jpg)





