Writing static typed style code in dynamic type language

By | February 22, 2016

I am not sure who else is doing this but I have a static typed language background and I tend to write code in static type style.

Don’t assign different value types to the same variable

Let’s see what dynamic type in the simplest form in Python:

While this is perfectly legitimate, but it’s perfectly unacceptable, the rule of thumb is, you never assign a different type of value to a already defined variable.

This way, when someone reads your code, one can safely assume result is always a ‘String’ type, seriously this will help a lot.

Don’t return different types of value in a function

Consider the following example, one would need to understand your condition in order to determine the type of result, also makes it difficult to consume this function.

Define your class members at the top of the class

While this is perfectly legitimate:

But it’s difficult to work out what class members at a glance, please define them.

Now you know this class has a class member without reading the work function.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.