String Interpolation And Replacement

adminguy's picture
Posted December 8th, 2014 by adminguy
   
 
 
 
If you have programmed in Java, you would know how cumbersome it is to concatenate multiple Strings in one statement. Imagine you are iterating over a database ResultSet which yields the name, age, and nationality of employees in a company, and you need to print these details on the screen. It's a simple example, but stay with me - it will lead to an interesting concept which spans across multiple programming languages. 
 
Coming back to the print statement - many developers new to Java would probably write something like this:
 
String name = // get the 'name' column from the result set
String country = // get the 'country' column from the result set
String age = // get the 'age' column from the result set

System.out.println(name + " is from " + country + " and is " + age + " years old");
 
This is not only cumbersome and difficult to read, but also error prone, because missing out a space, will create unreadable output. If you are wondering how, try replacing " and is " with "and is " in the String above, and see what it prints. 
 
An alternate, less error prone, and more readable way of writing the same code in Java is:
 
String template = "%s is from %s and is %s years old.";
System.out.println(String.format(template, name, country, age));
 
This is also known as String templating, because first we create a template string with placeholders, and then substitute the placeholders with actual values. It's a step ahead from what we did earlier, but if Java was a dynamic programming language, we could have used String interpolation, which is even more elegant. 
 
String interpolation, if it did exist in Java, would allow us to put variables right inside the String, such that those variables would be evaluated to actual values at runtime. Something like this: 
 
System.out.println("$name is from $country and is $age years old.");
 
See how elegant it is - greater clarity, less error prone, and fewer lines of code.
 
Java does not support String interpolation because it is a statically typed language, but Groovy a dynamic language which compiles to JVM bytecode does support it. Here's how:
 
def name = // get 'name' column from result set
def country = // get 'country' column from result set
def age = // get 'age' column from result set
println """${name} is from ${country} and is ${age} years old"""
 
Since Groovy was strongly influenced by Ruby, we would expect String interpolation to be supported by Ruby also - and it certainly is:
 
name = // the name
country = // the country
age = // the age
puts "#{name} is from #{country} and is #{age} years old"
 
Python unfortunately does not support String interpolation in the same spirit as Groovy or Ruby, but we can get close with a little Pythonic twist.
 
name = // the name
country = // the country
age = // the age
 
#The 's' in %(country)s is very important
print("%(name)s is from %(country)s and is %(age)s years old" % locals())
 
String interpolation is supported in several programming languages. You may have to search for how to achieve it in your favorite language, but the search should be worth the time spent, because it will get you to write idiomatic code in your language. The code will reflect the effort you put into honing your craft. It will tell tales about your depth of knowledge, and will whisper to bystanders about the developer who really cares for his craft. 
 
Do tell us how String interpolation is supported in your favorite programming language.
 
Technical Word Power
 
Before we say goodbye we would like to upload a technical phrase into your memory. Do you know what the phrase idiomatic code means in Computer Science ? 
 
Interestingly I could not find a definition for this phrase from any well known source. Looks like it's one of those things which programmers get by engaging with their community of practice. But this SO thread defines idiomatic code as - "Idiomatic means following the conventions of the language. You want to find the easiest and most common ways of accomplishing a task rather than porting your knowledge from a different language."