Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

There is a function. It takes in 4 parameters. One of them is called ID

Is ID a string, a number, a GUID? Better check the usage within the function.

Oh, the declaration is `id: number`

Mystery solved.

Even better if the language supports subtyping so it is something like id: userID and userID is a subtype of number.



In a dynamically duck typed language it should not matter if an ID is a string, a number or a GUID. The code should work with all of them. The semantically important thing is that this is an identifier. No String, number or GUI data type expresses this true meaning of the value.


It matters a lot even in a duck typed language.

If there are multiple types of user IDs, I don't want to pass the wrong one into a DB call.

This is often the case when dealing with systems that have internal IDs vs publicly exposed IDs. A good type system can correctly model which I have a hold of.

For complex objects proper typing is even more important. "What fields exist on this object? I better check the code and see what gets accessed!"

Even worse are functions where fields get added (or removed!) to an object as the object gets processed.

Absolute nightmare. The concept of data being a black box is stupid, the entire point of data is that at some point I'll need to actually use it, which is a pain in the ass to do if no one ever defines what the hell fields are supposed to be laying around.


By naming the variable ID it is crystal clear what the value is. Most of the time an explicit type only adds cognitive load to the reader, and limits the universality of the code. At an high abstraction level, most of the time a type is from a program logic point of view an irrelevant machine implementation detail. If a specific duck is required it is explicitly tested. This makes code very clear when the duck type is important and when not.


That's how you get a stray string in a column of integers.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: