Ruby vs JavaScript

Posted: 14 Nov 2015
Punctuation Terror

Coding languages are fussy when it comes to punctuation. The older the language the worse it gets. C++, for example, requires a semicolon after every expression, every variable must be declared with a type name ( integer, character, etc ), curly braces around every code block, and parentheses at the end of every function call even if the function takes no arguments.

Be warned! What follows is valid C++ code that I have tested. It will look intimidating if you've never worked in it before, and it's actually a really convoluted program designed purely as an example. Any viewer with a weak heart is advised to look away.

// C++

#include <cstring>
#include <iostream>
#include <stdio.h>

#define maximum 5
#define min(a, b) a < b ? a : b

int myNum = 0;
char myCharacter = '!';
std::string myWord = "world";

int incrementNumber( int numIn ) {
  return numIn++;
}

void sayHello() {
  std::cout << "Hello, " << myWord << myCharacter;
  std::cout << std::endl;
  myNum = incrementNumber( myNum );
  std::cout << myNum << " greeting.";
  std::cout << std::endl;
}

int main() {
  sayHello();
  sayHello();
  return 0;
}

// => Hello, world!
// => 1 greeting.
// => Hello, world!
// => 2 greeting.
# Ruby

# [ no need to '#include' here ]

$myNum = 0
$myCharacter = '!'
$myWord = 'world'

def incrementNumber( numIn )

  numIn += 1

end

def sayHello

  puts "Hello, " + $myWord + $myCharacter
  $myNum = incrementNumber( $myNum )
  puts $myNum.to_s + " greeting."

end

sayHello
sayHello

# => Hello, world!
# => 1 greeting.
# => Hello, world!
# => 2 greeting.
// JavaScript

// [ no need to '#include' here ]

var myNum = 0;
var myCharacter = '!';
var myWord = 'world';

function incrementNumber( numIn ) {
  return numIn++;
}


function sayHello() {
  console.log( "Hello, " + myWord + myCharacter );
  myNum = incrementNumber( numIn );
  console.log( myNum.toString() + " greeting." );
}

sayHello();
sayHello();

// => Hello, world!
// => 1 greeting.
// => Hello, world!
// => 2 greeting.

Ruby is an outlier in this sense: being based on C, it needs all these punctuation marks, but it knows where to put them based on line breaks and keywords. Ruby secretly adds everything it needs to allow programmers to create less cluttered, more quickly written code.

JavaScript is more closely related to C++; it requires parentheses after function calls, but it doesn't need curly braces for every code block, or a semicolon for every expression. This presents a difficult choice for the JavaScript programmer: whether to suffer an error for every crucial brace or semicolon you thought you could omit, or to take shelter with every punctuation mark included however cluttered the lines will be.

I err on the side of caution; JavaScript is intended for a web environment, so your code should be easily manipulable by other programmers. For example: you do not need to terminate the last expression in a code block with a semicolon. However, if someone ever needs to add another expression to that block, they have to add a semicolon to the end of your expression. This leaves a lot of opportunities for simple errors that are hard to spot. JavaScript won't throw an error if there is a semicolon at the end of your block, so put it there just to be safe.

Previous: Ruby's So Classy | Next: Computer Memory