Softpanorama

May the source be with you, but remember the KISS principle ;-)
Home Switchboard Unix Administration Red Hat TCP/IP Networks Neoliberalism Toxic Managers
(slightly skeptical) Educational society promoting "Back to basics" movement against IT overcomplexity and  bastardization of classic Unix

Dr. Nikolai Bezroukov's "Devil's Advocate Commentary" on Thinking in Java by Bruce Eckel. Chapter 3
version 0.5 (April 5, 1998)


Disclaimer: I would like to stress that the role of devil's advocate is to be critical. At the same time I believe than Java has a bright future and that TIJ can be listed among ten best books on Java. That's why I am writing this Commentary. This Commentary is a work in progress. Currently it is incomplete, buggy, and may contains bad links. All views expressed in the Commentary are my own.

Chapter 3 "Controlling Program Flow"

In Chapter 3 the author included both  the explanation of  Java operators with the explanation of control flow constructs. It's OK to do so, but I believe that chapter would better be separated into two (the second starting with "Execution control" on p.130).

Java operators are explained well with a lot of examples. At the same time examples does not represent typical patterns of the usage of control structures in Java. There are much more like laboratory experiments that help to understand the syntax and semantics of the control statements. And they need to be refined -- the example of the program on pages 120-129 contains a lot of useful experiments, but it's too long and horribly typeset. In future editions the fond probably should be smaller and it could be typeset in two columns. Or may be its content can be reworked into a table and the program provided only in electronic form.

Now let's talk about treatment of the control structures. I believe it should be expanded as here Java make a "revolutionary" decision to get rid of goto statement. And as one knows from history revolutions usually lead to sufferings,  at least at the beginning.

Generally in order to check the quality of such a chapter in any Java book one can use the following test:

A use of this test on Chapter 3 of TIJ is a good exercise that is left to participants of the seminar.

The problem with Java is that IMHO Java designers did not understand issues of safety of control structures well. IMHO they made three important mistakes:

The only extension that I see from rather Spartan set of C control structures is a multilevel break/continue with the label. It's useful bit its insufficient to compensate for the absence of goto.

As a result Java is neither well suited to writing complex algorithms like algorithms from the Art of Computer Programming by D.Knuth nor it represent the state of the art in safe control structure. There is no such thing as a free lunch and if one drops goto from the programming language one needs to add a lot of additional specialized "structured" statements to compensate for that. Or the language will remain crippled. Bruce Eckel recognize that the decision to drop rather than restrict goto was probably a mistake (p.137). But he does not provide any recommendations for compensating Java weaknesses.

Here I would like to stress  again that one should consider Java as a better VB, not a better C++.  But it's really unfortunate that Java did not benefit from additional control structures found in other modern languages, for example Perl and Icon.

The author correctly includes a discussion of the return statement into the control structure section as in Java the return statement can sometimes compensate for the absence of a goto statement.

Studying of control structures in programming languages is often provided under the umbrella of so called structured programming. The idea of "real" structured programming, or "structured programming with goto statements" is that one could write, understand and modify the program much more easily by organizing and coding computer programs in which all control structures have a single entry and a single exit point. It is this idea that constitute rational, useful part of the whole structured programming movement.

But there is a lot of religious junk around this sound idea (as around many other sound ideas). Some authors go too  far and declare that just three types of control flow are allowed: sequential, test, and iteration (kosher control constructs ;-).

Those who are interested in the study of a history of programming probably know that structured programming created probably the first strong religious movement in programming as scientific ground of usefulness of just three kosher control structures are very weak, if exist at all. Here I would like to cite Edsger W. Dijkstra -- a high priest of structured programming and verification (unlike many of his "structured" followers Dijkstra is a real computer scientist; before initiating structured programming movement, he invented several useful algorithms including semaphore synchronization; currently he is a professor at the University of Texas at Austin):

Summarizing: as a slow-witted human being I have a very small head and I had better learn to live with it and to respect my limitations and give them full credit, rather than try to ignore them, for the latter vain effort will be punished by failure.

Edsger W. Dijkstra, Structured Programming.

 

Like a lot of slogans, it looks appealing, but it was often used for the advocacy of "structured primitivism" writing of any program using only the "kosher"  control structures mentioned above. This "orthodox" structured programming movement now looks a little bit strange (much like an idea to verify each and every program instead of testing), but in the early 70es a lot people jumped on this bandwagon and tried to do what the apostles of the movement preached.  The remnant of this days is an entry in the famous Jargon File:

considered harmful

adj. Edsger W. Dijkstra's note in the March 1968 "Communications of the ACM", "GOTO Statement Considered Harmful", fired the first salvo in the structured programming wars. Amusingly, the ACM considered the resulting acrimony sufficiently harmful that it will (by policy) no longer print an article taking so assertive a position against a coding practice. In the ensuing decades, a large number of both serious papers and parodies have borne titles of the form "X considered Y". The structured-programming wars eventually blew over with the realization that both sides were wrong, but use of such titles has remained as a persistent minor in-joke (the `considered silly' found at various places in this lexicon is related).

 

Java control structures do not provide the possibility of programming each and every one-entry one-exit control structure without goto, so generally some additional computation needs to be specified. But again clever use of return and exceptions sometimes can compensate for this drawback.

Java has the following two statements to improve the control of execution of loops:

Each of these statements can have suffix that should correspond to a loop label. In this case it applies to the loop labeled with the same label. So multilevel breaks are possible. 

Note on the structure of the for loop

For people who did not program in C the easiest way to remember the correct sequence of components in the for loop is by remembering the magic word "STAB" that corresponds to the following general scheme of the for statement"

       for (start; test; action) { body }

One can see that for loop consist of four parts. The for loop works well where the number of iterations of the loop is known before the loop is entered. The head of the loop consists of three parts separated by semicolons.  I recommend always to use the braces for the body of the for loop, even if the body consist of a single statement.

The continue statement

Java continue statement is used to start next iteration immediately without executing the rest of the of body of the loop. Use of continue in Java helps to avoid excessive nesting in the loop. For example:

 

for (;:)

    if (condition1) {

          block1;

           if (condition2) {

                block2;

            }

     }

}

for (;;) {

if (!(condition1)) {continue;}

block1;

if (!(condition2) {continue;}

block2;

}

One can see that the loop at the left is more difficult to understand that the loop at the right.

 

What Java is definitely missing -- Perl foreach loop

The foreach loop is similar to the "for..in" loop that can be found in many scripting languages including MS DOS batch language.  It has a built in iterator which iterates through each of the elements of an array or list. Here we do need to specify the order in which elements will be processed. Foreach can be generalized so that elements can be proceed in any order (those who programmed in assembler know that loops in reverse are slightly more efficient)  foreach is very convenient when modifying or processing several elements of an array or list that meat certain conditions.  For example

Cat X = new Cat[];

... ... ...

foreach (Cat c  in X) {

   if (c==my_cat) {found=1; break;}

}

if (found==1) {

}

Note that Java even with the foreach statement does not permit to program simple search loop without additional computation (flag found is used to determine whether we have found the element of an array or no).

In Java 1.2 one need to specify iterator to achieve the same effect, but it applies only to collections. For example

Collection c = new ArrayList();

... ... ...

Iterator it = c.iterator();

while (it.hasNext()) {

  System.out.print(it.next());

}

For Java 1.1 the ObjectSpace company created Generic collection Library for Java   similar to STL in C++. It's available from www.ObjectSpace.com.


Discussion of exercises and additional exercises

Exercises provided are somewhat weak taking into account that book is oriented on the programmers who know at least one programming language. IMHO it would be beneficial to rework them in the second edition of the book.


Some additional exercises

Exercise 1

Write a program that allocate an array of, say, N integer elements and then add elements(to the end of the array) from other preallocated and initialized array containing N*N elements with some of them repeating several times. When overflow occurred in a loop that search for an element of an array the program should print the message

Overflow occurred when trying to add the element <element>. Remaining elements are:

and print all remaining elements in N columns after that message so that each remaining element that is already in the first array should be printed in brackets. Recommenced test values for the second array for N=3 are

5 5 3 4 4 0 4 2 3

The error should occurs when the program tries to add element "0" of the second array. After the message the program should print:

0     (4)    2

(3)


Exercise 2

Program binary search algorithm and consider what control structure should be added to Java to program this algorithm most efficiently. 


Exercise 3

Explain how continue statement with label to external loop works in Java. What will be the value of i after continue in the following example. Will the external loop ever terminate ?

n=5;

i=5;

loop1:
   for(;;) {

      for (i=0;i<n;i++)
        continue loop1;
      }

   break;
   }


Useful links

Some examples of "considered harmful" movement ;-)

Other links

Bibliography

C. B�hm and G. Jacopini, "Flow Diagrams, Turing Machines, and Languages with Only Two Formation Rules", Communications of the ACM 9
(1966), pp. 366-371.

E. W. Dijkstra, "Goto Statement Considered Harmful", Communications of the ACM 11 (1968), pp. 147-148.

E. W. Dijkstra, "Guarded Commands, Nondeterminacy and Formal Derivation of Programs", Communications of the ACM 18, (1975), pp. 453-457.

E. W. Dijkstra, "Notes on Structured Programming", in Dahl, Dijkstra and Hoare, Structured Programming, Academic Press, 1972, pp. 1-82.

D. E. Knuth, "Structured Programming with Goto Statements", ACM Computing Surveys 6 (1974), pp. 261-302.


Etc

Society

Groupthink : Two Party System as Polyarchy : Corruption of Regulators : Bureaucracies : Understanding Micromanagers and Control Freaks : Toxic Managers :   Harvard Mafia : Diplomatic Communication : Surviving a Bad Performance Review : Insufficient Retirement Funds as Immanent Problem of Neoliberal Regime : PseudoScience : Who Rules America : Neoliberalism  : The Iron Law of Oligarchy : Libertarian Philosophy

Quotes

War and Peace : Skeptical Finance : John Kenneth Galbraith :Talleyrand : Oscar Wilde : Otto Von Bismarck : Keynes : George Carlin : Skeptics : Propaganda  : SE quotes : Language Design and Programming Quotes : Random IT-related quotesSomerset Maugham : Marcus Aurelius : Kurt Vonnegut : Eric Hoffer : Winston Churchill : Napoleon Bonaparte : Ambrose BierceBernard Shaw : Mark Twain Quotes

Bulletin:

Vol 25, No.12 (December, 2013) Rational Fools vs. Efficient Crooks The efficient markets hypothesis : Political Skeptic Bulletin, 2013 : Unemployment Bulletin, 2010 :  Vol 23, No.10 (October, 2011) An observation about corporate security departments : Slightly Skeptical Euromaydan Chronicles, June 2014 : Greenspan legacy bulletin, 2008 : Vol 25, No.10 (October, 2013) Cryptolocker Trojan (Win32/Crilock.A) : Vol 25, No.08 (August, 2013) Cloud providers as intelligence collection hubs : Financial Humor Bulletin, 2010 : Inequality Bulletin, 2009 : Financial Humor Bulletin, 2008 : Copyleft Problems Bulletin, 2004 : Financial Humor Bulletin, 2011 : Energy Bulletin, 2010 : Malware Protection Bulletin, 2010 : Vol 26, No.1 (January, 2013) Object-Oriented Cult : Political Skeptic Bulletin, 2011 : Vol 23, No.11 (November, 2011) Softpanorama classification of sysadmin horror stories : Vol 25, No.05 (May, 2013) Corporate bullshit as a communication method  : Vol 25, No.06 (June, 2013) A Note on the Relationship of Brooks Law and Conway Law

History:

Fifty glorious years (1950-2000): the triumph of the US computer engineering : Donald Knuth : TAoCP and its Influence of Computer Science : Richard Stallman : Linus Torvalds  : Larry Wall  : John K. Ousterhout : CTSS : Multix OS Unix History : Unix shell history : VI editor : History of pipes concept : Solaris : MS DOSProgramming Languages History : PL/1 : Simula 67 : C : History of GCC developmentScripting Languages : Perl history   : OS History : Mail : DNS : SSH : CPU Instruction Sets : SPARC systems 1987-2006 : Norton Commander : Norton Utilities : Norton Ghost : Frontpage history : Malware Defense History : GNU Screen : OSS early history

Classic books:

The Peter Principle : Parkinson Law : 1984 : The Mythical Man-MonthHow to Solve It by George Polya : The Art of Computer Programming : The Elements of Programming Style : The Unix Hater�s Handbook : The Jargon file : The True Believer : Programming Pearls : The Good Soldier Svejk : The Power Elite

Most popular humor pages:

Manifest of the Softpanorama IT Slacker Society : Ten Commandments of the IT Slackers Society : Computer Humor Collection : BSD Logo Story : The Cuckoo's Egg : IT Slang : C++ Humor : ARE YOU A BBS ADDICT? : The Perl Purity Test : Object oriented programmers of all nations : Financial Humor : Financial Humor Bulletin, 2008 : Financial Humor Bulletin, 2010 : The Most Comprehensive Collection of Editor-related Humor : Programming Language Humor : Goldman Sachs related humor : Greenspan humor : C Humor : Scripting Humor : Real Programmers Humor : Web Humor : GPL-related Humor : OFM Humor : Politically Incorrect Humor : IDS Humor : "Linux Sucks" Humor : Russian Musical Humor : Best Russian Programmer Humor : Microsoft plans to buy Catholic Church : Richard Stallman Related Humor : Admin Humor : Perl-related Humor : Linus Torvalds Related humor : PseudoScience Related Humor : Networking Humor : Shell Humor : Financial Humor Bulletin, 2011 : Financial Humor Bulletin, 2012 : Financial Humor Bulletin, 2013 : Java Humor : Software Engineering Humor : Sun Solaris Related Humor : Education Humor : IBM Humor : Assembler-related Humor : VIM Humor : Computer Viruses Humor : Bright tomorrow is rescheduled to a day after tomorrow : Classic Computer Humor

The Last but not Least Technology is dominated by two types of people: those who understand what they do not manage and those who manage what they do not understand ~Archibald Putt. Ph.D


Copyright � 1996-2021 by Softpanorama Society. www.softpanorama.org was initially created as a service to the (now defunct) UN Sustainable Development Networking Programme (SDNP) without any remuneration. This document is an industrial compilation designed and created exclusively for educational use and is distributed under the Softpanorama Content License. Original materials copyright belong to respective owners. Quotes are made for educational purposes only in compliance with the fair use doctrine.

FAIR USE NOTICE This site contains copyrighted material the use of which has not always been specifically authorized by the copyright owner. We are making such material available to advance understanding of computer science, IT technology, economic, scientific, and social issues. We believe this constitutes a 'fair use' of any such copyrighted material as provided by section 107 of the US Copyright Law according to which such material can be distributed without profit exclusively for research and educational purposes.

This is a Spartan WHYFF (We Help You For Free) site written by people for whom English is not a native language. Grammar and spelling errors should be expected. The site contain some broken links as it develops like a living tree...

You can use PayPal to to buy a cup of coffee for authors of this site

Disclaimer:

The statements, views and opinions presented on this web page are those of the author (or referenced source) and are not endorsed by, nor do they necessarily reflect, the opinions of the Softpanorama society. We do not warrant the correctness of the information provided or its fitness for any purpose. The site uses AdSense so you need to be aware of Google privacy policy. You you do not want to be tracked by Google please disable Javascript for this site. This site is perfectly usable without Javascript.

Last modified: March 12, 2019