본문 바로가기

StackOverflow

자바 인터페이스 이름관련: I로 시작하는 interface? impl로 끝나는 클래스?

stackoverflow.com/questions/2814805/java-interfaces-implementation-naming-convention

 

Java Interfaces/Implementation naming convention

How do you name different classes / interfaces you create? Sometimes I don't have implementation information to add to the implementation name - like interface FileHandler and class SqlFileHandler....

stackoverflow.com

 

Upvote: 363

 

Q: Java interfaces / Implementation naming convention

Interface naming in Java [closed] (11 answers)

Closed 5 years ago.

How do you name different classes / interfaces you create? Sometimes I don't have implementation information to add to the implementation name - like interface FileHandler and class SqlFileHandler.

When this happens I usually name the interface in the "normal" name, like Truck and name the actual class TruckClass. How do you name interfaces and classes in this regard?

 

edited Mar 11 '16 at 20:13 Joshua Taylor 80.5k
asked May 11 '10 at 22:12 Amir Rachum 66.8k 

 


질문: 자바 인터페이스 / 구현체의 이름을 짓는 법

클래스 / 인터페이스의 이름을 어떻게 지으시나요? 가끔 저는 구현된 클래스의 이름에 "구현" 과 관련된 정보를 더 넣을 수 없는 경우가 있습니다. 예를 들면 FileHandler 인터페이스와 SqlFileHandler 처럼 클래스에 적절한 상세정보를 넣을 수 있으면 좋을 것 같은데, 더 넣을만한 정보가 없습니다.

 

제 경우에는, 만약 인터페이스를 "일반적인" 이름으로 지어야 한다면, 예를 들어 트럭의 인터페이스라면 Truck 으로 짓고, 그 구현 클래스를 TruckClass 로 짓는 경우가 많습니다.

 

인터페이스 이름과 클래스 이름을 어떻게 지어야 할까요?

 


edited Mar 11 '16 at 20:13 Joshua Taylor 80.5k
asked May 11 '10 at 22:12 Amir Rachum 66.8k 

 

 

 

Answer:

Upvote: 960

Name your Interface what it is. Truck. Not ITruck because it isn't an ITruck it is a Truck.An Interface in Java is a Type. Then you have DumpTruck, TransferTruck, WreckerTruck, CementTruck, etc that implement Truck.

 

 

When you are using the Interface in place of a sub-class you just cast it to Truck. As in List<Truck>. Putting I in front is just Hungarian style notation tautology that adds nothing but more stuff to type to your code.

 

 

All modern Java IDE's mark Interfaces and Implementations and what not without this silly notation. Don't call it TruckClass that is tautology just as bad as the IInterface tautology.

 

 

If it is an implementation it is a class. The only real exception to this rule, and there are always exceptions, could be something like AbstractTruck. Since only the sub-classes will ever see this and you should never cast to an Abstract class it does add some information that the class is abstract and to how it should be used. You could still come up with a better name than AbstractTruck and use BaseTruck or DefaultTruck instead since the abstract is in the definition. But since Abstract classes should never be part of any public facing interface I believe it is an acceptable exception to the rule. Making the constructors protected goes a long way to crossing this divide.

 

 

And the Impl suffix is just more noise as well. More tautology. Anything that isn't an interface is an implementation, even abstract classes which are partial implementations. Are you going to put that silly Impl suffix on every name of every Class?

The Interface is a contract on what the public methods and properties have to support, it is also Type information as well. Everything that implements Truck is a Type of Truck.

 

 

Look to the Java standard library itself. Do you see IList, ArrayListImpl, LinkedListImpl? No, you see List and ArrayList, and LinkedList. Here is a nice article about this exact question. Any of these silly prefix/suffix naming conventions all violate the DRY principle as well.

 

 

Also, if you find yourself adding DTO, JDO, BEAN or other silly repetitive suffixes to objects then they probably belong in a package instead of all those suffixes. Properly packaged namespaces are self documenting and reduce all the useless redundant information in these really poorly conceived proprietary naming schemes that most places don't even internally adhere to in a consistent manner.

 

 

If all you can come up with to make your Class name unique is suffixing it with Impl, then you need to rethink having an Interface at all. So when you have a situation where you have an Interface and a single Implementation that is not uniquely specialized from the Interface you probably don't need the Interface.

 

edited Sep 4 '18 at 17:36

Dave Jarvis 28.6k

user177800

 

 

답변: 

당신의 Interface 이름을 그 의미 자체에 따라 지으세요. Truck 처럼요. ITruck 은 안됩니다. 그건 Truck 이 아니니까요.

 

 

만약 그 Interface 의 자리에 해당 인터페이스를 구현한 클래스가 들어간다면, 당신을 그걸 Truck 이라고 불러야 합니다. 예를 들면 List<Truck> 으로 쓸 수 있습니다. I 를 인터페이스 이름에 붙이는 것은 단순히 헝가리안 표기법 이고, 동어 반복 이라서 코드에 타입 정보를 넣는 것일 뿐입니다.

 

 

현대적인 IDE는 인터페이스와 구현체를 이런 웃긴 표기법 없이도 잘 표시해줍니다. 구현체를 TruckClass 로 부르지 마세요. 그건 동어 반복 이며, IInterface 라고 쓰는 것과 다를 바 없는 안좋은 표기법입니다.

 

 

인터페이스를 구현했다면, 그것은 클래스입니다. 유일한 예외가 있다면 (어디에나 예외는 있으니까요) AbstractTruck 과 같은 추상화클래스의 경우입니다. 이 추상클래스를 구현한 자식클래스에서만 이 클래스를 볼 것이고, 자식클래스는 절대로 Abstract 로 캐스팅 되지 않을 것이기 때문에, 이 클래스가 추상 클래스이고 이것을 어떻게 써야 하는지에 대한 정보를 제공하기 위해 이런 이름을 쓸 수 있습니다. 그리고 AbstractTruck 보다 좀 더 나은 이름을 쓸 수 있는데, BaseTruck 이나 DefaultTruck 같은 이름을 쓰고 abstract 와 같은 예약어를 피할 수 있습니다. 하지만 추상 클래스는 절대 외부로 노출되는 인터페이스에 포함되지 않아야 하기 때문에, 이 Abstract 라는 이름을 써도 외부에서는 볼 수 없으니 문제될 것은 없을 것 같습니다. 생성자를 protected 로 선언하면 명확하게 외부생성을 차단하는 것이 도움이 될 것입니다.

그리고 Impl 접미사는 좀 더 거슬리는 짓입니다. 더 심한 동어반복입니다. 인터페이스(Interface)가 아닌 모든 것은 구현(Implementation) 이고, 추상 클래스조차도 구현된 것을 일부 가지고 있을 수 있습니다. 모든 클래스 뒤에 Impl 을 넣고 싶습니까?

 

 

Interface 는 외부에 노출된 메소드와 지원되어야 하는 속성 사이의 계약이면서, 또한 일종의 Type 입니다. Truck 인터페이스를 구현한 모든 것의 TypeTruck 입니다. 

 

 

자바 스탠다드 라이브러리를 예로 들어 보겠습니다. IList, ArrayListImpl, LinkedListImpl 이런 이름을 찾을 수 있습니까? 아닙니다. 자바 표준 라이브러리는 List, ArrayList, LinkedList 라고 표현합니다. 여기 이 질문에 대한 정확한 답변 글 이 있습니다. 이런 접두어 / 접미어에 대한 웃기는 이름짓기 관례는 모두 DRY Do not Repeat Your Self (반복하지 말라) 원칙에 어긋나는 것이기도 합니다.

 

 

만약 당신이 DTO, JDO, BEAN 같은 웃기는 동어 반복적인 접미어를 사용하고 있다면, 그것은 접미어로 넣는 대신 패키지package 명에 넣어야 할겁니다. 적절하게 이름지어진 패키지명은 "자체 문서화" 이며, 자바 내부에서는 쓰이지도 않는 이런 정말 잘못 만들어진 명명 체계를 쓰는 것은 모순적인 일입니다.

 

 

만약 클래스 뒤에 Impl 이라는 접미어를 붙일 수 밖에 없는 상황이라면, Interface 를 쓰는 것 자체에 대해 다시 생각해 볼 수 있습니다. 예를 들면 Interface 와 그것을 구현한 단 하나의 구현체가 있는 경우라면, 그것은 고유하게 특수화 된 Interface 가 아니기 때문에 아마도 Interface 를 쓰지 말아야 하는 경우일 것입니다.

 

 

(역주: 테스트를 위해 클래스를 교체해야 할 필요가 있을 때, 테스트 만을 위한 Interface를 만드는 대신 Mock 등의 현대적인 방식으로 이를 대체할 수 있을 것입니다)

 

edited Sep 4 '18 at 17:36

Dave Jarvis 28.6k

user177800