Skip to content
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

Flag locally declared instance variables #162

Open
mattrberry opened this issue Jul 18, 2020 · 2 comments
Open

Flag locally declared instance variables #162

mattrberry opened this issue Jul 18, 2020 · 2 comments
Labels

Comments

@mattrberry
Copy link

Hey all!

One feature I'd love to see here (well, I'd really love to see it in the compiler but I don't think that's going to happen) is the ability to flag instance variables that are defined in a local scope but haven't been declared at a in the body of the class (or initialize).

As an example of what I mean, take the following

class Foo
  @enabled : Bool = false
  def bar : Nil
    @enable = true
  end
end

What I'd really like to do in the bar method is update the @enabled instance variable to true. However, I've accidentally misspelled the variable, creating an entirely new instance variable that is not used anywhere else.

In my opinion, newly-created instance variable outside of the scope of the class body or the initialize method are usually mistakes, especially if they're never referenced anywhere else. I'd love to have a tool like ameba alert me of issues like this.

P.S.
Proper unit testing would probably be a solution to problems like this, so maybe it isn't entirely necessary. However, in my experience working on CryBoy, my approach to testing has transitioned away from unit tests and towards test roms to run through the emulator. I run into problems as demonstrated above more often than I'd like to share... 😬

@veelenga
Copy link
Member

Your suggestion totally makes sense. I will try to implement a rule to catch this.

However, there could be some exceptions, like variable caching:

def bar
  @bar ||= foo
end

@bar will be used nowhere since the main purpose is to cache the result of foo.

@mattrberry
Copy link
Author

Awesome, thank you!! At least having this be a configurable option would be awesome :)

And I agree that there are definitely valid use-cases. In the example you've provided, I'd personally still write it more like this since I'm a fan of declaring instance variable ahead of time. But not everyone shares my preferences :)

class Baz
  @bar : String?
  def bar=(foo : String) : Nil
    @bar ||= foo
  end
end

@Sija Sija added rule and removed feature labels Oct 30, 2022
@Sija Sija changed the title [Feature Request] Flag locally declared instance variables Flag locally declared instance variables Nov 25, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants