Create a RubyGem 94: Fix a Cognitive Complexity Code Smell from Code Climate @StatelessCode
Create a RubyGem 94: Fix a Cognitive Complexity Code Smell from Code Climate  @StatelessCode
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.
Create a RubyGem 94: Fix a Cognitive Complexity Code Smell from Code ClimateConfigure Devise Settings and Add Environment-Specific Rails CredentialsInstall balenaEtcher on Ubuntu 24.04Install balenaEtcher on Ubuntu 22.04Inkscape 07: Create a logo for the statelesscode.com WordPress SeriesUpgrade to Devise 4.9 on Rails 7 and Decommission the 4.8 HacksCodecast: Getting Started with Rails 7 24: Use ActionText for Rich Text ContentCreate Pre and Post Sign-In Pages to Test DeviseInstall VirtualBox 7 on Ubuntu, Windows, and Intel MacCodecast: Getting Started with Rails 7 27: Add a Stimulus Controller for Client-Side InteractionCodecast: Create a RubyGem 87: Downtime Hodgepodge and a ContributorCodecast: Getting Started with Rails 7 20: Basic Auth Part 2 and Finish the Guide
Stateless Code |

Create a RubyGem 94: Fix a Cognitive Complexity Code Smell from Code Climate

SHARE TO X SHARE TO REDDIT SHARE TO FACEBOOK WALLPAPER