Friday, June 11, 2021

Exploring the Ternary Operator

I’ve never had a lot of use for the ternary operator. Until recently, I think I under appreciated it. I Don’t recall ever using it in professional development though that may be largely related to using languages that didn’t support it. I never taught it to beginners either. Between time available and topics to be covered it never rose to a high enough priority for me. I’m starting to rethink that. But first, what am I talking about?

In C-style languages the syntax is:

(expression-1) ? expression-2 : expression-3

There is a Boolean expression inside the parenthesis, followed by a ? and a value for a true case, a : and a value for a false case. For example:

(Player == WHITE) ? "White" : "Black";

Other languages have different formats. For example, in Python the true option comes before the Boolean expression.

[on_true] if [expression] else [on_false]
>>> x, y = 5, 6
>>> print("x" if x> y else "y")

We actually see this sort of thing in spreadsheets though I never really thought of it as a type of ternary operator. This example from Excel is really a ternary operation

=IF(G10 <  G11  ,"big","small")

Back in the day we had something not all that different in FORTRAN. There was (probably still is) an IF statement in FORTRAN that branches to one of three lines depending on if an arithmetic value is less than, equal to, or greater than zero.

IF ( N ) 10, 20, 30 

But enough history. What really changed my mind about this operator? As is typical for me I revalued it when I found a good use for it. I have been writing a version of the Reversi program in .NET and C#. I borrowed a bunch of Java code from my friend Tom Indelicato (read about that program here) Steal from the best is my motto and Tom is an outstanding programmer. Along with the syntactic changes moving from Java to C# I wrote a lot of code to make the game work in a graphical user interface. I made some different design decisions as well which changed how some things are handled. I created a user control class for the game squares for starters. As part of this I used three different integer values to indicate whether the square held a black disk,a white disk, or was empty.

In the past I have often used a Boolean value to indicate which of two players was the current player. Switching players is pretty simple with a Boolean value.

currentPlayer = !curentPlayer;

That wasn’t going to work if my player indicators were integers. I decided to use the ternary operator because I didn’t want to write multiple lines of code for something that simple. So I wrote:

Player = (Player == WHITE) ? BLACK : WHITE;

WHITE and BLACK are defined constants. The next thing I knew, I was finding uses for this operator in all sorts of places. The really cool thing (ok for a geek like me) was that I could use this statement inside other statements.

message = “No legal moves for " + ((Player == BLACK) ? "Black" : "White");

I guess this old dog can learn new (to him) tricks. Do you use/teach this operator? Are there other language features you have ignored until one day you realized they would be just right for something you were doing?

Thursday, June 03, 2021

Trip Report: Teaching as a Long Term Sub

Well, this is not actually a trip report but its something like it. I spend the last couple of months teaching at Phillips Exeter Academy as a long term substitute. It was an interesting experience for many reasons. Not the least of those was the pandemic precautions.  Everyone wore masks inside and out, there were clear dividers between all of the seats around the table (more about that table in a minute), and for most of the time faculty were tested for COVID twice a week. I was fully vaccinated before I started there and the school ran vaccination clinics for faculty and students during the term.

Phillips is different from most other schools. It is the literal definition of an exclusive New England boarding school for a start. With a very competitive admissions process one is not surprised that the students are smart and highly motivated. Many of the students I talked to were looking for more academic rigor than their local schools. Needless to say classroom management was never even close to an issue.

As I alluded to earlier, classes at PEA are taught around a table. It’s a big table but class size is limited to about 13. None of my classes were larger than 12 and two sections were 10 students. Yes, that’s wonderful for a teacher. Between the small class size and teaching around a table it is not easy for a student to get lost. Teaching around a table forces a more conversational method of teaching. Its very interactive. I really enjoyed the format.

Another part of the system there is something they call METIC. I forget what that stands for but basically what it is is a mid-term check in. Students in the class are given some time without the teacher present to discuss what was going well in the class, what is not going well, and what students and teacher can do to make the rest of the term go better. Now this can be hard on the ego for some teachers especially if students are hard on them. My students, and from what I hear  most students at PEA, are very respectful and honest. Following time without the teacher, the teacher returns and the items the students brought up (written on the white board) are discussed. For me as a new comer to the particular school environment this was extremely valuable. This is something I wish had happened earlier in my teaching career. I think it would have made me a better teacher.

Everything was not all smooth sailing of course. I took over the course in the fourth week of the term and my students had two other teachers in the first three weeks. Figuring out what students knew and didn’t know took me a while. I also had to figure out a plan for the rest of the term. Fortunately, other members of the CS department were incredibly helpful and supportive. I was give full access to curriculum from a previous time these two courses had been taught. This saved immeasurable amounts of prep time.They were also always available for my many questions about the courses and the school’s processes.

As I alluded to in my earlier post (Back in the Classroom Again) I had to reacquaint myself with Java and learn my way around two IDEs (Eclipse and Processing) that I had never used before. Its tough answering questions about IDEs when you have no real experience with them. Fortunately I am good at exploring menus and looking up things on the Internet. I’m far from expert at either IDE but I’m not totally lost either. I found Processing and its graphics library fun and interesting. My old experience with XNA from years ago helped me out there. Though I do wish Processing had some built in collision detection. Probably good for students to figure out and code that for themselves but I’ve been spoiled by .NET. I would use Processing again for Java.

Overall, I had a great experience. I learned a lot, got to work with great teachers, and teach some awesome students. I’m not unhappy about getting back into retirement though.