How to write sarcasm
There’s no objective standard for what qualifies as sarcasm in writing (short of the author explicitly pointing it out). It’s a question of tone and purpose, and therefore subject to interpretation. But since sarcasm is a form of verbal irony, the first step is always to write an ironic line of dialogue – that is, one in which the character says the opposite of what he or she means.
To turn that verbal irony into sarcasm, you have to set the stage in a way that shows the reader what sort of tone the character is probably using. This may involve putting the character in a frustrating situation (such as in the first example above) or establishing a relationship between two characters that suggests one of them may be using sarcasm.
Of course, sometimes it’s not immediately clear to a reader whether your character is being sarcastic or not, and in these cases it’s important to specify – for example by describing a character’s line explicitly as a “sarcastic comment.” If you leave readers guessing about these things, the risk is that they’ll guess wrong, and completely misinterpret what a character means!
When to use sarcasm
Naturally, sarcasm has no place in formal essays. In general, it also makes sense to avoid it in creative writing unless you have a character who would definitely say something sarcastic in a given situation. Always remember that a character who speaks sarcastically may come across to readers as crabby, childish, or unkind.
Sarcastic characters are very popular in modern television and movies largely because they can be very funny. As always, it’s difficult to theorize about why people laugh at any particular thing, but in this case it may be a sort of “he’s saying what we’re all thinking” laughter. Cynical, sardonic, and sarcastic characters can give voice to the dark, brooding side of everyone’s personality, and it’s often pleasurable to let those thoughts free in a humorous setting. But it’s a fine line – sometimes, a cynical/sardonic/sarcastic character can be too dark for the audience to relate to, and in this case the character becomes unsympathetic and therefore unfunny. This is a judgement call and entirely dependent on the sensibilities of the audience.
The other benefit of sarcastic characters is that they provide options for redemption, which is a key part of what audiences (especially in America) demand. Dr. Cox from Scrubs, Dr. House from House, and Sherlock Holmes from Sherlock are three television characters who use sarcasm and a sardonic tone of voice to express their deeply cynical attitudes toward life. But all of these characters have one very important characteristic in common – they save lives. Their whole quest throughout the show is to heal the sick and prevent violent crime, which shows that the characters are ultimately ethical human beings despite their cynicism. (We usually interpret the cynicism as a symptom of deep psychological or, in House’s case, physical pain.) This interplay of pain and morality is key to the folklore of American popular culture, and goes a long way toward explaining why we get so attached to characters who employ sarcasm.