报价需要:预处理器的使用是不好的做法OO [英] Quote needed: Preprocessor usage is bad OO practice

查看:174
本文介绍了报价需要:预处理器的使用是不好的做法OO的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我相信,预处理指令像使用#如果UsingNetwork 是不好的做法OO - 其他同事不知道。
我认为,使用IoC容器(如春季)时,组件可以方便地配置,如果相应的编程。在这种情况下任一个属性格式 IsUsingNetwork 可通过IoC容器或设置,如果使用网络实现的行为不同,该接口的另一种实施应实施并注射(例如: IService ServiceImplementation NetworkingServiceImplementation

I believe, that the usage of preprocessor directives like #if UsingNetwork is bad OO practice - other coworkers do not. I think, when using an IoC container (e.g. Spring), components can be easily configured if programmed accordingly. In this context either a propery IsUsingNetwork can be set by the IoC container or, if the "using network" implementation behaves differently, another implementation of that interface should be implemented and injected (e.g.: IService, ServiceImplementation, NetworkingServiceImplementation).

有人可以请提供OO-大师的引用书籍基本上写着预处理器的使用是不好的OO实践参考如果你尝试配置应通过IoC容器来配置行为?

Can somebody please provide citations of OO-Gurus or references in books which basically reads "Preprocessor usage is bad OO practice if you try to configure behaviour which should be configured via an IoC container"?

我需要这个引文说服同事重构...

编辑:我知道并同意使用预处理指令改变上投放特定的代码在编译是好的,这就是预处理指令是为制作。但是,我认为,运行时的配置应该使用,而不是编译时的配置,以获得良好的设计和测试的类和组件。换句话说:使用#define和#如果超出了他们所意味着将导致难以测试的代码和设计拙劣类

I do know and agree that using preprocessor directives to change targetplatform specific code during compilation is fine and that is what preprocessor directives are made for. However, I think that runtime-configuration should be used rather than compiletime-configuration to get good designed and testable classes and components. In other words: Using #defines and #if's beyond what they are meant for will lead to difficult to test code and badly designed classes.

有没有人读的东西沿着这些路线?,可以给我,让我可以参考

Has anybody read something along these lines and can give me so I can refer to?

推荐答案

亨利·斯宾塞写了一纸所谓的#ifdef是有害的

Henry Spencer wrote a paper called #ifdef Considered Harmful.

另外,Bjarne的Stroustrup的自己,在他的书的设计C ++ 的演变和,皱眉,并希望完全消除。不过,斯特劳斯还认识到#ifdef指令和条件编译的必要性和接着说明有在C ++中它没有很好的选择。

Also, Bjarne Stroustrup himself, in the chapter 18 of his book The Design and Evolution of C++, frowns on the use of preprocessor and wishes to eliminate it completely. However, Stroustrup also recognizes the necessity for #ifdef directive and the conditional compilation and goes on to illustrate that there is no good alternative for it in C++.

最后,皮特Goodliffe在他的书码工艺的第13章:编写出色的代码的实践,给人怎样,用于其最初目的,即使一个例子,#IFDEF可以使惹出来的你的代码。

Finally, Pete Goodliffe, in chapter 13 of his book Code Craft: The Practice of Writing Excellent Code, gives an example how, even when used for its original purpose, #ifdef can make a mess out of your code.

希望这有助于。但是,如果你的同事不会听摆在首位合理的论点,我怀疑书中的报价将有助于说服他们;)

Hope this helps. However, if your co-workers won't listen to reasonable arguments in the first place, I doubt book quotes will help convince them ;)

这篇关于报价需要:预处理器的使用是不好的做法OO的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆