Method Name Collisions

Lately, I have run into a number of method name collisions in my Ruby and Javascript development. I want to document them for myself.

A method name collision is when you use a word that is already defined by the language but is not a reserved word. Generally things will compile just fine, it is only when the code is running that you experience a collision between the existing functionality and your new functionality.

call

I have been doing a lot of Twilio development. We needed to track phone calls. I first called the object Call. Naming something call, like a PhoneCall is a bad idea. The method works as a class name, but when you start using it as an ActiveRecord relationship that is when you run into trouble.

A call has many conversations.

# example of the problem
c = Conversation.last  
c.call  

You would think that c.call would show you the call, but in reality you just tried the ruby call class method. I solved the problem by renaming the class from Call to PhoneCall.

# renamed version
c = Conversation.last  
c.phone_call  

send

Again working with the Twilio API, this time I was working with SMS messages. I had the TextMessage model and could send it with the .send method.

# example of the problem
m = TextMessage.create(body: 'Hello World!', phone: '+1234567890')  
m.send  

This time .send was not calling the TextMessage.send method I wrote. Instead it was calling the ruby .send method. I renamed send to deliver.

# renamed version
m = TextMessage.create(body: 'Hello World!', phone: '+1234567890')  
m.deliver  

method

For method I was doing some meta programming. I allow the model to specify a dynamic method that will be called. This time, I was getting a syntax error. Renaming it from method to dynamic_method fixed it.

Conclusion

Be mindful of method names that might already be in use, but are not reserved words in the language. Often times these words will work fine as class names, but fall apart when you use them as method names. The side effects can hard to debug.