In the late 90s I was asked to teach an "introduction to C for programmers" course at a large Swedish company. Turned out that my definition of programmer was quite different than that of the company.
I expected a group of peers that never had developed in C. But the class was full of people with no computer science education at all. If I remember correctly most of them were manual testers that knew how to do minor changes to see if it fixed the problem.
One thing I remember most from this week was when I had to explain to the class that parameters sent to a method did not have to have the same name as in the declaration of the method. Let me explain that again; the class was convinced that if a method Foo took one argument named bar, the calling code had to have a variable called bar or it would not work. At the time I found that assumption really "out there".
But I recently thought about this and figured maybe it would be a good idea. The same way I have explained before that using explicit types rather than primitive types can save the day, forcing callers of a method to do so by naming arguments would actually help you and reduce the risk of using the wrong value in a method.
Consider a function AverageCost(int items, int totalCost). If you, in the calling method, have two variables sum and count it would be easy to make a mistake and make the call look like this: AverageCost(sum, count). While using explicit types would be the best solution, what if your programming language forced you to use the same names in your calling code as the method you try to call?
Obviously it would soon become cumbersome as different methods would use slightly different names for the same thing so now you need to create new variables just to rename them. An alternative would be to only allow named (or explicit) parameter passing. Then the call would look like this: AverageCost(items = count, totalCost = sum). That is probably a much better idea than only using the name of the variable passed in.
So all in all, using explicit types (as described in the link above) is in my opinion to prefer when you work in a a language with type safety. If you don't have type safety I kind of like the idea of only allowing named parameter passing. Not sure if there is a (fairly common) language out there that would enforce that. Let me know if you know of one. Or is it time to create a new language or compiler plugin?
No comments:
Post a Comment