This sort of thing needs to be viewed in terms of large numbers. Of course some few would not be deterred, this we know by the laws of probability. But when perps make the direct and immediate association between their potential crime and the death penalty, then vast numbers of them choose to turn from that crime.
if the punishment is swift and brutal. Like Vlad the Impaler :)