Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Nominalized Adjectives as Names for Decorators

DZone's Guide to

Nominalized Adjectives as Names for Decorators

Some standard naming conventions can actually work against your code's readability. Here's some advice on naming your decorators.

· Java Zone ·
Free Resource

Bring content to any platform with the open-source BloomReach CMS. Try for free.

There is a strong tendency among Java and C# programmers to prefix or suffix their extended types, such as naming a “smart” View as SmartView or a Work that is “delegated” as DelegatingWork. In this post, I will focus on decorators and how this widespread naming scheme reduces readability and adds no value to the code’s context. I think it’s time that we retire this needless naming redundancy.

Office SpaceMilton, from Office Space

Composable decorators are small, highly cohesive objects that work off of another instance of their same type and, thus, are unable to function on their own. You can think of decorators as adjectives.

  final Collection<Product> products = new FilteredCollection<>(
      Product::active,
      new MappedCollection<>(
          Subscription::product,
          new JoinedCollection<>(
              subscriptions1,
              subscriptions2,
              ...
          )
      )
  );


The problem with the traditional naming scheme is the needless repetition — we know from the outset that products is a Collection, but the code keeps hammering this point over and over again as we read on. This code is tedious to write, but, more importantly, it is tedious to read because of how the words are composed:

'Product’ is a filtered collection, a mapped collection, and a joined collection

Normal, everyday speech is not encumbered like this; nouns are routinely omitted when sufficient meaning can be extracted from the context. You don’t normally say The rich people and the poor people.You just say the rich and the poor. Nouns are omitted and adjectives are nominalized.

Following this same principle, let's make the code below read like this:

‘Product’ is a filtered, mapped, joined collection

It would have to look like this:

  final Collection<Product> products = new Filtered<>(
      Product::active,
      new Mapped<>(
          Subscription::product,
          new Joined<>(
              subscriptions1,
              subscriptions2,
              ...
          )
      )
  );


I recommend that we make our code more terse by removing redundancy and allowing the code’s context to work in our favor for readability’s sake. For example, let’s use nominalized adjectives as names for our decorators.

BloomReach CMS: the API-first CMS of the future. Open-source & enterprise-grade. - As a Java developer, you will feel at home using Maven builds and your favorite IDE (e.g. Eclipse or IntelliJ) and continuous integration server (e.g. Jenkins). Manage your Java objects using Spring Framework, write your templates in JSP or Freemarker. Try for free.

Topics:
decorator pattern ,class names ,java ,product ,adjective ,collection

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}