ruby picture

RCR 290: Add warning for misspellings of 'initialize'

Submitted by flgr (Sat Jan 01 17:01:49 UTC 2005)

Abstract

'initialize' is an important callback done by Class#new because it is used for initializing object state. Misspelling it causes Exceptions at other methods of the Object which are hard to trace back. Therefore Ruby should issue warnings on potential misspellings of this method.

Problem

Ruby newcomers frequently have to deal with this. They get exceptions that do not directly relate to the problem like "undefined method `empty?' for nil:NilClass" -- it is not too hard for a long time Ruby user to go to the line associated with the exceptions and have a look at the instance variables involved and if he finds out that it is uninitialized he might get suspicious and have a look at the initialize method. If he is lucky he will detect the misspelling. Situations like this happen frequently and are not spotted. There's also the issue with "initialize" vs. "initialise".

Proposal

That's why I think that Ruby should use its warning infrastructure and do a warning for this if $-w is false or true. It could still be turned off by setting it to nil. In my Ruby implementation (see below) I have used the Regexp /\Ain[^_]{1,6}li[sz]e\Z/ for detecting misspellings, but this could also be implemented via a similar pure C check.

Analysis

This can unfortunately not be solved generally via a Ruby implementation because it would not be available to every newcomer by default -- if you already knew that you had mistyped the method name you would not need the warning anymore. Also note that a pure Ruby implementation would need to use Binding.of_caller for getting the original __LINE__ and __FILE__.

Implementation

class Class
  def method_added(name)
    if name.to_s[/\Ain[^_]{1,6}li[sz]e\Z/] and name != :initialize then
      warn "warning: probably misspelled `initialize' as `#{name}'"
    end
  end
end

ruby picture
Comments Current voting
I'll offer provisional support since only American English spells the word "initialize," but think that it should only warn about #initialise if and only if there is no #initialize defined in the class. Something like:

  class Class
    def method_added(name)
      unless self.instance_methods.include?("initialize")
        if name.to_s[/\Ainitiali[sz]e\Z/] and name != :initialize
          warn "warning: probably misspelled `initialize' as `#{name}'"
        end
      end
    end
  end

-austin


I agree with that it should only warn if the current Class does not already have an initialize method by itself. I disagree with that it should only warn about 'initialise' -- mistyping it in other ways is just way too common to ignore it. So I'd suggest this new implementation:

class Class
  def method_added(name)
    if not private_instance_methods(false).include?("initialize") and
      name.to_s[/\Ain[^_]{1,6}li[sz]e\Z/] and name != :initialize
    then
      warn "warning: probably misspelled `initialize' as `#{name}'"
    end
  end
end

--Florian Gross


Sigh, this problem would be practically mute if we simply used #init instead.


I agree, but I don't see the name getting changed soon, unfortunately.

-- Florian Gross


I often have a problem when I misspell initialize as "intialize". Tracking down the source of the problem isn't always straightforward. A warning would definitely help.

-- Paul Brannan


How about testing whether there is a method with a similar sounding name as "initialize" defined. Something like levenstein-measure or whatever else (approximate matching). To limit the effect, that should only be applied for methods that start with "init".

I got this several times wrong, mostly "initalize" IIRC.


That would work for me as well -- I only used the Regexp approach because Ruby comes with that built-in -- if Levenstein or Soundex measure is easier to implement and complains about the common typos without causing trouble in other cases then I'm definitely okay with that.

-- Florian Gross


I just don't feel that this belongs in Ruby. Any decent unit testing would quickly reveal this error.

-- Dan


Would it only reveal it or trace it back to the misnaming as well?

-- Florian Gross


Alternative solution, arbitrary initializer names:

  class Module
      def init(initializer_name)
            alias_method(:initialize, initializer_name.to_sym)
      end
  end
  class A
      def initialise
            ...
      end
      init :initialise
  end
  class B
     def make
         ...
     end
     init :make
  end
  class C
      def initialize
        ...
      end
      init :initialize
  end

If you misspell it again when you call init it raises an exception much earlier. If you don't misspell it again it works fine anyway.


This is a task much better suited to an IDE than an interpreter, just as typos should be caught by web browsers, not DNS servers.


I strongly oppose this suggestion.

  • Maybe it's just me but I've never had any problem spelling "initialize".
  • I think this has no place in the interpreter, like the previous comment says it's more the job of an IDE/editor.
  • This could potentially be very annoying, trying to define a method with a similar name.

As for changing the method name to "init", I find that less rubyistic. I think "init" is more cryptic and less readable.


Strongly opposed 3
Opposed 3
Neutral 0
In favor 6
Strongly advocate 2
ruby picture
If you have registered at RCRchive, you may now sign in below. If you have not registered, you may sign up for a username and password. Registering enables you to submit new RCRs, and vote and leave comments on existing RCRs.
Your username:
Your password:

ruby picture

Powered by .