My Favorite Pieces of Syntax in 8 Different Programming Languages
We love to criticize programming languages.

We also love to quote Bjarne Stroustrup:

“There are only two kinds of languages: the ones people complain about and the ones nobody uses.”

So today I decided to flip things around and talk about pieces of syntax that I appreciate in some of the various programming languages I have used.

This is by no means an objective compilation and is meant to be a fun quick read.

I must also note that I’m far from proficient in most of these languages. Focusing on that many languages would probably be very counter-productive.

Nevertheless, I’ve at least dabbled with all of them. And so, here’s my list:

List Comprehension

def squares(limit):

return [num*num for num in range(0, limit)]

Python syntax has a lot of gems one could pick from, but list comprehension is just something from heaven.

It’s fast, it’s concise, and it’s actually quite readable. Plus it lets you solve Leetcode problems with one-liners. Absolute beauty.

Spread Operator

let nums1 = [1,2,3]

let nums2 = [4,5,6] let nums = [...nums1, ...nums2]

Introduced with ES6, the JavaScript spread operator is just so versatile and clean that it had to be on this list.

Want to concatenate arrays? Check.

let nums = [...nums1, ...nums2]

Want to copy/unpack an array? Check.

let nums = [...nums1]

Want to append multiple items? Check.

nums = [...nums, 6, 7, 8, 9, 10]

And there are many other uses for it that I won’t mention here.

In short, it’s neat and useful, so that earns my JS syntax prize.

Goroutines

go doSomething()

Goroutines are lightweight threads in Go. And to create one, all you need to do is add go in front of a function call.

I feel like concurrency has never been so simple.

Here’s a quick example for those not familiar with it. The following snippet:

fmt.Print("Hello")

go func() {

doSomethingSlow()

fmt.Print("world!")

}()

fmt.Print(" there ")

Prints:

Hello there world!

By adding go in front of the call to the closure (anonymous function) we make sure that it it is non-blocking.

Very cool stuff indeed!

Case & Underscore Indifference

proc my_func(s: string) =

echo s myFunc("hello")

Nim is, according to their website, a statically typed compiled systems programming language. And, according to me, you probably never heard of it.

If you haven’t heard of Nim, I encourage you to check it out, because it’s actually a really cool language. In fact, some people even claim it could work well as Python substitute.

Either way, while the example above doesn’t show it too much, Nim’s syntax is often very similar to Python’s.

As such, this example is not actually what I think is necessarily the best piece of syntax in Nim, since I would probably pick something inherited from Python, but rather something that I find quite interesting.

I have very little experience with Nim, but one of the first things I learned is that it is case and underscore-insensitive (except for the first character).

Thus, HelloWorld and helloWorld are different, but helloWorld , helloworld , and hello_world are all the same.

At first I thought this could be problematic, but the Docs explains that this is helpful when using libraries that made use of a different style to yours, for example.

Since your own code should be consistent with itself, you most likely wouldn’t use camelCase and snake_case together anyway. However, this could be useful if, for instance, you want to port a library and keep the same names for the methods while being able to make use of your own style to call them.

In-line Assembly

function getTokenAddress() external view returns(address) {

address token;

assembly {

token := sload(0xffffffffffffffffffffffffffffffffffffffff)

}

return token

}

Solidity is the main language for writing smart contracts on the Ethereum blockchain.

A big part of writing smart contracts is optimizing the code, since every operation on the Ethereum blockchain has an associated cost.

As such, I find the ability to add in-line Solidity assembly right there with your code extremely powerful, as it lets you get a little closer to the Ethereum Virtual Machine for optimizations where necessary.

I also think it fits in very nicely within the assembly block.

And, last but not least, it makes proxies possible, which is just awesome.

For-Each Loop

for (int num : nums) {

doSomething(num);

}

In a language generally considered verbose, the for-each loop in Java is a breath of fresh air. I think it looks pretty clean and is quite readable (although not quite Python num in nums readable).

Macros

#define MAX_VALUE 10

I got introduced to C-style macros when building my first Arduino project and for a while had no clue exactly what they did.

Nowadays, I have a better idea of what macros are and am quite happy with the way they are declared in C.

Not hating on C by any means, but, like Java, there’s little about the actual syntax that stands out, so these last two ones are a little meh, unfortunately.

‘using namespace’

using namespace std;

Sorry :(

And that’s it! So, what are your favorite pieces of syntax?
['Yakko Majuri']
['Programming', 'Software Development', 'Technology', 'Software Engineering', 'JavaScript']