A student found the word 'had' doubled, as “had had”, and she asked me if it was an error. Usually doubled words are errors. However sometimes they are, in fact, correct, if a bit awkward.
"...he had had an affair with Shirley.”
This sentence tells us two things first that he had an affair and second that it is no longer continuing.
To understand “had had”, it is better to look at the present perfect and past perfect tenses of the sentence structure. Consider this example: 'He has had an affair with Shirley.' This sentence is in the present perfect tense. One uses the present perfect tense to talk about a past action that is (presumed to be) continuing into the present or that occurred at an unspecified time before the present. This example sentence means that he started the affair in the past but the activity is assumed to be continuing up into the present.
The present perfect tense uses have and has (the present tense of the verb to have) plus the past participle (of the main verb), as in have slept and has kissed.
To put the affair-sentence in the past tense one needs to use past perfect tense. The past perfect tense uses “had” plus the past participle, as in had had and had kissed. The sentence fragment “he had had an affair” tells us two things. First that he had an affair and second that it is no longer continuing.
The sentence may also mean that the affair occurred before the time that he made the statement which is also in the past. Specifically the past perfect tense indicates that something was past before some other past time; it is the past in the past tense.
To avoid the double had the sentence can be reworded. But the new structure might be even more awkward. For example: 'He had an affair with Shirley. The affair is no longer continuing.' One could possibly put another term between the two hads, e.g. 'He had previously had an affair'.
For a sentence that has two past-tense events, one uses the past perfect to express the first event. When using the verb to have in past perfect, one needs to use a double had. Sometimes double had is correct. However I recommend avoiding it, if it is confusing. Such sentences can usually be broken into two and made clearer.