Click here to Skip to main content
15,867,986 members
Articles / Programming Languages / Ruby
Tip/Trick

The Dangers of Duck-Typed Languages

Rate me:
Please Sign up or sign in to vote.
4.73/5 (10 votes)
22 Apr 2016CPOL2 min read 14.2K   3   7
A short look into what you need to be aware of when using duck-typed languages

Try these examples yourself in repl.it.

First, is the ambiguity of what something is. For example, consider this Python example:

Python
<code>> a=[] </code>

We have no idea what "a" is an array of. Now, many people will say, it doesn't matter, you're not supposed to know, you just operate on the array. There is a certain point to that, which however can lead to trouble.

Let's try this:

Python
> a=[1, 2, 3]

Ah, so you think we have an array of integers? Think again:

Python
> a.append('4')
> a
[1, 2, 3, '4']

Whoa! That's an array of mixed types. In some ways that's cool, in other ways, that's dangerous. Let's say we want to add one to each element in the array, and we trust that the programmer that created / modified the array knows that it is supposed to be an array of ints. But how would they know? Someone else can come along and not realize that they're appending the array with a string. So now we come along, expecting a happy array of ints, and do this:

Python
> [x+1 for x in a]
TypeError: cannot concatenate 'str' and 'int' objects

Oops - we get a runtime error!

What happens in Ruby:

Python
> a=[1, 2, 3, '4']
[1, 2, 3, "4"]
> a.map {|x| x+1}
no implicit conversion of Fixnum into String

What happens in Javascript:

Python
> a=[1, 2, 3, '4']
[1, 2, 3, '4']
> a.map(function(x) {return x+1})
[2, 3, 4, '41']

Holy Cow, Batman! In JavaScript, the string element is concatenated!

What does this mean?

It means that, among other things, the programmer must be defensive against, not necessarily the errors (sorry, I meant "usage") of other programmers, but certainly the lack of strong typing in the language. Consider these "solutions":

Python

Python
> [int(x)+1 for x in a]
[2, 3, 4, 5]

Ruby

Ruby
> a.map {|x| x.to_i + 1}
[2, 3, 4, 5]

JavaScript

JavaScript
> a.map(function(x) {return parseInt(x)+1})
[ 2, 3, 4, 5 ]

Of course, if you have a floating point number in the array, it'll be converted to an integer, possibly an unintended side-effect.

Another "stronger" option is to create a class specifically for integer arrays:

Python

Python
class IntArray(object):
  def __init__(self, arry = []):
    self._verifyElementsAreInts(arry)
    self.arry = arry

  # support appending to array.
  def __add__(self, n):
    self._verify(n)
    self.arry.append(n)
    return self

  # support removing element from array.
  def __sub__(self, n):
    self._verify(n)
    self.arry.remove(n)
    return self

  def _verifyElementsAreInts(self, arry):
    for e in arry:
      self._verify(e)

  def _verify(self, e):
    if (not isinstance(e, int)):
      raise Exception("Array must contain only integers.")


# good array
a = IntArray([1, 2, 3])
a += 4
print(a.arry)
a -= 4
print(a.arry)

try:
  a += '4'
except Exception as e:
  print(str(e))

# bad array
try:
  IntArray([1, 2, 3, '4'])
except Exception as e:
  print(str(e))

With the results:

Python
[1, 2, 3, 4]
[1, 2, 3]
Array must contain only integers.
Array must contain only integers.

What this accomplishes is:

  1. Creating a type checking system that a strongly typed language does for you at compile-time
  2. Inflicting a specific way for programmers to add and remove items from the array (what about inserting at a specific point?)
  3. Actually doesn't prevent the programmer from manipulating array directly at least in Python, which has no way of designating member attributes as protected or private.
  4. JavaScript? It doesn't have classes, unless you are using ECMAScript 6, in which case, classes syntactical sugar over JavaScript's existing prototype-based inheritance.

The worst part about a duck-typed language is that the "mistake" can be made but not discovered until the program executes the code that expects certain types. Would you use a duck-typed language as the programming language for, say, a Mars reconnaissance orbiter? It'll be fun (and costly) to discover a type error when the code executes that fires up the thrusters to do the orbital insertion!

Which is why developers who promote duck-typed languages also strongly promote unit testing. Unit testing, particularly in duck-typed languages, is the "fix" for making sure you haven't screwed up the type.

And of course the irony of it all is that underlying, the interpreter still knows the type. 

It's just that you don't.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Architect Interacx
United States United States
Blog: https://marcclifton.wordpress.com/
Home Page: http://www.marcclifton.com
Research: http://www.higherorderprogramming.com/
GitHub: https://github.com/cliftonm

All my life I have been passionate about architecture / software design, as this is the cornerstone to a maintainable and extensible application. As such, I have enjoyed exploring some crazy ideas and discovering that they are not so crazy after all. I also love writing about my ideas and seeing the community response. As a consultant, I've enjoyed working in a wide range of industries such as aerospace, boatyard management, remote sensing, emergency services / data management, and casino operations. I've done a variety of pro-bono work non-profit organizations related to nature conservancy, drug recovery and women's health.

Comments and Discussions

 
QuestionWAT Pin
Chris Maunder2-May-16 13:56
cofounderChris Maunder2-May-16 13:56 
AnswerRe: WAT Pin
Marc Clifton2-May-16 15:03
mvaMarc Clifton2-May-16 15:03 
GeneralMy vote of 5 Pin
johannesnestler2-May-16 4:38
johannesnestler2-May-16 4:38 
GeneralMy vote of 5 Pin
Member 1236439025-Apr-16 21:10
Member 1236439025-Apr-16 21:10 
GeneralMy vote of 5 Pin
dmjm-h25-Apr-16 18:00
dmjm-h25-Apr-16 18:00 
GeneralMy main issue with "duck typing" is Pin
PIEBALDconsult22-Apr-16 17:00
mvePIEBALDconsult22-Apr-16 17:00 
GeneralRe: My main issue with "duck typing" is Pin
Marc Clifton26-Apr-16 6:35
mvaMarc Clifton26-Apr-16 6:35 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.