I’m having a test soon where I’ll need to write specific pieces of code in theory from my knowledge. But I already have some of the exam problems in advance, and I’m wondering: what’s would be the most effective technique for memorizing lines of code? I am fairly new to memory techniques and training, but can’t seem to find the best option
would be more helpful if you could tell us what those
are.
I mean it really depends. Just lines of code, for example:
mid = floor(M/2);
hx_cut = hx(mid - floor(K/2) + 1 : mid + ceil(K/2));
h = hx_cut .* hamming(K)';
or
Ts = 1/fs;
P = @(tau) (tau >= 0 & tau < Ts);
y = zeros(size(t));
for k = 0:length(x)-1
y = y + x(k+1) * P( t - (t0 + k*Ts) );
end
I would break it into steps to make it more about memorizing concepts than syntax. (I don’t know MATLAB or Octave, but I asked an LLM to explain the syntax.)
To analyze, I’d start with the three main steps that were given:
midhx_cuth
I can’t remember it based on just those names, so I started breaking it down:
The hx_cut looks like it’s extracting part of an array named hx, but it’s written in a dense way: mid - floor(K/2) + 1 : mid + ceil(K/2).
You could make it easier to understand by writing those out as separate variables:
mid = floor(M/2);
start_index = mid - floor(K/2) + 1;
end_index = mid + ceil(K/2);
hx_cut = hx(start_index : end_index);
h = hx_cut .* hamming(K)';
That turns it into five steps. If needed, you could break it down even further by separating out things like hamming(K)' (transposing to a row vector to match hx_cut) into another step.
I’d then walk through it to be sure I understand what it’s doing:
- take the middle of
M - find the start and end of the slice
- cut the array
- transpose the
hamming(K)using the apostrophe - matrix multiply that with the cut
After doing that analysis of the code I can recall all the steps without using memory techniques, but if I needed it on a test, I might put the list of steps into a peg list or memory palace.
Active recall helps with memorization, so every couple of days, I would try to do it from scratch without looking at my notes.
I pasted the second example into an LLM and asked it to create better variable names:
current_amplitudetime_shiftnew_pulse_shapescaled_pulse- etc.
I’d do that before trying to memorize it, unless you already understand it well enough to remember the denser way of writing it.
What are you studying? It looks interesting.
Thank you so much, I’ll try that.
This is from a subject called: Digital Signal Processing (DSP), honestly I don’t really like it/get it that well - that’s why I am trying to memorize some of the codes haha
I think it’s much easier to remember concepts than densely written code, so I would try to understand it first.
LLMs can help. Try ChatGPT 4.1, Gemini 2.5 Pro, or whatever is available. If you’re just using the LLM for coding, GitHub Copilot is probably cheaper, but Gemini probably has a 30-day free trial. I think LLMs tend to write bad code, but they can be great for explaining code.
If you don’t understand the code, ask an LLM to draw a diagram. This is an example from Gemini 2.5 Pro with your first code snippet:
Imagine the code is performing a physical operation, like cutting a piece of ribbon and shaping its ends.
Diagram: Creating a Windowed Filter
The goal is to take a long signal hx, cut out a small piece from the middle, and then smooth the edges of that piece.
Step 1: Start with the Full Signal (hx)
First, you have a long array of data, hx, which has a total length of M. Let’s visualize it as a long sequence of values.
hx (Length M)
[value, value, value, value, value, value, value, value, value, value, value, value, ...]
Graphically, it might look like some continuous signal:
/\
/ \
...-´ `--´--´--´--´--´--...
Step 2: Cut Out the Middle Section (hx_cut)
Next, the code finds the middle of hx and cuts out a smaller piece of length K. This piece is your new variable, hx_cut.
(Length M)
hx: [................|=================|................]
| CUT |
V
hx_cut: [=================]
(Length K)
Graphically, you’re isolating just the central peak:
hx_cut
/\
/ \
...-´ `-...
This is what the line hx_cut = hx(start_index : end_index); accomplishes.
Step 3: Create the “Shaper” (hamming(K)')
Now, create a “shaper” tool. This is the Hamming window. It’s a vector of the same length K whose values are large in the middle and small at the ends, forming a smooth bell curve. The apostrophe (') just makes sure it’s oriented as a row vector.
hamming(K)' (A bell-shaped vector of length K)
(1.0)
/-----\
/ \
/ \
(small)./ \.(small)
Values: [0.08, 0.2, 0.5, 0.8, 1.0, 0.8, 0.5, 0.2, 0.08] <-- Example values
Step 4: Apply the Shaper (.*)
This is the key step. The element-wise multiplication (.*) operator multiplies each value in hx_cut by the corresponding value in the Hamming window.
You are essentially laying the “shaper” over your “cut-out piece” and multiplying them together point by point.
hx_cut: [ val1, val2, val3, val4, val5, ... ]
| | | | |
.* .* .* .* .* <-- Multiply each pair
| | | | |
hamming(K)': [ wgt1, wgt2, wgt3, wgt4, wgt5, ... ]
| | | | |
V V V V V
Step 5: The Final Result (h)
The result is a new vector, h. It has the same shape as hx_cut in the middle, but the edges are smoothly tapered down toward zero by the small values at the ends of the Hamming window.
h (The final filtered signal piece)
(Original shape of hx_cut)
- - - - /\ - - - -
/ \
(Tapered down) / \ (Tapered down)
----´ `----
Values: [val1*wgt1, val2*wgt2, val3*wgt3, val4*wgt4, val5*wgt5, ...]
This final piece h is your windowed filter. It has a well-defined shape without the abrupt, sharp edges that the simple “cut” would have left.
I approve of 100% of what Josh said!
I’ll answer too anyways, in case that my different wording helps ![]()
( My academic coding was done in 2016 so it’s been a while, I’ve been working as a programmer since, but it’s not quite the same )
First : I picture the algorithms as a physical process as much as possible, not just as meaningless text(code). Looking at algorithm visualization on Youtube can help. There’s ton of cool videos for sorting, path finding, tree sorting etc. Personally, it helps if I can visualize something physical executing the algorithm ( like for bubble sort, each item floating to it’s place one at a time, or me doing the sorting with a deck of cards for each algorithm ).
Second: Meaning-full names are important. And some people never learn this. I have collegues who coded for 20 years who still name variables “list”, “a”, etc… I understand that sometimes it’s just math being math, but don’t be afraid of longer names. That applies to variables AND functions. Don’t be afraid of making a private function just to make the code more understandable.
Third: Try visualizing concepts more than each individual lines. Get to know each data structure: int, float, string, array, list, stack, queue, tree, nodes, …
and algorithm paradigms : divide and conquer, dynamic programming, greedy, …
Also different concepts like : parallelization, functional coding, caching, etc.
Fourth: You can use a memory palace to memorize some key words for the specific language you use : how variables are declared, functions, classes, string functions, array functions, …
Finally : Combine these concepts when you learn a new algorithm. It’s a basic application of “chunking” to learn more easily.
An example of what that looked like for me in school : Learning a 100-300 lines algorithm with a few key facts ( could be for ai, graphics computing, sorting , etc. ):
- The algorithm uses : divide and conquer to sort first (example)
- Store partial results in a matrix of size (item1 * item2)
- loop to add to the partial result
- each iteration uses the previous neighboor value + the formula (exampe) to add to the partial result
- returns the final value of the matrix as an answer
It’s been many years as I said, so I don’t have a concrete example, but this is what I did and writing by hand algorithms of a few 100 lines wasn’t a problem, even with very limited studying time ![]()
Of course, it all requires the building blocks of the former learned concepts, but take them one at a time, and use youtube visualization of these concepts and learning all of it should be a breeze!
Good luck, have fun!
Not sure if this will help.
But the underlying problem appears similar
Hey, you are right that memorising code requires a different strategy. Here below are some steps that will help you to memorise coding:
Step 1: Understand before you Memorize.
Step 2: Break the code into small parts.
Step 3: Use Active Recall and Spaced Repetition.
Step 4: Repetition Through writing (Muscle Memory)
Step 5: Create Visual on Mnemonic Anchors
Step 6: Practice Variation Recall
Step 7: Test Under Exam Condition.
Hope this will help you.