RCA can be a lot of things. Most of the usage of RCA I’ve seen has something to do with the Radio Corporation of America
and all of the inventions and spin-offs from it. But more recently, RCA means root cause analysis. As in “Why did that really happen?
As in “Why did the program crash?” Easy. Like it says right there in the stack trace,
panic: runtime error: invalid memory address or nil pointer dereference [signal SIGSEGV: segmentation violation code=0x1 addr=0x0 pc=0x4a52c6] goroutine 1 [running]: main.log(0x0, 0xc0000a9f68, 0x1, 0x1) /tmp/sandbox483371455/prog.go:28 +0x66 main.main() /tmp/sandbox483371455/prog.go:23 +0x74 Program exited: status 2.
So the fix is to test if format is
nil and drop the message or log an error. That will always work. But
is that the root cause? That’s certainly the proximal,
immediate cause, but that doesn’t tell the whole story. Why did it really panic? I mean the program usually
works (outside the playground. It always works there, but that’s a different issue). But is that the right fix? To
know that you need to know why it really failed.
Figuring that out is doing root cause analysis. And figuring out the root cause is crucial to making sure it doesn’t happen again. One way to figure out the root cause is to keep asking why.
Why was there a
nil pointer reference on line 23? Because nil was passed in. Why was nil passed in? Because it
wasn’t set in the conditional. Why wasn’t it set in the conditional? Because the conditional checks for
before/after noon, and before/after 5PM, but ignores those two specific times. Why are those two skipped? Because
English is a slippery language and computers are very literal. The requirements say “Greet with good morning
before noon and good afternoon after noon”. The code does that. The requirements don’t say what to
do AT noon, and the code does nothing. Now depending on how you define noon, the chances of the test happening exactly
at noon go down, but because computers are discrete, there’s always that chance of testing exactly at noon.
In this case the right answer is probably to check for
hour <= 12 instead of
hour < 12, but you’d have to go back
to the user to be sure. Or you could go for the snarky developer solution and set the default format to
`‘Good <indeterminate time of day because the specs were incomplete> %s\n’.
There’s one more why you should have probably asked in this case. Why does
log take a
*string as a parameter instead of
string. If you fix that problem the compiler will make it much harder to make the mistake. But before you go fix that,
you need to understand Chesterton’s Fence, but that’s a topic for another