<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://msmvps.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Single-Entry, Single-Exit, Should It Still Be Applicable In Object-oriented Languages?</title><link>http://msmvps.com/blogs/peterritchie/archive/2008/03/07/single-entry-single-exit-should-it-still-be-applicable-in-object-oriented-languages.aspx</link><description>Before the modern high-level languages Edsger Dijkstra came up with &amp;quot;Structured Programming&amp;quot;. This programming methodology relied on the programmer to form and enforce most of the structure of the program--manually keeping sub-structures and</description><dc:language>en</dc:language><generator>CommunityServer 2008.5 SP2 (Build: 40407.4157)</generator><item><title>re: Single-Entry, Single-Exit, Should It Still Be Applicable In Object-oriented Languages?</title><link>http://msmvps.com/blogs/peterritchie/archive/2008/03/07/single-entry-single-exit-should-it-still-be-applicable-in-object-oriented-languages.aspx#1635359</link><pubDate>Sun, 15 Jun 2008 16:37:01 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1635359</guid><dc:creator>PeterRitchie</dc:creator><description>&lt;p&gt;In most OO languages you can allocate data as needed, not like most procedural languages where you have to allocate data at the top of a function.&lt;/p&gt;
&lt;p&gt;If you get away from that procedural method of allocation, SESE becomes very difficult--time better spent writing value-added code.&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1635359" width="1" height="1"&gt;</description></item><item><title>re: Single-Entry, Single-Exit, Should It Still Be Applicable In Object-oriented Languages?</title><link>http://msmvps.com/blogs/peterritchie/archive/2008/03/07/single-entry-single-exit-should-it-still-be-applicable-in-object-oriented-languages.aspx#1635344</link><pubDate>Sun, 15 Jun 2008 15:39:26 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1635344</guid><dc:creator>Eric Eilebrecht</dc:creator><description>&lt;p&gt;I always thought the big benefit of SESE was that it allowed you to do some things at the top of the method (set up data structures, validate args, etc.) or at the end of the method (cleanup, etc.) and be sure that those things would actually be executed.&lt;/p&gt;
&lt;p&gt;OO doesn&amp;#39;t have anything to do with this. &amp;nbsp;Exceptions do foil it, as many have pointed out.&lt;/p&gt;
&lt;p&gt;But every modern language I&amp;#39;m familiar with has some way of automating back-out/cleanup code. &amp;nbsp;C++ stack-allocated object destructors, most other languages&amp;#39; &amp;quot;finally&amp;quot; constructs, etc. &amp;nbsp;And the single-entry thing is just assumed these days (coroutines are not an exception to this; a C# iterator method is conceptually a single method with a single entrypoint - the fact that the compiler breaks it up is an implementation detail. &amp;nbsp;You still get to do some initialization at the top, and any cleanup can be put in finally blocks that will run with the enumerator is disposed.)&lt;/p&gt;
&lt;p&gt;I&amp;#39;ve found that most people who try to religiously follow the SESE ideal don&amp;#39;t end up writing the kind of code people have been posting here (with lots of nice nested ifs and such.) &amp;nbsp;What they do is write a bunch of &amp;quot;goto CLEANUP;&amp;quot; statements, and then do their cleanup at the end. &amp;nbsp;This is an abomination in a world that has &amp;quot;finally&amp;quot; or its equivalents.&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1635344" width="1" height="1"&gt;</description></item><item><title>re: Single-Entry, Single-Exit, Should It Still Be Applicable In Object-oriented Languages?</title><link>http://msmvps.com/blogs/peterritchie/archive/2008/03/07/single-entry-single-exit-should-it-still-be-applicable-in-object-oriented-languages.aspx#1631530</link><pubDate>Thu, 05 Jun 2008 02:05:08 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1631530</guid><dc:creator>Gopi Chand</dc:creator><description>&lt;p&gt;We don&amp;#39;t have to go overboard with the &amp;quot;single exit&amp;quot; rule but I have seen code that doesn&amp;#39;t even attempt to reduce the number of exits. For instance the following code taken from one of the posts above...&lt;/p&gt;
&lt;p&gt;public static bool IsNullOrEmpty(string value)&lt;/p&gt;
&lt;p&gt;{&lt;/p&gt;
&lt;p&gt; &amp;nbsp; if (value != null)&lt;/p&gt;
&lt;p&gt; &amp;nbsp; {&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; return (value.Length == 0);&lt;/p&gt;
&lt;p&gt; &amp;nbsp; }&lt;/p&gt;
&lt;p&gt; &amp;nbsp; return true;&lt;/p&gt;
&lt;p&gt;}&lt;/p&gt;
&lt;p&gt;... can be rewritten as follows (by putting short-circuit booleans to good use):&lt;/p&gt;
&lt;p&gt;public static bool IsNullOrEmpty(string value)&lt;/p&gt;
&lt;p&gt;{&lt;/p&gt;
&lt;p&gt; &amp;nbsp; return value == null || value.Length == 0;&lt;/p&gt;
&lt;p&gt;}&lt;/p&gt;
&lt;p&gt;See how the implementation matches the method name. &lt;/p&gt;
&lt;p&gt;I sincerely believe that structured programing is an art that vastly reduces code complexity and those that don&amp;#39;t believe in it don&amp;#39;t know what they are missing. With the exception of exceptions/gaurds/assertions, it&amp;#39;s easy enough to write code with a single exit. Nesting can be minimized by breaking down large methods into smaller ones. If a method is over a page long, it&amp;#39;s probably too long.&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1631530" width="1" height="1"&gt;</description></item><item><title>re: Single-Entry, Single-Exit, Should It Still Be Applicable In Object-oriented Languages?</title><link>http://msmvps.com/blogs/peterritchie/archive/2008/03/07/single-entry-single-exit-should-it-still-be-applicable-in-object-oriented-languages.aspx#1615272</link><pubDate>Tue, 06 May 2008 15:57:35 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1615272</guid><dc:creator>Jason</dc:creator><description>&lt;p&gt;I view &amp;quot;return&amp;quot; and &amp;quot;exception&amp;quot; differently. A return is the path back from a method, while an exception is...well, an exception. I will aim towards having a single return, and an exception whereever it is logical. &lt;/p&gt;
&lt;p&gt;I generally dispise multiple return statements. I can&amp;#39;t recall having a real problem (either authoring, or reading others&amp;#39; code) with overly deep nesting, and I think this can be addressed with other coding practices and guidlines. &lt;/p&gt;
&lt;p&gt;If you have four nestings or four return statements, your method is likely not refactored to begin with. &lt;/p&gt;
&lt;p&gt;All this being said, I think the most important and unbreakable rule is that all the rules can be broken if done with thought and reason. I have seen a few cases where there have been multiple returns and have thought, &amp;quot;Ok...that makes sense...&amp;quot; &lt;/p&gt;
&lt;p&gt;Now, let&amp;#39;s get to a real topic of contention: ternary operation!!&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1615272" width="1" height="1"&gt;</description></item><item><title>re: Single-Entry, Single-Exit, Should It Still Be Applicable In Object-oriented Languages?</title><link>http://msmvps.com/blogs/peterritchie/archive/2008/03/07/single-entry-single-exit-should-it-still-be-applicable-in-object-oriented-languages.aspx#1612999</link><pubDate>Sun, 04 May 2008 13:43:08 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1612999</guid><dc:creator>PeterRitchie</dc:creator><description>&lt;p&gt;A method all on it's own as shown as an example can't really assume anything; which is why the example checks parameters. &amp;nbsp;It's advisable for any publicly available method to always check it's arguments.&lt;/p&gt;
&lt;p&gt;What if(!(text is string)) actually compiles to is:&lt;/p&gt;
&lt;p&gt;if(text == null)&lt;/p&gt;
&lt;p&gt;Replacing that with String.IsNullOrEmpty avoids having to begin a for loop with no iterations. &amp;nbsp;So, we're back to what I have (minus the extraneous Length check, that someone else pointed out).&lt;/p&gt;
&lt;p&gt;But, for some algorithms you don't want to throw an exception. &amp;nbsp;Let's say we did throw an exception if we passed an invalid string (in your example). &amp;nbsp;What would it mean to an algorithm that needs the count of commas in a string? &amp;nbsp;If it doesn't pass a valid string, would it continue processing any differently if it caught the exception? &amp;nbsp;I.e. it's likely just going to assume in the light of an InvalidArgumentException that what it passed obviously doesn't have an commas in it and process as such.&lt;/p&gt;
&lt;p&gt;But, the point isn't to write a perfect CountCommas method, it's just an academic example showing the difference between SESE and non-SESE styles.&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1612999" width="1" height="1"&gt;</description></item><item><title>re: Single-Entry, Single-Exit, Should It Still Be Applicable In Object-oriented Languages?</title><link>http://msmvps.com/blogs/peterritchie/archive/2008/03/07/single-entry-single-exit-should-it-still-be-applicable-in-object-oriented-languages.aspx#1612981</link><pubDate>Sun, 04 May 2008 13:21:37 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1612981</guid><dc:creator>w</dc:creator><description>&lt;p&gt;The preconditions should not have to be checked. As they are assumed to be valid.&lt;/p&gt;
&lt;p&gt;int CountCommas(string text)&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;// pre: text is a string&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;// we add overhead to check if the text is a string (which is a bit of misplaced check imho)&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;if (!(text is string))&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;// maybe this is only possible if text == null but then, how would we know if this holds in the near future &lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;// maybe in the near future we can also call CountCommas(infinitestringofcommas); where infinitestringofcommas is a new type&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;// so we cannot anticipate on the text being null&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;throw new ArgumentException(&amp;quot;text is not a valid string&amp;quot;);&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;// lets count comma&amp;#39;s &lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;int N = text.Length;&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;int numberOfCommas = 0;&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;for (int n = 0; n != N; n++)&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;if (text&lt;img src="http://msmvps.com/emoticons/emotion-45.gif" alt="No" /&gt; == &amp;#39;,&amp;#39;)&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;numberOfCommas++;&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;// one exit &lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;return numberOfCommas;&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1612981" width="1" height="1"&gt;</description></item><item><title>re: Single-Entry, Single-Exit, Should It Still Be Applicable In Object-oriented Languages?</title><link>http://msmvps.com/blogs/peterritchie/archive/2008/03/07/single-entry-single-exit-should-it-still-be-applicable-in-object-oriented-languages.aspx#1606936</link><pubDate>Mon, 28 Apr 2008 07:23:22 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1606936</guid><dc:creator>Kumosan</dc:creator><description>&lt;p&gt;There is no rule in software development, which cannot be broken, when there is a pressing need to do so. For some rules this justification is harder (rarer) to find, for some rules it is easier to find. IMHO the SESE rule is one of the weakest rules, i.e. it is very easy to find examples where it makes sense to break SESE in many real world examples. Personally I prefer to avoid long if/else if constructs and replace them with many similar looking code blocks:&lt;/p&gt;
&lt;p&gt;if(statement1)&lt;/p&gt;
&lt;p&gt;{&lt;/p&gt;
&lt;p&gt; &amp;nbsp;do stuff&lt;/p&gt;
&lt;p&gt; &amp;nbsp;return&lt;/p&gt;
&lt;p&gt;}&lt;/p&gt;
&lt;p&gt;if(statement2)&lt;/p&gt;
&lt;p&gt;{&lt;/p&gt;
&lt;p&gt; &amp;nbsp;so stuff2&lt;/p&gt;
&lt;p&gt; &amp;nbsp;return;&lt;/p&gt;
&lt;p&gt;}&lt;/p&gt;
&lt;p&gt;If &amp;#39;statements&amp;#39; and &amp;#39;do stuffs&amp;#39; are similar in size and function this is very easy to read and understand. If there are only two or three such blocks I usually don&amp;#39;t bother and let them as they are. If there are more it is easy to see where to refactor and to apply a behavioral pattern like state, strategy or command. This very often leads back to a heeded SESE rule.&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1606936" width="1" height="1"&gt;</description></item><item><title>Single-Entry, Single-Exit em Java ?! &amp;laquo; TJRN Developers</title><link>http://msmvps.com/blogs/peterritchie/archive/2008/03/07/single-entry-single-exit-should-it-still-be-applicable-in-object-oriented-languages.aspx#1575837</link><pubDate>Mon, 07 Apr 2008 01:25:55 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1575837</guid><dc:creator>Single-Entry, Single-Exit em Java ?! « TJRN Developers</dc:creator><description>&lt;p&gt;Pingback from &amp;nbsp;Single-Entry, Single-Exit em Java ?! &amp;amp;laquo; TJRN Developers&lt;/p&gt;
&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1575837" width="1" height="1"&gt;</description></item><item><title>re: Single-Entry, Single-Exit, Should It Still Be Applicable In Object-oriented Languages?</title><link>http://msmvps.com/blogs/peterritchie/archive/2008/03/07/single-entry-single-exit-should-it-still-be-applicable-in-object-oriented-languages.aspx#1542477</link><pubDate>Fri, 14 Mar 2008 02:08:15 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1542477</guid><dc:creator>David Nelson</dc:creator><description>&lt;p&gt;Like pretty much every programming concept/idiom/pattern ever conceived, its not a black and white issue. Religiously adhering to or ignoring SSEE will not make more readable code. Single-exit can create deep nesting or excessive guard clauses; multiple-exit can result in code that is hard to trace by visual inspection. Both of these are undesirable; the key is finding an appropriate balance between the two by understanding the pros and cons of each. As always, there is no silver bullet.&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1542477" width="1" height="1"&gt;</description></item><item><title>re: Single-Entry, Single-Exit, Should It Still Be Applicable In Object-oriented Languages?</title><link>http://msmvps.com/blogs/peterritchie/archive/2008/03/07/single-entry-single-exit-should-it-still-be-applicable-in-object-oriented-languages.aspx#1540354</link><pubDate>Tue, 11 Mar 2008 20:45:52 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1540354</guid><dc:creator>PeterRitchie</dc:creator><description>&lt;p&gt;@Dave: multiple exceptions in a method is multiple exits, not single exit. &amp;nbsp;The argument for a single exit with regard to return statements is easier to take than a single throw per method; but they're tantamount the same thing.&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1540354" width="1" height="1"&gt;</description></item><item><title>re: Single-Entry, Single-Exit, Should It Still Be Applicable In Object-oriented Languages?</title><link>http://msmvps.com/blogs/peterritchie/archive/2008/03/07/single-entry-single-exit-should-it-still-be-applicable-in-object-oriented-languages.aspx#1540342</link><pubDate>Tue, 11 Mar 2008 20:25:35 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1540342</guid><dc:creator>Dave</dc:creator><description>&lt;p&gt;I would submit that throwing exceptions whenever they need to be thrown (throughout the method when they are encountered) and having at most one return statement in each function doesn&amp;#39;t have to be conflicting.&lt;/p&gt;
&lt;p&gt;For example, look at the last chunk of code you wrote. That is the one I prefer. Maybe this means I&amp;#39;m not strict SESE? If so thats fine. Being strict anything is limiting.&lt;/p&gt;
&lt;p&gt;What if you want to add some form of tracing at the end of a method to log the result?&lt;/p&gt;
&lt;p&gt;What if there is some extra work you need to do on the return value before it leaves the method?&lt;/p&gt;
&lt;p&gt;If I am stuck with debugging 1 of 2 existing projects... One with all methods having (potentially) multiple exit points anywhere in the method. Versus another with all methods having exactly 1 exit point. I know which one I&amp;#39;m choosing.&lt;/p&gt;
&lt;p&gt;That said, I do recognize the issue of deeper nesting and that IS a problem.&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1540342" width="1" height="1"&gt;</description></item><item><title>re: Single-Entry, Single-Exit, Should It Still Be Applicable In Object-oriented Languages?</title><link>http://msmvps.com/blogs/peterritchie/archive/2008/03/07/single-entry-single-exit-should-it-still-be-applicable-in-object-oriented-languages.aspx#1540109</link><pubDate>Tue, 11 Mar 2008 14:54:04 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1540109</guid><dc:creator>PeterRitchie</dc:creator><description>&lt;p&gt;@Dave: multiple return statements in a method does not preclude being able to set a break-point at the end of a method to break when the method is complete. &amp;nbsp;&amp;quot;return&amp;quot; is just a language syntax detail; pretty much every language must generate prolog and epilog code for methods, meaning it does implement an single exit; but that's an implementation detail of the language. &amp;nbsp;SESE was created before these high-level languages started doing this, and is useful is that particular context. &amp;nbsp;But, in higher-level languages this &amp;quot;sense of security&amp;quot; makes you limit what you can do with evolving capabilities of the language. &amp;nbsp;Point in case are exceptions, it makes no sense for them to follow single-exit; which means you now have conflicting (confusing, hard-to-maintain, hard to test, etc.) styles.&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1540109" width="1" height="1"&gt;</description></item><item><title>re: Single-Entry, Single-Exit, Should It Still Be Applicable In Object-oriented Languages?</title><link>http://msmvps.com/blogs/peterritchie/archive/2008/03/07/single-entry-single-exit-should-it-still-be-applicable-in-object-oriented-languages.aspx#1540080</link><pubDate>Tue, 11 Mar 2008 14:13:34 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1540080</guid><dc:creator>Dave</dc:creator><description>&lt;p&gt;I still use the single exit strategy. I sometimes wince when I am reading someone elses method and halfway through I see return statement.&lt;/p&gt;
&lt;p&gt;I like the ability to set a break point at the very end of a function and let the code run to see what i&amp;#39;m going to get while still being in the context of that method.&lt;/p&gt;
&lt;p&gt;Theres something reassuring about knowing all code paths will leave my function at 1 spot and 1 spot only. That way if I later find myself needing to .ToUpper() my string result or something, I only need to do it in 1 spot.&lt;/p&gt;
&lt;p&gt;And as far as exceptions go, I throw them at the point they need to be thrown rather than holding onto them.&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1540080" width="1" height="1"&gt;</description></item><item><title>re: Single-Entry, Single-Exit, Should It Still Be Applicable In Object-oriented Languages?</title><link>http://msmvps.com/blogs/peterritchie/archive/2008/03/07/single-entry-single-exit-should-it-still-be-applicable-in-object-oriented-languages.aspx#1539258</link><pubDate>Mon, 10 Mar 2008 17:57:45 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1539258</guid><dc:creator>Troy DeMonbreun</dc:creator><description>&lt;p&gt;@Peter&lt;/p&gt;
&lt;p&gt;Gotcha. :-)&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1539258" width="1" height="1"&gt;</description></item><item><title>re: Single-Entry, Single-Exit, Should It Still Be Applicable In Object-oriented Languages?</title><link>http://msmvps.com/blogs/peterritchie/archive/2008/03/07/single-entry-single-exit-should-it-still-be-applicable-in-object-oriented-languages.aspx#1539022</link><pubDate>Mon, 10 Mar 2008 14:15:54 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1539022</guid><dc:creator>PeterRitchie</dc:creator><description>&lt;p&gt;@Troy: I choose a academic example that was supposed to exemplify multiple returns. &amp;nbsp;A caveat of &amp;quot;pay no attention to the logic&amp;quot; should have been added :-). &amp;nbsp;It's an academic example that shows two different ways of implementing some logic (one where null/zero-length strings are not errors, and one where they are--and generate exceptions).&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1539022" width="1" height="1"&gt;</description></item><item><title>re: Single-Entry, Single-Exit, Should It Still Be Applicable In Object-oriented Languages?</title><link>http://msmvps.com/blogs/peterritchie/archive/2008/03/07/single-entry-single-exit-should-it-still-be-applicable-in-object-oriented-languages.aspx#1538990</link><pubDate>Mon, 10 Mar 2008 13:47:00 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1538990</guid><dc:creator>Ron</dc:creator><description>&lt;p&gt;I agree with the idea of multiple exit points. Your example shows how much cleaner the code can look (by the way, the length==0 check is superflouous, String.IsNullOrEmpty already checks that).&lt;/p&gt;
&lt;p&gt;However, when you&amp;#39;re dealing with developers who aren&amp;#39;t exactly the brightest of the bunch, on large projects, you end up with massive multi-thousand-line functions that have dozens of exit points, and it becomes an absolute maintenance nightmare. However, this is more a problem with huge functions than it is with the multi-exit principle.&lt;/p&gt;
&lt;p&gt;So yeah, I completely agree that SEME is a good idea, but you just need to make sure your team isn&amp;#39;t dumb enough to make functions more than 2-3 dozen lines long.&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1538990" width="1" height="1"&gt;</description></item><item><title>re: Single-Entry, Single-Exit, Should It Still Be Applicable In Object-oriented Languages?</title><link>http://msmvps.com/blogs/peterritchie/archive/2008/03/07/single-entry-single-exit-should-it-still-be-applicable-in-object-oriented-languages.aspx#1538916</link><pubDate>Mon, 10 Mar 2008 11:32:22 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1538916</guid><dc:creator>vP</dc:creator><description>&lt;p&gt;I often use a flag to keep track whether operations are successful or not. This can give us SESE approach without too deep nested structures. As exceptions should be exceptional, I&amp;#39;d rather throw them right away in the beginning of a method, just like the last example has done.&lt;/p&gt;
&lt;p&gt;The final solution takes a shortcut. If the business case would require the method to handle null (not likely) and String.Empty objects (more likely), more checks are required in code.&lt;/p&gt;
&lt;p&gt;Here is my take for comma counter with flag variable.&lt;/p&gt;
&lt;p&gt;public static int CountCommas(string text){&lt;/p&gt;
&lt;p&gt; &amp;nbsp;// Exceptions would be thrown right here. Maybe for null parameter?&lt;/p&gt;
&lt;p&gt; &amp;nbsp;// Non-exceptional code starts here.&lt;/p&gt;
&lt;p&gt; &amp;nbsp;bool success;&lt;/p&gt;
&lt;p&gt; &amp;nbsp;int result = 0;&lt;/p&gt;
&lt;p&gt; &amp;nbsp;success = !String.IsNullOrEmpty(text) ? true : false;&lt;/p&gt;
&lt;p&gt; &amp;nbsp;if(success){&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;success = text.Length &amp;gt; 0 ? true : false;&lt;/p&gt;
&lt;p&gt; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt; &amp;nbsp;if(success){&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;int index = 0;&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;while (index &amp;gt; 0){&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;index = text.IndexOf(&amp;#39;,&amp;#39;, index);&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;if (index &amp;gt; 0){&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;++result;&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt; &amp;nbsp;return result;&lt;/p&gt;
&lt;p&gt;}&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1538916" width="1" height="1"&gt;</description></item><item><title>re: Single-Entry, Single-Exit, Should It Still Be Applicable In Object-oriented Languages?</title><link>http://msmvps.com/blogs/peterritchie/archive/2008/03/07/single-entry-single-exit-should-it-still-be-applicable-in-object-oriented-languages.aspx#1538853</link><pubDate>Mon, 10 Mar 2008 08:44:18 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1538853</guid><dc:creator>Matt</dc:creator><description>&lt;p&gt;Just for the ones that don&amp;#39;t know, checking text.Length is included in IsNullOrEmpty():&lt;/p&gt;
&lt;p&gt;public static bool IsNullOrEmpty(string value)&lt;/p&gt;
&lt;p&gt;{&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;if (value != null)&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;return (value.Length == 0);&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;return true;&lt;/p&gt;
&lt;p&gt;}&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1538853" width="1" height="1"&gt;</description></item><item><title>Reflective Perspective - Chris Alcock  &amp;raquo; The Morning Brew #49</title><link>http://msmvps.com/blogs/peterritchie/archive/2008/03/07/single-entry-single-exit-should-it-still-be-applicable-in-object-oriented-languages.aspx#1538841</link><pubDate>Mon, 10 Mar 2008 08:21:03 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1538841</guid><dc:creator>Reflective Perspective - Chris Alcock  » The Morning Brew #49</dc:creator><description>&lt;p&gt;Pingback from &amp;nbsp;Reflective Perspective - Chris Alcock &amp;nbsp;&amp;amp;raquo; The Morning Brew #49&lt;/p&gt;
&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1538841" width="1" height="1"&gt;</description></item><item><title>re: Single-Entry, Single-Exit, Should It Still Be Applicable In Object-oriented Languages?</title><link>http://msmvps.com/blogs/peterritchie/archive/2008/03/07/single-entry-single-exit-should-it-still-be-applicable-in-object-oriented-languages.aspx#1538803</link><pubDate>Mon, 10 Mar 2008 06:55:52 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1538803</guid><dc:creator>Nikola Malovic</dc:creator><description>&lt;p&gt;I am not following the Single Exit mantra because it leads to multi level nested statements and in case that level&amp;gt;3 that is very hard to read&lt;/p&gt;
&lt;p&gt;Instead of SESE mantra, I prefer Fowler &amp;quot;Guard clause&amp;quot; style which IMHO increase code readibility &lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1538803" width="1" height="1"&gt;</description></item></channel></rss>