Path: csiph.com!usenet.pasdenom.info!aioe.org!news.swapon.de!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail From: Robert Klemme Newsgroups: comp.lang.java.programmer Subject: Re: About java program. Date: Fri, 05 Jul 2013 23:15:32 +0200 Lines: 78 Message-ID: References: <324f929f-772e-4004-81e1-6b454cdae794@googlegroups.com> <19wnriq53qxel.u9rt8wy6z52g.dlg@40tude.net> <1lvu4yv8sxe73$.n4o05ixuag9y.dlg@40tude.net> <85GdncWZdo04-lzMnZ2dnUVZ8mydnZ2d@bt.com> <51d6396c$0$295$14726298@news.sunsite.dk> Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: quoted-printable X-Trace: individual.net gPhYU6WDY76oMCS55BD1SQm5B67PX3PgksJ/0Rm+/5dkwsG38= Cancel-Lock: sha1:W1t1Wo6if4wgUgU9eucfqL+OA/A= User-Agent: Mozilla/5.0 (Windows NT 6.0; WOW64; rv:17.0) Gecko/20130620 Thunderbird/17.0.7 In-Reply-To: X-Antivirus: avast! (VPS 130705-1, 05.07.2013), Outbound message X-Antivirus-Status: Clean Xref: csiph.com comp.lang.java.programmer:24665 On 05.07.2013 10:13, lipska the kat wrote: > On 05/07/13 04:11, Arne Vajh=F8j wrote: >> The above has nothing to do with defensive coding. It does >> not protect against anything that the simple: > > For goodness sake *read the post* > > I said 'lack of error handling notwithstanding' > > Do you understand what that means? > > Do you want an example of defensive coding? > > READ THIS FIRST > This is an *example* ... got that, good. Funny thing is that people will only see in an example what is presented = in the example. So if there is an example which leaves place for=20 specific error handling and that error handling is not included people=20 won't see it. They just see a method with unnecessary complexity. Maybe you'll say it's not easy to pick a good example. But examples are = there for a reason: they are used as tools to communicate something. If = they fail to do that they should be improved or left out. > and no, I haven't compiled it and no, I wouldn't normally hard code > messages in this way (jeez) > > Now then > > private boolean askYesNoquestion (String str) throws BadArgumentExcepti= on{ > > boolean result =3D false; > > if(null =3D=3D str){ > logger.log("Argument is null"); > throw new BadArgumentException("Argument is null"); > } > else{ > if(str.equals("yes")){ > result =3D true; > } > else if(!str.equals("no")){ > logger.log("Incorrect Argument, argument is " + str); > throw new BadArgumentException("argument is " + str); > } > } > return result; > } > > This is *one* way of doing it > > Overkill ... no, defensive yes. This example demonstrates what I will call bad practice: the exception=20 is thrown so the calling code can decide how to handle the error. It=20 may actually be that the caller can perfectly deal with that situation=20 and proceed normally. Method askYesNoquestion() cannot know this. But=20 because the method does the logging itself you'll end up seeing messages = in a log file for things which are not noteworthy. I say, either log=20 and deal with the error locally OR throw an exception containing a=20 meaningful message - but not both. That is misleading. Cheers robert --=20 remember.guy do |as, often| as.you_can - without end http://blog.rubybestpractices.com/