{"id":3954,"date":"2014-03-30T06:39:44","date_gmt":"2014-03-30T06:39:44","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/quote-needed-preprocessor-usage-is-bad-oo-practice-collection-of-common-programming-errors\/"},"modified":"2014-03-30T06:39:44","modified_gmt":"2014-03-30T06:39:44","slug":"quote-needed-preprocessor-usage-is-bad-oo-practice-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/quote-needed-preprocessor-usage-is-bad-oo-practice-collection-of-common-programming-errors\/","title":{"rendered":"Quote needed: Preprocessor usage is bad OO practice-Collection of common programming errors"},"content":{"rendered":"<p>IMO it is important to differentiate between #if and #define. Both can be useful and both can be overused. My experience is that #define is more likely to be overused than #if.<\/p>\n<p>I spent 10+ years doing C and C++ programming. In the projects I worked on (commercially available software for DOS \/ Unix \/ Macintosh \/ Windows) we used #if and #define primarily to deal with code portability issues.<\/p>\n<p>I spent enough time working with C++ \/ MFC to learn to detest #define when it is overused &#8211; which I believe to be the case in MFC circa 1996.<\/p>\n<p>I then spent 7+ years working on Java projects. I cannot say that I missed the preprocessor (although I most certainly did miss things like enumerated types and templates \/ generics which Java did not have at the time).<\/p>\n<p>I&#8217;ve been working in C# since 2003. We have made heavy use of #if and [Conditional(&#8220;DEBUG&#8221;)] for our debug builds &#8211; but #if is just a more convenient, and slightly more efficient way of doing the same things we did in Java.<\/p>\n<p>Moving forward, we have started to prepare our core engine for Silverlight. While everything we are doing could be done without #if, it is less work with #if which means we can spend more time adding features that our customers are asking for. For example, we have a value class which encapsulates a system color for storage in our core engine. Below are the first few lines of code. Because of the similarity between System.Drawing.Color and System.Windows.Media.Color, the #define at the top gets us a lot of functionality in normal .NET and in Silverlight without duplicating code:<\/p>\n<pre><code>using System;\nusing System.Collections.Generic;\nusing System.Text;\nusing System.Diagnostics;\n#if SILVERLIGHT\nusing SystemColor = System.Windows.Media.Color;\n#else\nusing SystemColor = System.Drawing.Color;\n#endif\n\nnamespace SpreadsheetGear.Drawing\n{\n    \/\/\/ \n    \/\/\/ Represents a Color in the SpreadsheetGear API and provides implicit conversion operators to and from System.Drawing.Color and \/ or System.Windows.Media.Color.\n    \/\/\/ \n    public struct Color\n    {\n        public override string ToString()\n        {\n            \/\/return string.Format(\"Color({0}, {1}, {2})\", R, G, B);\n            return _color.ToString();\n        }\n\n        public override bool Equals(object obj)\n        {\n            return (obj is Color &amp;&amp; (this == (Color)obj))\n                || (obj is SystemColor &amp;&amp; (_color == (SystemColor)obj));\n        }\n        ...\n<\/code><\/pre>\n<p>The bottom line for me is that there are many language features which can be overused, but this is not a good enough reason to leave these features out or to make strict rules prohibiting their use. I must say that moving to C# after programming in Java for so long helps me to appreciate this because Microsoft (Anders Hejlsberg) has been more willing to provide features which might not appeal to a college professor, but which make me more productive in my job and ultimately enable me to build a better widget in the limited time anybody with a ship date has.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>IMO it is important to differentiate between #if and #define. Both can be useful and both can be overused. My experience is that #define is more likely to be overused than #if. I spent 10+ years doing C and C++ programming. In the projects I worked on (commercially available software for DOS \/ Unix \/ [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-3954","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/3954","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=3954"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/3954\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=3954"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=3954"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=3954"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}