Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Ruby, what are the naming conventions for mixins when there is no proper english "<verb>able"?

Example: I have a Person class and a Role class. I want to extract the role functionality from both Role and Person into a mixin. I get two mixins: Actable to be included in Role and Adaptable for Person.

I sometimes can't think of a proper english word that fits. In the above example, adaptable seems more fitting for roles than for people, because roles can be adapted and thus are adaptable. A better name would be versatile but that breaks the convention of mixin names with ending in ...able. (It's still an adjective though.)

Other example is when I want to add a module that allows checking, if a Person is allowed to view other persons. How do you find a name for that ending in ...able? Viewable seems impropriate, the person that can be viewed should be viewable. I stuck to showable for now (fitting with the method name in the controller). But then again, it's the Person that can be shown, that should be showable not the person that wants to show.

So I guess my question is, When there is no proper english adjective ending in ...able for a module name, should I choose another adjective that does not end in ...able or should I give it a name that does not quite fit?

like image 648
Conkerchen Avatar asked May 17 '13 07:05

Conkerchen


People also ask

What are the conventions for naming methods in Ruby?

By convention, method names begin with a lowercase letter. (Method names can begin with a capital letter, but that makes them look like constants.) When a method name is longer than one word, the usual convention is to separate the words with underscores like_this rather than using mixed case likeThis .

What are Mixins in Ruby?

Mixins in Ruby allows modules to access instance methods of another one using include method. Mixins provides a controlled way of adding functionality to classes. The code in the mixin starts to interact with code in the class. In Ruby, a code wrapped up in a module is called mixins that a class can include or extend.


2 Answers

How about a prefix then?

CanDoSomethingMoreComplexThanASingleVerb

edit: While I understand I'm not offering a convention per se (thanks arulmr for bringing it up), the Can Naming convention to describe something is part of de-facto localised naming standards in many companies, as it allows a more verbose description of the functionality you're trying to mixin. e.g. CanAct and CanBeCastInPlay are distinct properties using the original author's context of Person and Role and are easier to percieve and understand than Actable and PlayCastable or other equivalents.

Bottom line: Don't be constrained by language and conventions alone :)

like image 155
Fox Avatar answered Sep 30 '22 18:09

Fox


I tend to go verbose and name modules with a longer description of the code they include; something like: PersonRoleFunctionality

I'll often namespace it too; and file it with other mixins under a directory that applies to the object it was extracted from, so I end up with include Extracts:Person:RoleFunctionality in the app\models\extracts\person\ directory

like image 31
Pavling Avatar answered Sep 30 '22 17:09

Pavling