tag:blogger.com,1999:blog-8131994348885474777.post1620751347406750490..comments2023-10-03T07:31:04.439-07:00Comments on Micky Dionisio's Software Architecture Blog: Quick Tip - See nested conditionals? Consider Guard ClausesMicky Dionisiohttp://www.blogger.com/profile/12849444647466518787noreply@blogger.comBlogger5125tag:blogger.com,1999:blog-8131994348885474777.post-30836991477588867042009-12-13T09:47:32.461-08:002009-12-13T09:47:32.461-08:00@Steve-
You could use a case, whatever your prefe...@Steve-<br /><br />You could use a case, whatever your preference is. The important thing to take away from this example is not the whether to use an if or a case because both would work. I agree to remove the first variable assignment and simply return the method, this helps with code readability and in your example above would throw an error.<br /><br />The important thing to take away is that you if you find yourself setting a variable in your if/case and returning it at the very end, try to see if you can instead return the method altogether and rid methods of variable assignments. It removes ambiguity and is more testable that way.Micky Dionisiohttps://www.blogger.com/profile/12849444647466518787noreply@blogger.comtag:blogger.com,1999:blog-8131994348885474777.post-69667869652259321382009-12-13T08:16:06.674-08:002009-12-13T08:16:06.674-08:00I should have made the first option a "return...I should have made the first option a "return" to stay consistent. As an aside, however, I prefer the use of the intermediate variable so that I only have one result at the end of my method. Personal preference perhaps.Steve Bryanthttp://www.bryantwebconsulting.com/noreply@blogger.comtag:blogger.com,1999:blog-8131994348885474777.post-65726465610269965732009-12-13T08:14:09.074-08:002009-12-13T08:14:09.074-08:00In your example all of your conditionals are testi...In your example all of your conditionals are testing the same variable. In which case, you could use a switch:<br /><br />switch ( variables.type ) {<br /> case "something cool":<br /> result = doSomethingCool();<br /> break;<br />case "something else":<br /> return doSomethingElse();<br /> break;<br />case "some other thing":<br /> return someOtherThing();<br />break;<br />default:<br />return someDefault();<br />}Steve Bryanthttp://www.bryantwebconsulting.com/noreply@blogger.comtag:blogger.com,1999:blog-8131994348885474777.post-17488180494133944092009-12-12T10:12:27.873-08:002009-12-12T10:12:27.873-08:00@Anonymous - not sure what you mean, elaborate?@Anonymous - not sure what you mean, elaborate?Micky Dionisiohttps://www.blogger.com/profile/12849444647466518787noreply@blogger.comtag:blogger.com,1999:blog-8131994348885474777.post-83867169528257982872009-12-12T07:52:21.945-08:002009-12-12T07:52:21.945-08:00Why not a case statement?Why not a case statement?Anonymousnoreply@blogger.com