-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Prohibition against prefixing interfaces with "I" #121
Comments
I'm not entirely sure what more to write. .NET is the one with the rule (always prefix interfaces with Any constructive ideas on how to phrase this? |
"It is not necessary to prefix interfaces with 'I', as is done in C#." Otherwise, what it sounds like you mean is that prefixing with I is a great way to encounter the ancient demons of code collision or bad coding practices. Right now it says "Do not", which is prohibitory language, not advisory language. If there is a recommended style (like postfixing with Interface or Type or the like), that would be even better to note, rather than the discouraged prefix. |
not to sound pedantic, but necessary/not necessary is not the right word, i do not think it is necessary to add I in C# nor is it in TS. i would say "you shouldn't use 'I'" is more accurate. |
Agreed with @mhegazy. |
Fix wording of interface naming conventions, fix #121
@mhegazy prefixing namespaces with I in c# is common sense how would you else like your code written using I distinguishes interfaces and abstracted classes ex: CarBase describes a abstract base for cars and the interface IAccelerate describes the method which incises the speed of the car not prefixing this with I means it could be anything i would never in the world work with code that dont prefix interfaces with I in c# its absurt. |
@robgha01 this is a style choice. you are free to write C# code with no I prefixes, as well as TS code with them. This is a style recommendation we make, and one we use in our internal code base. the compile does not enforce any rules regarding names of interfaces or classes. |
I 'IS' for Interface. Nothing to do with .Net (although common sense prevails there). Reasons are obvious and I fail to understand why anyone would not prefix an interface with 'I'.
I kind of feel the world has gone a little mad and lost it's marbles! Please prefix ALL interfaces with 'I' because 'I' IS for Interface. |
Everyone knows to follow C++ naming conventions, so the class will be called |
Most complete answer for this topic I found in this stackoverflow answer: |
I just ran across this issue and read @Raketar stackoverflow link. The first response is a very logical and reasoned answer. The ability to decipher an interface from an abstraction or implementation by simply seeing an "I" in front of it will speed up anybody trying to process what they are reading (instead of having to go to the file). In Berkov's post he says 'choose appropriate name for abstraction and concrete implementation' but then uses the implementation example 'FileBasedAutosuggestManager' and interface 'AutosuggestManager', yet 'FileBasedAutosuggestManager' could just as easily been an interface also... which seems to be overlooked here. It appears this whole thread boils down to preference not correctness. Whether someone wants to write Async at the end of their methods to denote async/await is preference, or Base at the end of an abstraction. Languages borrow conventions all the time. It's what people get used to. If that helps speed up learning and development then use it, unless something better comes along. But this is one of the cases for me at least, there's not clear evidence that dropping the 'I' as a convention is more beneficial. |
I don't understand the confusion. Classes should always start with a three-letter prefix:
|
@RyanCavanaugh Theres no confusion, context is important. If someone is writing a pull request into typescript, you should be using typescript convention, .Net use "I" for interface.... etc. Cavanaugh is just establishing a convention for the library which is fine. The whole "what about" argument for the three-letter prefix boils down to context IMO. If its the convention of the library to prefix three-letters (albeit a terrible one IMO for numerous reasons), that's the end of it. So you could either have a argument over the correctness of the pattern or you could state it's the preference for the library. The difference between "I" for interface and the three-letter argument (for correctness sake), is the context in which the variables/classes exist. If the company "Whispering Oak" creates a game called "Zebra Surprise", the the resulting package would typically be name "zerbra-surprise.exe" (or w/e) under the "Whispering Oaks" directory... and one could make the argument that three-letter prefixes are unnecessary due to the context its in. Redundancies can and probably should be removed, that would be my argument, at least. Contrary to this, you cannot infer that an interface is an interface by using context clues, therefor adding an "I" would be a reasonable response. |
@UTGuy There is one more reason for not using |
The prefix in the .NET world was never necessary it only added confusion, some people reads the prefix as if it is the pronoun I which is wrong.
In practice an interface name in the .NET world shouldn't match the implementation name. Interfaces in the .NET world should be small and implementations should be composed of multiple interfaces and if you have a single interface that describes an implementation and you want to distinguish between them a better convention could have been something like this Consumers shouldn't know whether That said, I still use the prefix |
I have been struggling with this as well. When I was a C# and ActionScript developer we always prefaced our interfaces with I and it was one of the first things I learned about TypeScript. I do understand the arguments "against" it but then I run into community standards like adding a $ at the end of observables. Obviously, we do seem to desire instant gratification in identifying things. I'm curious as to how often I am going to start writing "new Thing()" only to find out it is an interface and I can't do that. :-) |
Ideally, you should always use an interface, and so dropping the prefix makes sense to me. Let the abstract and implementation classes be explicit with verbose prefix and/or suffix, and let the interfaces be the pure function name. |
@stasberkov Even in the above article he suggested using Contracts (via abstract class). When thinking more about your example of
While other develops can start coming in and use this car class by extending it:
and he changes Car to be:
In this case, as people add additional behavior in the interface, how do we avoid breaking the original contract that other callers depend on? (I am not even coming from the C# community but I just didn't see how removing the I in the name would solve any problems.) |
|
It is necessary to come to a realization that the violation of encapsulation doesnt lie in the naming itself, but on the languages' restrictions. For example, in Java, there's no way to implements a class and you can only implements an interface. The violation of encapsulation automatically happens when someone tries to use an interface. Yet, I have arrived at a unanswered question for myself: does it necessarily mean something is bad as soon as we coin it as a violation of some rules? In this case, we have seen no problems of having these language restrictions. |
If you came here looking for answers as I did, just check TypeScript's handbook <3. |
@RyanCavanaugh I know it is bit old, could you maybe tell us why your team decided not to prefix interfaces with I? I mean, what was the reason? I think thats because in TypeScript the interface is not just an pure object interface but it can be used to define interface to ctors, call signatures, indexable objects and maybe more I can't imaigine right now |
Indeed. Since the first comment was posted more than 4 years ago, we have not received clear answer. So why you decided that it's better to avoid using i-prefix for interfaces naming? Is there any significant reason or is it just the wishes of your team? I apologize for being straightforward |
I'm not sure what you're after? This question was answered right away by multiple team members, and the opinion of the team hasn't changed in the last 5 years to be honest.
For example, we also don't prefix our types with T either: type TThing = {} You're all welcome to make your own decisions about this on a per-team basis, and if your teams come from C# then maybe that is keeping an I around, but there's no need to bring another language's conventions over when you can conform to JavaScript's conventions. |
|
Stop this slippery slope fallacy. Please. Just stop. No one will start writing 'lpcstr' in JS, let alone in TS.
Yes, let's ASSUME you get some black-box, and let's stick to that assumption regardless of whether the box has "I" written on it or not. ... and what is hard is precisely what shouldn't be deliberately made harder.
That's their problem. |
@RyanCavanaugh type FbEvent = 'post' | 'repost' | 'comment'; // Error: Duplicate identifier 'FbEvent'
export interface FbEvent { // Error: Duplicate identifier 'FbEvent'
type: FbEvent
data: object
} Why I must not use prefixes in similar cases? type FbEvent = 'post' | 'repost' | 'comment'; // No errors
export interface IFbEvent { // No errors
type: FbEvent
data: object
}
@stasberkov, Emmm, no. It doesn't actually. Encapsulation describes following things:
Prefixing does not prevent restricting direct access to some of the object's components.
Do prefixing somehow violates that rule? Of course not. |
@FlamesoFF in your case, I believe you've created a false dilemma. I wouldn't agree with the naming of your Normally, when modeling a type like this, I'd personally start with an anonymous type for export interface FbEvent {
type: 'post' | 'repost' | 'comment';
data: object;
} Then, if I wanted to derive a type from export type FbEventType = 'post' | 'repost' | 'comment';
export interface FbEvent {
type: FbEventType;
data: object;
} Note, in this case, we're not naming Note, I'm not saying the above should be a code guideline. I'm just saying that using an In general, I do think this argument boils down |
The Typescript Handbook tells the developer not to prefix interfaces with I. This needs more detail. Lots of .NET developers are familiar with I-prefixed interfaces. Simply stating that the concept is broader is not sufficient. If it's an antipattern, let's describe why. If it's not an antipattern, then let's say "it is not necessary to prefix interfaces with I, and Typescript Contributors do X"
The text was updated successfully, but these errors were encountered: