-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Leon,
On 4/25/16 10:38 AM, Leon Rosenberg wrote: > On Mon, Apr 25, 2016 at 4:13 PM, Christopher Schultz < > ch...@christopherschultz.net> wrote: > > Leon, > > On 4/22/16 12:24 PM, Leon Rosenberg wrote: >>>> Hi guys, >>>> > > I would always choose the case with the elses. > > First, I think it's more clear: if you expect that only one branch > of the code will run, and no others, then the elses tell the reader > that fact without any further commentary. The zero-else case might > help you catch some logic errors (because for example you can log > each entry into a branch) but there are of course other ways to do > that. > > Second, it's more efficient, regardless of what type of processor > you have. Let's take an example where there are more than 3 > branches. How about something like a million branches (just to > accentuate the point)? If one of the branches runs, the others are > skipped. If all branches have an equal change of being chosen, then > the CPU has to perform on average 500000 predicates. If you put the > common cases at the top, you can do even better. Let's assume the > 80/20 rule applies, here, and you can take a guess that, on > average, the CPU will only have to perform 100000 predicates on > average. > > >> I don't think the example is valid (even if machines with 100.000 >> cpus and more actually exist). But I remember from days of my >> study, which lies way way back, that languages that are optimized >> for parallel processing would be able to tell the compiler to >> execute all ifs in parallel. So if we stick with a number of ifs >> which is less than the number of available cores/pipes we could >> run it all in parallel. I don't think it is possible with if >> else, unless it is transformed into something else. While there may be some systems that do this kind of thing, Java isn't one of them. Java is single-threaded unless you explicitly dispatch work to other threads. If you use else-less-if, then there is never an opportunity for parellelization, since the program is going to assume that those predicates are (a) independent and (b) ordered in a way that the programmer intended. If you use if-with-else then those statements are also in intentional order and -- at someone else said elsewhere in this thread -- the predicates may have side-effects that could be dangerous to execute out-of-order (or just "early"). >> The other thing that made me wonder is that most people on the >> list (or all except me) actually considered if-else-if-else more >> readable. It not only creates a more complex structure (visually >> and syntactically (more letters)). But also the semantics of an >> *else* are different as of an *if*. This is like North Carolina >> ;-) if (man){ do_man_thing; } else { do_woman_thing(); } doesn't >> work anymore, even it worked 20 years ago. Talking about >> maintaining :-) You *did* mention that the cases were all mutually exclusive. This is precisely what if/elseif was designed for. - -chris -----BEGIN PGP SIGNATURE----- Comment: GPGTools - http://gpgtools.org Comment: Using GnuPG with Thunderbird - http://www.enigmail.net/ iEYEARECAAYFAlced1YACgkQ9CaO5/Lv0PAFXQCfYPdxnWhuAIozpjp6m0/X79B2 fTkAniMHNCkr9nRzXm/A1jNCQVDXB6Ke =scIU -----END PGP SIGNATURE----- --------------------------------------------------------------------- To unsubscribe, e-mail: users-unsubscr...@tomcat.apache.org For additional commands, e-mail: users-h...@tomcat.apache.org