I love linguistic quirks, so when my friend AJ posted about this sentence on Facebook, I had to investigate.
It turns out that, “James while John had had had had had had had had had had had a better effect on the teacher” is a valid sentence, and an example of lexical ambiguity. With a bit of explanation, punctuation and emphasis it does actually make sense.
Wikipedia explains it as follows,
The example refers to two students, James and John, who are required by an English test to describe a man who, in the past, had suffered from a cold. John writes “The man had a cold” which the teacher marks as being incorrect, while James writes the correct “The man had had a cold.” Since James’ answer was right, it had had a better effect on the teacher.
The sentence can be understood more clearly by adding punctuation and emphasis:James, while John had had “had”, had had “had had”; “had had” had had a better effect on the teacher.
There is a similar example that I was already familiar with, “Buffalo buffalo Buffalo buffalo buffalo buffalo Buffalo buffalo”. Wikipedia summarises this one pretty well too:
“The sentence’s intended meaning becomes clearer when it’s understood that it uses the city of Buffalo, New York and the somewhat-uncommon verb “to buffalo” (meaning “to bully or intimidate”), and when the punctuation and grammar is expanded so that the sentence reads as follows: “Buffalo buffalo that Buffalo buffalo buffalo, buffalo Buffalo buffalo.” The meaning becomes even clearer when synonyms are used: “Buffalo-origin bison that other Buffalo bison intimidate, themselves bully Buffalo bison.”"
If you’re still unclear, there’s a useful video that explains the phenomenon, and the first example, well.