Here's one that's been on my mind that I haven't seen people talk about: making O(1) and O(n) operations look the same.
Example in Python:
d = { ... }
for ...
if 'key' in d:
func()
L = [ ... ]
for ...
if 'key' in L:
func()
Python is my favorite language, and I teach Python classes, but the number of both beginner and production programs I've seen with quadratic blowups because of this issue is crazy.
I think a lot of the problem is the subtle syntax. People with a solid CS background find this distinction fairly natural, but most Python programmers apparently don't.
I guess the alternative syntax would be L.find(), which sort of emphasize the linear search. It seems WAY more elegant to have the polymorphic "in", but the number of mistakes is too high.
Actually now that I think about it, this was question #0 on the stripe CTF3. So I guess the same thing happens often enough in Ruby for them to use it as a question.
I see this as ultimately being a much better from an optimization standpoint. Now I can change L to a set or a linked list if I find that more efficient without having to change it everywhere. Language features are no substitute for algorithm and data structure knowledge, which your students can likely be forgiven for not knowing about.
I don't buy it. Physically editing text is never the problem when changing a data structure like this. The work is ensuring your program is still correct and performs better.
Ensuring code is "still correct and performs better" is made easier if only the data structure used has to change to get "performs better", and the "still correct" comes free with it because the operation that performs the same function is the same operation regardless of the efficiency of the algorithm.
Right. And this kind of polymorphism means that your program will still retain the same correctness no matter what data structure you use. Physically editing text is the most correctness-destroying thing you can do.
Example in Python:
Python is my favorite language, and I teach Python classes, but the number of both beginner and production programs I've seen with quadratic blowups because of this issue is crazy.I think a lot of the problem is the subtle syntax. People with a solid CS background find this distinction fairly natural, but most Python programmers apparently don't.
I guess the alternative syntax would be L.find(), which sort of emphasize the linear search. It seems WAY more elegant to have the polymorphic "in", but the number of mistakes is too high.
Actually now that I think about it, this was question #0 on the stripe CTF3. So I guess the same thing happens often enough in Ruby for them to use it as a question.