Nice piece of code for endstop mechanical switch?

Hey robot guys. I'm wondering if you could show me the best way to use a mechanical switch as an endstop in a sketch? Exactly the way it works on my 3d printer where the switch stops it, but the motor is still available to move again on command.

First question is how is the switch wired to the Arduino?

Reading a switch is trivial - that's what the digitalRead() function does.