There is a common misunderstanding that the inductive "kickback" is generated by the inductor itself...
I also have that misunderstanding, so please explain.
What else could generate that high voltage.
The impulse is of course generated after the transistor switch turns off,
but kickback current flows through the inductor.
The shortest current path is when the diode is connected directly to the inductor.
Any wire in between will act as an aerial and could transmit the spike to other circuits.
If the inductor carries a large current
placing a diode at the switch and at the inductor is a wise precaution, as the stray inductance / characteristic impedance of the cable may generate large voltages anyway.
R2) 470R What is the function between the gate and the optocoupler?