Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.java.programmer > #24445
| From | Joshua Cranmer đ§ <Pidgeot18@verizon.invalid> |
|---|---|
| Newsgroups | comp.lang.java.programmer |
| Subject | Re: About java program. |
| Date | 2013-06-17 17:36 -0500 |
| Organization | A noiseless patient Spider |
| Message-ID | <kpo2oa$j1e$1@dont-email.me> (permalink) |
| References | (1 earlier) <b24an6FlpvpU1@mid.individual.net> <UoednTl5R618JiDMnZ2dnUVZ8iadnZ2d@bt.com> <b263siF2vkkU1@mid.individual.net> <uBDvt.60965$5i1.39234@newsfe15.iad> <ILKdnZ-vg53OjiLMnZ2dnUVZ7tmdnZ2d@bt.com> |
On 6/17/2013 8:46 AM, lipska the kat wrote:
> ... and what is your opinion of defensive coding?
The term, as I understand it, refers to coding under the assumption that
everyone who uses your API is a stupid idiot who can't follow your rules
properly. The matter at hand here is basically a stylistic concern about
which is mentally easier to follow, which falls under the category of
"everyone has an opinion which is strongly held and won't be changed."
> 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
To you, maybe. To me, no. This kind of one liner immediately indicates
to me that the function is equivalent with the notion that the string is
exactly "yes". In your example, by contrast, that equivalence is not
obvious: it says that the answer is false unless the string is exactly
"yes", and uncovering the functional equivalence requires resolving a
double-negative.
> 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
Style wars much? To me, personally, early-return is often valuable,
especially in languages like C++ or Java which have useful constructs
for handling post-call cleanup such as RAII or try-finally blocks. I'm
going to stop short of saying I prefer early-return, since it is very
much a decision that applies in a specific context, and these examples
are all trying to apply a universally generic rule.
An example of my preferred code style would be something like this (the
comments preceding most of those blocks of code are necessary in this
style--if how you got there isn't trivially obvious, explain what it
means to get there):
Frabnaz getResourceFromKey(String key) {
// Normalize the key so we know exactly how to refer to it in our
// caches.
key = normalize(key);
if (!isValid(key))
throw new IllegalArgumentException(...);
// We have a hyper-optimized variant of this if we know this is
// already on a local filesystem
if (isLocalFile(key))
return getLocalFileResource(key);
// This resource must be on the network. To keep this from killing
// performance, cache uses...
MagicCache cache = foobarToGrabTheCache();
if (cache.hasResource(key))
return cache.getResource(key);
// Can't find it in the cache, so download it but save off a copy
// in the cache.
Frabnaz result = getResourceFromNet(key);
cache.putResource(key, result);
return result;
}
--
Beware of bugs in the above code; I have only proved it correct, not
tried it. -- Donald E. Knuth
Back to comp.lang.java.programmer | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
About java program. rajbhaumik@gmail.com - 2013-06-15 13:38 -0700
Re: About java program. Arne VajhĂžj <arne@vajhoej.dk> - 2013-06-15 16:50 -0400
Re: About java program. Robert Klemme <shortcutter@googlemail.com> - 2013-06-16 01:12 +0200
Re: About java program. lipska the kat <"nospam at neversurrender dot co dot uk"> - 2013-06-16 14:21 +0100
Re: About java program. Robert Klemme <shortcutter@googlemail.com> - 2013-06-16 17:28 +0200
Re: About java program. lipska the kat <"nospam at neversurrender dot co dot uk"> - 2013-06-16 19:52 +0100
Re: About java program. Arne VajhĂžj <arne@vajhoej.dk> - 2013-06-16 15:50 -0400
Re: About java program. lipska the kat <"nospam at neversurrender dot co dot uk"> - 2013-06-17 09:36 +0100
Re: About java program. Arved Sandstrom <asandstrom2@eastlink.ca> - 2013-06-17 09:49 -0300
Re: About java program. lipska the kat <"nospam at neversurrender dot co dot uk"> - 2013-06-17 14:46 +0100
Re: About java program. Arved Sandstrom <asandstrom2@eastlink.ca> - 2013-06-17 13:32 -0300
Re: About java program. Joerg Meier <joergmmeier@arcor.de> - 2013-06-17 20:32 +0200
Re: About java program. Eric Sosman <esosman@comcast-dot-net.invalid> - 2013-06-17 14:49 -0400
Re: About java program. Arved Sandstrom <asandstrom2@eastlink.ca> - 2013-06-17 18:38 -0300
Re: About java program. Eric Sosman <esosman@comcast-dot-net.invalid> - 2013-06-17 18:30 -0400
Re: About java program. Arved Sandstrom <asandstrom2@eastlink.ca> - 2013-06-18 17:33 -0300
Re: About java program. Arne VajhĂžj <arne@vajhoej.dk> - 2013-07-04 22:56 -0400
Re: About java program. Daniele Futtorovic <da.futt.news@laposte-dot-net.invalid> - 2013-06-18 00:13 +0200
Re: About java program. Arved Sandstrom <asandstrom2@eastlink.ca> - 2013-06-18 17:50 -0300
Re: About java program. lipska the kat <"nospam at neversurrender dot co dot uk"> - 2013-06-19 08:50 +0100
Re: About java program. Arved Sandstrom <asandstrom2@eastlink.ca> - 2013-06-19 15:16 -0300
Re: About java program. Lew <lewbloch@gmail.com> - 2013-06-19 16:20 -0700
Re: About java program. Jeff Higgins <jeff@invalid.invalid> - 2013-07-07 13:31 -0400
Re: About java program. Jeff Higgins <jeff@invalid.invalid> - 2013-07-07 18:16 -0400
Re: About java program. Jeff Higgins <jeff@invalid.invalid> - 2013-07-07 19:21 -0400
Re: About java program. Jeff Higgins <jeff@invalid.invalid> - 2013-07-07 20:07 -0400
Re: About java program. Jeff Higgins <jeff@invalid.invalid> - 2013-07-07 20:21 -0400
Re: About java program. Jeff Higgins <jeff@invalid.invalid> - 2013-07-07 20:40 -0400
Re: About java program. Jeff Higgins <jeff@invalid.invalid> - 2013-07-07 22:20 -0400
Re: About java program. Lew <lewbloch@gmail.com> - 2013-07-07 21:47 -0700
Re: About java program. Jeff Higgins <jeff@invalid.invalid> - 2013-07-08 06:58 -0400
Re: About java program. Jeff Higgins <jeff@invalid.invalid> - 2013-07-08 08:07 -0400
Re: About java program. Jeff Higgins <jeff@invalid.invalid> - 2013-07-08 08:58 -0400
Re: About java program. Jeff Higgins <jeff@invalid.invalid> - 2013-07-08 09:18 -0400
Re: About java program. Jeff Higgins <jeff@invalid.invalid> - 2013-07-08 09:47 -0400
Re: About java program. Jeff Higgins <jeff@invalid.invalid> - 2013-07-08 10:07 -0400
Re: About java program. Daniele Futtorovic <da.futt.news@laposte-dot-net.invalid> - 2013-06-19 16:42 +0200
Re: About java program. Arved Sandstrom <asandstrom2@eastlink.ca> - 2013-06-20 23:33 -0300
Re: About java program. Martin Gregorie <martin@address-in-sig.invalid> - 2013-06-21 04:16 +0000
Re: About java program. Joerg Meier <joergmmeier@arcor.de> - 2013-06-21 12:27 +0200
Re: About java program. Martin Gregorie <martin@address-in-sig.invalid> - 2013-06-21 21:14 +0000
Re: About java program. Joerg Meier <joergmmeier@arcor.de> - 2013-06-21 23:37 +0200
Re: About java program. Martin Gregorie <martin@address-in-sig.invalid> - 2013-06-21 23:42 +0000
Re: About java program. Gene Wirchenko <genew@telus.net> - 2013-06-24 13:00 -0700
Re: About java program. Martin Gregorie <martin@address-in-sig.invalid> - 2013-06-24 21:29 +0000
Re: About java program. Martin Gregorie <martin@address-in-sig.invalid> - 2013-06-24 21:42 +0000
Re: About java program. Arved Sandstrom <asandstrom2@eastlink.ca> - 2013-06-21 23:59 -0300
Re: About java program. Martin Gregorie <martin@address-in-sig.invalid> - 2013-06-22 13:30 +0000
Re: About java program. Daniele Futtorovic <da.futt.news@laposte-dot-net.invalid> - 2013-06-21 16:24 +0200
Re: About java program. Arved Sandstrom <asandstrom2@eastlink.ca> - 2013-06-21 22:39 -0300
Re: About java program. Daniele Futtorovic <da.futt.news@laposte-dot-net.invalid> - 2013-06-22 19:25 +0200
Re: About java program. lipska the kat <"nospam at neversurrender dot co dot uk"> - 2013-06-22 19:41 +0100
Re: About java program. Daniele Futtorovic <da.futt.news@laposte-dot-net.invalid> - 2013-06-23 03:53 +0200
Re: About java program. lipska the kat <"nospam at neversurrender dot co dot uk"> - 2013-06-23 09:21 +0100
Re: About java program. Daniele Futtorovic <da.futt.news@laposte-dot-net.invalid> - 2013-06-24 18:57 +0200
Re: About java program. lipska the kat <"nospam at neversurrender dot co dot uk"> - 2013-06-24 18:14 +0100
Re: About java program. lipska the kat <"nospam at neversurrender dot co dot uk"> - 2013-06-22 09:03 +0100
Re: About java program. Daniele Futtorovic <da.futt.news@laposte-dot-net.invalid> - 2013-06-22 19:26 +0200
Re: About java program. lipska the kat <"nospam at neversurrender dot co dot uk"> - 2013-06-22 18:51 +0100
Re: About java program. Arne VajhĂžj <arne@vajhoej.dk> - 2013-07-04 22:58 -0400
Re: About java program. Joerg Meier <joergmmeier@arcor.de> - 2013-06-19 00:38 +0200
Re: About java program. Eric Sosman <esosman@comcast-dot-net.invalid> - 2013-06-18 18:56 -0400
Re: About java program. Arved Sandstrom <asandstrom2@eastlink.ca> - 2013-06-18 21:26 -0300
Re: About java program. Robert Klemme <shortcutter@googlemail.com> - 2013-06-19 21:20 +0200
Re: About java program. Sven Köhler <remove-sven.koehler@gmail.com> - 2013-06-20 11:23 +0300
Re: About java program. Robert Klemme <shortcutter@googlemail.com> - 2013-06-20 19:18 +0200
Re: About java program. blmblm@myrealbox.com <blmblm.myrealbox@gmail.com> - 2013-07-15 20:20 +0000
Re: About java program. lipska the kat <"nospam at neversurrender dot co dot uk"> - 2013-06-19 09:14 +0100
Re: About java program. lipska the kat <"nospam at neversurrender dot co dot uk"> - 2013-06-19 09:11 +0100
Re: About java program. Arne VajhĂžj <arne@vajhoej.dk> - 2013-07-04 23:11 -0400
Re: About java program. lipska the kat <"nospam at neversurrender dot co dot uk"> - 2013-07-05 09:13 +0100
Re: About java program. Robert Klemme <shortcutter@googlemail.com> - 2013-07-05 23:15 +0200
Re: About java program. Arne VajhĂžj <arne@vajhoej.dk> - 2013-07-05 22:03 -0400
Re: About java program. Robert Klemme <shortcutter@googlemail.com> - 2013-07-06 13:01 +0200
Re: About java program. lipska the kat <"nospam at neversurrender dot co dot uk"> - 2013-07-06 13:43 +0100
Re: About java program. Martin Gregorie <martin@address-in-sig.invalid> - 2013-07-06 14:17 +0000
Re: About java program. Arne VajhĂžj <arne@vajhoej.dk> - 2013-07-06 16:18 -0400
Re: About java program. lipska the kat <"nospam at neversurrender dot co dot uk"> - 2013-07-06 21:42 +0100
Re: About java program. lipska the kat <"nospam at neversurrender dot co dot uk"> - 2013-07-06 09:16 +0100
Re: About java program. Robert Klemme <shortcutter@googlemail.com> - 2013-07-06 13:14 +0200
Re: About java program. Eric Sosman <esosman@comcast-dot-net.invalid> - 2013-07-06 08:22 -0400
Re: About java program. Robert Klemme <shortcutter@googlemail.com> - 2013-07-07 20:43 +0200
Re: About java program. Eric Sosman <esosman@comcast-dot-net.invalid> - 2013-07-07 18:07 -0400
Re: About java program. Arne VajhĂžj <arne@vajhoej.dk> - 2013-07-07 19:35 -0400
Re: About java program. Eric Sosman <esosman@comcast-dot-net.invalid> - 2013-07-07 20:42 -0400
Re: About java program. Robert Klemme <shortcutter@googlemail.com> - 2013-07-08 20:50 +0200
Re: About java program. Eric Sosman <esosman@comcast-dot-net.invalid> - 2013-07-08 15:01 -0400
Re: About java program. Robert Klemme <shortcutter@googlemail.com> - 2013-07-08 23:29 +0200
Re: About java program. Arne VajhĂžj <arne@vajhoej.dk> - 2013-07-06 16:27 -0400
Re: About java program. Lew <lewbloch@gmail.com> - 2013-07-06 13:41 -0700
Re: About java program. Arne VajhĂžj <arne@vajhoej.dk> - 2013-07-05 21:59 -0400
Re: About java program. lipska the kat <"nospam at neversurrender dot co dot uk"> - 2013-06-17 21:06 +0100
Re: About java program. Joshua Cranmer đ§ <Pidgeot18@verizon.invalid> - 2013-06-17 17:36 -0500
Re: About java program. Robert Klemme <shortcutter@googlemail.com> - 2013-06-18 08:24 +0200
Re: About java program. lipska the kat <"nospam at neversurrender dot co dot uk"> - 2013-06-18 09:03 +0100
Re: About java program. Joshua Cranmer đ§ <Pidgeot18@verizon.invalid> - 2013-06-18 13:07 -0500
Re: About java program. Lew <lewbloch@gmail.com> - 2013-06-18 14:48 -0700
Re: About java program. lipska the kat <"nospam at neversurrender dot co dot uk"> - 2013-06-19 08:36 +0100
Re: About java program. Arved Sandstrom <asandstrom2@eastlink.ca> - 2013-06-18 23:33 -0300
Re: About java program. lipska the kat <"nospam at neversurrender dot co dot uk"> - 2013-06-19 08:34 +0100
Re: About java program. Arne VajhĂžj <arne@vajhoej.dk> - 2013-07-04 22:53 -0400
Re: About java program. Roedy Green <see_website@mindprod.com.invalid> - 2013-06-16 07:04 -0700
Re: About java program. Jeff Higgins <jeff@invalid.invalid> - 2013-06-18 04:06 -0400
csiph-web