Wednesday, April 24, 2024
HomeRuby On RailsFew static evaluation methods to bulletproof your software

Few static evaluation methods to bulletproof your software


Static evaluation is the method of inspecting code with out executing it to determine potential points and enhance its high quality.
By using worthwhile static evaluation strategies, you possibly can improve your software’s reliability.
On this article, I focus on three sensible strategies that may enable you resolve points in your codebase.

Badly named checks

Just lately, whereas monitoring down unused code in our shopper’s software,
We got here throughout a RSpec check that clearly couldn’t move.
Certainly, once we executed the check individually utilizing rspec and specifying its path, it failed as anticipated.
Nonetheless, when working your entire check suite with bundle exec rspec, all checks handed.
Upon additional investigation, it turned out that this check file didn’t comply with RSpec’s check naming conference.

As said within the RSpec documentation:

# Default: Run all spec information (i.e., these matching spec/**/*_spec.rb)
$ bundle exec rspec

We wished to confirm whether or not different check information is likely to be bypassed by RSpec.
The venture was enormous, so it was unimaginable to do it manually.

Utilizing the next command, we managed to determine all problematic information:

discover ./spec -type f -not -name *_spec.rb -not -path "./spec/factories/*" -not -path "./spec/assist/*" | xargs rg RSpec.describe

We discovered quite a few information with incorrect naming patterns, akin to *.spec.rb, *_sepc.rb, and so forth. After renaming these information, half of those checks turned out to be failing.

Not resolving constants

In certainly one of my earlier posts, I defined in-depth how we tracked down not resolving constants with a parser gem.
These not resolving constants symbolize potential runtime errors that may be simply prevented with static evaluation.
I’ve shared our script on the public repository permitting you to repeat collector.rb and effortlessly run it in opposition to your venture.

bundle exec ruby collector.rb app/

Pointless routes

One other helpful script for cleansing up your codebase checks in case your routes.rb file outline any routes which would not have a corresponding controller motion nor a view for implicit rendering.

# unused_routes.rb
require_relative "config/setting"

Rails.software.routes.routes.map(&:necessities).every do |route|
  subsequent if route.clean?
  subsequent if route[:internal]

  controller_name = "#{route[:controller].camelcase}Controller"
  subsequent if controller_name.constantize.new.respond_to?(route[:action])

  implicit_render_view = Rails.root.be a part of("app", "views", *route[:controller].cut up('::'), "#{route[:action]}.*")
  subsequent if Dir.glob(implicit_render_view).any?

  places "#{controller_name}##{route[:action]}"
rescue NameError, LoadError
  places "#{controller_name}##{route[:action]} - controller not discovered"
finish

Merely copy the script and run it utilizing ruby unused_routes.rb. It’s possible you’ll be stunned by the outcomes.



RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments