Humor can be a tricky business. Get it right and you surprise and delight you audience; cross the thin line between what's appropriate and what's not, and who knows how many people you might offend. 

Consider the backlash against Oscar host Seth MacFarlane and his off-color jokes Sunday night. Or Geico’s latest spokes-animal, Maxwell the pig, which came under fire after a recent commercial featured him missing some obvious come-ons from a human woman. The Christian group One Million Moms believe the ad belies the car insurance company's lax attitude toward bestiality.

There may not be a formula for a perfect joke, but research from the Harvard Business School suggests that there may be a formula for creating funny--and effective--commercials.

In the upcoming research paper, Why, When and How Much to Entertain Consumers in Advertisements? A Web-based Facial Tracking Study, professor Thales Teixeira and his colleagues suggest audiences want to be entertained--but not too much. Here are three tips for creating the perfect commercial from their recent findings.

Don’t be annoying.

The purpose of advertisements is, well, to advertise. But viewers grow bored with overt product placement and constant brand reminders. People seem to have an unconscious aversion to being persuaded, so when they see a logo, they resist, explains Teixeira in a summary of his findings. 

When strategizing successful brand placement, he writes, “A good question to ask when conceiving an ad is: If I removed the brand image, would the content still be intrinsically interesting?”

If the answer is yes, Teixeira says, viewers are more likely to continue watching.

Don’t bet everything on the twist.

According to Teixeira, in the past, advertisers have been able to rely on a clever twist or surprise at the end of a commercial to clinch an audience’s interest and make their brand memorable. This is no longer the case, he warns in a video interview.

In the era of video streaming, when consumers have any number of ways to avoid or tune-out commercials, advertisers can’t beat around the bush, he says.

Instead, advertisements must hook an audience from the get-go. One of the easiest ways to do this, Teixeira says, is by inducing feelings of joy or humor from the very start. He cites Bud Light’s "Swear Jar" ad as an example of humor being put to good use.

In the commercial, office workers intentionally integrate swear words into their daily conversations in an attempt to fill up the office swear jar and, presumably, buy a case of Bud Light. Teixeira explains that this commercial is surprising from the get-go--as viewers do not typically expect office workers to swear flagrantly--and also disarming because the product placement is neither overt nor overwhelming, allowing viewers to get lost in the simple humor of the situation.

Entertain them--but not too much.

Not surprisingly, Teixeira found that a viewer’s level of entertainment increases her likelihood to watch an ad. However, after a certain level of entertainment, viewers lost sight of the brand or product being advertised.

“If you want to persuade consumers as do Pepsi, Skittles, and Coke, it’s about getting people to associate your brand with fun,” Teixeira explains, “But you have to show the brand and then entertain. That’s when the conditioning occurs. The other way around--entertain then brand--doesn’t work as well.” 

The same can be said of shock-value. While shocking humor can be fun and engaging to an audience, it overlooks one crucial facet of advertising: the likelihood that an audience will share it.

Bud Light’s “Clothing Drive” utilizes the same setting and structure as the “Swear Jar” ad. An office worker offers a Bud Light for every article of clothing donated to a clothing drive, and soon the entire office is prancing around in their birthday suits. “Clothing Drive” and “Swear Jar” garnered similar degrees of viewership, says Teixeira, but viewers did not share the “Clothing Drive” video as frequently.

One possible explanation: The content wasn’t family friendly enough for viewers to share with their networks. Teixeira suggests that advertisers consider not only their immediate audience, but also those that the audience is connected with, like... your mother.

As the media company Upworthy advises writers, "Don't sexualize your headlines in a way your mom wouldn't approve of." The same goes for ad campaigns.