by Waterluvian · 9y ago · 0 comments
I had a frustrating moment the other day where a colleague said that
float(5)
was more explicit and personally preferred in Python than a literal 5.0
I realised I was being pedantic but I genuinely believed in "good craftsmanship" when I really pushed him to change it.I need to get better at this because the social friction I caused may cost more than the poor craftsmanship. But the other part of me is thinking, "c'mon man... Seriously?"
The other upside is that code reviews are higher quality and design focused.
Which is why I call it craftsmanship. It works the same for all intents and purposes, but c'mon. :)
It causes calls to strlen and potentially memory allocations. Just use the default constructor!
Compilers can do some pretty fancy things with literals because they know they optimize constants known at compile time.
"Good craftsmanship" is both subjective AND hard to relate to an increase business value. Better to stick with third-party recommendations, like textbooks or linters.
FWIW, I'm firmly on the 5.0 side.
Personally I like to go with "is accepted by the community" as opposed to "is accepted by most people in this room" or "Greg thinks it's beautiful".
Does he also prefer to right integers as 'int(float(5))'? The mind boggles, it really does.
Manager-Tools.com speaks very well to relationships being very valuable, worth more than float(5) vs 5.0... though... I really do agree. Float(5)? Really? And 5 should be replaced with IntNoReallyItJustAnInt(5)?? ;)
Then again, I'm not a software developer (even though I did write some Python scripts for my workflow in the past).
$ python -m timeit '5.0' 100000000 loops, best of 3: 0.00719 usec per loop
$ python -m timeit -n 10000000 'float(5)' 10000000 loops, best of 3: 0.0771 usec per loop
$ python -m timeit -n 10000000 '5.0' 10000000 loops, best of 3: 0.00717 usec per loop
[timeit] provides a simple way to time small bits of Python code. It has both a Command-Line Interface as well as a callable one. It avoids a number of common traps for measuring execution times. See also Tim Peters’ introduction to the “Algorithms” chapter in the Python Cookbook, published by O’Reilly.