First time here? Please, start at the beginning.

Thursday, September 16

Rewind: back to syntax

I've decided to back up a bit, away from uChat, and talk more about syntax. Some of this is near universal to all programming languages, some is more Java-specific.

Statements and expressions
Put simply, a statement is any executable line of code in your program. Several languages (Java, C et. al.) require a semicolon at the end of each statement, others don't (optional in perl).

Before I delve too deeply, a couple more terms. A "token" is the compiler's equivalent of a word; it generally has no meaning by itself. Keywords, operators, and identifiers are all tokens, as are braces and other punctuation-like symbols. An "identifier" is a name, it can be the name of a variable, a method, a class, or even a package.

A statement is generally built from expressions. An expression is a token or string of tokens that evaluate to a value. Time for an example:

(Assume that x is an int variable.)
5;     // expression: integer literal (also, by itself, a compiler error)
x = 5; // statement: puts the value on the right (5) in the variable on the left
3 + 2; // expression: integer literal, evaluates to five (also a compiler error)
x = 3 + 2; // statement

A method call is also an expression: is evaluates to the return value of the method. For example, the Integer.parseInt() method accepts a String and returns an int.

Tuesday, September 7

uChat as an FSM

No, not a Flying Spaghetti Monster, a Finite State Machine.

Wikipedia's article is rather long and dry. To put it briefly, a finite state machine is a device (or program) that has a set number of "states". An example would be a light switch: it has 2 states, "on" and "off". A dimmer switch, to contrast, would be an Infinite State Machine: it has a (nearly) infinite number of positions.

It addition to its set of states, a state machine also has "transition conditions" that cause it to change its state and (usually) a set of reactions that change depending on its state.

Here's another example, you could model a body of water as an FSM. It's states would be "solid", "liquid" and "gas". Its transition conditions would be defined as temperature changes, and its reactions would change depending on its state. If you poke a piece of ice sitting on a table, it will likely slide away; however, if you poke the surface of a pool of water, it will ripple. A slightly odd example, but I believe it serves its purpose.

Virtually every computer program you interact with is a Finite State Machine. Some have obvious states, others are more subtle, but few complex programs can be written without maintaining an internal state and altering its reactions to user input accordingly.

"But," you ask, "how is a simple program like uChat a Finite State Machine?" It may not be obvious from a glance at the source, but it is. A simple one, granted, with only 2 states: "connected" and "disconnected".

Fun fact: this picture's filesize is larger than uChat's source.

Lets list uChat's input sources, they're all part of the UI: the Name field, the Join button, the Message field, and the Send button.

The text area is marked an non-editable, so no user input can come from it. Also, clicking the Send button has the same effect as pressing enter in the Message field. There's a similar relation exists between the Name field and the Join button.

So, simplified slightly to "user intents" (actions the user intends to perform) we now have: "join the chat" (connect), "leave the chat" (disconnect), and "send a message".

The program's possible responses are: join the chat, leave the chat, send a message, ignore.

This post is incomplete. I may or may not return to this subject later, but I believe I've explained sufficiently for you to understand where I was going.