4 ms·
> val player = new Player(new Vector2(100, 100), new Vector2(50, 50)) > No problems here. The first noticible crack in the system comes after a few weeks vacat
by hcarvalhoalves 13y ago
> val player = new Player(new Vector2(100, 100), new Vector2(50, 50))
> No problems here. The first noticible crack in the system comes after a few weeks vacation away from this code. You come back, and you want to change the starting point of the player. Will you remember which Vector2 to change?
player = Player(position=Vector2(100, 100), size=Vector2(50, 50))
The problem is not typing here, it's lack of named arguments.
- TheLoneWolfling 13y agoI'd disagree with you: named arguments are good, but they don't save you in a lot of cases. For example when you have one function returning (pos, size) and another function expecting (size, pos).
- hcarvalhoalves 13y agoNamed arguments is syntatic sugar for packing/unpacking data structures in some languages, so you can leverage that. In Python: >>> from collections import namedtuple >>> Vector2 = namedtuple('Vector2', ('x', 'y')) >>> PlayerOptions = namedtuple('PlayerOptions', ('position', 'size')) >>> opt = PlayerOptions(position=Vector2(100, 100), size=Vector2(50, 50)) >>> Player(**opt._asdict()) The order arguments are passed doesn't matter anymore. You can also enforce this calling convention with a constructor signature like this (in 3): >>> class Player(object): >>> def __init__(self, *, size, position):
- q845712 13y agoi agree that in languages without compiler defined types, but with other tools such as named arguments, the class of errors described here can be to some extent avoided by making good use of named arguments. I'm gonna do this more often in my python code!
- jasode 13y agoNamed arguments by themselves do not fundamentally prevent data type errors when the underlying types are identical. Short variable names (v1, v2) or some other cosmetic ambiguity will deceptively compile with named arguments as the only defense. Both of the following would compile successfully using named arguments but one of them is semantically wrong: Player(pos = v1, size = v2); Player(pos = v2, size = v1); Using a compiler's static type checking enforces correct semantics more than named arguments. Of course, this only works if the programmer creates new differentiated types so that they are no longer identical (which was the crux of the article.)
- felipehummel 13y agoAlthough named arguments are good, you won't use it in all function calls.
- judk 13y agoThat's tiresomely verbose.