4 ms·
Or just treat id as whatever and pass it on the service. If it quacks like a duck, it's a duck.
by dalore 9y ago
Or just treat id as whatever and pass it on the service. If it quacks like a duck, it's a duck.
- pjmlp 9y agoThe problem is when it doesn't quack and screams instead.
- dalore 9y agoYep, but you can catch that and say sorry the id is in the wrong format. Easier to ask for forgiveness then permission.
- brazzy 9y agoThat's wrong on so many levels.
- pjmlp 9y agoI rather have the compiler do that for me.
- hasenj 9y agoThat's one of the biggest pain points. You look at code and you have no idea what is what. You have to be on your toes ALL THE TIME! Even when you are editing a method on a class you cannot safely assume that all the properties will exist because someone can come along and create an instance of the class in a non-standard way such that a property you expect to always exist will not exist in that one situation.
- wyldfire 9y ago> someone can come along and create an instance of the class in a non-standard way shrug just document the interface and stick to it. If you want to keep things simple, create data structures using namedtuple. You get really sane objects that are definitely POLA [1] compliant. > You look at code and you have no idea what is what. You should consider type hints [2] and/or docstrings [2]. [1] https://en.wikipedia.org/wiki/Principle_of_least_astonishment https://en.wikipedia.org/wiki/Principle_of_least_astonishmen... [2] https://docs.python.org/3/library/typing.html https://docs.python.org/3/library/typing.html [3] https://docs.python.org/devguide/documenting.html https://docs.python.org/devguide/documenting.html
- hasenj 9y agoOur project at work is using 3.4 and full of legacy code, and typical python code always has something astonishing. I always use type hints when I write new code. Because 3.4 has no type hints for class member declarations I end up writing constructors that take parameters then assign them to self. This way PyCharm will properly detect the type of class members.
- hasenj 9y agoYou don't even know whether it quacks or barks or what.
- ProblemFactory 9y agoBut you need to get the "id as whatever" first. Let's say you get the id from a HTML form as a string, and want to pass it to the library. Do you need to first convert it to an int? A uuid.UUID object? A custom ID class in the library? Is passing None allowed? A type declaration on the get_product() function would tell you, and let the compiler to check your code for mistakes.
- occams_chainsaw 9y agoIf they didn't document it, and aren't handling input for an ID that may be passed as an integer, then your problem is with the API, not the language.
- wruza 9y agoAnd if it quacks like a duck only nine times of ten, you’re outta luck.