by breadwinner · 3y ago · 0 comments
var fireable = someMethod();
firable.fire();
The programmer intended this code to fire an employee. Let's say this code is in a military application, and another programmer modified the someMethod() function to return a missile. As long as the missile object has a fire() method this code will compile just fine... and do something the code didn't intend to do, i.e., fire a missile!How likely are you to have employee firing and missile firing in the same program? Not very likely, but the principle is valid, nonetheless. You need to express your intention more clearly like this:
Employee fireable = someMethod();
Now if someMethod() is modified to return a missile you get a compilation error, and the world will be a lot safer. You don't want missiles being fired by accident!"Type inference" is what you're referring to, and is an optional feature of static type checkers. For instance, C has static types but no type inference, while C++ has static types with type inference. The concept of "type inference" doesn't make sense for dynamic typing.
Dynamic typing and type inference are not synonymous.
Actually anyone arguing against that i don’t even think it’s worth my breath since you likely just don’t have much experience and really like whatever your pet language is
It isn't applicable because there are no static types to infer.
Type inference can in some cases lead to type confusion in a way that can also happen with dynamic types. In my experience it is extraordinarily rare, especially in languages with only limited or local type inference like Rust or C++.
* Dynamic typing: types are not known at compile time
* Static typing: types are known at compile time and written down in the source code.
* Static typing with type inference: types are known at compile time and inferred rather than being explicitly written down
* Dynamic typing with type inference: nonsensical; not a thing
* Type introspection: types can be inspected by code at runtime. Unrelated to any of the above. Can be found (or not found) in both dynamically typed and statically typed languages.
https://twitter.com/tef_ebooks/status/1108101434141298695
I suspect many people didn't get past the first five words.
That said, I agree with you that the point is more legitimate, but I don't think it's a particularly strong argument.
val underperformingEmployee = selectLowestPerformingEmployee();
which can be reduced to selectLowestPerformingEmployee().terminateEmployment();
But there are already a lot of strange state management issues with this code style. I would expect something more like this in the wild terminateWorstEmployee(){
var employee = hrService.lookupEmployeeByLowestPerformanceReview();
employeeTerminationService.initiateTermination(employee.id);
}
When things are named and abstracted properly, it should be very clear what is happening. var conventions won't save you here.Assignment from method calls seems to be the most likely place where this could POSSIBLY cause confusion. Maybe we can compromise on variable assignment directly from constructors? That seems to be the clearest example where the extra text is purely noise. It also causes a lot of line wrapping which indirectly causes even more noise.