Syntax Tricks: TODO Statement

I’ve always been fascinated by the brilliant simplicity of the Perl ellipsis statement (colloquially known as the “yada-yada” statement). Python has a similar construct in the pass statement. These statements provide a means to mark unimplemented functionality in their respective languages.

For C-based languages, we can achieve a similar effect using clever syntax. In C, C++, and JavaScript, we can mark a function or area of code as being “to do” for later by using semicolons like so:

[c];;;[/c]

This is syntactically valid; they are just three empty statements.

Here’s an illustration of how this can be used in C:

[c]void doStuff() {
;;;
}[/c]

Likewise, in JavaScript:

[js]function doStuff() {
;;;
}[/js]

This is very useful for defining functions/methods you want to write later. However, it can be used beyond functions, and you can use it anywhere a statement would be valid. For example, in a video game, you might want to use a makeshift loading screen and develop a more detailed loading screen when more important functionality has been finished (or when the artwork for the loading screen finally becomes available). Most commonly, I use TODO statements to mark unimplemented functionality.

I also like to combine the TODO statement with comments for additional clarity:

[c]void renderLoadingScreen() {
doStuff();
doStuff2();

// TODO: Add progress bar to loading screen
;;;
}[/c]

If you are in a *nix environment, you can grep through your code to find all matches for TODO statements using this command:

$ grep -slR “;;;” *

Syntax Tricks: Region Blocks

One thing I really miss from Visual Studio is the ability to fold entire regions of code under a meaningful description. This allowed developers to hide code they weren’t particularly concerned with at a given moment. I work on several large projects, and the lack of regions has been driving me nuts, but I think I’ve stumbled on a clever way to organise JavaScript code…

Most IDEs and text editors support code folding. The detection algorithm for code folding usually comes down to “blocks” which are defined with curly braces: { … }. For example, a multi-line if statement requires a block to be defined:

[js]
if (true) {
doStuff();
doMoreStuff();
}
[/js]

Most editors will allow you to “fold” the if statement’s block here. However, it’s perfectly legal to define arbitrary blocks almost anywhere else in your code where a statement is typically valid:

[js]
var foo = 1;
{ // start block
doStuff();
} // end block
doMoreStuff();
[/js]

We can now “fold” the block and hide doStuff();

Finally, we’re now able to hide arbitrary pieces of code in JavaScript! However, we can go even further…

Continue reading Syntax Tricks: Region Blocks