Difference between revisions of "1188: Bonding"

Explain xkcd: It's 'cause you're dumb.
Jump to: navigation, search
m (Explanation: I expanded the title text definition.)
(Downgraded the section "Program description" to a subsection. The program description is part of the explanation.)
(14 intermediate revisions by 9 users not shown)
Line 8: Line 8:
  
 
==Explanation==
 
==Explanation==
This is {{w|source code}} written in the {{w|Java (programming language)|Java programming language}}, which models a parent and a child playing a {{w|Catch (game)|game of catch}}, in which the parent throws a ball to their child, who catches it and throws it back, which repeats. (The comic title “Bonding” refers to the {{w|Paternal bond|building of relationship}} between the parent and the child.) But the computer program representation of this game is funny, using a play on the keywords <code>throw</code> and <code>catch</code> (and <code>Throwable</code>), which, in Java, do not correspond to throwing and catching a ball, but to a so-called {{w|exception handling}}, a method of signaling error conditions and responding to them.
+
This is {{w|source code}} written in the {{w|Java (programming language)|Java programming language}}, which models a parent and a child playing a {{w|Catch (game)|game of catch}}, in which the parent throws a ball to their child, who catches it and throws it back, which repeats. The comic title "Bonding" refers to the {{w|Paternal bond|building of relationship}} between the parent and the child. But the computer program representation of this game is funny, using a play on the keywords <code>throw</code> and <code>catch</code> (and <code>Throwable</code>), which, in Java, do not correspond to throwing and catching a ball, but to a so-called {{w|exception handling}}, a method of signaling error conditions and responding to them.
  
The program, as written, will {{w|Recursion (computer science)|recursively}} call the <code>Aim</code> method alternately on the parent and the child indefinitely, until the program crashes with a {{w|stack overflow}}.
+
The program, as written, will {{w|Recursion (computer science)|recursively}} call the <code>aim</code> method alternately on the parent and the child indefinitely, until the program crashes with a {{w|stack overflow}}.
  
Also, the terms “parent” and “child” are usually interpreted more abstractly in programming, as a generic terms used in hierarchical {{w|Data structure|data structures}}.
+
Also, the terms "parent" and "child" are usually interpreted more abstractly in programming, as a generic terms used in hierarchical {{w|Data structure|data structures}}.
  
The title text refers to [http://www.eclipse.org/ Eclipse], which is a tool commonly used to develop software in Java. ("Building character" is something that you would expect a father to do to instill in his some some "masculine" abilities, "playing ball" apparently being one of them. However, here, “build” might be a play on the term of {{w|Software build|building}}a program, while "character" refers to a data type.)
+
The title text refers to [http://www.eclipse.org/ Eclipse], which is a tool commonly used to develop software in Java. "Building character" is something that you would expect a parent to do, in order to instill in his child positive traits, such as athleticism. However, here, "build" might be a play on the term of "{{w|Software build|building}}" a program, while "character" refers to a data type. It may also refer to the common notion that programming in C++ or Java builds character due to their powerful but sometimes finicky libraries.
 +
 
 +
===Program description===
 +
To compile this {{w|Java_(programming_language)|Java}} source code, the two [http://docs.oracle.com/javase/tutorial/java/javaOO/classdecl.html classes] would need to be in a .java file.
 +
#The Ball class [http://docs.oracle.com/javase/tutorial/java/IandI/subclasses.html extends] [http://docs.oracle.com/javase/7/docs/api/java/lang/Throwable.html Throwable], making it possible to use an instance of Ball in [http://docs.oracle.com/javase/tutorial/essential/exceptions/handling.html exception handling].
 +
#The P (short for Person) class contains the following members:
 +
:*a [http://docs.oracle.com/javase/tutorial/java/javaOO/variables.html class variable] 'target' to hold another instance of P to aim an instance of Ball at.
 +
:*a [http://docs.oracle.com/javase/tutorial/java/javaOO/constructors.html constructor] 'P' (in Java the constructor always has the same name as the class) used to create an instance of P and initialize its state (with a target). The keyword [http://docs.oracle.com/javase/tutorial/java/javaOO/thiskey.html this] refers to the current instance of P.
 +
:*a [http://docs.oracle.com/javase/tutorial/java/javaOO/methods.html method] 'aim' that takes an instance of Ball named 'ball' as a parameter.
 +
:*a [http://docs.oracle.com/javase/tutorial/java/javaOO/classvars.html static] method 'main' which is called when executing this class.
 +
 
 +
The program executes in the following order:
 +
#The static main method is called (with the command line arguments in the 'args' parameter).
 +
#An instance of P is created without a target ({{w|Nullable_type|null}}) using the 'new' keyword and assigned to a variable named 'parent'.
 +
#Another instance of P is created with 'parent' as target and assigned to a variable named 'child'.
 +
#The target variable of the parent is changed to the child instance.
 +
#An instance of Ball is created and passed as a parameter to the aim method of parent.
 +
#The aim method first sets up a [http://docs.oracle.com/javase/tutorial/essential/exceptions/try.html try] block to handle exceptions.
 +
#Next, the Ball instance 'ball' is [http://docs.oracle.com/javase/tutorial/essential/exceptions/throwing.html thrown] (= signal an exception situation).
 +
#The thrown exception (= the Ball instance) is handled in the [http://docs.oracle.com/javase/tutorial/essential/exceptions/catch.html catch] block with the matching exception class.
 +
#In the catch block the aim method of the target of the P instance is called with the Ball instance (now referred to as 'b').
 +
#The code continues with step 6, but the current class instance ('this') has changed from parent to child (or vv.).
 +
 
 +
Notes about code quality:
 +
#Good Java code should contain documentation in the form of {{w|Javadoc}} comments.
 +
#It is recommended to organize Java source code into [http://docs.oracle.com/javase/tutorial/java/package/packages.html packages] to make code easier to find and use and to avoid name conflicts.
 +
#The class interface should not expose inner logic and data. For this reason class variables are usually declared as [http://docs.oracle.com/javase/tutorial/java/javaOO/accesscontrol.html private] (or protected) and separate public methods (called getters and setters) are created to access the data. Making target private would also require using the setTarget setter method in main to set the parent target to child.
 +
#In most cases constructors should call their superclass constructor first. P descents from Object, which has an empty constructor, so this is not strictly required here.
 +
#The aim method references the class variable target directly. To avoid future name conflicts it is recommended to prefix the reference to target with 'this' or use the getter from #2.
  
 
==Transcript==
 
==Transcript==

Revision as of 12:55, 27 October 2014

Bonding
I'm trying to build character, but Eclipse is really confusing.
Title text: I'm trying to build character, but Eclipse is really confusing.

Explanation

This is source code written in the Java programming language, which models a parent and a child playing a game of catch, in which the parent throws a ball to their child, who catches it and throws it back, which repeats. The comic title "Bonding" refers to the building of relationship between the parent and the child. But the computer program representation of this game is funny, using a play on the keywords throw and catch (and Throwable), which, in Java, do not correspond to throwing and catching a ball, but to a so-called exception handling, a method of signaling error conditions and responding to them.

The program, as written, will recursively call the aim method alternately on the parent and the child indefinitely, until the program crashes with a stack overflow.

Also, the terms "parent" and "child" are usually interpreted more abstractly in programming, as a generic terms used in hierarchical data structures.

The title text refers to Eclipse, which is a tool commonly used to develop software in Java. "Building character" is something that you would expect a parent to do, in order to instill in his child positive traits, such as athleticism. However, here, "build" might be a play on the term of "building" a program, while "character" refers to a data type. It may also refer to the common notion that programming in C++ or Java builds character due to their powerful but sometimes finicky libraries.

Program description

To compile this Java source code, the two classes would need to be in a .java file.

  1. The Ball class extends Throwable, making it possible to use an instance of Ball in exception handling.
  2. The P (short for Person) class contains the following members:
  • a class variable 'target' to hold another instance of P to aim an instance of Ball at.
  • a constructor 'P' (in Java the constructor always has the same name as the class) used to create an instance of P and initialize its state (with a target). The keyword this refers to the current instance of P.
  • a method 'aim' that takes an instance of Ball named 'ball' as a parameter.
  • a static method 'main' which is called when executing this class.

The program executes in the following order:

  1. The static main method is called (with the command line arguments in the 'args' parameter).
  2. An instance of P is created without a target (null) using the 'new' keyword and assigned to a variable named 'parent'.
  3. Another instance of P is created with 'parent' as target and assigned to a variable named 'child'.
  4. The target variable of the parent is changed to the child instance.
  5. An instance of Ball is created and passed as a parameter to the aim method of parent.
  6. The aim method first sets up a try block to handle exceptions.
  7. Next, the Ball instance 'ball' is thrown (= signal an exception situation).
  8. The thrown exception (= the Ball instance) is handled in the catch block with the matching exception class.
  9. In the catch block the aim method of the target of the P instance is called with the Ball instance (now referred to as 'b').
  10. The code continues with step 6, but the current class instance ('this') has changed from parent to child (or vv.).

Notes about code quality:

  1. Good Java code should contain documentation in the form of Javadoc comments.
  2. It is recommended to organize Java source code into packages to make code easier to find and use and to avoid name conflicts.
  3. The class interface should not expose inner logic and data. For this reason class variables are usually declared as private (or protected) and separate public methods (called getters and setters) are created to access the data. Making target private would also require using the setTarget setter method in main to set the parent target to child.
  4. In most cases constructors should call their superclass constructor first. P descents from Object, which has an empty constructor, so this is not strictly required here.
  5. The aim method references the class variable target directly. To avoid future name conflicts it is recommended to prefix the reference to target with 'this' or use the getter from #2.

Transcript

class Ball extends Throwable {}
class P{
    P target;
    P(P target) {
        this.target = target;
    }
    void aim (Ball ball) {
        try {
            throw ball;
        }
        catch (Ball b) {
            target.aim(b);
        }
    }
    public static void main(String[] args) {
        P parent = new P(null);
        P child = new P(parent);
        parent.target = child;
        parent.aim(new Ball());
    }
}


comment.png add a comment! ⋅ comment.png add a topic (use sparingly)! ⋅ Icons-mini-action refresh blue.gif refresh comments!

Discussion

The aim method results in an infinite loop/stack overflow, note that ball is an exception of type Ball. This results in a logical flow of aim, "throw," "catch," repeat, though this is only logical by word choice, and is nonsensical from a programming perspective. -- ‎108.48.215.61 (talk) (please sign your comments with ~~~~)

Just as the game of catch is nonsensical from a logistics perspective! Mumiemonstret (talk) 14:20, 20 March 2015 (UTC)

Pretty sure the code is also intentionally hard to follow. -- ‎108.48.215.61 (talk) (please sign your comments with ~~~~)


The try/catch parts are just for show, they cancel each other out. The structure is that you have a parent and a child instance (of class P), each has a 'target' pointed to the other. Then calling aim with a ball will call the others aim with the ball, which will call the firsts aim with the ball. Etc etc.

I guess after about a 1000 aims the jvm will throw you out, stating stack overflow, and the bonding game is over. -- 212.214.117.162 (talk) (please sign your comments with ~~~~)


Nice catch game :) I had to test it:

Exception in thread "main" java.lang.StackOverflowError

in my setup with default VM settings after 6612 iterations (I added a static counter variable). The game could get even more "exciting" by using more than two Ps and adding randomization in who is aimed at. And maybe a miss block ;) (need to hack the compiler and VM for that though...) 134.106.146.36 09:31, 20 March 2013 (UTC)

I don't think any parent will last so long. On the other hand, if you always catch the ball, one iteration doesn't take so long, it's the missing which makes the game long ... -- Hkmaly (talk) 09:56, 20 March 2013 (UTC)
So true. I start to feel tempted to write a real catch game in Java. Btw: Of course not in Eclipse (please...). I use an IDE where everything works and I don't have to wait all the time. ;) 134.106.146.36 08:15, 21 March 2013 (UTC)


Hmmm "Eclipse: The Codex Persona" is also a d20 gaming system which offers enormous customization of characters. The mention of building character and Eclipse in the same sentence just brought that to the front of my mind. No idea if that has relation to the comic. --50.0.36.182 07:38, 20 March 2013 (UTC)


Did anyone else have a D'AWWWWWW moment when you realized what was happening? I knew it was a pun on throw and catch, but it took till the end for me to realize it was a parent and a child playing catch.74.14.31.164 12:53, 20 March 2013 (UTC)


The one problem with this is that the way the try/catch is set up, they aren't actually throwing to each other. Parent throws the ball, then catches it themselves, then child does the same thing. It's still clever though. Prometheusmmiv (talk) 11:38, 20 March 2013 (UTC)

I thought exactly the same. Here's a modified version where they actually throw to each other:
   class Ball extends Throwable{}
   
   class P{
       P target;
       Ball ballInTow;
       
       P(P target, Ball ball) {
           this.target = target;
           this.ballInTow = ball;
       }    
       void tease() {
           try {
               target.youDontDare();
           }
           catch(Ball b){
               ballInTow = b;
               target.tease();
           }        
       }    
       void youDontDare() throws Ball {
           throw ballInTow;
       }    
       public static void main(String [] args) {
           P parent = new P(null, new Ball());
           P child = new P(parent, null);
           parent.target = child;
           child.tease();
       }
   }

88.9.44.85 15:41, 20 March 2013 (UTC)

I was thinking about how do this correctly, with the throw bubbling up to the other person. And I realized that in order for the recursion to work, there would have to be a double method where the catcher asks (or "teases") the thrower to throw, then catches it in that method. I was going to write up a version like this, but I had to leave for work. But I'm glad that somebody else was thinking like me and was able to write up a correct version :) Prometheusmmiv (talk) 22:06, 20 March 2013 (UTC)

The code is an odd way of making a loop in Java -- creating two objects (of class P, called "parent" and "child") which repeatedly throw and catch another object (of class Ball) between one another. The sole purpose of this is to create the pun referred to in the title: it's a real-life cliché that a parent and child may "bond" by playing catch. 81.31.112.212 07:14, 20 March 2013 (UTC)


Title text talks about "to build character" in the way usually a father tries to help a child to define "attributes or features that make up and distinguish an individual"[1], so I suppose that the "confusing Eclipse" is a pun itself. Perhaps it is a reference to Eclipse novel by Stephenie Meyer (the kind of book that raises a lot of moral dilemma in a young adult). --Andcoz (talk) 12:49, 20 March 2013 (UTC)


I think there is another pun in it: The class "Ball" is a child-class of "Throwable" which makes sense because you can throw a ball. But "Throwable" is also the main exception-class from which the real exception classes like "Exception" or "Error" inherit. --DaB. (talk) 12:50, 20 March 2013 (UTC)


I think it's sad that this family always dies in a most unfortunate crash. Here's an alternate ending in which they try just a little bit harder, so they return home for dinner when they're out because they didn't catch the ball:

class Ball extends Throwable {}
class P{
    P target;
    P(P target) {
        this.target = target;
    }
    void aim (Ball ball) {
        try {
            throw ball;
        }
        catch (Ball B) {
            try {
                target.aim(B);
            }
            catch(Error made) {
                return;
            }
        }
    }
    public static void main(String[] args) {
        P parent = new P(null);
        P child = new P(parent);
        parent.target = child;
        parent.aim(new Ball());
        System.out.println("Dinner's ready!");
    }
}

Jfresen (talk) 14:10, 20 March 2013 (UTC)

Personally, I think Randal missed a trick with the alt text - "My parent tried playing catch with me and all I got was this lousy stack overflow." That said, Eclipse is driving me nuts at the moment, so I can sympathise! --81.187.166.32 22:59, 20 March 2013 (UTC)

I feel for you. But there is other IDEs out there, where things work, usage and shortcuts are not awkward (depending on what you are used too, of course) and you don't have to wait all the time, if you don't have a Core i7 and SSD... There is hope ;) (and I sometimes have to use Eclipse, too, for the GWT plugin) 134.106.146.36 08:18, 21 March 2013 (UTC)

Maybe "build character" refers to the filename. Since class P was not declared public, the filename could be character.java and things would work.  :-) --Divad27182 (talk) 11:49, 21 March 2013 (UTC)

I loved ingenious solution in original code to the "ball to the balls" side effect that is so often seen when teaching catch - 166.147.120.176 12:07, 21 March 2013 (UTC)

Just to clarify, programmer can't assume that child will inherit "catch" function as intended. That is something they really have to learn for themselves. I wonder if this is how terminator got started? - 166.147.120.148 17:32, 21 March 2013 (UTC)

The code quality remark #4 doesn't seem correct. In Java the constructor will always call the superclass constructor first. If not stated explicitly, it will happen implicitly. So the default empty constructor of Object will actually be called. Is this a misunderstanding, or is the guideline meant to be that the superclass constructor call should always be coded explicitly ? (which is a highly debatable guideline) --141.101.105.234 16:19, 8 January 2015 (UTC)

There's a lot debatable in those guidelines.
  1. Good API code needs good javadoc documentation, but this isn't exporting an API, it's a simple self-contained tool, and the code is written to be "readable" which tends to work better than even code documentation (which is always wrong, given enough time).
  2. Once again, this code describes a self-contained tool, and is nowhere near complex enough to require splitting into packages.
  3. While it contains good advice (don't expose inner logic and data), it also contains bad advice (getters and setters to access the data - you're back to exposing inner logic and data), and BETTER advice is to prefer immutable data where possible. All of this is moot, because this example doesn't expose anything - the data is package-private, and the main method can access even private members since it's in the same class as the member, and the data member can't be immutable because the parent object needs to be updated after construction to point to the child object.
  4. As mentioned above, this isn't even advice. In Java you MUST call a super class constructor as the first line in any constructor; omitting any such call implies a call to the super class constructor with no arguments, and is an error if no such constructor exists. Suggesting that you SHOULD call a super class constructor is just misleading - other than being required by the language, why else should you call a super constructor first (as opposed to later?)?
  5. Don't borrow trouble from the future. A method internal to a class has every right to refer to a member of the same class without qualification, and is usually more readable for it. Using getter methods from within the class simply to wrap access to internal fields is a terrible idea for a couple of reasons, although wrapping access with null-checks or common coersions can simplify the logical flow of your code... but even that is probably better done with utility methods (taking the field as an argument) than member methods (returning some variation on the field).
New Java developers who happen to come here and see the notes on quality, just note that they aren't notes that point you to GOOD quality.
108.162.250.161 02:15, 17 July 2015 (UTC)
I've removed the style guidelines, since they don't really make the explanation better. APerson (talk!) 00:11, 21 October 2017 (UTC)

The first time I used Eclipse, I couldn't find the run button for my Java applet XD 625571b7-aa66-4f98-ac5c-92464cfb4ed8 (talk) 05:54, 8 March 2017 (UTC)