Avalon A Programmer Blog

Comparing new languages that I learnt

The first half of 2017 has past. I learnt several new languages: Python, Swift and Kotlin. Despite I only experience them for a short period of time, I am going to talk about them and compare to the languages that I familiar with (i.e. C#, Java).

Python

Python is a scripting language that become very popular recently. I learnt this because I want to write a Plex agent.

My first impression to Python is the strict formatting is stupid. It can enforce nice format and eliminate the nested brackets but it also make it hard to copy-paste code and edit with text editor (incorrect space count). The problem that it tries to solve can be solved by IDE auto formating and good programming practice. The following cannot be run because there is accidentally one more space at the front.

num = 1
 if num < 3:
     print(f"{num} is smaller than 3!")

*args and **kwargs are the evil in Python. This is like passing a map in JavaScript. You won’t know what is accepts without read the code or the documentation . It hides the parameters of a function. While reading documentation is important but it should not make others to reading documentation to confirm the signature of a function.

Another annoying thing about Python is self. Why do I need to use self to access member functions and variables when there is no other functions and variables that have the same name. The interpreter should know what it tries to call. Why would member function need to include self in parameter when adding static for non-member function is easier because member functions are way less than member function.

The most unbelievable thing is Python allowing override EVERYTHING including how to access a variable. Also, this make there is no way to enforce a contract because anything can be change and removed.

The only reasons that I still use python because it can run on different platform with the same script and can be edit easily.

Some says Python is good for beginner. I think it is bad for them. No only Python allow many hack Pythonic way to write Python which can lead to bad habit, Python also have poor documentation for most libraries including official. There are usually no type hint in code and documentation and writing type hint is very verbose. You need to import a library and sometimes need to declare the type before use it!?

I would only use Python for short script to keep my sanity.

from typing import Sequence, TypeVar, Union


T = TypeVar('T')      # Declare type variable

def first(l: Sequence[T]) -> T:   # Generic function
    return l[0]

def handle_employees(e: Union[Employee, Sequence[Employee]]) -> None:
    if isinstance(e, Employee):
        e = [e]

Swift

Swift is a programming language developed by Apple for iOS, macOS and etc. It is designed to work with Cocoa and Objective-C.

Swift is the first language that I learnt which enforce null safety. A variable has to be defined as nullable before you can assign null to it. Also, you have to check if it is null before unwrapping it.

var nonNullString: String = nil // nil cannot be assigned to type
var nullString: String? = nil // OK

Also, it provides extension methods. Extension methods allow you to add extra functions to classes and interfaces without write add to it. I found that is quite useful when I was writing C#. For example you can add utilities based on the interfaces. All interfaces are now able to the same thing based on there contract.

However, there are things that I do not like about Swift. Protocols in Swift are very similar to interface but it does not support generic. Why would classes and functions support generic but not the interface? While there is associatedtype in protocol, you cannot problem against it.

protocol ApiMapperProtocol {
    associatedtype T
    associatedtype U
    func MapFromSource(T) -> U
}

class UserMapper: ApiMapperProtocol {
    typealias T = NSDictionary 
    typealias U = UserModel

    func MapFromSource(data:NSDictionary) -> UserModel {
        var user = UserModel()
        var accountsData:NSArray = data["Accounts"] as! NSArray
        return user
    }
}

No only you cannot apply any constraint on the generic type, it is just delegate the generics to a concrete class. So, you would not be able declare a variable as IList<String> in Java.

Another thing is the implementation of the official library. For example, UITableView has a lot of method call tableview but the only difference is the parameter names.

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell 
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) 

Why would you make all the functions the same name in a protocol. It makes it hard to guess the purposes of a function as the parameter name describe the parameter than the function.

If this is not the language the write iOS, I do not even want to use it.

Kotlin

Kotlin is a language built on top of JVM but it now able to compile to other languages as well. Like Swift, it provides null safety and extensions methods. It also fixes many problems in Java.

You may not want to use a new language designed by a company but it has been adopted for up coming Android development. Also, it is compatible with Java. It means you can call Java method from Kotlin and vice versa.

One of things that it cannot be fixed is it does not have generics information after compile like Java which means it cannot implement same generics interface twice with different type or use reflection to get the generics type at runtime.

However, this is still one of my favorite languages when I was writing it.