If it is a fundamental principle of justice that the punishment should fit the crime, then why is it that almost overnight, legal fashions have changed and the courts have suddenly embraced the baggy look? As the Liberal Democrats' home affairs spokesman Tom Brake put it: "There have been some cases where people who have committed petty offences have received sentences which, if they had committed the same offence the day before the riots, they would not have received a sentence of that nature."

Such as Ursula Nevin, a mother of two who was asleep when the Manchester riots were raging, but who was sent down for six months because she accepted a pair of looted shorts from a friend. Or Nicholas Robinson, who will pay with six months in prison rather than £3.50 for a case of water he took from a smashed-up Lidl in Brixton. Or Jordan Blackshaw and Perry Sutcliffe-Keenan who got four years each for using Facebook to incite a riot that never took place. All are in their early 20s, none have previous convictions.

Senior figures in the legal services are making no secret of the fact that sentencing for the riots is harsher than normal. Manchester crown court judge Andrew Gilbert QC said: "I consider that the sentencing guidelines for specific offences are of much less weight in the context of the current case, and can properly be departed from." Her Majesty's Courts and Tribunals Service has issued advice to magistrates that they should consider giving custodial sentences to riot-related crimes that would normally be punished less severely. And the Crown Prosecution Service has issued guidance to prosecutors to "ask the court to lift the anonymity of a youth defendant when they believe it is required in the public interest that the youth be identified".

The urgent question that all this raises is whether this swift shift to harsher punishment is compatible with a justice system that promises to treat everyone equally. It might seem obvious that the answer is no. For isn't it a basic principle of justice that like is treated as like? That's not only a legal principle; it's a deeply moral one. Fairness requires that what is morally good for one goose is morally good for another, and also for the gander, unless there is a relevant difference between them.

But therein lies the rub. All morality and justice require is that relevantly similar cases are treated the same, not that every case of the same general type is treated identically. No two crimes are completely alike: at most they are very, very similar. So punishment cannot be expected to fit the crime if it only comes in one size and one style, irrespective of who has to wear it, where and when.

To accommodate this need, the justice system needs to find a middle way between two unsustainable extremes. On the one hand, it cannot allow for bespoke tailoring for every sentence, as that would leave judges and magistrates with too much power that would result in an even more uneven administering of justice than we already have. On the other, sentences can't be completely off-the-peg, as that would allow too little flexibility to respond to morally relevant differences between two cases of, say, water theft. So what we have is something in-between: judges have a range of options and can make a limited number of bespoke alterations, within certain constraints.

Given that some version of this system is the closest we can get to fairness, the key question concerning riot sentencing is not whether it is permissible for the system to change sentencing according to circumstance. Clearly it must be. The real question is whether or not these particular circumstances justify these particular changes. Is there anything about crimes committed in the context of riots that takes them "completely outside the usual context of criminality", as Gilbert put it?

There is one such factor, but if anything, it points to treating people more leniently, not more harshly. That is to say, human beings are easily swept up in the herd and can find themselves doing together what they would never do alone, for better and for worse. And it seems quite clear that many normally law-abiding people got caught up in the riots. If part of the purpose of punishment is to deter, reform and rehabilitate, then the last thing we should do is lock up hitherto lawful citizens with career criminals. Nothing seems better designed to turn what would otherwise be a one-off aberration into a gateway to further crime.

The riot context could therefore be seen as a mitigating, not aggravating, factor. This would not be a good reason to actually treat offenders more leniently. If we want people to behave responsibly we have to hold them fully responsible. But it certainly at least acts as a counterweight to any considerations that would want us to treat them more severely.

Take, for instance, the stiff sentences Blackshaw and Sutcliffe-Keenan received for incitement to riot. Usually, the fact that they failed to rouse any kind of rabble would have meant they got off more lightly than had actual violence been provoked, which is itself a morally dubious principle. If you try to do something wrong, then you are as blameworthy as someone who tries and succeeds. To punish you less is to reward incompetence, not virtue. But given that is the way the law works, there is still an argument that the young men probably wouldn't have committed the crime at all had they not fallen witlessly into the anarchic mania of last week. So any reason we might have to want to come down harder on them than usual should be tempered by the knowledge that, under normal circumstances, they probably wouldn't have incited violence in the first place.

As for what these justifications for extra sternness might be, various possibilities have been bouncing around over the past week, in a loose, rather inchoate way. The most recurrent theme is the need to "send out a message". This is the line taken by Gilbert. "The principal purpose is that the courts should show that outbursts of criminal behaviour like this will be and must be met with sentences longer than they would be if the offences had been committed in isolation." David Cameron also defended the courts along the same lines, saying: "They've decided to send a tough message and it's very good that the courts feel able to do that".

The idea that sentencing should be governed by the desire to send a message strikes many as inherently unjust, since it inevitably means treating people differently according to how crimes appear, rather than simply how they are. But if you accept that punishment is partly justified by the need to deter, then there is no avoiding this unpalatable consequence. It may be necessary to come down harder on one crime rather than another, not because it is a worse crime, but because people are more inclined to do it unless they are given strong reasons not to. It is, perhaps, the same principle behind the obsession of many religions with sex. It is not that church leaders really believe that fornication is worse than assassination. It's just that people are more tempted to copulate than kill, and so you have to come down disproportionately hard on it if you want to put people off.

In the rioting case, the best argument for disproportionate sentencing would be that the looting and violence threatened public order. We saw how a kind of contagion took off, with violence spreading first around London and then to other cities. In such a situation, stealing a case of water might have consequences that it doesn't usually have. It is no longer an isolated crime but one that is part of a toxic spread of disorder. A normal shoplifter does not inspire copy-cat theft, so can just be punished solely for what they have stolen. A looter, however, has not just stolen an object but recklessly spread riot fever. So, the argument goes, it is imperative that a high price is exacted in order to prevent others from doing the same.

The logic is appealing, but in practice it just doesn't seem to work that way. If some people did get high on the adrenaline of last week, they didn't need excessive sentences in order to sober them up. It was enough to realise that innocent people living near shops were in fear of their lives, and that people were being arrested, charged and convicted normally.

Indeed, it seems that excessive sentencing can actually have the potential to backfire. One friend of a looter who was given six months for stealing two tennis rackets in Walworth, south London, told the Evening Standard that it seemed manifestly unfair, when MPs who fiddled their expenses had got only four months. Far from being a deterrent, this seemed to spur her on. "It makes me feel like I wish I had been part of the riots," she said. "And if it happens again next time I will be there with all guns blazing so that any sentences will be justified."

That reflects a problem with the whole "sending a message" approach to justice: the sender is never in control of what will actually be heard. In this case, the message many are getting is that, once again, "the system" is coming down harder on the unkempt masses than it does on the establishment. That is a message likely to provoke more unrest, not calm it. How can it help if people who already have a deep sense of disengagement from mainstream society believe that even the legal system treats them differently?

If sending a message really is important, in this case that is all the more reason not to change sentencing to fit the circumstances. People need to be reassured that this is indeed a fair society, in which all are equal before the law, in which no group is treated more harshly than another, and neither courts nor police take sides.

Other than the conjoined twins of deterrence and sending a message, there have been surprisingly few reasons offered as to why extra-tough sentencing should be favoured, despite the popularity of the measure. The main driver for retribution seems to be more visceral and emotional than philosophical. But could the mere fact that this feeling is widely shared be justification enough? If there is public pressure for harsh sentencing, then in a democratic society, shouldn't that pressure be yielded to?

It is indeed a standard justification of punishment that it should stand as some kind of expression of society's outrage. But there is a vital difference between reflecting the settled will of the people and echoing its shifting moods. Justice cannot be served by doing the latter. Indeed, it would be a terrible irony if the response to an illogical, harmful outbreak of infectious mob violence were an illogical, harmful outbreak of infectious mob justice. At times like these, society as a whole needs to show that it is better than its problematic part.

The right response to this outbreak of exceptionally harsh sentencing is therefore pretty much the same as the right response to the outbreak of exceptionally anarchic violence. We need to understand why it has happened and the real social problems and issues that led otherwise decent people to support, or at least be sympathetic, to it. But in the end, calmly and with good reason, we need to explain why it is wrong, and put an end to it.