I do declare

Declarative vs. Imperative

During one of our classes at The Iron Yard we had a discussion on declarative vs. imperative coding. On short, the primary difference is the basic structure and rhythm of your code. Here's how I understand it:

Imperative coding is structured in such a way that it provides something akin to line by line instructions stating how we would like a certain function to be performed. Imagine a todo list.
Declarative coding is structured more like a sentence, even though it's still code and reads like code, once you have a grasp of your coding language it reads more like a request.

If I were to write it in English the difference would look something like this:

Go to the fruit basket and look through all the fruit; find all the apples; if they are red collect them; bring all the red apples to me.
Please pass me all the red apples from the fruit basket.

Simply put, when we write declaratively we separate our common functions from our working code and call those functions when we need them. I can see four immediate benefits:

  • We can maintain a repository of common utility functions which become available to all our code
  • It simplifies our primary code
  • Our primary code becomes significantly more legible.
  • If we're planning on using outside libraries declarative code already utilizes the type of syntax that calls the functions from those libraries in an understandable manner.

The following example shows a simple search function and a subsequent declarative function which uses the search function to look for vowels.

// contains function finds specific characters in a string
var contains = function(terms, findIn) {
    // We assume we're not going to find anything
    var result = false
    // We loop through the string
    for (var i = 0; i < findIn.length; i++) {
        // using indexOf we search for characters in our string at a specific index
        if (terms.indexOf(findIn.toLowerCase()[i]) != -1) {
        // we return true if we find them. an index of -1 equals no result
        result = true
    //we pass our result back to the function used to invoke this function

// this function uses the contains function 
// we use this function to check if our inputSentence hasVowels.
var hasVowel = function(inputSentence) {
    // invoking this function will return an answer to the question: are the letters aeiou contained in the inputSentence
    return contains("aeiou",inputSentence)

// let's test it
console.log(hasVowel("Can you find the toad?"))

Regular Expressions

Regular expressions are commonly used in advanced search queries. We use them to replace, for instance, multiple characters, which even though not found together, might be found throughout a string as in the example below. Regex can also be used to, for example, find text between certain strings, or certain patterns.

A simple example of regex in action:

// a function that trims punctation from a string
var trimPunc = function(text) {
    return text.replace()

var test = "Jack's sister said: 'To sing, or to holler! What's the big deal?'; Jack's sister is nutty (we surmise). "
// we're using regex in the replace function to find a list of characters

Every single task should have its own function, and every function should perform one subtask - Justin Richards