Leaning on the compiler
I run into this post, claiming that typed languages can reduce bugs by 15%. I don’t have a clue if this is true, but I wanted to talk about a major feature that I really like with typed languages. They give you compiler errors.
That sounds strange, until you realize that the benefit isn’t when you are writing the code, but when you need to change it. One of the things that I frequently do when I’m modifying code, especially if this is a big change is to make a change that will intentionally cause a compiler error, and work my way from there. A good example of that from a short time ago was a particular method that return an object. That object was responsible for a LOT of allocations, and we needed to reduce that.
So I introduced pooling, and the change looked like this:
This change broke all the call sites, allowing me to go and refactor each one in turn, sometimes pushing the change one layer up in the code, until I recursively fixed all the changes and then… everything worked.
The idea isn’t to try to use the type system to prove that the system will work. That is what I have all these tests for, but the idea is that I can utilize the structure of the code to be able to pivot it. Every time that I tried to do stuff like that in a language that wasn’t strongly typed, I run into a lot of trouble.
Comments
I like it. One caveat. If your code is public and used by code outside of your solution, you may have a few more steps to achieve the same thing.
I have very mixed feelings about dynamic vs static typing. I am doing a lot of microservices which consume complex JSON-objects and spit out JSON objects. Reading / writing JSON objects in plain braindead simple in Python (json.loads(...)) and you can work with the element. In Net Core, I spend a lot of time to create DTOs for the JSON objects and to tune the JSON structure in a way, which can be turned into a C# object easily.
When the object structure changes, the change in Python is often quick and easy, while C# requires a lot of tuning until all works again.
When building something quick, Python just wins. C# wins if you want to build a library. But I think that Pythons new Typing system is a good compromise: Optional Typing for libraries.
HdS, Are you familiar with the
dynamic
keyword in C#, this allows you to selectively chose to use dynamic typing, and it is great to reduce the need in DTOs.I love compiled code for this very same reason. TypeScript has been a huge help in this regard for my browser work - refactoring & leaning on the compiler significantly reduces the cognitive load.
I agree. After working in C# for so long, it's painful to use a dynamically typed language. It feels like the dark ages.
Comment preview