by jacobsenscott · 4y ago · 0 comments
def add_item_to_cart(item)
vs void add_item_to_cart(IItem item)
They are equally easy to understand. The first is easier to read.I guess you now need to read through the implementation or docs. The first is much easier to read incorrectly.
You've never worked with vaguely named variables? What you are suggesting is guessing the data type based off the name.
- What type of object? The type that can be added to a cart.
Okay sure, but what precisely is that?
- Nobody just throws random objects at a function.
I couldn't agree more - so the follow up question is what is the fastest way to get familiar with what type of input or output this function returns?
- They are familiar with the code in general and they know what to do.
For very small projects with very small teams after some onboarding time perhaps, but outside of this I would disagree.
Code changes over time, parts that you use to know intimately get changed subtley and erode knowledge away. Having types in place highlights these changes if your assumptions are incorrect.
It doesn't matter "what precisely" is the the thing that you are adding to the chart, and a static type system won't tell you that either. There could be be any of 1000 things that implement IItem. And probably half those things just throw exceptions for methods they aren't actually able to implement.
addItemsToCart(items)
Vs Type ItemCode: string;
Type ItemDetails = {...};
addItemsToCart(items:ItemCode[])
or, for a slightly different implementation: addItemsToCart(items:Record<ItemCode, ItemDetails>)
If you only use trivial examples, types seem silly. But in real examples they become more useful. In this case looking at the function signature give you immediate information about the implementation that is missing from the untyped version.EDIT: please excuse formatting, I'm on mobile and cannot get it to add spaces before the last code block
You need the same tests from a typed system in a non-typed. You _don't_ need all the tests from a non-typed system in a typed system.
Writing tests to enforce types just hand-rolls a type system, in my experience.
You do though, because invariably people violate the LSP and just "throw Unimplemented" in the methods required by the interface they can't figure out how to implement. In other words all system are duck typed in reality.
You can do that with Python (sometimes) because many libraries have type hints today, so even if you don't use types yourself, the type checker can infer them in your code and help you out.
The same way Rust checks for object lifespans with the borrow checker, which is distinct from the compiler and type system.
The same way valgrind for C can check for use after free.
The same way errorprone can look for null checks in Java.
This is a well tested and proven technique. Static code analysis is a staple of the industry, when it comes to automated code analysis.
void add_item_to_cart(auto item)
will still statically verify that item has the required syntax; C++ is very poor on this aspect on only doing the verification at instantiation time, languages with more sophisticate typing systems can infer the correct type from tome add_time_to_cart definition alone.