0

Classes should be nouns and methods should be verbs. What about Groovy traits? Should they also be nouns? Or maybe should they be adjectives / ability words?

3

Finding inspiration from other languages (C# in this case), interfaces are generally used as traits, i.e. particular behaviors. For example, consider IDisposable and IEnumerable.

You see the same thing happen on the page you linked:

class Person implements Greetable {                     
    String name() { 'Bob' }                             
}

However, keep in mind that interfaces can in some cases be a noun. For example, IList.

You can also see the same thing happen on the page you linked:

trait FlyingAbility {                           
    String fly() { "I'm flying!" }          
}

When you think about it, an adjective wouldn't have worked here. "Flyable" does not mean "able to fly" (e.g. a bird is able to fly), it means "able to be flown" (e.g. a plane can be flown by a pilot). In order to avoid confusion, the noun form was a better choice here.

Trying to distill a naming description for interfaces, the focus is not really on whether it's a noun or an adjective, but rather that it describes an ability. This can be done in noun form (FooAbility) or adjective form (FooAble), but the intention is the same: pointing out the ability to do/be something.

Whether you choose a noun or an adjective depends on the semantics surrounding the word (in the language that your code is written in, which is not always English).

Sidenote
In codebases that use dependency injection, core classes (Foo) get a boilerplate interface (IFoo). This breaks the naming convention of pointing out an ability, unless you read it as meaning "able to behave as a Foo", which I don't quite agree with. In my opinion that inverts the logic. We should instead think of it as "Foo can behave as an IFoo" (because of Foo : IFoo).

Regardless, DI interfaces have a slightly different naming convention, but they are not really used as "traits" anyway, so I'm choosing to ignore them for your specific question on traits.


Edit

Come to think of it, the distinction between noun/adjective seems to be related to active/passive voice.

  • (adjective) Flyable = able to be flown (passive)
  • (adjective) Throwable = able to be thrown (passive)
  • (adjective) Flammable = able to be set on fire (passive)

  • (noun) FlyingAbility = able to fly (active)

  • (noun) ThrowingAbility = able to throw something (active)
  • (noun) FireAbility = able to set fire to things (active)

I'm not sure if this is universally applicable in English. I'm not sure if this applies to other languages.

  • Flyable = able to be flown. "Flow" means something else entirely. :) – Neil Jul 11 '18 at 7:36
  • 1
    @Neil: Edited the typo. – Flater Jul 11 '18 at 7:56
  • "volitational"? – Ewan Jul 11 '18 at 9:45
  • @Ewan Volitational is defined as "relating to flying". That's not the same as "able to fly". It doesn't describe an ability, it denotes a relation. – Flater Jul 11 '18 at 9:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.