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!
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 Car
s 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.
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:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With