21

I'm running into more and more naming clashes between Android activities and other classes. I was wondering if you could tell me how you avoid these. Sadly, my particular naming problems are not covered in the related questions on SO.

First example

I have an activity that displays a level of the game. However, the data required for that level (background artwork, entities etc.) is stored in a separate class. Naturally, I would call the latter class Level. However, I would call the activity Level as well, because it displays levels.

Second example

I have an activity that plays back a cut scene. It basically displays several images in a row. The information which image is shown for how long is stored in a separate class. As in the previous case, I would naturally call both classes CutScene.

How would you solve these naming issues? Name the activities LevelActivity and CutSceneActivity? Name the representation classes LevelModel and CutSceneModel? Something else?

futlib
  • 8,258
  • 13
  • 40
  • 55

2 Answers2

19

I solve those problems by either prefixing or postfixing classes with their "type", like you suggested at the end of your question :

  • LevelActivity, GameActivity, MainActivity, ...
  • CommentsListAdapter, ...
  • CheckNewCommentsService, ...
  • and so on.

But I generally do an execption for the model classes, which are the objects that contain that data : I would still name my Level model class Level, and not LevelModel, to indicate I'm manipulating, and working with, a Level.


Another solution (longer to type ^^) might be to use fully-qualified names (see here) when referencing your classes :

  • com.something.yourapp.activity.Level
  • com.something.yourapp.model.Level

With this, you always know which class is really used.

Pascal MARTIN
  • 395,085
  • 80
  • 655
  • 663
  • So you add the "Activity" suffix to all activity classes? I like that, but the Android developer guide gives the impression that Google doesn't do this. Would you wager they don't run into these problems (hard to believe!) or do they have another solution? – futlib Feb 26 '11 at 11:49
  • 1
    Just had a look at some random stock Android app and they add "Activity" to activities too! https://github.com/android/platform_packages_apps_music/tree/master/src/com/android/music – futlib Feb 26 '11 at 12:00
  • Glad you answered to yourself ;-) – Pascal MARTIN Feb 26 '11 at 12:05
2

In general the best way to name android application components is to add its "component type" as suffix. Example :-

  • LevelActivity (LevelActivity extends Activity)
  • InboxUpdateService (InboxUpdateService extends Service)
  • ContactsContentProvider (ContactsContentProvide extends ContentProvider)
  • SMSBroadcastReceiver (SMSBroadcastReceiver extends BroadcastReceiver)

By naming using above method there will be minimal chances of losing track when you're working on big code flow with lots of similar names in your application.

So, name your Activities with suffix "Activity".

And name the Class which provides Data to your LevelActivity as Level.

In Contradiction to second part of Pascal MARTIN's answer, you can also use LevelActivity and LevelInfo together. Because they offer clear difference as quoted below:

Distinguish names in such a way that the reader knows what the differences offer - Robert. C. Martin, author of Clean Code

But the suffix are often redundant on cognitive basis. Using only the word Level clearly emphasises that class Level offers information about Level. So, use Level for class that provides data about Level.

NOTE : If you're using suffixes, choose one word per concept. For Example: If you're using the suffix Info to identify classes that offer information then only Info should be used (not Data or Model) throughout your application to avoid confusions.

Harley
  • 46
  • 6
  • Funny that you quote Robert C. Martin right below suggesting to name a class LevelInfo - Martin explicitly discourages the use of "Info" or "Data" suffixes in his book as they are not clear and offer no distinction from using just the domain object name itself. How is Level different from LevelInfo? – jiroch May 20 '19 at 11:08