Something I've noticed from mentoring approximately 400,000,000 solutions to 'Raindrops' is that a couple of things in Exalysis's output for this exercise can cause confusion.
First, we say:
- When you use
+= to append to an empty variable, you could replace it with just = instead. But where you have several almost identical cases, some programmers would prefer to keep the code the same for each case, even if that's not quite optimal: readability beats performance, most of the time.
A lot of students don't understand this. Some take it as the mentor requesting that they should change the first += to an =, so they do. Personally, I think this makes the code less maintainable, because instead of three identical code blocks, we now have two the same and one different, in exchange for a tiny performance improvement. That's the exact opposite of the advice I usually give students, which is to focus on readability first, and only start optimizing when the code has proved to be too slow in production.
The slightly deeper problem with this is that (in a very early core exercise) it subconsciously trains the students that what we're really looking for is absolute performance. The message is that they should tweak and twiddle every line of code, benchmarking it to death until they've wrung out the last nanosecond. This leads to some over-engineered and overcomplicated solutions all the way down the track.
Secondly, if the student is using a strings.Builder or a bytes.Buffer, we say:
- I see you are using a
%s here. That works, but it's probably overkill for this exercise. The string append operator += is faster and uses less memory. You can run the benchmarks as described in the exercise instructions, and check that for yourself.
I get a lot of puzzled students asking why this is, and which is better to use in the general case, strings.Builder or +=. Obviously strings.Builder is what you should use 99% of the time, and it's only not the fastest option here because we have an unrealistically small number of appends. I think sending students down this benchmarking rabbit-hole creates a lot of confusion, and effectively penalises them for their superior knowledge of the standard library. Additionally, as before, it sends the message that everything in Go programming is about performance optimization.
I suggest that we drop the advice about changing += to =, and don't mention strings.Builder either. If they use it, great. If not, that's fine too. This is such an early exercise I think it's important not to overload students with too many concepts.
Something I've noticed from mentoring approximately 400,000,000 solutions to 'Raindrops' is that a couple of things in Exalysis's output for this exercise can cause confusion.
First, we say:
A lot of students don't understand this. Some take it as the mentor requesting that they should change the first
+=to an=, so they do. Personally, I think this makes the code less maintainable, because instead of three identical code blocks, we now have two the same and one different, in exchange for a tiny performance improvement. That's the exact opposite of the advice I usually give students, which is to focus on readability first, and only start optimizing when the code has proved to be too slow in production.The slightly deeper problem with this is that (in a very early core exercise) it subconsciously trains the students that what we're really looking for is absolute performance. The message is that they should tweak and twiddle every line of code, benchmarking it to death until they've wrung out the last nanosecond. This leads to some over-engineered and overcomplicated solutions all the way down the track.
Secondly, if the student is using a
strings.Builderor abytes.Buffer, we say:I get a lot of puzzled students asking why this is, and which is better to use in the general case,
strings.Builderor+=. Obviouslystrings.Builderis what you should use 99% of the time, and it's only not the fastest option here because we have an unrealistically small number of appends. I think sending students down this benchmarking rabbit-hole creates a lot of confusion, and effectively penalises them for their superior knowledge of the standard library. Additionally, as before, it sends the message that everything in Go programming is about performance optimization.I suggest that we drop the advice about changing
+=to=, and don't mentionstrings.Buildereither. If they use it, great. If not, that's fine too. This is such an early exercise I think it's important not to overload students with too many concepts.