2020-10-30 14:08:56 -04:00
---
stage: none
group: unassigned
2020-11-26 01:09:20 -05:00
info: To determine the technical writer assigned to the Stage/Group associated with this page, see https://about.gitlab.com/handbook/engineering/ux/technical-writing/#assignments
2020-10-30 14:08:56 -04:00
---
2019-07-24 09:10:06 -04:00
# Modules with instance variables could be considered harmful
2017-07-11 14:29:33 -04:00
2019-07-24 09:10:06 -04:00
## Background
2017-07-11 14:29:33 -04:00
Rails somehow encourages people using modules and instance variables
everywhere. For example, using instance variables in the controllers,
helpers, and views. They're also encouraging the use of
`ActiveSupport::Concern` , which further strengthens the idea of
saving everything in a giant, single object, and people could access
everything in that one giant object.
2019-07-24 09:10:06 -04:00
## The problems
2017-07-11 14:29:33 -04:00
Of course this is convenient to develop, because we just have everything
within reach. However this has a number of downsides when that chosen object
is growing, it would later become out of control for the same reason.
There are just too many things in the same context, and we don't know if
those things are tightly coupled or not, depending on each others or not.
It's very hard to tell when the complexity grows to a point, and it makes
tracking the code also extremely hard. For example, a class could be using
3 different instance variables, and all of them could be initialized and
manipulated from 3 different modules. It's hard to track when those variables
start giving us troubles. We don't know which module would suddenly change
one of the variables. Everything could touch anything.
2019-07-24 09:10:06 -04:00
## Similar concerns
2017-07-11 14:29:33 -04:00
People are saying multiple inheritance is bad. Mixing multiple modules with
multiple instance variables scattering everywhere suffer from the same issue.
The same applies to `ActiveSupport::Concern` . See:
[Consider replacing concerns with dedicated classes & composition](
2020-05-21 02:08:25 -04:00
https://gitlab.com/gitlab-org/gitlab/-/issues/16270)
2017-07-11 14:29:33 -04:00
There's also a similar idea:
[Use decorators and interface segregation to solve overgrowing models problem](
2020-05-21 02:08:25 -04:00
https://gitlab.com/gitlab-org/gitlab/-/issues/14235)
2017-07-11 14:29:33 -04:00
Note that `included` doesn't solve the whole issue. They define the
dependencies, but they still allow each modules to talk implicitly via the
instance variables in the final giant object, and that's where the problem is.
2019-07-24 09:10:06 -04:00
## Solutions
2017-07-11 14:29:33 -04:00
We should split the giant object into multiple objects, and they communicate
2021-07-19 14:08:23 -04:00
with each other with the API, that is, public methods. In short, composition over
2017-07-11 14:29:33 -04:00
inheritance. This way, each smaller objects would have their own respective
2021-07-19 14:08:23 -04:00
limited states, that is, instance variables. If one instance variable goes wrong,
2017-07-11 14:29:33 -04:00
we would be very clear that it's from that single small object, because
no one else could be touching it.
With clearly defined API, this would make things less coupled and much easier
to debug and track, and much more extensible for other objects to use, because
they communicate in a clear way, rather than implicit dependencies.
2019-07-24 09:10:06 -04:00
## Acceptable use
2017-07-11 14:29:33 -04:00
2017-11-21 10:15:24 -05:00
However, it's not always bad to use instance variables in a module,
as long as it's contained in the same module; that is, no other modules or
objects are touching them, then it would be an acceptable use.
2017-07-11 14:29:33 -04:00
2017-09-18 13:25:23 -04:00
We especially allow the case where a single instance variable is used with
2018-09-19 12:03:00 -04:00
`||=` to set up the value. This would look like:
2017-07-11 14:29:33 -04:00
2021-06-02 02:09:48 -04:00
```ruby
2017-09-18 13:25:23 -04:00
module M
def f
@f ||= true
end
end
```
Unfortunately it's not easy to code more complex rules into the cop, so
2017-09-19 06:33:47 -04:00
we rely on people's best judgement. If we could find another good pattern
we could easily add to the cop, we should do it.
2017-09-18 13:25:23 -04:00
2019-07-24 09:10:06 -04:00
## How to rewrite and avoid disabling this cop
2017-09-18 13:25:23 -04:00
Even if we could just disable the cop, we should avoid doing so. Some code
2017-11-22 03:34:52 -05:00
could be easily rewritten in simple form. Consider this acceptable method:
2017-09-18 13:25:23 -04:00
2021-06-02 02:09:48 -04:00
```ruby
2017-09-18 13:25:23 -04:00
module Gitlab
module Emoji
def emoji_unicode_version(name)
@emoji_unicode_versions_by_name ||=
JSON.parse(File.read(Rails.root.join('fixtures', 'emojis', 'emoji-unicode-version-map.json')))
@emoji_unicode_versions_by_name [name]
end
end
end
```
2017-11-22 03:34:52 -05:00
This method is totally fine because it's already self-contained. No other
methods should be using `@emoji_unicode_versions_by_name` and we're good.
However it's still offending the cop because it's not just `||=` , and the
cop is not smart enough to judge that this is fine.
On the other hand, we could split this method into two:
2017-09-18 13:25:23 -04:00
2021-06-02 02:09:48 -04:00
```ruby
2017-09-18 13:25:23 -04:00
module Gitlab
module Emoji
def emoji_unicode_version(name)
emoji_unicode_versions_by_name[name]
2017-07-11 14:29:33 -04:00
end
2017-09-18 13:25:23 -04:00
private
def emoji_unicode_versions_by_name
@emoji_unicode_versions_by_name ||=
JSON.parse(File.read(Rails.root.join('fixtures', 'emojis', 'emoji-unicode-version-map.json')))
end
2017-07-11 14:29:33 -04:00
end
end
```
2021-01-27 04:09:01 -05:00
Now the cop doesn't complain.
2017-07-11 14:29:33 -04:00
2019-07-24 09:10:06 -04:00
## How to disable this cop
2017-11-21 11:59:16 -05:00
Put the disabling comment right after your code in the same line:
2021-06-02 02:09:48 -04:00
```ruby
2017-11-21 11:59:16 -05:00
module M
def violating_method
2017-11-22 02:50:36 -05:00
@f + @g # rubocop:disable Gitlab/ModuleWithInstanceVariables
2017-11-21 11:59:16 -05:00
end
end
```
If there are multiple lines, you could also enable and disable for a section:
2021-06-02 02:09:48 -04:00
```ruby
2017-11-21 11:59:16 -05:00
module M
2017-11-22 02:50:36 -05:00
# rubocop:disable Gitlab/ModuleWithInstanceVariables
2017-11-21 11:59:16 -05:00
def violating_method
@f = 0
@g = 1
@h = 2
end
2017-11-22 02:50:36 -05:00
# rubocop:enable Gitlab/ModuleWithInstanceVariables
2017-11-21 11:59:16 -05:00
end
```
2020-12-14 13:09:48 -05:00
Note that you need to enable it at some point, otherwise nothing below
that point is checked.
2017-11-21 11:59:16 -05:00
2019-07-24 09:10:06 -04:00
## Things we might need to ignore right now
2017-07-11 14:29:33 -04:00
2017-11-21 10:15:24 -05:00
Because of the way Rails helpers and mailers work, we might not be able to
2017-07-11 14:29:33 -04:00
avoid the use of instance variables there. For those cases, we could ignore
2020-12-14 13:09:48 -05:00
them at the moment. Those modules are not shared with
2017-11-21 10:15:24 -05:00
other random objects, so they're still somewhat isolated.
2017-07-11 14:29:33 -04:00
2019-07-24 09:10:06 -04:00
## Instance variables in views
2017-07-11 14:29:33 -04:00
2017-11-22 03:10:19 -05:00
They're bad because we can't easily tell who's using the instance variables
(from controller's point of view) and where we set them up (from partials'
point of view), making it extremely hard to track data dependency.
2017-07-11 14:29:33 -04:00
We're trying to use something like this instead:
2021-06-02 02:09:48 -04:00
```haml
2017-07-11 14:29:33 -04:00
= render 'projects/commits/commit', commit: commit, ref: ref, project: project
```
And in the partial:
2021-06-02 02:09:48 -04:00
```haml
2017-07-11 14:29:33 -04:00
- ref = local_assigns.fetch(:ref)
- commit = local_assigns.fetch(:commit)
- project = local_assigns.fetch(:project)
```
2017-11-22 03:10:19 -05:00
This way it's clearer where those values were coming from, and we gain the
benefit to have typo check over using instance variables. In the future,
2017-07-11 14:29:33 -04:00
we should also forbid the use of instance variables in partials.