A lot of my friends will bother me for help with compsci or math stuff for some reason, and I usually try to help. Math and programming are hard, especially to someone who doesn’t already know it, and people can quickly get frustrated as a result of it being confusing. Throwing in a swear can be useful to signal to the person to relax, and to tell them that it’s ok to think of this stuff in goofy terms, and in my experience it can help lower the tension.
It’s not the best tool for every scenario, I don’t use that tactic when teaching kids, but I do think there’s utility in it.
I agree that it can be overdone, but it's pretty lightly sprinkled here. But it's the same as any other useful literary device, to answer your question. To make things more interesting or different.
Another commenter mentioned shock value, which doesn't seem far off. A little taste of shock is what jolts the reader's attention.
With that said, it should be used sparingly (if at all), otherwise the message loses eloquence and the intended expression is diminished by vulgarity. Someone also once said that using curse words makes people dumber and lazier because they end up using curse words in place of more descriptive words, which I tend to agree with.