Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Typescript style guide for interfaces

this is a question that may not have a single correct answer, as I do realise coding styles are quite varied, especially between different languages, eg camel case function names in javascript vs pascal casing methods in C#. I can quite accept that.

Maybe I am over worrying about this, but I am just starting to look into typescript, really like the looks of it and plan to use it along with Angular2, and want to establish a good style guide.

What I really don't get is point 2 here, not to use I prefix for interfaces. Until this, I thought that was almost universal. I have a class Car, so a natural name if the interface is just to add an I in front... ICar. As soon as you see the I prefix you know you have an interface.

I want to follow any suggested practice, but this one I really do not know which why to go.

Does none know why, what I thought was an almost universal convention, is being discouraged here? I know you can use whatever conventions you like, just wondering if there is some reason for this common convention not to be used in Typescript.

Thanks in advance for any opinions/info!

like image 918
peterc Avatar asked Mar 03 '16 14:03

peterc


2 Answers

I as prefix was big at some time for Java and C# (probably also others) but I don't think this is still considered a good idea but how can one change something that is used by the majority of developers and existing code base.

It's similar to hungarian notation which is universally considered bad practice. Just give it a meaningful name. If you have different kinds of Cars than make Car the universal interface and class FancyCar implements Car is much more natural. Things like prefixes just prevent people from thinking about what they really want to express. See also http://c2.com/cgi/wiki?IntentionRevealingNames

There are also languages like Dart (probably many others I don't know) where there is not such a clear distinction between interface and class. In Dart you can implement any class. The class' interface just acts as an interface.

update

I don't say naming is easy. In fact I think it's the most or at least among the most difficult parts of software development. It's just that the general consesus of "the elite" is that prefixes for technical reasons are not the best approach. This doesn't mean there are alternative that have only advantages and no drawbacks. It seems in this case naming like UserService, UserServiceImpl, MockUserService is used instead. This way in most parts of your code the most natural way UserService is used and the derivates only in prividers. Otherwise, as mentioned above, consistency is way more important. If some style is more common in the language you use, I suggest to use this in your code as well.

like image 94
Günter Zöchbauer Avatar answered Sep 22 '22 15:09

Günter Zöchbauer


Similar question is asked here Confused about the Interface and Class coding guidelines for TypeScript

My answer on it: https://stackoverflow.com/a/41967120/586609

Reasons:

  1. The times of the Hungarian notation have passed
  2. I-prefix violates encapsulation principle
  3. Protection from bad naming
  4. Properly chosen names vaccinate you against API Design Myth: Interface as Contract.
like image 29
Stanislav Berkov Avatar answered Sep 25 '22 15:09

Stanislav Berkov