简体   繁体   English

如果不是绝对必要的,我应该删除 Angular CLI 创建组件时生成的 OnInit 实现吗?

[英]Should I remove the OnInit implementation which is generated when a component is created by the Angular CLI if it isn't strictly necessary?

My team is working on an Angular project and are trying to establish team code style conventions.我的团队正在开发一个 Angular 项目,并试图建立团队代码风格约定。 A teammate has encouraged me to remove the OnInit implementation which was generated when I created the component using Angular CLI .一位队友鼓励我删除使用Angular CLI创建组件时生成的OnInit实现。

My teammate's argument in favor of removing the OnInit implementation is that this is unnecessary code and that failure to remove unused code may lead to misunderstanding by other developers who fail to recognize the implementation is unnecessary.我的队友赞成删除OnInit实现的论点是,这是不必要的代码,并且未能删除未使用的代码可能会导致其他开发人员误解,因为他们没有认识到实现是不必要的。 This argument seems reasonable to me in general, but the idea of removing some generated code makes me somewhat uncomfortable.总的来说,这个论点对我来说似乎是合理的,但是删除一些生成的代码的想法让我有点不舒服。

I was curious if others in the Angular community have considered this type of coding convention and what factors led to a decision.我很好奇 Angular 社区中的其他人是否考虑过这种类型的编码约定以及哪些因素导致了决定。 Have you asked teammates to remove generated code which isn't strictly necessary?您是否要求队友删除并非绝对必要的生成代码? Would you consider this an unreasonable convention?你会认为这是一个不合理的约定吗?

So really there is no correct answer, it's for you and your team to decide on such conventions.所以真的没有正确的答案,由你和你的团队来决定这样的约定。 I prefer to remove empty functions for the same reason as removing blocks of commented-out code;我更喜欢删除空函数,原因与删除注释掉的代码块相同; it just takes up space and distracts from the code that matters.它只是占用空间并分散了重要代码的注意力。
It's not a big deal either way but static code analysis tools such as SonarQube would pick this up as a code smell (see https://rules.sonarsource.com/javascript/RSPEC-1186 ).这两种方式都不是什么大问题,但像 SonarQube 这样的静态代码分析工具会将其视为代码异味(请参阅https://rules.sonarsource.com/javascript/RSPEC-1186 )。

暂无
暂无

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

相关问题 我可以使用哪个 Angular CLI 命令将新创建的组件移动到文件夹下? - Which Angular CLI command can I use to move a newly created component under a folder? 在我创建的角度组件文件中找不到 OnInit 生命周期挂钩 - The OnInit lifecycle hook is not found in my created angular component file 我不明白为什么 Angular 组件 class 添加实现 onInit 接口 - I can't figure out why Angular component class add implements onInit interface 在Angular中何时以及为哪个组件创建DOM? - When and for which component is the DOM created in Angular? 对于 rxjs 和 angular,是否有必要取消订阅在应用组件的 onInit 方法中仅发生一次的订阅? - with rxjs and angular, Is it necessary to unsubscribe for the subscriptions that happen only once in app component's onInit method? Angular 子组件在父 oninit 期间父函数未初始化 - Angular child component not initialised when parent is function during parent oninit 从组件的 OnInit 调用时,Angular 2 路由器无法导航 - Angular 2 router unable to navigate when called from OnInit of component 什么时候会为动态加载的角度分量触发OnInit事件? - When is OnInit event triggered for a dynamically loaded angular component? Angular:无法调用我创建的组件 - Angular: Can't call component I created 角4:我生成组件并运行构建命令? 应该有一些错误? - angular 4: I generated component and run the build command? there is should be some errors?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM