1

So, it seems like those one letter application names are becoming way too popular, but if we must deal with them, what would be the conventional way to name a Java class?

Note : we are not talking about interfaces/implementations convention names but uses of application names in code, like iCode, jQuery, eSomething, etc.

For example, which variation would be correct, or atleast is more often found in the real world.

public class ICode..

..

public class iCode..

..

public class Icode..
wattostudios
  • 8,666
  • 13
  • 43
  • 57
Andy
  • 8,841
  • 8
  • 45
  • 68
  • 4
    Is this about interface naming conventions? In that case, don't prefix! As described here: http://stackoverflow.com/questions/541912/interface-naming-in-java – Jonas Bötel Mar 30 '11 at 16:47
  • It's not a real class, just an example. Say my application is named "ICode App", what should I name my util class "ICodeUtility" – Andy Mar 30 '11 at 16:49
  • Avoid using the `I` if your code is a library or framework. Most people infer the `I` to mean an interface, unless you plan on it being an interface. In that case, I refer you to @LumpN's comment. – Jeremy Mar 30 '11 at 16:53
  • @LumpN It's not about interfaces, as Tanguy graciously pointed out this is a generic question about libraries like jQuery, eSomething...etc – Andy Mar 30 '11 at 18:11

8 Answers8

2

Java convention dictates that each word in a class name starts with a capital, so

public class ICode

seems most appropriate.

That being said, this doesn't say much for what it's supposed to do... so perhaps neither of them is the best name

What is the class supposed to do? You should be able to tell what a class does by reading the class name.

Furthermore, some conventions dictate that INTERFACES start with a capital I. So naming a CLASS ICode would be ill-advised, instead it seems something like

public class MyCode implements ICode

Although, if your users are all mac-fanboys, maybe this would be best:

public class iCode /* on my iPad */ {
corsiKa
  • 81,495
  • 25
  • 153
  • 204
2

By convention:

public interface Code

for an interface and

public class CodeImpl implements Code
public abstract class AbstractCode implements Code

for classes that implement that interface.

Derek Mahar
  • 27,608
  • 43
  • 124
  • 174
Stan Kurilin
  • 15,614
  • 21
  • 81
  • 132
  • I'm not going to -1, but I've always hated `*Impl` classes. – corsiKa Mar 30 '11 at 16:52
  • I hate `*Impl` class as well, but I +1 because I agree with the interface naming convention. I tend to name my implementations with intuitive names. Like a `UserMapper` would be implemented by `UserHibernateMapper`. – Jeremy Mar 30 '11 at 16:54
  • 1
    @Stas they provide no information about what it does or why it does it that way. Just imagine how useless would `java.util.MapImpl` be. – corsiKa Mar 30 '11 at 16:54
  • 1
    @Stas that's true. That being said, I've never seen `My` in production code, but I have seen `*Impl`. – corsiKa Mar 30 '11 at 16:57
  • @Jeremy, @glowcoder I use *Impl classes when I have single implementation, but I wan't work with some abstraction for different reasons (it can be design reasons or technical, like DI, AOP and so on ) – Stan Kurilin Mar 30 '11 at 16:58
  • @Stas: I understand, and sometimes I fall back to `Impl` as well, but all I was saying is that I try to make the implementation names descriptive if I can. Which I think is good practice. – Jeremy Mar 30 '11 at 17:00
  • @Jeremy, I think it's good practice too. But not in all cases. – Stan Kurilin Mar 30 '11 at 17:02
  • I'm not crazy about *Impl classes, either. – Derek Mahar Mar 30 '11 at 17:05
  • 1
    @Stas Programming to an interface is a good practice, but programming to an interface for a single implementation is an unnecessary layer. If you're programming for a single implementation and need to go to a second, then refactor to use an interface (you only have one class to refactor.) Only if you really anticipate other implementations should you use an interface from the start. – corsiKa Mar 30 '11 at 17:05
  • @glowcoder, OK. I'll provide you real example. Did you hear about GWT? There are some feature - Remote services (http://code.google.com/webtoolkit/doc/1.6/DevGuideServerCommunication.html#DevGuidePlumbingDiagram). There are Service interface that used by client code (Javascript compiled from Java) and server's Service implementation. What name would you suggest for this implementation? – Stan Kurilin Mar 30 '11 at 17:11
  • @Stas that's not a real example. Unless `com.example.foo.server` and the `MyService` interface are considered real. But I'll go along. What is MyService supposed to do? The `myMethod` method is supposed to do something. Whatever the purpose of the class is, and how the class works, that's what I would name it. – corsiKa Mar 30 '11 at 17:31
  • @glowcoder, I mean, I have smt like UserService that I pass to client. And I should have some implementation that would be probably same for long time. – Stan Kurilin Mar 30 '11 at 17:35
  • @Stas I consider it a poor decision to call any interface `UserService`. I would much prefer my interface to be a "verbing adjective." Now this can be improved upon, because it's just off the top of my head, but `class UserService implements UserServable` comes to mind. In the future if I need a new implementation (say that deals with banned users), I'm not left with `UserServiceImpl` and `BannedUserServiceImpl` but rather `UserService` and `BannedUserService`. – corsiKa Mar 30 '11 at 17:53
  • @glowcoder, I get your point. But if I interact with `UserServiceImpl` only throw `UserService` there isn't any trouble too change it's name.. And so on. So, let's say in this way. We _can_ use *Impl pattern and it is not bad practices in _all cases_ but we should understand it's disadvantages. Is it OK? – Stan Kurilin Mar 30 '11 at 18:00
  • 1
    @Stas to me, the most important thing is what works for you and your team. If `*Impl` works for your team, and you guys put out rock-star code, I say go for it! I personally don't feel it's a naming convention that I would use in any situation, but like most conventions house rules trump. I'm currently at a shop where everyone uses Hungarian Notation. I hate it, detest it, and find it to be counter productive. But I'm the only one that feels that way, and so I move forward with it. Similiarly, if I were in your shop, I would use `*Impl` because that's what the local convention is. – corsiKa Mar 30 '11 at 18:06
  • @glowcoder Good words. I wish you find shop with notations that you like. – Stan Kurilin Mar 30 '11 at 18:08
1

From my point of view, the first one, the ICode, is the most readable option. It starts with capital I as classes should start and the rest is in camel case notation.

Kojotak
  • 2,020
  • 2
  • 16
  • 29
  • 1
    That's actually referred to as "Capital Case". thisIsCamelCase, ThisIsCapitalCase, and of course, THIS IS SPARTA (CASE). – corsiKa Mar 30 '11 at 17:03
0

The convention would be ICode, but note that this is mostly used for interfaces in the C, C++, C#-world rather than in the Java world. Or where did your observation come from?

Puce
  • 37,247
  • 13
  • 80
  • 152
0

I always use a strict convention where the first letter is capitalized even though the real name would be with a small letter. The reason for my choice I think is that a small letter at start looks like an interface-prefix.

I have no references to back up my choice, it's pure personal taste.

So, I would go for

public class ICode..
Xenovoyance
  • 330
  • 1
  • 2
  • 10
0

there are many conventions to name a class and this change from one language to another. In java its commond to add the prefix I only when you are using interfaces and its a good practice to name a class begining with an upper case letter

Dave
  • 532
  • 5
  • 8
0

Well public class ICode is the correct way to use as far as IKnow. The rule of thumb - first letter upper-case and first letter in every word upper-case.

MByD
  • 135,866
  • 28
  • 264
  • 277
0

You can find all about Java naming conventions in this document, it is always a great reference. What it says about class names is:

"Class names should be nouns, in mixed case with the first letter of each internal word capitalized. Try to keep your class names simple and descriptive. Use whole words-avoid acronyms and abbreviations (unless the abbreviation is much more widely used than the long form, such as URL or HTML)."

and interfaces names:

"Interface names should be capitalized like class names."

So in this case I think the correct way would be ICode, however if the "I" means something I I think is better to use the whole name, like for example let's say "I" is for "Instant", so it is clearer if the class is named InstantCode instead and if the "I" doesn't mean much maybe is better to just call the class Code.

Maricel
  • 2,089
  • 13
  • 17