loading...

What are your favorite programming language syntax features?

geocine profile image Aivan Monceller Updated on ・3 min read

I have my own non-exhaustive list, and these are purely based on what I have used: JavaScript, TypeScript, C#

Safe Navigation Operator

This is also known as optional chaining operator, safe call operator, null-conditional operator.

Instead of writing multiple nested ifs, we just use usual chaining, but put question mark symbols ? before dots (or other characters used for chaining).

String name = articles?[0]?.author?.name;

Null Coalescing Operator

This is the shorthand of the ternary conditional if operator x ? x : y

string pageTitle = suppliedTitle ?? "Default Title";

In JavaScript the OR operator || has the same behavior as the above. It returns the first operand if it evaluates to true, otherwise, it returns the second operand. When the left hand side is true, the right hand side is not even evaluated; it is "short-circuited."

 let pageTitle = suppliedTitle || "Default Title"

Lambda

This is also known as anonymous function, function literal, lambda abstraction, or lambda expression.

const hello = (name) => `Hello ${name}`

You can do a lot of things with this construct. Currying, closures, high order functions etc.

Auto Implemented Properties

In C# auto-implemented properties make property-declaration more concise when no additional logic is required in the property accessors.

public string Name { get; set; }

In C# 6 and later, you can initialize auto-implemented properties similarly to fields:

public string FirstName { get; set; } = "Jane"; 

You can learn more about automatic properties here

Async/Await

The async/await pattern is a syntactic feature of many programming languages that allows an asynchronous, non-blocking function to be structured in a way similar to an ordinary synchronous function.

C#

public async Task<int> FindPageSize(Uri uri) 
{
    byte[] data = await new WebClient().DownloadDataTaskAsync(uri);
    return data.Length;
}

JS

async function createNewDoc() {
  let response = await db.post({}); // post a new doc
  return await db.get(response.id); // find by id
}

Intersection / Union Types

In TypeScript, the intersection type combines types, so that you can have all properties and members of both types on your particular object or method.

target: string & Element;

Union types, meanwhile, are an either/or scenario. Instead of combining types to receive all members and properties, we use union types to specify that a method accepts either of several different types.

target: number | string;

JSON Serialize / Deserialize

JavaScript makes it very easy for us to serialize and deserialize JSON.

const obj = { name: 'Aivan' }
JSON.stringify(obj) // serialize object to JSON string

const str = `{name : "Aivan"}`
JSON.parse(str) // deserialize string to object 

Template Literals

Template literals are string literals allowing embedded expressions. You can use multi-line strings and string interpolation features with them.

`string text 
   ${expression} 
 string text`

Destructuring and Spread Operator

The destructuring assignment syntax is a JavaScript expression that makes it possible to unpack values from arrays, or properties from objects, into distinct variables.

var a, b, rest;
[a, b] = [10, 20];
console.log(a); // 10
console.log(b); // 20

[a, b, ...rest] = [10, 20, 30, 40, 50];
console.log(a); // 10
console.log(b); // 20
console.log(rest); // [30, 40, 50]

({ a, b } = { a: 10, b: 20 });
console.log(a); // 10
console.log(b); // 20

({a, b, ...rest} = {a: 10, b: 20, c: 30, d: 40});
console.log(a); // 10
console.log(b); // 20
console.log(rest); // {c: 30, d: 40}

This is taken from the MDN

Object Property Value Shorthand

Instead of writing your objects like this:

const name = 'Aivan'
const obj = { name: name } // { name: 'Aivan'}

You can write it like this. This is possible if the name of the key and the variable name of the value is the same.

const name = 'Aivan'
const obj = { name } // { name: 'Aivan'}

Annotations / Decorators

Decorators provide a very simple syntax for calling higher-order function. It also allows for a cleaner syntax for applying wrappers around your code, resulting in something that detracts less from the actual intention of what you’re writing.

@Wove()
class DataMapper {
  // ...
}

You can learn more about decorators here.

Default Parameters

Default parameters allow named parameters to be initialized with default values if no value or undefined is passed.

function multiply(a, b = 1) {
  return a * b;
}

// you can call it like this
multiply(1) // 1
multiply(2, 2) // 4

Posted on by:

geocine profile

Aivan Monceller

@geocine

Building frontend applications, web APIs, system utilities and development tools

Discussion

markdown guide
 

List Comprehensions in Python is an interesting one. I absolutely love them in some cases, and despise them on others.

# Simple and elegant...
squares = [n**2 for n in numbers]


list_a = [...]
list_b = [...]
# ...not on this one IMO, I'd prefer loops instead.
different_num = [(a, b) for a in list_a for b in list_b if a != b]
 

It's my first time to see such syntax since I haven't really touched Python. Looks interesting , I couldn't understand what the last line does though.

 

The last line is:

for a in list_a:
    for b in list_b:
        if a != b:
            different_num.append((a, b))
 

Most languages have something nice, but here are a few random things I like:

  • Left/right sections in Haskell: (2+) This is essentially a function that adds 2 to its — anonymous — argument:
map (2+) [1..10]
=> [3,4,5,6,7,8,9,10,11,12]
  • Threading/pipe macros and operators like Clojure's ->, ->> and |> in Elixir and F# since they allow to rewrite expressions in terms of data flow instead of function application order.
iex> "Elixir rocks" |> String.upcase() |> String.split()
["ELIXIR", "ROCKS"]
  • Erlang's bit syntax, which allows pattern matching on binary data, like the following pattern that describes the parts of an MP3's id3 tag and extracts it into relevant variables (see in use here):
<<Tag:30/binary,Artist:30/binary,Album:30/binary,Year:4/binary,Comment:30/binary,Genre:1/binary>>)
  • CommonLisp's format macro, even though it always scared me a bit. There are even format strings for converting numbers to ordinals or Roman numerals:
(format nil "~:r" 1234) ==> "one thousand two hundred thirty-fourth"
(format nil "~@r" 1234) ==> "MCCXXXIV"
  • Not really syntax, but MicroPython's asm_thumb annotation is super handy for writing parts of your application in ARM assembly.
@micropython.asm_thumb
def asm_add(r0, r1):
    add(r0, r0, r1)

print(asm_add(1, 2))
 

All these things almost look alien to me. I haven't done any heavy functional programming and let alone use a language that was specifically built for such purpose. This is something I'll definitely look into.

 

Pattern matching like you see in OCaml variants (ReasonML, F#, etc.)

match x with
| 0 -> "Nothin\'"
| 1 -> "One"
| 2 | 3 -> "Deuce or trey!"
| i when i < 10 -> "Still a single digit!"
| _ -> "Big stuff!"

Elixir has something similar in guards:

def f(0), do: "Nothin\'"
def f(1), do: "One"
def f(x) when x == 2 or x == 3, do: "Deuce or trey!"
def f(x) when x < 10, do: "Still a single digit!"
def f(_x), do: "Big stuff!"
 

+1 for Guards.

Also I really enjoy Pipes, like

my_data
|> dedupe
|> reverse
|> skedoodle

instead of

skedoodle(reverse(dedupe(my_data)))

I think they are even coming to JS.

 

I'm not sure if I like it really, and I think its an anti-pattern in almost all cases, and some of it can be done via reflection in most languages, and I don't know the name for it...

This BS in PHP:

class Foo {
    public function bar() : int {
        return 42;
    }
}

$functionName = 'bar';
$foo = new Foo();
echo $foo->{$functionName}(); //42
$instanceName = 'foo';
echo $$instanceName->bar(); //42
echo $$instanceName->{$functionName}(); //42

$variableName = 'blackMagic';
$$functionName = 'whut?';
echo $blackMagic; //'whut?'
 

This comes from function pointer in C. You can create a function pointer, like this:

void (* init_sgbd_ptr)(void);
void (* connect_sgbd_ptr)(const char* const);

Let's say that we have an application which could connect to either MySQL or PostgreSQL. In a config file, the software could detect which DBMS is used, and then link all the DBMS interface correctly. Like this

// PostgreSQL interface
void postgres_init(void) { ... }
void postgres_connect(const char* const dsn) { ... }

// MySQL interface
void mysql_init(void) { ... }
void mysql_connect(const char* const dsn) { ... }

So, in the DBMS manager module:

void init_dbms(dbms_type_t dbms_type)
{
    switch (type) {
    case DBMS_TYPE_POSTRESQL:
        init_sgbd_ptr = postgres_init;
        connect_sgbd_ptr = postgres_connect;
        break;

    case DBMS_TYPE_MYSQL:
        init_sgbd_ptr = mysql_init;
        connect_sgbd_ptr = mysql_connect;
        break;
    }
}

This is basically what is happening here. Since PHP is VERY weakly typed, any variable can be either an integer, float, string, boolean, a function pointer. In fact, they are all of those types. It is when you use them that they fall into the intended one #quantumTyped.

 

Kotlin's function literals with receiver and infix functions are pretty cool.

Function literals with receivers:

class HTML {
    fun body() { ... }
}

fun html(init: HTML.() -> Unit): HTML {
    val html = HTML()
    html.init()
    return html
}

html {
    body()
}

Infix:

infix fun Int.shl(x: Int): Int { ... }

// calling the function using the infix notation
1 shl 2

// is the same as
1.shl(2)
 

Of those, async/await is certainly my favourite. It makes asynchronous programming so wonderfully obvious. I'm not too big a fan of decorators, because they hide underlying logic, making code less readable, if you're not handling them with care.

Even though it's not yet a native language feature in JavaScript or TypeScript, Observables (reactive programming style) are on their way to become one. Until then, we're fine with rxjs.

I also like the unique concept of borrowing in Rust and I like its traits, which allow for a lot of flexibility in the type system.

 

I was thinking of adding traits here but I have not used it extensively while I was doing PHP. The same is true with it though I believe that it should be handled with care.

I'll definitely checkout Rust.

 

I would say variadics, multiple returns, and interfaces in Go.

 

golang's "empty" switch statement (a cleaner else-if).

switch {
case cut == true:
    return superReducedString(result)
case result == "":
    return "Empty String"
default:
    return result
}
 

The way switch case work on golang alone is already interesting 😁

 

It has been a while since I've used C#, but I remember I really liked LINQ queries and local anonymous types that can be type-checked on compilation