I've been writing as a part-time job on humor and angst (ah, the wonders of mood swings), and I think a large part of the humor depends on who your audience is. Jokes come in 4 different categories - that's how I learned it.

The first type is the shortest. There's the cause - a normal remark, statement, something that the audience could relate to, something everyday-ish and then the effect. The effect need to twist the persepective into something the audience would not have thought up by themselves. It needs to be easily understood, no thinking involved, and yet not fall into the category of 'lame'. These are the hardest to think by when you need them. The best way would be to bounce sentences or ideas off a friend who have a good sense of humor. That way, the joke flows, doesn't seem awkward and the friend can point out if it's, well, lame.

The second type is a more story kind of format. The person telling the joke sets up a senario and teases the audience with hints of what the ending (the part where they laugh at) might be and this makes the audience involved in the joke. That's why some jokes just never seem to get old - because there's always this anticipation, or fond memory of the anticipation. Women are scientifically proven to be better at this type of jokes (^^ Ha!), and the problem with this joke is that every single word must be necessary. People want to hear the joke, wants the feel happy but the brain can only focus on something for so long before moving on to something else.

The third type is nonsensical things. Essentially, it's things we think are silly, maybe childish, and not something people would normally do. This is sometimes hard for the person telling/acting it because the audience is no longer laughing with you, they're laughing at you. And it's still damn hard to do. Nonsensical jokes tend to have a hit-or-miss on the audience. If someone in the audience thinks it's funny and they laugh, soon the entire audience laughs because laughter really can be catching. But before that someone laughs, there's an awkward silence. Very very awkward. The golden rule seems to be : first time, we laugh; second time, we laugh harder (because, again, anticipation); third time, we start wondering if the weather is going to be better tomorrow.

Lastly, the 'inside' kind of jokes. These are the most common in written material or tv-series because the writer can build it up - there's interaction between characters that are more solid than any of the above so we can relate, understand and 'get into' the character. Because of something that happened before that the writer knows the audience knows about, the writer can draw the audience in, make the audience like the characters, and make the writer him/herself seem witty. This tends to be the easiest humor to write because there's so many openings. The difference between this and the previous three is that the three above has a goal, if you will. The point is the build and build and build and then have a big burst of laughter at the end. This type of humor though is not always for humor's sake. The 'build' part is the most important bit - that's your plot and characters, the actual book. The humor is more slapped on, put in to fill the cracks of character development, plot movement, tension diffusion, etc. Humor becomes a tool. And of course, it can also be used as a weapon, to make a point, like satires. The humor is no longer why it is written but used as a tool to draw audiences.

Now that I have suitably confused everyone, I shall step off my soap-box. Hope that helped.

Kudos to anyone who actually read though the whole thing. ^^