To write a funny book you really only need to master one thing: surprise. And how do we surprise someone? Set them up to expect one thing, and then give them an egg.
That was a lazy example, but give it a little thought and stretch out that set up for longer, and you’ve got some excellent humor on your hands.
Shock-value humor absolutely relies on surprise. Rick and Morty is a great example of shock value that uses contrast (cartoons and graphic violence? How unusual! Disney would spin in his grave!) to push the surprise. Though the more graphically violent cartoons we see (looking at you, Adult Swim), the less likely we are to find blood and guts in cartoon form funny. Once the humor dries up, so too should the blood (looking, again, at you, Adult Swim).
This is exactly why horror and humor work rather surprisingly well together and why certain people (who aren't serial killers, believe it or not) laugh during horror movies or at haunted houses instead of screaming.
Scatological humor, as a subset of shock humor, also relies on surprise, but it's also important to maintain contrast. Now, as you may know, I don't tend towards the scatological in my own work, but I think we can all agree that a little boy farting is no where near as funny as the Queen of England letting one loose. That's because it's a great deal more surprising.
Now, for a note on set-up and pay off: the surprise has to come at the end of the joke, which is why it’s a common tactic to write the punchline (aka the surprise) first, then set it up. You write a pun, then figure out the set up to it, right? So why shouldn't writing all types of humor the same? I frequently write out a messy first draft of a joke, then re-arrange it so that the punch line is at the end.
Let's take the Queen of England gag from earlier as an example.
An early draft might have looked like this: "The Queen of England farting is funnier than a little boy farting." It gets exactly the same point across, but with absolutely no humor or style.
The next draft might be: "A little boy farting is not as funny as the Queen of England farting." which is a marked improvement, but I think we can take it a step further by employing a little tasteful re-wording.
"A little boy farting is not as funny as the Queen of England letting one loose." is what I arrived at, as it both ends with the punchline and twists the terminology to add a comment on the absurdity of euphemisms in there, too.
A further option might be: "A common fart joke gets funnier if the dealer is the Queen of England." in which we can save the punch line "Queen of England" for even later in the line and toss in just a bit of an inside joke. You must know the phrase "They who smelt it, dealt it" in order to understand the euphemism in the above line, and yet another subset of humor is derived from feelings of superiority.
It just tickles us to think we’re so smart. This can go south rather quickly, but there are certainly benign examples, too. Inside jokes are “superior humor” because we feel included. You can have inside jokes with people you’ve never met, too. This is why so many people will ask you to bring them a shrubbery if you even so much as hint at a fondness for British humor. You’ve watched a movie! Congratulations! So clever. So superior.
Sarcasm, incidentally, combines superiority AND surprise because—surprise! I do not think you’re clever for having watched a movie.
And that brings us to irony, which I have just used in the above paragraph to introduce the concept of sarcasm. The word “incidentally” implies that I’m adding a thought that's unconnected to the previous thought when, in fact, it was a direct result of said thought.
When you’re writing humor, each word must be a carefully selected little gift, a language lagniappe. Reading humor should feel like biting into a Kinder egg: sweet, full of surprises, and illegal in some parts of the world.