I enjoy using humour when I lecture. Lectures aren’t built for people’s natural attention spans, and even after long experience, it is almost impossible for a person to focus on a lecture for 50 minutes straight. Humour provides a break for the audience, but more than that, the best jokes are *memorable*, making a hook that the lecture material can hang off of in the students’ minds. Perhaps most grandiosely, humour requires empathy; you can’t tell a funny joke to your students unless you understand what they find funny, which means that however briefly you were able to see things from their perspective. This is perhaps why humorous lecturers are popular.
The point behind this philosophy is that when you tell a joke in class, you want to tell it for good reason. If your only goal is to give the class a bit of a rest—perhaps the weakest reason, but still fine—then there’s no need to tell the joke in the first 10 minutes. Whereas if you’re using humour to provide a hook for new material, then that’s exactly where you would put it.
Perhaps the first rule of lecture comedy is: Your mileage will vary. It’s hard to predict how a class will react to a particular joke. For example, more than once, I have walked into a room of teenagers and said, “Right, so today class, we’re going to do PCP.” (The Post Correspondence Problem, of course.) One time the class immediately broke out laughing, and another time they sat in bemused (I think) silence. Do not be discouraged by the silence.
For this reason, make your jokes offhand. Make them an aside to your lecture rather than a detour. Then, if they don’t work, you simply go on with your lecture as normal and you don’t look (so) bad.
The ideal joke is one that makes a serious point. An example is the classic pair of sentences that illustrate syntactic ambiguity: Time flies like an arrow / Fruit flies like a banana. This example has the additional merit of being part of the folklore of the field. Stories like this acculturate students to an intellectual area, which is part of the reason they spend the money on University rather than taking a correspondence course.
All of this said, you have to be natural. Humour is subtle enough that if you force yourself to tell jokes you don’t believe in, they won’t work. Your lecture style needs to arise naturally from your personality, so what works for me might not work for you. That said, it’s not as if you’re doing stand up: the standards are much lower for lectures, so even a mildly amusing attempt might get a positive (and perhaps relieved!) response from your students.