-
Notifications
You must be signed in to change notification settings - Fork 102
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Prefer require_relative
for internal requires
#521
Conversation
`require_relative` is preferred over `require` for files within the same project because it uses paths relative to the current file, making code more portable and less dependent on the load path. This change updates internal requires to use `require_relative` for consistency, performance, and improved portability. Retain require for dynamic paths in `lib/haml_lint.rb` because they use absolute paths. Refs: - rubocop/rubocop#8748
93ce339
to
7e3fb4e
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Supportive, just confused about the term "relative" here.
require 'haml_lint/version' | ||
require 'haml_lint/version_comparer' | ||
require 'haml_lint/severity' | ||
require_relative 'haml_lint/constants' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why would this not be ./constants? (Just curious)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi,
no because haml_lint
is in lib
, and constants is in lib/haml_lint/constants.rb
, so the relative path does include haml_lint
lib/
├── haml_lint/
│ └── constants.rb
└── haml_lint.rb
This is a good reference in a (former) ruby core gem: https://github.com/ruby/logger/blob/d1d704a6336a82903011336a0116012e9aa4a504/lib/logger.rb#L17-L21
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Some other reference in Ruby core that are not linked in this PR:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Reading from the GitHub app on my phone was throwing me off. Pathing makes sense 👍
Thanks. Reads odd to me for a "relative" path to not include a leading ./, but no objections otherwise. |
Thanks, I'm going to do the same for slim_lint |
`require_relative` is preferred over `require` for files within the same project because it uses paths relative to the current file, making code more portable and less dependent on the load path. This change updates internal requires to use `require_relative` for consistency, performance, and improved portability. Retain `require` for dynamic paths in `lib/slim_lint.rb` because they use absolute paths. Refs: - rubocop/rubocop#8748 - sds/haml-lint#521
In case it helps, the way I see it, in general, every path is "relative" unless it starts with a In the case of requires within a gem, what is nearly always desired is relative to the current file (requirrelative to gem's root could make sense too, but that would be extra work to figure out with little benefit) . This is what |
`require_relative` is preferred over `require` for files within the same project because it uses paths relative to the current file, making code more portable and less dependent on the load path. This change updates internal requires to use `require_relative` for consistency, performance, and improved portability. Retain `require` for dynamic paths in `lib/slim_lint.rb` because they use absolute paths. Refs: - rubocop/rubocop#8748 - sds/haml-lint#521
`require_relative` is preferred over `require` for files within the same project because it uses paths relative to the current file, making code more portable and less dependent on the load path. This change updates internal requires to use `require_relative` for consistency, performance, and improved portability. Retain `require` for dynamic paths in `lib/slim_lint.rb` because they use absolute paths. Refs: - rubocop/rubocop#8748 - sds/haml-lint#521
Can you confirm any actual speed-up? I'm having mixed results, it appears to be no differences between the two versions in a third-party application after a few runs |
From the rubocop thread you linked it sounds like this potentially has more of an impact on Windows machines? But perhaps there's a reason that issue was never closed. |
I've tested the released gems also on windows and unfortunately, at least on my environment, there are no performance differences
|
Thank you for performing the benchmarks. Agree even if there's no significant difference it's still worth keeping the use of require_relative. |
require_relative
is preferred overrequire
for files within the same project because it uses paths relative to the current file, making code more portable and less dependent on the load path.This change updates internal requires to use
require_relative
for consistency, performance, and improved portability.Retain require for dynamic paths in
lib/haml_lint.rb
because they use absolute paths.Refs:
require_relative
rubocop/rubocop#8748Hello, this should also provide a minor performance improvement in loading time because require_relative is
O(1)
, but please confirm my findings./bin/haml-lint --version
(with changes to prefer the current library) appears to be 7%/4.5% faster on M1 Pro, according to Ruby version and architectureIf this is going to be accepted, I'll do the same for
slim-lint