Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!news.mixmin.net!eternal-september.org!feeder.eternal-september.org!mx05.eternal-september.org!.POSTED!not-for-mail From: Daniele Futtorovic Newsgroups: comp.lang.java.programmer Subject: Re: About java program. Date: Sat, 22 Jun 2013 19:25:57 +0200 Organization: A noiseless patient Spider Lines: 153 Message-ID: References: <324f929f-772e-4004-81e1-6b454cdae794@googlegroups.com> <19wnriq53qxel.u9rt8wy6z52g.dlg@40tude.net> Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit Injection-Date: Sat, 22 Jun 2013 17:20:49 +0000 (UTC) Injection-Info: mx05.eternal-september.org; posting-host="f367d9e8a7c7f2a10160e0639e957a17"; logging-data="15734"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+LInm802ctw4LJprA8F+/C" User-Agent: Mozilla/5.0 (Windows NT 5.1; rv:17.0) Gecko/20130509 Thunderbird/17.0.6 In-Reply-To: Cancel-Lock: sha1:fqUJ88PCZ/ZO5+1VdxuVVghQ3e4= Xref: csiph.com comp.lang.java.programmer:24511 On 22/06/2013 03:39, Arved Sandstrom allegedly wrote: > On 06/21/2013 11:24 AM, Daniele Futtorovic wrote: >> On 21/06/2013 04:33, Arved Sandstrom allegedly wrote: >>> On 06/19/2013 11:42 AM, Daniele Futtorovic wrote: >>>> I'm all for defensive programming, but I think that there's a limit >>>> where it can be on the verge of silliness, and that that limit has been >>>> grazed here. >>>> >>>> Less-than-bright programmers IMO cannot be a justification for crossing >>>> that line; less-than-bright programmers have more ways to mess >>>> things up >>>> than you could possibly ever guard against. "Do not underestimate the >>>> ingenuity of complete fools", as the saying goes. >>>> >>>> And for me, there is an additional aspect: if a piece of code does >>>> something simple, something basic, then I will want it to be short, and >>>> vice-versa. Verbosity of code, to me, should hold a first-glance >>>> indication of the complexity of the algorithm. In other words: be terse >>>> IFF it's basic, be verbose IFF it's not. >>>> >>>> Perhaps your mileage does vary. No beef with that. Just throwing in my >>>> two cents. >>>> >>> It's a good discussion. I'm not personally convinced that verbosity adds >>> complexity, as a rule. There's really two types of complexity here: >>> maintainability complexity - as in, can Joe Blow 12 months later >>> understand the code best if it's compact versus unrolled, and formal >>> complexity, cyclomatic and otherwise. >>> >>> I'll admit that verbosity does usually add maintainability complexity, >>> if not the other kind. But not always. I've seen plenty of code that is >>> conceptually quite complex but very terse. >> >> Sorry to nitpick, but I don't think you understood me right. I'm not >> saying that verbosity adds complexity; I'm saying: if the piece of code >> does something tricky, something complicated, tend to make it verbose, >> otherwise, tend to make it terse. Thus, seeing a big blob of (e.g. >> unrolled, step-by-step) code holds the message "here be dragons". (And >> if it cannot be made verbose, then it means it absolutely requires an >> in-line comment). > > We're very much in agreement, on all points you just made. > >> At least the way I perceived it, this discussion was about making stuff >> verbose indiscriminately, viz. basic code as well. I disagree with that >> in the above sense. >> > I won't speak for anyone else, the argument I was making was for > verbosity in support of defensive programming. I see Java code all the > time where exceptions are try-caught with no thought placed into > handling the problem somewhere - defensive coding fail...and handling it > properly would add verbosity. > > I see main methods all the time where someone assumes the number and > type of arguments. Defensive coding fail. I see methods all the time - > like I said - where the same coder wrote the calling code and called > code, and makes assumptions about what the calling code will do. > Defensive coding fail. > > Or they make the naive assumption that people read Javadocs thoroughly. > It's not the calling code's responsibility to protect the called code, > after all. Given that we have to consider the bounds of parameters > carefully, I don't see how some verbosity in the form of defensive code > is objectionable. > > "Here be dragons" often means simply that there is - that we have to > guard against what the calling code is doing. I see. Thanks for clarifying. It looks like I had not understood the discussion about verbosity in the same sense as you do. In my view, when talking about verbosity, we talk about a measure on, and that's the important point, an iso-functional basis. To illustrate, when I switch on a "verbose" flag on my command line, I do not expect the command to perform any differently than before -- only that it should be more talkative about it. But the functions it performs should not be impacted by it. To illustrate with code, and to pick some of the original examples again, take the following two methods: boolean method1( String input ){ return "yes".equals( input ); } boolean method2( String input ){ final boolean ret; if( input == null ){ ret = false; } else if( input.equals("yes") ){ ret = true; } else { ret = false; } return ret; } This is what I mean. Both methods perform exactly the same for any input. The only matter in which they differ is their verbosity. And to pick up the argument again: it could be argued that the latter form is preferable, because it is more obvious what happens in each case. I argue against that. Not generally, but in this case. Because what's going on there is a very basic operation (making sure the input is exactly the String "yes"). The second form is too verbose -- that is, so verbose that it becomes clutter. The first (non-verbose) form expresses the intent clearly enough. If there is a "programmer" who does not understand that (especially the null-implication), then I'm sure Lew would have some poignant recommendations as to what avenues of employment they rather ought to pursue, and I would happen to be in agreement with him. Some of what you are talking about, on the topic of defensive programming, on the other hand, I would call functional defects. I do not perceive those as being matters of verbosity or not, but of whether the code is functionally complete or not. To your hypothetical programmer who does not handle exceptions because it would supposedly add verbosity, "verbosity" is but a pathetic front to mask either their lack of professionalism. The other examples you give definitely fall in the realm of defensive programming. Yes, a method should not make hidden assumptions as to its parameters (OTOH there would be nothing wrong per se with what I'd call an explicit assumption along the lines of: if( argumentNotValid ) throw new IllegalArgumentException(); ). Yes, every caller should be defensive about what the callee might return (within the boundaries of its contract). It should be noted that annotations have given us a potential tool forcibly to impose more stringency in these regards, although the way I see it it won't really be usable until teh Oracle makes the effort to formalise them and adds them to the core lib (I mean things like @NotNull). It still needs to mature a bit I think. The aforementioned is mitigated in code that is internal to a component (in the spirit of encapsulation, code ought to be divided into components such that the density of interrelation of code within a component is always strictly greater than that of interrelation of code in different components). Think private or package-private methods. It is often impractical and counter-productive to have to forego any hidden assumptions in those parts of the code. Then again, it is precisely the combination of these two aspects, that it is impractical and that it would be necessary, which makes OO and esp. encapsulation such a virtuous tool: by structuring code and data flow, you *can* relegate, notably, input checks to the boundaries of the component and operate on assumptions within the component. (Granted, this does not help the case where multiple programmers who are not in relation with each other work on the internals of the same component, which is why such a situation should be avoided). -- DF.