Variables & Methods
Variables
A variable is like a box that contains information (data).
Each variable has a label that starts with $
.
Data values are assigned to variables with =
.
$location = 'Paris'; $numArtists = 9000; $isBeautiful = true;
Case Matching
Variable and function names are case-sensitive. They must match exactly.
$numApples = 10; // ✖ ERROR // 'A' should be uppercase. print($numapples); // ✖ ERROR // 'print' should be lowercase. Print('Hello!');
Basic Data Types
There are 3 basic types of data:
- Number. Whole numbers, fractions, or negative numbers.
- String. Text: a sequence of individual characters, including spaces.
- Boolean. Either
true
orfalse
.
// Numbers 42 // positive -62 // negative 1.618 // fraction (aka 'floating point' or 'float') // String (surrounded with single-quotes) 'This is a string!' // Booleans true false
Initializing Variables
New variables must always be given a value before they are used.
If you’re not sure what to use, pick the “empty” version of the type you need:
$myString = ''; $myNumber = 0; $myBoolean = false;
Null
Unlike many other languages, THT does not have a null
, nil
, or undefined
value. (See “The Billion Dollar Mistake”).
If you need a way to represent “nothing”, consider using a Result object, or an empty value for the given type.
Variable Names
Variables and function names are always written in camelCase format:
- All letters and numbers.
- No spaces or separators.
- The first letter must be a lowercase letter.
- The first letter of each extra word is Uppercase.
- Acronyms are spelled as a single word.
Examples:
$user $userId $firstName $isDeleted $numRecords $httpVersion $userHasIphone
Naming Tips
Coming up with good names is one of the most important (and difficult) skills in programming.
Always take the extra time to find names that clearly represent what they are labelling.
Common Guidelines:
- Use full words. But prefer shorter words. (e.g. “users” vs “customers”)
- Don’t drop random letters. It’s 2 hrd 2 rd and rmmbr the rght splng.
- Common acronyms and abbreviations are okay (e.g. “http”, “px”)
- Truncate very long words. (e.g. “administrator” to “admin”)
- Number counts are prefixed with “num-” (not “no-”, “nbr-”, or “cnt-”)
- Booleans are prefixed with “is-”, “has-”, “can-”, etc. (e.g. “isDeleted”)
- Include units of measurement or time. (e.g. “days”, “km”)
- Joiner words like “of” and “the” can be dropped. (e.g. numOfDaysInTheWeek = numDaysInWeek)
- Lists are plural, since they represent many things. (e.g. activeUsers)
- Maps are singular, since they represent one thing. (e.g. blogComment)
Examples:
✖ Bad ✔ Better ------ ------------ $usr $user $fname $firstName $nbrPosts $numPosts $cmntTxt $commentText $banned $isBanned $activeTime $numDaysActive $distance $distanceKm $userList $users $active $activeUsers, $isActive $a (be more specific) $temp (be more specific)
Methods
Methods are functions that are attached to data. They are called using the dot .
operator.
All built-in data types have methods.
$text = ' Tipsy Turtle '; $text.trim(); //= 'Tipsy Turtle' (no extra spaces) $text.contains('Turtle'); //= true $text.length(); //= 14 (including spaces)
Methods can be chained together.
$text = ' This is a string!!! '; $text = $text.trim().toUpperCase().replace(r'!+', '.'); //= 'THIS IS A STRING.'
Modules
A module is a bundle of related functions. Module names are always UpperCamelCase.
THT comes with essential modules like Math
, Date
, and Web
.
Math.floor(3.15); //= 3 (rounded down) Math.pi(); //= 3.1415926535898 Date.now(); //= current timestamp in seconds File.read('words.txt'); //= (contents of 'words.txt' file)