Re:Why is OOP still so widely spread?
by teekert on Thursday September 03, 2020 @04:23AM
(#24361904)
Thank this is a very nice answer and it makes sense to me. Indeed I once sat down to try Kotlin for a day. The amount of code I had to write to get a json from an api into a dict that I could address by key was baffling to me. In python it took met 2 imported modules and then 2 lines (something like
I gave up after a day, when I just wrote down exactly what I expected to pull down and what types were in the values and keys. I can understand it helps you prevent errors but man is there a learning curve... BTW, I could write those checks in Python as well, but they are optional.
[ Parent
| on HN ]
Re:Why is OOP still so widely spread?
by garethrowlands on Thursday September 03, 2020 @09:13AM
(#24363690)
> I could write those checks in Python as well, but they are optional
You're probably going to need to check `value_I_want` though. So maybe the checks aren't quite as optional as they look.
Checks in Python are pretty concise. If you just want to have a default value if the key my_value doesn't exist, this is all you need:
value_I_want = some_dict.get('my_value', default_value)
If you want a more strict check including type checking: value_I_want = some_dict.get('my_value')
if type(value_I_want) is not str:
do_something()
In Java the real problem is nested JSON objects -- it's a real PITA to read those. You can't do something like this (Python): value = some_dict.get("foo", {}).get("bar", {}).get("blah", default_value)