简体   繁体   English

接口的Typescript样式指南

[英]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#. 这是一个可能没有一个正确答案的问题,因为我确实编码样式是多种多样的,特别是在不同语言之间,例如javascript中的驼峰案例函数名称和C#中的pascal大小写方法。 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. 也许我对此过于担心,但我刚刚开始研究打字稿,非常喜欢它的外观,并计划将它与Angular2一起使用,并希望建立一个好的风格指南。

What I really don't get is point 2 here , not to use I prefix for interfaces. 我真的不明白的是2点在这里 ,不要用我前缀接口。 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. 我有一个类Car,所以如果界面只是在前面添加一个自然名称... 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. 我知道你可以使用你喜欢的任何约定,只是想知道这个常见约定是否有某些原因不能在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. I作为Java和C#(可能还有其他人)的前缀很大,但我不认为这仍然是一个好主意但是如何改变大多数开发人员和现有代码库使用的东西。

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. 如果你有不同种类的Car不是制造Car的通用接口和class FancyCar implements Car更自然。 Things like prefixes just prevent people from thinking about what they really want to express. 像前缀这样的东西只会阻止人们思考他们真正想要表达的内容。 See also http://c2.com/cgi/wiki?IntentionRevealingNames 另请参阅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 . 还有像Dart这样的语言(可能还有许多其他我不知道的),其中interfaceclass之间没有这么明显的区别。 In Dart you can implement any class. 在Dart中,您可以实现任何类。 The class' interface just acts as an interface . 类的接口只是一个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. 在这种情况下,似乎使用了像UserServiceUserServiceImplMockUserService这样的命名。 This way in most parts of your code the most natural way UserService is used and the derivates only in prividers. 这种方式在代码的大多数部分中使用UserService最自然的方式,而衍生工具仅在私有方中使用。 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 类似的问题在这里被问到困惑TypeScript的接口和类编码指南

My answer on it: https://stackoverflow.com/a/41967120/586609 我的回答是: https//stackoverflow.com/a/41967120/586609

Reasons: 原因:

  1. The times of the Hungarian notation have passed 匈牙利符号的时代已经过去了
  2. I-prefix violates encapsulation principle I前缀违反了封装原则
  3. Protection from bad naming 防止命名错误
  4. Properly chosen names vaccinate you against API Design Myth: Interface as Contract. 正确选择的名称可以针对API Design Myth:Interface as Contract进行接种。

There is no reason why to do or not do something other than because it is idiomatic. 除了因为它是惯用之外,没有理由做或不做某事。 If you want your code to fit in, you follow the rules everyone else follows in the language you are using and don't think about it (or pick another language). 如果您希望您的代码适合,您可以按照您正在使用的语言遵循其他人遵循的规则,而不要考虑它(或选择其他语言)。

As far as specifically why not put a prefix, there are three very good reasons: 至于具体为什么不加前缀,有三个很好的理由:

  1. Interfaces are implicitly created when you use the class keyword, and are also created when you use the type keyword. 使用class关键字时会隐式创建接口,并且在使用type关键字时也会创建接口。 So, by adding a prefix to your explicitly declared interfaces, now you have some interfaces with a prefix (the ones you created yourself with the prefix), plus a ton more interfaces without a prefix. 因此,通过向显式声明的接口添加前缀,现在您有一些带有前缀的接口(您自己使用前缀创建的接口),还有一些没有前缀的接口。 This is confusing and inconsistent. 这是令人困惑和不一致的。
  2. Types (interfaces) and code exist in independent namespaces in the compiler, so you cannot conflict a type name and a variable name. 类型(接口)和代码存在于编译器的独立命名空间中,因此您不能与类型名称和变量名称冲突。 Therefore there is no reason to try to make interface names unique by prefixing them. 因此,没有理由通过为它们添加前缀来尝试使接口名称唯一。
  3. I prefixing is just another form of systems hungarian notation; I加前缀只是匈牙利符号系统的另一种形式; saying a type is an interface provides zero additional information to the human writing the code. 说一个类型是一个接口,为人类编写代码提供附加信息。

Two more reasons 还有两个原因

Modularization As the code base grows, the code starts to be modularized. 模块化随着代码库的增长,代码开始模块化。 The usual design goal with modules is to expose contracts and hides the internals. 模块的通常设计目标是暴露合同并隐藏内部。 Contracts are best expressed by interfaces and after a while most if not all exposed artifacts are interfaces and factories. 合同最好用接口表示,过了一段时间,大多数(如果不是全部)暴露的工件都是接口和工厂。 For consumers of these modules it is nicer to work with Users s and Credentials rather than IUsers and ICredentials 对于这些模块的消费者来说,使用UsersCredentials而不是IUsersICredentials

Alphabetic order For instance quick access to Interfaces and Classes in lists sorted in alphabetic order in a File navigator. 字母顺序例如,快速访问“文件”导航器中按字母顺序排序的列表中的“接口和类”。 When working with Users, it is best to have User , UserService and UserImpl next to each other in the list, rather than having to go though the I entries, then the C entries then the Service entries 与用户合作时,最好在列表UserImpl UserUserServiceUserImpl一起,而不是通过I条目,然后是C条目,然后是Service条目

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM