NNTP-Posting-Date: Mon, 17 Jun 2013 08:46:59 -0500 Date: Mon, 17 Jun 2013 14:46:57 +0100 From: lipska the kat <"nospam at neversurrender dot co dot uk"> Organization: Trollbusters 3 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:17.0) Gecko/20130308 Thunderbird/17.0.4 MIME-Version: 1.0 Newsgroups: comp.lang.java.programmer Subject: Re: About java program. References: <324f929f-772e-4004-81e1-6b454cdae794@googlegroups.com> In-Reply-To: Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 8bit Message-ID: Lines: 97 X-Usenet-Provider: http://www.giganews.com X-AuthenticatedUsername: NoAuthUser X-Trace: sv3-dOGzmQmJBsGrSCeKFsdIJpeb3JHlJ3j1QsVUiBzjpKUQUCq/JPx3tHPfyP109VQy18v9D8IGN0RSx6u!UI0k+6Ia7p8iEgKX9Vqo4tws4xb9kPMCp52o9hn55W7hF+uI0LfGKE5SMXMGpA2UQCH+Aet6BJU= X-Complaints-To: abuse@btinternet.com X-DMCA-Complaints-To: abuse@btinternet.com X-Abuse-and-DMCA-Info: Please be sure to forward a copy of ALL headers X-Abuse-and-DMCA-Info: Otherwise we will be unable to process your complaint properly X-Postfilter: 1.3.40 X-Original-Bytes: 4323 Path: csiph.com!usenet.pasdenom.info!news.stben.net!border3.nntp.ams.giganews.com!Xl.tags.giganews.com!border1.nntp.ams.giganews.com!nntp.giganews.com!local2.nntp.ams.giganews.com!nntp.bt.com!news.bt.com.POSTED!not-for-mail Xref: csiph.com comp.lang.java.programmer:24436 On 17/06/13 13:49, Arved Sandstrom wrote: > On 06/16/2013 12:28 PM, Robert Klemme wrote: >> On 16.06.2013 15:21, lipska the kat wrote: >>> On 16/06/13 00:12, Robert Klemme wrote: >>>> On 15.06.2013 22:38, rajbhaumik@gmail.com wrote: >>>>> import acm.program.*; >>>>> public class askYesNoQuestion extends ConsoleProgram { >>>>> public void run(){ >>>>> String str=readLine("Would you like instructions"); >>>>> println(askYesNoquestion(str)); >>>>> } >>>>> 7. private boolean askYesNoquestion (String str){ >>>>> if(str.equals("yes"))return true; >>>>> if(str.equals("no")) return false; >>>>> } >>>>> >>>>> } >>>>> the false is that , the program cannot be executed, because no 7 >>>>> said, this method must return a type of boolean. >>>> >>> >>> One possible solution (not compiled or tested) >>> >>> private boolean askYesNoquestion (String str){ >>> boolean response = false; >>> if(str.equals("yes")){ >>> response = true; >>> } >>> return response; >>> } [snip] > To the original point, I agree to an extent. But the actual idiom that > lipska used is not bad in general, if you're used to single return. It's > sort of like *always* providing braces for conditionals, even for one > liners, defensive coding in part, but also adhering to personal coding > style. ... and what is your opinion of defensive coding? Anyone who has ever written software to interact with a user via a device interface knows (or should know) that defensive coding is de-riguer. If you don't want the device to crash every time someone does something unexpected you need to anticipate no only the unexpected but the unanticipated. There is *no* room for error. This discipline is hard to learn and once learned even harder to un-learn. This is why I don't like things like return str.equals("yes"); It's lazy coding and harder to understand than my example and yes, I realize that it's only a one liner but the next stage in the 'development' of this style is if(something){ return something; } else if(something else){ return something else; } else{ return some other value } It is much easier to understand and debug the following type var; if(something){ var = something; } else if(something else){ var = something else; } else{ var = some other value } return var; If I was doing a proper job and not just providing a simple example I would have had a pre-condition on the method *and* exception handling *and* braces around every conditional, even one liners, *and* a fallback position if the operation was mission critical. So yes, it's my personal style learned from painful experience. Thank you for understanding lipska -- Lipska the KatŠ: Troll hunter, sandbox destroyer and farscape dreamer of Aeryn Sun