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

kind_of? vs is_a? #307

Closed
anthonyryan1 opened this issue Mar 31, 2014 · 12 comments
Closed

kind_of? vs is_a? #307

anthonyryan1 opened this issue Mar 31, 2014 · 12 comments

Comments

@anthonyryan1
Copy link

My understanding is that the kind_of? and is_a? methods are exactly the same.

I would like to propose defining a guideline for the use of theses two methods. I recently encountered code that used both of these in random places, and felt like it harmed the quality of the code. Upon consulting this guide I felt this was something that could be improved upon.

I would propose:
kind_of? makes sense when extending classes: cat.kind_of?(Animal)
is_a? is preferable with all built-in types: input.is_a?(Hash)

@eclubb
Copy link

eclubb commented Mar 31, 2014

is_a? matches on an exact class name, while kind_of? will match the given class and any sub-classes. Because of this, I tend to use kind_of? almost exclusively. This lets me use a child class in place of its parent class without breaking the test.

@anthonyryan1
Copy link
Author

Are you certain? The documentation appears to indicate that this behavior is identical: http://ruby-doc.org/core-2.1.1/Object.html#method-i-is_a-3F

@eclubb
Copy link

eclubb commented Mar 31, 2014

Hrm... I was certain, but it appears my memory has failed me. I ran into an issue a long time ago that caused me to prefer kind_of?. Might have been trying to use instance_of? or some such. Anyhow I take back my original comment. Apologies.

@bbatsov
Copy link
Collaborator

bbatsov commented Apr 14, 2014

I guess someone should measure what's more popular in the wild. I have a feeling it will be is_a?.

@NARKOZ
Copy link

NARKOZ commented May 4, 2014

The method is_a? is used before class names starting with a vowel sound

0.is_a? Fixnum

Where the class name begins with a consonant sound, kind_of? is used

0.kind_of? Array

@sos4nt
Copy link

sos4nt commented May 21, 2014

Comment from http://stackoverflow.com/questions/3893278/ruby-kind-of-vs-instance-of-vs-is-a#comment4147181_3893305

It just reads better sometimes. Think @honda.kind_of? Car and @person.is_a? Administrator, Ruby's all about the aesthetics. In fact, notice the grammatical error... with active support you can write @person.is_an? Administrator :)... That might have made it into Ruby core by now, actually.

@bbatsov
Copy link
Collaborator

bbatsov commented May 21, 2014

@NARKOZ I guess you meant to say the opposite. Even so, given Ruby's authors total disregard for rules of English such suggestions seem pretty far fetched.

@bbatsov
Copy link
Collaborator

bbatsov commented Jun 8, 2014

Has anyone looked into this further?

@fuadsaud
Copy link
Contributor

I agree with the comment quoted by @sos4nt. It's the same as with inject/reduce. I'm not sure if a guideline can be defined easily in this case.

@jdickey
Copy link
Contributor

jdickey commented Jun 10, 2014

I'd tend to agree with @fuadsaud (and hence presumably with @sos4nt). Given that, it's hard to make a rule that's both unambiguous and comprehensively correct, isn't it, and that would cause problems for automation. Not saying the Style Guide shouldn't say something about it, but enforcing the convention with RuboCop would be an interesting challenge, yes?

@anthonyryan1
Copy link
Author

I have looked into this more and the move I've looked the more I'm observing different projects using different rules to distinguish when one or the other is used. Sometimes there's no pattern (which is what bothered me initially into creating this).

Given the validity of all the examples and rules brought forward in this issue, I agree it would be too hard to define a rule that would universally benefit people.

@bbatsov
Copy link
Collaborator

bbatsov commented Jun 10, 2014

@jdickey Doing a cop for that should be pretty straight-forward. Guess you can open a ticket so we won't forget about this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants