Welcome to THT
If you already know PHP, then THT will feel familiar. It’s still just a dynamic, curly brace language with $ variables.
While THT provides a lot of handy shortcuts, you’ll see that it also adds a few limitations. For example, identifiers are always camelCase.
We like to think of these conventions as guard rails. They aren’t meant to cage you in, but to keep you moving forward.
They will help you avoid common mistakes that all programmers tend to make, and also free up more of your “cognitive bandwidth” to focus on higher-level problems, instead of getting sidetracked with trivial bugs & decisions.
If you’re a beginner, this means you will develop good programming habits from the start — habits that will be useful in other languages, as well.
If you use TextMate or Sublime Text, you can download the THT.tmbundle to add syntax highlighting support.
A Very Simple Program
Let’s start with a one-line program.
As you can probably guess, it prints this to the browser:
How It Works
Let’s look at each part:
print('Hello World!'); ^^^^^
2) The text, or string, is surrounded by single quote marks. (The quotes are not printed.)
// ✖ NO print(Hello World!); // ✔ YES print('Hello World!'); ^ ^
3) Our string is passed into the
// ✖ NO print 'Hello World!'; // ✔ YES print('Hello World!'); ^ ^
4) Every statement ends in a semicolon, just like a period at the end of a sentence.
// ✖ NO print('Hello World!') // ✔ YES print('Hello World!'); ^
If you try to print a data type other than text, it will be displayed in a readable format.
To send HTML output to the browser, you will use the
Response module, as described in How to Create a Basic Web App.
In the process of getting your program to run, it’s easy to end up with code that is a bit messy. It happens to everyone, whether they are new or experienced.
Messy code can be frustrating and stressful to work with, because it is harder to read and creates places where bugs can hide.
To help you write clean code, THT includes a Format Checker. It provides instant feedback about how to format your code so that it’s easier to understand and work with.
Spacing is especially important!
// ✖ NO $lineWidth=(10+5)~'px'; // ✔ YES Cleaner spacing $lineWidth = (10 + 5) ~ 'px';
Read More: Format Checker
A comment is a line starting with
Comments are not executed as part of the program. A comment can appear on a line by itself, or at the end of a code line.
// A standalone comment. // Here is another line. $a = 1; // Or next to a line of code.
How to Use Comments
Comments are useful for a few things:
Explaining code. Explain parts of the program to help any future reader (including you!) modify it later.
// Apply the Zuckerdorf Friendship Formula to determine // how close this friend is to the current user. $friendLevel = ($numFriendsInCommon * 1.2345) + 42;
Disabling code. Temporarily remove lines of code while debugging. (Be careful not to clutter your files with too much unused code.)
TODO's. Record “To Do” reminders or other notes for the future.
// TODO: add in the rest of the seven dwarves $dwarves = ['Sleepy', 'Sneezy', 'Doc'];
Surrounding text with
*/ creates a multi-line comment block.
/* This is a multi-line comment. */
The Right Balance
Instead of writing long comments, try to make the code itself more self-explanatory, by using clear variable names and organizing it into small, simple functions.
Rule of Thumb: Comments should explain the “why” of the code. The code itself already tells you the “what”.