Julia’s Blog

puts “my thoughts here”

Gimme Some Syntactic Sugar

As a beginner programmer, learning a new language can be scary and daunting. So, I was delighted to learn about “syntactic sugar”.

The term “syntactic sugar” sounds sweet and inviting, and it is! It is syntax that does not follow the regular conventions and it makes for easier writing and reading of code. Ruby was designed with it’s users in mind so its syntax is rather natural and not much different from the way we actually speak at times. Syntactic sugar makes Ruby (and other languages) more user (and beginner) friendly!

Life with no sugar…

1
2
3
4
2.+(5)
x=x+1
"hello world".send(:capitalize)
a||a=b

A spoon full of sugar helps the code look much prettier…

1
2
3
4
2+5
x+=1
"hello world".capitalize
a||=b

Wow, look how much clearer and “more human” the code looks with a touch of sugar!

Unfortunately, not everyone is a fan of syntactic sugar. There are critics that see these exceptions as rather unimportant.

“Syntactic sugar causes cancer of the semicolon.” - Alan Perlis