Perhaps as most succintly put by Justice Gerein in R v Melnyk:
"The theory of deterrence is that fear of punishment will cause potential wrongdoers to act within the parameters of the law."
Or in Re H:
"... to protect the public from the commission of such crimes by making it clear to the offender and to other persons with similar impulses that if they yield to them, they will meet with severe punishment."
Most justice systems either impose sentencing guidelines which reduce judicial discretion, or use general principles, or a combination of both.
The United Kingdom's Criminal Justice Act 2003 states, at §142 (emphasis added):
"Any court dealing with an offender in respect of his offence must have regard to the following purposes of sentencing (a) the punishment of offenders, (b) the reduction of crime (including its reduction by deterrence), (c) the reform and rehabilitation of offenders, (d) the protection of the public, and (e) the making of reparation by offenders to persons affected by their offences."
Canada's Criminal Code provides, at §718 (emphasis added):
"The fundamental purpose of sentencing is to contribute, along with crime prevention initiatives, to respect for the law and the maintenance of a just, peaceful and safe society by imposing just sanctions that have one or more of the following objectives: (a) to denounce unlawful conduct; (b) to deter the offender and other persons from committing offences; (c) to separate offenders from society, where necessary; (d) to assist in rehabilitating offenders; (e) to provide reparations for harm done to victims or to the community; and (f) to promote a sense of responsibility in offenders, and acknowledgment of the harm done to victims and to the community."
In R v Morrissette, Justice Culliton wrote, in the context of a discussion on the factors to take into account when sentencing:
"Deterrence is also an important factor. The problem is different if the purpose of sentence is to deter the offender from repeating the offence from that if the purpose is to deter others who may be inclined to commit the same offence. In neither case does it necessarily follow that a long sentence is required to achieve the purpose. Deterrence should be considered from an objective view if the purpose is to deter others who may be inclined to commit the same offence. In such case, the gravity of the offence, the incidence of the crime in the community, the harm caused by it either to the individual or to the community and the public attitude toward it are some of the matters to be considered. If the purpose is to deter the offender from repeating the offence, then greater consideration must be given to the individual, his record and attitude, his motivation and his reformation and rehabilitation. If both purposes are to be achieved, then there must be a weighing of all of the factors and a sentence determined that gives a proper balance to each of them."
Deterrence can be distinguished as specific deterrence (aimed at the person being sentenced) and general deterrence (aimed at the population at large).