How do I teach myself to care more about writing efficient code?
-
How do I teach myself to care more about writing efficient code? Coming from math I am happy if it simply always works and if you can read it and know why. But I would like to set a better example for my students. Unlike the math profs who taught me.
Dead simple but more theoretical things like search optimization might be a place to start?
-
How do I teach myself to care more about writing efficient code? Coming from math I am happy if it simply always works and if you can read it and know why. But I would like to set a better example for my students. Unlike the math profs who taught me.
Dead simple but more theoretical things like search optimization might be a place to start?
@futurebird Hm.
Dead serious suggestion from someone with experience:
Don't think about efficiency as an end in itself. Think about efficiency *as it relates to a specific application*. *Why* do you want efficiency? Do you want better time efficiency? Better memory-space efficiency? Better *responsiveness* (which is very different from time effiiciency)? Do you have realtime constraints (a third layer, past responsiveness?)
The *reason* you want efficiency changes *how* you seek efficiency.
-
How do I teach myself to care more about writing efficient code? Coming from math I am happy if it simply always works and if you can read it and know why. But I would like to set a better example for my students. Unlike the math profs who taught me.
Dead simple but more theoretical things like search optimization might be a place to start?
@futurebird I'm not sure how to change yourself, but I find writing efficient code absolutely joyful - in many cases, it's like simplifying a mathematical proof to a more clear and elegant formation. There are other parts of writing efficient code that _don't_ have that same "I'm making it better and simpler" aspect, but perhaps the spot where they overlap will be one where you can find joy in it?
One of the starting points of writing efficient code is finding a good match between the data and problem you're solving and the data structures you're using to manipulate it. The right match can often be really elegant - again, in the way mathematicians use the term elegant, as well as performant. Like "I could solve this problem with a binary tree, or I just happened to notice I could use a hash table instead and make it faster", or whatnot.
Do you have some examples of code areas / applications where you wish you could care more about it? Happy to dive in.
(This is kind of my thing professionally, so, um, feel free to also say 'ok that's enough'.
)
-
@futurebird Hm.
Dead serious suggestion from someone with experience:
Don't think about efficiency as an end in itself. Think about efficiency *as it relates to a specific application*. *Why* do you want efficiency? Do you want better time efficiency? Better memory-space efficiency? Better *responsiveness* (which is very different from time effiiciency)? Do you have realtime constraints (a third layer, past responsiveness?)
The *reason* you want efficiency changes *how* you seek efficiency.
@mcc @futurebird this is a fantastic bit of advice, and I want to add an additional flavor of efficiency: Efficiency of code readability.
Sometimes you don't need the performance of a tightly coded loop, your memory usage is fine, and it runs fast enough, but you *do* want to be able to understand the code in a month when you come back after forgetting all of it
-
How do I teach myself to care more about writing efficient code? Coming from math I am happy if it simply always works and if you can read it and know why. But I would like to set a better example for my students. Unlike the math profs who taught me.
Dead simple but more theoretical things like search optimization might be a place to start?
@futurebird There are many math problems that take a long time to compute with inefficient code, but can be solved really, really fast with a better solution. Matt Parker frequently writes inexpert Python code for a math problem he's interested in and sometimes some of his viewers are interested enough to write alternate and faster solutions
https://youtu.be/c33AZBnRHks?si=-ae9e1qSUG7fyw8z -
@futurebird There are many math problems that take a long time to compute with inefficient code, but can be solved really, really fast with a better solution. Matt Parker frequently writes inexpert Python code for a math problem he's interested in and sometimes some of his viewers are interested enough to write alternate and faster solutions
https://youtu.be/c33AZBnRHks?si=-ae9e1qSUG7fyw8zOh god. I would totally run code for a month if I knew it'd do what I want. This is just the tendency I'm talking about.
-
F myrmepropagandist shared this topic
-
@futurebird I'm not sure how to change yourself, but I find writing efficient code absolutely joyful - in many cases, it's like simplifying a mathematical proof to a more clear and elegant formation. There are other parts of writing efficient code that _don't_ have that same "I'm making it better and simpler" aspect, but perhaps the spot where they overlap will be one where you can find joy in it?
One of the starting points of writing efficient code is finding a good match between the data and problem you're solving and the data structures you're using to manipulate it. The right match can often be really elegant - again, in the way mathematicians use the term elegant, as well as performant. Like "I could solve this problem with a binary tree, or I just happened to notice I could use a hash table instead and make it faster", or whatnot.
Do you have some examples of code areas / applications where you wish you could care more about it? Happy to dive in.
(This is kind of my thing professionally, so, um, feel free to also say 'ok that's enough'.
)
It's dead simple things like finding 5 letter words that meet all of the conditions thrown up by wordle results.
I like this problem for my students, but it shouldn't take 10 seconds to do that. Even with a list of tens of thousands of words and a few dozen conditions.
And I ... know what some of the problems are: it's more intuitive to make a list of invalid words then remove them from the valid ones.
And I should use "continue" more ...
-
How do I teach myself to care more about writing efficient code? Coming from math I am happy if it simply always works and if you can read it and know why. But I would like to set a better example for my students. Unlike the math profs who taught me.
Dead simple but more theoretical things like search optimization might be a place to start?
@futurebird From what I’ve seen, the general guidelines are to focus on clarity/readability. Everything else will become clear when it is needed. As a student knowing how to write code that I can actually understand in six months to a year after writing it seems a more useful skill.
-
@futurebird From what I’ve seen, the general guidelines are to focus on clarity/readability. Everything else will become clear when it is needed. As a student knowing how to write code that I can actually understand in six months to a year after writing it seems a more useful skill.
Well I do teach them that. They know I don't take assignments without readme.txt in the folder.
What the heck is this?
Why would I run it?
How do I use it?Explain.
-
@futurebird From what I’ve seen, the general guidelines are to focus on clarity/readability. Everything else will become clear when it is needed. As a student knowing how to write code that I can actually understand in six months to a year after writing it seems a more useful skill.
And to be clear, I teach *beginner* programmers and I don't really want them focused on efficiency. I just feel bad that I don't know as much about it mostly due to never needing to and being lazy.
So. Time to learn.
-
@futurebird From what I’ve seen, the general guidelines are to focus on clarity/readability. Everything else will become clear when it is needed. As a student knowing how to write code that I can actually understand in six months to a year after writing it seems a more useful skill.
Also a lot of the lessons I've developed are designed around getting the computer to do something the way that a person would to help them understand algorithms from math.
For example factoring quadratics. If you know it factors you can just use the formula. Done.
But, we write a program that generates the steps of the "AC Method" because it's just such a good way to learn about lists, and functions.