{"id":250,"date":"2010-01-29T20:55:35","date_gmt":"2010-01-29T20:55:35","guid":{"rendered":"https:\/\/simpleprogrammer.com\/?p=250"},"modified":"2016-09-19T19:02:46","modified_gmt":"2016-09-19T23:02:46","slug":"static-methods-will-shock-you","status":"publish","type":"post","link":"https:\/\/simpleprogrammer.com\/static-methods-will-shock-you\/","title":{"rendered":"Static Methods Will Shock You"},"content":{"rendered":"<p>I&#8217;ll be frank. \u00a0I don&#8217;t like static methods. \u00a0They make me cringe. \u00a0In the universe of OO static methods are anti-matter.<\/p>\n<p>They don&#8217;t have to be bad, but they are dangerous, because they are used incorrectly.<\/p>\n<p><strong>When static methods can be good<\/strong><\/p>\n<p>There are only two situations when static methods or variables are being used and it&#8217;s not an abomination.<\/p>\n<ol>\n<li>Declaring a true global constant, not a global variable. \u00a0A global constant. \u00a0Example: <em>Math.PI<\/em>. \u00a0This is a fair shorthand for really saying that there is one instance of the universe, and that universe <em>singleton <\/em>contains a mathematical concept\u00a0<em>singleton<\/em> in which there is a property PI which does not change. This concept seems strange to us because we are used to not thinking about PI in the context of object oriented responsibility. \u00a0It would become more obvious if we were designing some strange game where there were alternate universes with different mathematical concepts and constants.<\/li>\n<li>Object creation. \u00a0Static methods are a valuable and valid method of object creation. \u00a0Overloaded constructors that take different arguments are not very clear and are often made clearer by replacing them with a static constructor.<\/li>\n<\/ol>\n<p><script src=\"https:\/\/gist.github.com\/simpleprogrammer-shared\/a592d6f1ef51d51e4938de4980307370.js\"><\/script><\/p>\n<p>is made much more clear when written as<\/p>\n<p><script src=\"https:\/\/gist.github.com\/simpleprogrammer-shared\/980ef32a78d9e39285678ba3be3a47ea.js\"><\/script><\/p>\n<p>it becomes really clear in the usage<\/p>\n<p><script src=\"https:\/\/gist.github.com\/simpleprogrammer-shared\/0b29d33bf95f00345a8d280d7a75da6d.js\"><\/script><\/p>\n<p><strong>When static methods are bad<\/strong><\/p>\n<p>Aside from those two uses, in my opinion, any other use is an abomination. \u00a0(I suppose I let <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/bb383977.aspx\">C# extension methods<\/a> slide here, because they are so useful under the right conditions, but I reserve the right to make judgement on a future date.)<\/p>\n<p>Static methods usually indicate a method that doesn&#8217;t know where it belongs. \u00a0It is sitting out there trying to belong to the class it is on, but it doesn&#8217;t really belong, because it doesn&#8217;t use the internal state of the class. \u00a0When we look at our classes from the Single Responsibility Principle (<a href=\"http:\/\/en.wikipedia.org\/wiki\/Single_responsibility_principle\">SRP<\/a>) viewpoint, a static method is usually a violation because it tends to have a responsibility that is not the same of the class it is attached on.<\/p>\n<p>One way to think about static methods is as global procedures. \u00a0Essentially a static method can be called anywhere from anywhere. \u00a0It just pretends to be part of a class, when really the class is only used as a &#8220;tag&#8221;, which organizes the method by some logical division. \u00a0I look at static methods in these terms, because creating global procedures is the exact opposite of object oriented design.<\/p>\n<p>Another major problem with static methods is the testability. \u00a0Testability is a big deal when building software. \u00a0Static methods are notoriously difficult to test, especially when they create new instances of concrete classes. \u00a0If you have ever worked in legacy code and tried to write a unit test for a static method, you know my pain.<\/p>\n<p>Static methods also are not polymorphic. \u00a0If you create a static method on a class, there is no overriding that behavior. \u00a0You are stuck with a hard coded reference to that implementation.<\/p>\n<p>Finally, static methods increase the complexity of an application. \u00a0The more static methods there are, the more a programmer working in the application has to know about. \u00a0When instance methods are used in a class, having an instance of that object will allow a programmer to determine all the actions that can be taken. \u00a0When static methods are used the programmer has to know about the secret methods that may not even be on the object he is working with, but manipulate that object. \u00a0Let&#8217;s look at one common example: \u00a0Date and DateUtilities.<\/p>\n<p>In more than one application I have worked on, Date and DateUtilities existed. \u00a0Date existed to provide an implementation of date and time, and DateUtilities existed to manipulate the date and do things like figure out the first day of the month, or determine holidays, or see if two dates overlap. \u00a0I don&#8217;t know how many times I would try to write a method to do one of the things DateUtilities already did because I didn&#8217;t know to look for a static class called DateUtilities with static methods that did what I wanted. \u00a0As a programmer, I have to remember to always check all the methods in DateUtilities to see if there is one that does what I want. \u00a0This can be a large amount of overhead when you are working in a large application, with many static methods hanging around in helper classes. \u00a0Perhaps I am spilling over into another problem here, but the point is that it is much more difficult to remember what utility methods exist floating out there than it is to use a method on a class that the functionality really belongs to. \u00a0(Side note here: C# extension methods kind of solve this problem, by allowing you to type &#8220;.&#8221; and see the static methods that can operate on that class).<\/p>\n<p><strong>Final words<\/strong><\/p>\n<p>So just remember when you create a static method to think about it carefully. \u00a0I am not advocating never using them. \u00a0I am advocating having a really good reason and checking first to see if the static method really belongs to another class where it can use state information.<\/p>\n<p><em>If you have the skills but you just aren&#8217;t seeing the success you deserve in your career, then do check out my course <a href=\"https:\/\/simpleprogrammer.com\/store\/products\/how-to-market-yourself\/\">&#8220;How to Market Yourself as a Software Developer&#8221;.<\/a><\/em><\/p>\n","protected":false},"excerpt":{"rendered":"<p>I&#8217;ll be frank. \u00a0I don&#8217;t like static methods. \u00a0They make me cringe. \u00a0In the universe of OO static methods are anti-matter. They don&#8217;t have to be bad, but they are dangerous, because they are used incorrectly. When static methods can be good There are only two situations when static methods or variables are being used&#8230;<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_kad_post_transparent":"","_kad_post_title":"","_kad_post_layout":"","_kad_post_sidebar_id":"","_kad_post_content_style":"","_kad_post_vertical_padding":"","_kad_post_feature":"","_kad_post_feature_position":"","_kad_post_header":false,"_kad_post_footer":false,"_kad_post_classname":"","footnotes":""},"categories":[2426,148,1017,1934],"tags":[],"class_list":["post-250","post","type-post","status-publish","format-standard","hentry","category-c","category-design","category-java","category-language"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.6 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Static Methods Will Shock You<\/title>\n<meta name=\"description\" content=\"Two instances when static methods should be used and four reasons why using static methods is not a good common practice. An example and code is included.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/simpleprogrammer.com\/static-methods-will-shock-you\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Static Methods Will Shock You\" \/>\n<meta property=\"og:description\" content=\"Two instances when static methods should be used and four reasons why using static methods is not a good common practice. An example and code is included.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/simpleprogrammer.com\/static-methods-will-shock-you\/\" \/>\n<meta property=\"og:site_name\" content=\"Simple Programmer\" \/>\n<meta property=\"article:published_time\" content=\"2010-01-29T20:55:35+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2016-09-19T23:02:46+00:00\" \/>\n<meta name=\"author\" content=\"John Sonmez\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"John Sonmez\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"4 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/simpleprogrammer.com\\\/static-methods-will-shock-you\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/simpleprogrammer.com\\\/static-methods-will-shock-you\\\/\"},\"author\":{\"name\":\"John Sonmez\",\"@id\":\"https:\\\/\\\/simpleprogrammer.com\\\/#\\\/schema\\\/person\\\/1abc70edfb189184827740310672de67\"},\"headline\":\"Static Methods Will Shock You\",\"datePublished\":\"2010-01-29T20:55:35+00:00\",\"dateModified\":\"2016-09-19T23:02:46+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/simpleprogrammer.com\\\/static-methods-will-shock-you\\\/\"},\"wordCount\":897,\"commentCount\":11,\"articleSection\":[\"C#\",\"Design\",\"Java\",\"Language\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/simpleprogrammer.com\\\/static-methods-will-shock-you\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/simpleprogrammer.com\\\/static-methods-will-shock-you\\\/\",\"url\":\"https:\\\/\\\/simpleprogrammer.com\\\/static-methods-will-shock-you\\\/\",\"name\":\"Static Methods Will Shock You\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/simpleprogrammer.com\\\/#website\"},\"datePublished\":\"2010-01-29T20:55:35+00:00\",\"dateModified\":\"2016-09-19T23:02:46+00:00\",\"author\":{\"@id\":\"https:\\\/\\\/simpleprogrammer.com\\\/#\\\/schema\\\/person\\\/1abc70edfb189184827740310672de67\"},\"description\":\"Two instances when static methods should be used and four reasons why using static methods is not a good common practice. An example and code is included.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/simpleprogrammer.com\\\/static-methods-will-shock-you\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/simpleprogrammer.com\\\/static-methods-will-shock-you\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/simpleprogrammer.com\\\/static-methods-will-shock-you\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/simpleprogrammer.com\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Static Methods Will Shock You\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/simpleprogrammer.com\\\/#website\",\"url\":\"https:\\\/\\\/simpleprogrammer.com\\\/\",\"name\":\"Simple Programmer\",\"description\":\"\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/simpleprogrammer.com\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/simpleprogrammer.com\\\/#\\\/schema\\\/person\\\/1abc70edfb189184827740310672de67\",\"name\":\"John Sonmez\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/dc2f18dfa76e017566ce607de002d7abe4acfd5ec19a6db82c180b00867b8d94?s=96&d=mm&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/dc2f18dfa76e017566ce607de002d7abe4acfd5ec19a6db82c180b00867b8d94?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/dc2f18dfa76e017566ce607de002d7abe4acfd5ec19a6db82c180b00867b8d94?s=96&d=mm&r=g\",\"caption\":\"John Sonmez\"},\"url\":\"https:\\\/\\\/simpleprogrammer.com\\\/author\\\/jsonmez\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Static Methods Will Shock You","description":"Two instances when static methods should be used and four reasons why using static methods is not a good common practice. An example and code is included.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/simpleprogrammer.com\/static-methods-will-shock-you\/","og_locale":"en_US","og_type":"article","og_title":"Static Methods Will Shock You","og_description":"Two instances when static methods should be used and four reasons why using static methods is not a good common practice. An example and code is included.","og_url":"https:\/\/simpleprogrammer.com\/static-methods-will-shock-you\/","og_site_name":"Simple Programmer","article_published_time":"2010-01-29T20:55:35+00:00","article_modified_time":"2016-09-19T23:02:46+00:00","author":"John Sonmez","twitter_card":"summary_large_image","twitter_misc":{"Written by":"John Sonmez","Est. reading time":"4 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/simpleprogrammer.com\/static-methods-will-shock-you\/#article","isPartOf":{"@id":"https:\/\/simpleprogrammer.com\/static-methods-will-shock-you\/"},"author":{"name":"John Sonmez","@id":"https:\/\/simpleprogrammer.com\/#\/schema\/person\/1abc70edfb189184827740310672de67"},"headline":"Static Methods Will Shock You","datePublished":"2010-01-29T20:55:35+00:00","dateModified":"2016-09-19T23:02:46+00:00","mainEntityOfPage":{"@id":"https:\/\/simpleprogrammer.com\/static-methods-will-shock-you\/"},"wordCount":897,"commentCount":11,"articleSection":["C#","Design","Java","Language"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/simpleprogrammer.com\/static-methods-will-shock-you\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/simpleprogrammer.com\/static-methods-will-shock-you\/","url":"https:\/\/simpleprogrammer.com\/static-methods-will-shock-you\/","name":"Static Methods Will Shock You","isPartOf":{"@id":"https:\/\/simpleprogrammer.com\/#website"},"datePublished":"2010-01-29T20:55:35+00:00","dateModified":"2016-09-19T23:02:46+00:00","author":{"@id":"https:\/\/simpleprogrammer.com\/#\/schema\/person\/1abc70edfb189184827740310672de67"},"description":"Two instances when static methods should be used and four reasons why using static methods is not a good common practice. An example and code is included.","breadcrumb":{"@id":"https:\/\/simpleprogrammer.com\/static-methods-will-shock-you\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/simpleprogrammer.com\/static-methods-will-shock-you\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/simpleprogrammer.com\/static-methods-will-shock-you\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/simpleprogrammer.com\/"},{"@type":"ListItem","position":2,"name":"Static Methods Will Shock You"}]},{"@type":"WebSite","@id":"https:\/\/simpleprogrammer.com\/#website","url":"https:\/\/simpleprogrammer.com\/","name":"Simple Programmer","description":"","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/simpleprogrammer.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/simpleprogrammer.com\/#\/schema\/person\/1abc70edfb189184827740310672de67","name":"John Sonmez","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/dc2f18dfa76e017566ce607de002d7abe4acfd5ec19a6db82c180b00867b8d94?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/dc2f18dfa76e017566ce607de002d7abe4acfd5ec19a6db82c180b00867b8d94?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/dc2f18dfa76e017566ce607de002d7abe4acfd5ec19a6db82c180b00867b8d94?s=96&d=mm&r=g","caption":"John Sonmez"},"url":"https:\/\/simpleprogrammer.com\/author\/jsonmez\/"}]}},"_links":{"self":[{"href":"https:\/\/simpleprogrammer.com\/wp-json\/wp\/v2\/posts\/250","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/simpleprogrammer.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/simpleprogrammer.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/simpleprogrammer.com\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/simpleprogrammer.com\/wp-json\/wp\/v2\/comments?post=250"}],"version-history":[{"count":0,"href":"https:\/\/simpleprogrammer.com\/wp-json\/wp\/v2\/posts\/250\/revisions"}],"wp:attachment":[{"href":"https:\/\/simpleprogrammer.com\/wp-json\/wp\/v2\/media?parent=250"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/simpleprogrammer.com\/wp-json\/wp\/v2\/categories?post=250"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/simpleprogrammer.com\/wp-json\/wp\/v2\/tags?post=250"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}