{"id":599,"date":"2010-05-17T03:58:24","date_gmt":"2010-05-17T08:58:24","guid":{"rendered":"http:\/\/vgable.com\/blog\/?p=599"},"modified":"2010-05-17T03:58:29","modified_gmt":"2010-05-17T08:58:29","slug":"dont-check-for-nil-in-your-dealloc-methods","status":"publish","type":"post","link":"https:\/\/vgable.com\/blog\/2010\/05\/17\/dont-check-for-nil-in-your-dealloc-methods\/","title":{"rendered":"Don&#8217;t Check For nil in Your dealloc Methods"},"content":{"rendered":"<p>There&#8217;s no need to check if a variable is\u00a0<span style=\"color: #d000a6; font-family: Menlo; font-size: 11px;\">nil<\/span> before\u00a0<span style=\"color: #4a0086; font-family: Menlo; font-size: 11px;\">release-<\/span>ing it, because\u00a0<a href=\"http:\/\/vgable.com\/blog\/2008\/05\/31\/messages-to-nowhere\/\">calling a method on a NULL variable is a no-op in Objective-C<\/a>. And the runtime already has a\u00a0<a href=\"http:\/\/www.friday.com\/bbum\/2009\/12\/18\/objc_msgsend-tour-part-3-the-fast-path\/\">super-fast check for this case<\/a>.\u00a0\u00a0So adding an extra\u00a0<span style=\"color: #d000a6; font-family: Menlo; font-size: 11px;\">if\u00a0<span style=\"color: #000000; font-family: Helvetica; font-size: medium;\">test into your code will probably slow things down.<\/span><\/span><\/p>\n<p>Doing a pointless &#8221;\u00a0<span style=\"color: #3c8289; font-family: Menlo; font-size: 11px;\"><span style=\"color: #000000;\">==?<span style=\"color: #3c8289;\"><span style=\"color: #d000a6;\">nil<span style=\"color: #000000; font-family: Helvetica; font-size: medium;\">&#8221; \u00a0test is bad for two reasons.<\/span><\/span><\/span><\/span><\/span><\/p>\n<p><span style=\"color: #3c8289; font-family: Menlo; font-size: 11px;\"><span style=\"color: #000000;\"><span style=\"color: #3c8289;\"><span style=\"color: #d000a6;\"><span style=\"color: #000000; font-family: Helvetica; font-size: medium;\">Firstly, <strong>it&#8217;s <a href=\"http:\/\/www.codinghorror.com\/blog\/2007\/05\/the-best-code-is-no-code-at-all.html\">more code<\/a> for no good reason.<\/strong> Even if it&#8217;s just one more line, It&#8217;s one more line to debug and test. It&#8217;s one more place where something could go wrong.<\/span><\/span><\/span><\/span><\/span><\/p>\n<p>Secondly, it&#8217;s not idiomatic Cocoa-code, so\u00a0<strong>it signals that something strange is going on.\u00a0<span style=\"font-weight: normal;\">That&#8217;s a problem for whoever is reading the code, because they have to stop and look around more carefully to figure out <em>why the pointless tests are happening<\/em>.<\/span><\/strong><\/p>\n<p>In summary, <strong>do NOT do this<\/strong>:<\/p>\n<p style=\"margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo;\">&#8211; (<span style=\"color: #d000a6;\">void<\/span>)dealloc;<\/p>\n<p style=\"margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo;\">{<\/p>\n<p style=\"margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #3c8289;\"><span style=\"color: #000000;\"><span style=\"white-space: pre;\"> <\/span><\/span><span style=\"color: #d000a6;\">if<\/span><span style=\"color: #000000;\">(<\/span>baseImage<span style=\"color: #000000;\">) {<\/span><\/p>\n<p style=\"margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #3c8289;\"><span style=\"color: #000000;\"><span style=\"white-space: pre;\"> <\/span>[<\/span>baseImage<span style=\"color: #000000;\"> <\/span><span style=\"color: #4a0086;\">release<\/span><span style=\"color: #000000;\">];<\/span><\/p>\n<p style=\"margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #3c8289;\"><span style=\"color: #000000;\"><span style=\"white-space: pre;\"> <\/span><\/span>baseImage<span style=\"color: #000000;\"> = <\/span><span style=\"color: #d000a6;\">nil<\/span><span style=\"color: #000000;\">;<\/span><\/p>\n<p style=\"margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo;\"><span style=\"white-space: pre;\"> <\/span>}<\/p>\n<p style=\"margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #4a0086;\"><span style=\"color: #000000;\"><span style=\"white-space: pre;\"> <\/span>[<\/span><span style=\"color: #d000a6;\">super<\/span><span style=\"color: #000000;\"> <\/span>dealloc<span style=\"color: #000000;\">];<\/span><\/p>\n<p style=\"margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo;\">}?<\/p>\n<p><strong>Do this:<\/strong><\/p>\n<p><p style=\"margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo;\">&#8211; (<span style=\"color: #d000a6;\">void<\/span>)dealloc;<\/p>\n<p style=\"margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo;\">{<\/p>\n<p style=\"margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #3c8289;\"><span style=\"color: #000000;\"><span style=\"white-space: pre;\"> <\/span>[<\/span>baseImage<span style=\"color: #000000;\"> <\/span><span style=\"color: #4a0086;\">release<\/span><span style=\"color: #000000;\">];<\/span><\/p>\n<p style=\"margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #3c8289;\"><span style=\"color: #000000;\"><span style=\"white-space: pre;\"> <\/span><\/span>baseImage<span style=\"color: #000000;\"> = <\/span><span style=\"color: #d000a6;\">nil<\/span><span style=\"color: #000000;\">;<\/span><\/p>\n<p style=\"margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #4a0086;\"><span style=\"color: #000000;\"><span style=\"white-space: pre;\"> <\/span>[<\/span><span style=\"color: #d000a6;\">super<\/span><span style=\"color: #000000;\"> <\/span>dealloc<span style=\"color: #000000;\">];<\/span><\/p>\n<p style=\"margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo;\">}<\/p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>There&#8217;s no need to check if a variable is\u00a0nil before\u00a0release-ing it, because\u00a0calling a method on a NULL variable is a no-op in Objective-C. And the runtime already has a\u00a0super-fast check for this case.\u00a0\u00a0So adding an extra\u00a0if\u00a0test into your code will probably slow things down. Doing a pointless &#8221;\u00a0==?nil&#8221; \u00a0test is bad for two reasons. Firstly, [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6,4],"tags":[241,106,242],"class_list":["post-599","post","type-post","status-publish","format-standard","hentry","category-cocoa","category-programming","tag-best-practices","tag-nil","tag-programming-style"],"_links":{"self":[{"href":"https:\/\/vgable.com\/blog\/wp-json\/wp\/v2\/posts\/599","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/vgable.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/vgable.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/vgable.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/vgable.com\/blog\/wp-json\/wp\/v2\/comments?post=599"}],"version-history":[{"count":1,"href":"https:\/\/vgable.com\/blog\/wp-json\/wp\/v2\/posts\/599\/revisions"}],"predecessor-version":[{"id":600,"href":"https:\/\/vgable.com\/blog\/wp-json\/wp\/v2\/posts\/599\/revisions\/600"}],"wp:attachment":[{"href":"https:\/\/vgable.com\/blog\/wp-json\/wp\/v2\/media?parent=599"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/vgable.com\/blog\/wp-json\/wp\/v2\/categories?post=599"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/vgable.com\/blog\/wp-json\/wp\/v2\/tags?post=599"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}