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

Published: 24 February 2023
on channel: Stateless Code
160
4

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)    • Create a RubyGem: NerdDice  
Codecast: Create a RubyGem 66: Overview of Metaprogramming using method_missing    • Codecast: Create a RubyGem 66: Overvi...  
Codecast: Create a RubyGem 86: Retro on Version 0.4.0 (Now with Mediocre Karaoke)    • Codecast: Create a RubyGem 86: Retro ...  

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.


Watch video Create a RubyGem 94: Fix a Cognitive Complexity Code Smell from Code Climate online without registration, duration hours minute second in high quality. This video was added by user Stateless Code 24 February 2023, don't forget to share it with your friends and acquaintances, it has been viewed on our site 16 once and liked it people.