I can see a certain amount of logic to it, with an M16 and an infantryman in the dark. Otherwise, not so much...
It is remotely possible, though unlikely, that the top round in a magazine could have been moved forward by something, enough to stick out, and prevent insertion of the magazine into the gun. Slapping the mag, banging it against your helmet (or other hard surface) is a way (though not a 100% positive way) to ensure the rounds are in their proper place in the mag, before sticking it into the rifle.
It was taught to grunts that way, for a long time. Don't know if they still do, or not.
It may NOT matter, but giving the mag a couple of wacks to (hopefully) insure all is well is a comforting habit that seldom causes any trouble, when done right.
Can't help but imagine that a lot of military ideas would rapidly change if armies were once again led from the front instead of from the rear.
No army in modern history has ever been led from the front.
Generals, Field Marshals, the King or the God/Emperor do not lead assaults themselves, personally. They may be AT the front, but they don't lead attacks, not since the idea that the personal honor & glory coming from being first in the charge went by the wayside.