QUESTIONS LAST TIME: - SiteRiter questions, tough grammars for top-down parsing, enum subtleties TODAY: - Internships - SiteRiter questions - Design thoughts for top-down parsing - Code design: Where do you put the state? - Memory and pointer diagrams - The uber data structure PROJECT 1 STATUS DAY 15: - First project FINAL DELIVERABLE in progress! - Interim deliverable #2 Now one day late - Interim deliverable #1 ALL DONE DEFINITION OF THE DAY CODE SMELL, n. In computer programming, any superficially obvious symptom in the source code of a program that may indicate a deeper problem. PROJECT 1 QUESTIONS PARSING THOUGHTS - Recognizing vs parsing (vs 'transducing', aka 'filtering'..) - Importance of recursion: Keeping track of where you are in rule nesting = TIP: When doing recursive designs: AVOID using data members to pass state between methods! Instead, add EXTRA METHOD ARGUMENTS to pass state between methods. - Why? Because the extra method arguments are unique to each method call, so they are specific to the step of recursion. The data members of an object, in contrast, are the same for all levels of recursion. - Don't try to force line-at-a-time processing onto a non-line-oriented grammar! = TIP: If you have ANY calls on BufferedReader.readLine() in your SDLParserImpl, you're making a poor design choice -- at best! - KEEP IT CLEAN! WHERE DO YOU PUT THE STATE? - 'State': Information to be remembered for later in the computation. WHERE DO YOU PUT THE STATE? - 'State': Information to be remembered for later in the computation. = Always two basic questions about state: 1. LIFETIME: How long does it need to be remembered? 2. DIRECT ACCESS: Who most needs to get at it during its life? WHERE DO YOU PUT THE STATE? int foo(final int bar) { ... } - 'State': Information to be remembered for later in the computation. = Always two basic questions about state: 1. LIFETIME: How long does it need to be remembered? 2. DIRECT ACCESS: Who most needs to get at it during its life? - Choices: 1. ARGUMENT TO OR LOCAL VARIABLE IN A METHOD Lifetime: Lives until the method finishes (by return or exception) Direct Access: Only within the method in which it's declared 2. PRIVATE NON-STATIC FIELD IN AN OBJECT Lifetime: Lives as long as the object lives Direct Access: Within all non-static methods of the object's class 3. PRIVATE STATIC FIELD IN A CLASS Lifetime: Lives 'forever' (as long as the class is accessible) Direct Access: Within all methods of the class, static and non-static 4. IN A DATA STRUCTURE (array, list, map, etc); which in turn is located in 1, 2, or 3. (Or 4, recursively, but ultimately 1-3). WHERE DO YOU PUT THE STATE? - 'State': Information to be remembered for later in the computation. = Always two basic questions about state: 1. LIFETIME: How long does it need to be remembered? 2. DIRECT ACCESS: Who most needs to get at it during its life? - Choices: 1. ARGUMENT TO OR LOCAL VARIABLE IN A METHOD Lifetime: Lives until the method finishes (by return or exception) Direct Access: Only within the method in which it's declared 2. PRIVATE NON-STATIC FIELD IN AN OBJECT Lifetime: Lives as long as the object lives Direct Access: Within all non-static methods of the object's class 3. PRIVATE STATIC FIELD IN A CLASS Lifetime: Lives 'forever' (as long as the class is accessible) Direct Access: Within all methods of the class, static and non-static 4. IN A DATA STRUCTURE (array, list, map, etc); which in turn is located in 1, 2, or 3. (Or 4, recursively, but ultimately 1-3). - Rule of thumb: Use the shortest-lived, least-accessible place sufficient for the needs of the particular state. WHERE DO YOU PUT THE STATE? - 'State': Information to be remembered for later in the computation. = Always two basic questions about state: 1. LIFETIME: How long does it need to be remembered? 2. DIRECT ACCESS: Who most needs to get at it during its life? - Choices: 1. ARGUMENT TO OR LOCAL VARIABLE IN A METHOD Lifetime: Lives until the method finishes (by return or exception) Direct Access: Only within the method in which it's declared 2. PRIVATE NON-STATIC FIELD IN AN OBJECT Lifetime: Lives as long as the object lives Direct Access: Within all non-static methods of the object's class 3. PRIVATE STATIC FIELD IN A CLASS Lifetime: Lives 'forever' (as long as the class is accessible) Direct Access: Within all methods of the class, static and non-static 4. IN A DATA STRUCTURE (array, list, map, etc); which in turn is located in 1, 2, or 3. (Or 4, recursively, but ultimately 1-3). - Rule of thumb: Use the shortest-lived, least-accessible place sufficient for the needs of the particular state. - What about public fields? WHERE DO YOU PUT THE STATE? - 'State': Information to be remembered for later in the computation. = Always two basic questions about state: 1. LIFETIME: How long does it need to be remembered? 2. DIRECT ACCESS: Who most needs to get at it during its life? - Choices: 1. ARGUMENT TO OR LOCAL VARIABLE IN A METHOD Lifetime: Lives until the method finishes (by return or exception) Direct Access: Only within the method in which it's declared 2. PRIVATE NON-STATIC FIELD IN AN OBJECT Lifetime: Lives as long as the object lives Direct Access: Within all non-static methods of the object's class 3. PRIVATE STATIC FIELD IN A CLASS Lifetime: Lives 'forever' (as long as the class is accessible) Direct Access: Within all methods of the class, static and non-static 4. IN A DATA STRUCTURE (array, list, map, etc); which in turn is located in 1, 2, or 3. (Or 4, recursively, but ultimately 1-3). - Rule of thumb: Use the shortest-lived, least-accessible place sufficient for the needs of the particular state. - What about public fields? What about 'final' state? OBJECTS AND THEIR INTERACTIONS - Object-oriented programming is about building islands and bridges. int foo() { int i,j,k,l,m; // Bad char myChar; // Bad ..n if (..) { char myChar = '.'; // Better ... } OBJECTS AND THEIR INTERACTIONS - Object-oriented programming is about building islands and bridges. - Actually it's about building blueprints for islands with bridges, _and_ building islands with bridges from the blueprints and connecting the islands to each other with the bridges. OBJECTS AND THEIR INTERACTIONS - Object-oriented programming is about building islands and bridges. - Actually it's about building blueprints for islands with bridges, _and_ building islands with bridges from the blueprints and connecting the islands to each other with the bridges. - Actually it's about building construction companies that know how to build islands with bridges, and having those companies build islands with bridges, and having the islands get connected to each other by the bridges. OBJECTS AND THEIR INTERACTIONS - Object-oriented programming is about building islands and bridges. - Actually it's about building blueprints for islands with bridges, _and_ building islands with bridges from the blueprints and connecting the islands to each other with the bridges. - Actually it's about building construction companies that know how to build islands with bridges, and having those companies build islands with bridges, and having the islands get connected to each other by the bridges. - Actually it's about... To have a clue what is going on, we need to be able to visualize the stages of construction, and draw maps of the resulting island chains.. - Lots of pictures for lots of purposes. E.g., UML class diagrams. OBJECTS AND THEIR INTERACTIONS - Object-oriented programming is about building islands and bridges. - Actually it's about building blueprints for islands with bridges, _and_ building islands with bridges from the blueprints and connecting the islands to each other with the bridges. - Actually it's about building construction companies that know how to build islands with bridges, and having those companies build islands with bridges, and having the islands get connected to each other by the bridges. - Actually it's about... To have a clue what is going on, we need to be able to visualize the stages of construction, and draw maps of the resulting island chains.. - Lots of pictures for lots of purposes. E.g., UML class diagrams. - Basic: Memory and pointers diagrams OBJECTS AND THEIR INTERACTIONS - Object-oriented programming is about building islands and bridges. - Actually it's about building blueprints for islands with bridges, _and_ building islands with bridges from the blueprints and connecting the islands to each other with the bridges. - Actually it's about building construction companies that know how to build islands with bridges, and having those companies build islands with bridges, and having the islands get connected to each other by the bridges. - Actually it's about... To have a clue what is going on, we need to be able to visualize the stages of construction, and draw maps of the resulting island chains.. - Lots of pictures for lots of purposes. E.g., UML class diagrams. - Basic: Memory and pointers diagrams => 'memory', places that information can be stored, represented by a box => 'pointers', memory that's treated as an address, represented by an arrow OBJECTS AND THEIR INTERACTIONS - Object-oriented programming is about building islands and bridges. - Actually it's about building blueprints for islands with bridges, _and_ building islands with bridges from the blueprints and connecting the islands to each other with the bridges. - Actually it's about building construction companies that know how to build islands with bridges, and having those companies build islands with bridges, and having the islands get connected to each other by the bridges. - Actually it's about... To have a clue what is going on, we need to be able to visualize the stages of construction, and draw maps of the resulting island chains.. - Lots of pictures for lots of purposes. E.g., UML class diagrams. - Basic: Memory and pointers diagrams => 'memory', places that information can be stored, represented by a box => 'pointers', memory that's treated as an address, represented by an arrow - Java uses memory and pointers in fixed ways. So the pictures have rules. JAVA'S USE OF MEMORY AND POINTERS - CARTOON VERSION There are two things in the Java universe VARIABLES and OBJECTS JAVA'S USE OF MEMORY AND POINTERS - CARTOON VERSION There are two things in the Java universe VARIABLES and OBJECTS How do you tell them apart? JAVA'S USE OF MEMORY AND POINTERS - CARTOON VERSION There are two things in the Java universe VARIABLES and OBJECTS How do you tell them apart? VARIABLES have NAMES OBJECTS have TYPES JAVA'S USE OF MEMORY AND POINTERS - CARTOON VERSION There are two things in the Java universe VARIABLES and OBJECTS How do you tell them apart? VARIABLES have NAMES (and TYPES) OBJECTS have TYPES (but no NAMES) JAVA'S USE OF MEMORY AND POINTERS - CARTOON VERSION Two rules: (1) Memory contains variables, and objects, which are collections of variables. (2) A variable can contain either (2.1) A primitive value (int, short, char, byte, long, float, double), or (2.2) A null reference that isn't pointing anywhere, or (2.3) A reference pointing to an object. JAVA'S USE OF MEMORY AND POINTERS - CARTOON VERSION Two rules: (1) Memory contains variables, and objects, which are collections of variables. (2) A variable can contain either (2.1) A primitive value (int, short, char, byte, long, float, double), or (2.2) A null reference that isn't pointing anywhere, or (2.3) A reference pointing to an object. To draw a memory and pointers diagram: (1) Variables and objects are both drawn as boxes (WARNING! Confusion risk!) (2) A variable is drawn as a box (2.1) If the variable contains a primitive value, that value is drawn inside the box (2.2) If the variable contains a null reference, 'null' goes in the box (2.3) If the variable contains an object reference, an arrow is drawn, with its tail inside the box and its head on the object it refers to (3) An object is drawn as a stack of variables. VARIABLE OR OBJECT - JAVA'S USE OF MEMORY AND POINTERS - CARTOON VERSION i int i = 0; +----+ | 0| variable or object? +----+ VARIABLE OR OBJECT - JAVA'S USE OF MEMORY AND POINTERS - CARTOON VERSION i int i = 0; +----+ | 0| VARIABLE +----+ VARIABLE OR OBJECT - JAVA'S USE OF MEMORY AND POINTERS - CARTOON VERSION i int i = 0; +----+ | 0| VARIABLE +----+ j Integer j = null; +----+ |null| variable or object? +----+ VARIABLE OR OBJECT - JAVA'S USE OF MEMORY AND POINTERS - CARTOON VERSION i int i = 0; +----+ | 0| VARIABLE +----+ j Integer j = null; +----+ |null| VARIABLE +----+ VARIABLE OR OBJECT - JAVA'S USE OF MEMORY AND POINTERS - CARTOON VERSION i int i = 0; +----+ | 0| VARIABLE +----+ j Integer j = null; +----+ |null| VARIABLE +----+ k Integer k = +----+ new Integer(3); | | variable or object? +----+ VARIABLE OR OBJECT - JAVA'S USE OF MEMORY AND POINTERS - CARTOON VERSION i int i = 0; +----+ | 0| VARIABLE +----+ j Integer j = null; +----+ |null| VARIABLE +----+ k Integer k = +----+ new Integer(3); | | VARIABLE ..But the picture is incomplete.. +----+ VARIABLE OR OBJECT - JAVA'S USE OF MEMORY AND POINTERS - CARTOON VERSION i int i = 0; +----+ | 0| VARIABLE +----+ j Integer j = null; +----+ |null| VARIABLE +----+ k Integer k = +----+ new Integer(3); | | VARIABLE +--|-+ | Integer \---->+----------+ | 3 | variable or object? +----------+ VARIABLE OR OBJECT - JAVA'S USE OF MEMORY AND POINTERS - CARTOON VERSION i int i = 0; +----+ | 0| VARIABLE +----+ j Integer j = null; +----+ |null| VARIABLE +----+ k Integer k = +----+ new Integer(3); | | VARIABLE +--|-+ | Integer \---->+----------+ | 3 | OBJECT +----------+ MEMORY AND POINTERS - A memory and pointers picture must be associated with a specific moment in the execution of the program. = 'Draw the memory and pointers diagram for this program: class Foo { .. public static void main(String[] args) { .. } .. }' MEMORY AND POINTERS - A memory and pointers picture must be associated with a specific moment in the execution of the program. = 'Draw the memory and pointers diagram for this program: class Foo { .. public static void main(String[] args) { .. } .. }' Answer is that's impossible until you say _when/where_ in the program execution you want the picture. = 'Draw the memory and pointers diagram as of the point marked 'HERE' in this program: class Foo { .. public static void main(String[] args) { .. Foo bar = new Foo(); /* HERE */ .. }' Well okay then. MEMORY AND POINTERS - A memory and pointers picture must be associated with a specific moment in the execution of the program. - A box for every primitive variable alive at that moment. Primitive variable boxes have no arrows into them or out of them. Instead they have their values inside them. .. boolean foo = true; if (foo) { int bar = 12; System.out.println(2*bar); } short gah = 9; /* HERE */ .. MEMORY AND POINTERS - A memory and pointers picture must be associated with a specific moment in the execution of the program. - A box for every primitive variable alive at that moment. Primitive variable boxes have no arrows into them or out of them. Instead they have their values inside them. .. boolean foo = true; if (foo) { int bar = 12; System.out.println(2*bar); } short gah = 9; /* HERE */ gah .. +-----+ | 9 | +-----+ MEMORY AND POINTERS - A memory and pointers picture must be associated with a specific moment in the execution of the program. - A box for every primitive variable alive at that moment. Primitive variable boxes have no arrows into them or out of them. Instead they have their values inside them. .. boolean foo = true; if (foo) { int bar = 12; System.out.println(2*bar); } short gah = 9; /* HERE */ gah foo .. +-----+ +-----+ | 9 | | true| +-----+ +-----+ MEMORY AND POINTERS - A memory and pointers picture must be associated with a specific moment in the execution of the program. - A box for every primitive variable alive at that moment. Primitive variable boxes have no arrows into them or out of them. Instead they have their values inside them. = We can treat a String reference variable as if it was a primitive variable, even though it's really not. MEMORY AND POINTERS - A memory and pointers picture must be associated with a specific moment in the execution of the program. - A box for every primitive variable alive at that moment. Primitive variable boxes have no arrows into them or out of them. Instead they have their values inside them. = We can treat a String reference variable as if it was a primitive variable, even though it's really not. int foo = 3; String bar = "ha"; bar = "ah"+bar; /* HERE */ MEMORY AND POINTERS - A memory and pointers picture must be associated with a specific moment in the execution of the program. - A box for every primitive variable alive at that moment. Primitive variable boxes have no arrows into them or out of them. Instead they have their values inside them. = We can treat a String reference variable as if it was a primitive variable, even though it's really not. int foo = 3; String bar = "ha"; bar = "ah"+bar; /* HERE */ foo bar bar String +----+ +---------+ +-----+ +---------+ | 3 | | "ahha" | | ----->| | +----+ +---------+ +-----+ | "ahha" | | | | | | | +---------+ MEMORY AND POINTERS - A memory and pointers picture must be associated with a specific moment in the execution of the program. - A box for every primitive variable alive at that moment. Primitive variable boxes have no arrows into them or out of them. Instead they have their values inside them. = We can treat a String reference variable as if it was a primitive variable, even though it's really not. - A box for every reference variable alive at that moment. Reference variable boxes either have 'null' in them or have precisely one arrow coming out of them. MEMORY AND POINTERS - A memory and pointers picture must be associated with a specific moment in the execution of the program. - A box for every primitive variable alive at that moment. Primitive variable boxes have no arrows into them or out of them. Instead they have their values inside them. = We can treat a String reference variable as if it was a primitive variable, even though it's really not. - A box for every reference variable alive at that moment. Reference variable boxes either have 'null' in them or have precisely one arrow coming out of them. r r r +----+ +----+ +----+ |null| | ------> (stuff) | |----> (stuff) +----+ +----+ +----+ COULD BE RIGHT COULD BE RIGHT DEFINITELY WRONG MEMORY AND POINTERS - A memory and pointers picture must be associated with a specific moment in the execution of the program. - A box for every primitive variable alive at that moment. Primitive variable boxes have no arrows into them or out of them. Instead they have their values inside them. = We can treat a String reference variable as if it was a primitive variable, even though it's really not. - A box for every reference variable alive at that moment. Reference variable boxes either have 'null' in them or have precisely one arrow coming out of them. = Reference variable boxes can never have any arrows pointing at them. MEMORY AND POINTERS - A memory and pointers picture must be associated with a specific moment in the execution of the program. - A box for every primitive variable alive at that moment. Primitive variable boxes have no arrows into them or out of them. Instead they have their values inside them. = We can treat a String reference variable as if it was a primitive variable, even though it's really not. - A box for every reference variable alive at that moment. Reference variable boxes either have 'null' in them or have precisely one arrow coming out of them. = Reference variable boxes can never have any arrows pointing at them. a Foo Foo a = new Foo(); +-----+ +---+ Foo b = a; b | -------->+---+ /* HERE */ +----+ +-----+ ^ | ------------------------/ +----+ MEMORY AND POINTERS - A memory and pointers picture must be associated with a specific moment in the execution of the program. - A box for every primitive variable alive at that moment. Primitive variable boxes have no arrows into them or out of them. Instead they have their values inside them. = We can treat a String reference variable as if it was a primitive variable, even though it's really not. - A box for every reference variable alive at that moment. Reference variable boxes either have 'null' in them or have precisely one arrow coming out of them. = Reference variable boxes can never have any arrows pointing at them. a Foo a = new Foo(); +----+ Foo Foo b = a; | ------>+----+ /* HERE */ +----+ | .. | | | +----+ SO FAR SO GOOD.. MEMORY AND POINTERS - A memory and pointers picture must be associated with a specific moment in the execution of the program. - A box for every primitive variable alive at that moment. Primitive variable boxes have no arrows into them or out of them. Instead they have their values inside them. = We can treat a String reference variable as if it was a primitive variable, even though it's really not. - A box for every reference variable alive at that moment. Reference variable boxes either have 'null' in them or have precisely one arrow coming out of them. = Reference variable boxes can never have any arrows pointing at them. b a Foo a = new Foo(); +---+ +----+ Foo Foo b = a; | ------------->| ------>+----+ /* HERE */ +---+ +----+ | .. | | | +----+ TOTALLY BLOWN! MEMORY AND POINTERS - A memory and pointers picture must be associated with a specific moment in the execution of the program. - A box for every primitive variable alive at that moment. Primitive variable boxes have no arrows into them or out of them. Instead they have their values inside them. = We can treat a String reference variable as if it was a primitive variable, even though it's really not. - A box for every reference variable alive at that moment. Reference variable boxes either have 'null' in them or have precisely one arrow coming out of them. = Reference variable boxes can never have any arrows pointing at them. b a Foo a = new Foo(); +---+ Foo +----+ Foo Foo b = a; | ---->+----+ | ------>+----+ /* HERE */ +---+ | .. | +----+ | .. | | | | | +----+ +----+ ALSO WRONG! MEMORY AND POINTERS - A memory and pointers picture must be associated with a specific moment in the execution of the program. - A box for every primitive variable alive at that moment. Primitive variable boxes have no arrows into them or out of them. Instead they have their values inside them. = We can treat a String reference variable as if it was a primitive variable, even though it's really not. - A box for every reference variable alive at that moment. Reference variable boxes either have 'null' in them or have precisely one arrow coming out of them. = Reference variable boxes can never have any arrows pointing at them. b a Foo a = new Foo(); +---+ +----+ Foo Foo b = a; | ----\ | ------>+----+ /* HERE */ +---+ \ +----+ | .. | \----------------->| | +----+ OKAY! MEMORY AND POINTERS - A memory and pointers picture must be associated with a specific moment in the execution of the program. - A box for every primitive variable alive at that moment. Primitive variable boxes have no arrows into them or out of them. Instead they have their values inside them. = We can treat a String reference variable as if it was a primitive variable, even though it's really not. - A box for every reference variable alive at that moment. Reference variable boxes either have 'null' in them or have precisely one arrow coming out of them. = Reference variable boxes can never have any arrows pointing at them. b a Foo a = new Foo(); +---+ +----+ Foo Foo b = a; | ----\ | ------>+----+ /* HERE */ +---+ \ +----+ / | .. | \--------------/ | | +----+ ALSO OKAY! MEMORY AND POINTERS - A memory and pointers picture must be associated with a specific moment in the execution of the program. - A box for every primitive variable alive at that moment. Primitive variable boxes have no arrows into them or out of them. Instead they have their values inside them. = We can treat a String reference variable as if it was a primitive variable, even though it's really not. - A box for every reference variable alive at that moment. Reference variable boxes either have 'null' in them or have precisely one arrow coming out of them. = Reference variable boxes can never have any arrows pointing at them. b a Foo a = new Foo(); +---+ +----+ Foo Foo b = a; | ----\ | ---\ +----+ /* HERE */ +---+ \ +----+ -->| .. | \--------------/ | | +----+ ALSO ALSO OKAY! MEMORY AND POINTERS - A memory and pointers picture must be associated with a specific moment in the execution of the program. - A box for every primitive variable alive at that moment. Primitive variable boxes have no arrows into them or out of them. Instead they have their values inside them. = We can treat a String reference variable as if it was a primitive variable, even though it's really not. - A box for every reference variable alive at that moment. Reference variable boxes either have 'null' in them or have precisely one arrow coming out of them. = Reference variable boxes can never have any arrows pointing at them. b a Foo a = new Foo(); +---+ +----+ Foo Foo b = a; | ----\ | ------>+----+ /* HERE */ +---+ \ +----+ | .. | \-------------------> | +----+ WRONG AGAIN! YOU CAN ONLY POINT AT A WHOLE OBJECT, NOT AT A PIECE INSIDE! MEMORY AND POINTERS - A memory and pointers picture must be associated with a specific moment in the execution of the program. - A box for every primitive variable alive at that moment. Primitive variable boxes have no arrows into them or out of them. Instead they have their values inside them. = We can treat a String reference variable as if it was a primitive variable, even though it's really not. - A box for every reference variable alive at that moment. Reference variable boxes either have 'null' in them or have precisely one arrow coming out of them. = Reference variable boxes can never have any arrows pointing at them. - A box for every (user-created, relevant) Object alive at that moment. = With 'slots' (sub-boxes) inside for every non-static field in the object = And each slot is either a primitive variable box or a reference variable box. A slot CANNOT be a whole object!