A few years ago, someone suggested I add another workshop to my repertoire: writing humor. I hedged. Then I visited libraries, bookstores and the Internet to find books on writing comedy. I found only a few, and none of them helped me.
Drama is easy. Melodrama is easy. Comedy is eff-ing hard.
Comedy comes from two sources. One is the situation, the basis of slapstick humor. Shakespeare's drunks and fools usually followed this tradition, which goes back to the Greek and Roman playwrights (Remember, Will lifted The Comedy of Errors wholesale from Plautus). This often becomes farce, where the characters become puppets in service to the plot.
The other source is more intellectual or verbal. Puns, wordplay and irony replace the pratfalls, and some people appreciate this more than others. If you tell the same joke to ten people, a few will roar, some will chuck, a couple will smile, and at least one will say, "Oh, that's it?"
Like American English, comedy relies on rhythm. Years ago, I attended a one-day workshop on directing comedy, and the instructor stressed "The Machine," the progression and rhythm that make a scene or play "funny." He said if you change the order or any component, you'll kill the joke. I agree. Years ago, my wife played the fussy roommate in the female version of The Odd Couple, and the other actress insisted on adding "uh-huh, oh really" and other ad libs to the famous exchange about "It's not spaghetti, it's linguini." She never got a laugh. Ever. Not one single night.
The only other specific hint I remember about directing comedy came from my directing mentor in grad school: Gorgeous is not funny...unless she slips on a banana peel.

My first drafts aren't funny. Humor grows out of revision, usually from a character's reaction to the situation, more ironic than slapstick. If it doesn't feel like part of the character and the whole milieu, it doesn't work for me. I try not to reach for it because if it emerges, it's a pleasant surprise for me, too, and that's how punchlines work. They deliver what the audience expects, but not the way they expect it.
My favorite authors write humor better than I do. Maybe that's one reason I like them. Louise Penny uses twisted literary allusions and puns, usually as responses from the residents of Three Pines, whom we've grown to know and love over the course of her Armand Gamache series.
Dennis Lehane's irony--karma comes to town--often involves character, too. Don Winslow can use irony, but he can also go slapstick. His recent novella "The San Diego Zoo" builds on an outrageous situation seen through the eyes of a cop who becomes a laughingstock on social media. The opening line is "Nobody knows how the chimp got the revolver," and the story races to the logically absurd conclusion from that premise. Elvis Cole, the PI of many Robert Crais novels, loves self-deprecating throw-aways.
Several romance authors write great comedy, too. Look at Jennifer Crusie's dialogue, especially late in a book where her characters paraphrase earlier speeches and turn them on their heads.
None of these writers could steal another's joke and make it work in their own stories. Comedy is personal, and that's what makes it so hard.
You really do reveal yourself on the page.