Initialize a List's Capacity I received my code review comments for the following method:

private RequiredDocumentEntity GenerateRequiredDocumentMapping(string documentTypeCode, params string[] alternativeDocumentIds)
{
    int alternativeDocumentCount = alternativeDocumentIds?.Length ?? 0;
    List&lt;DocumentTypeEntity&gt; satisfyingDocuments = new List&lt;DocumentTypeEntity&gt;(alternativeDocumentCount + 1);  // A document can always satisfy itself, hence the + 1
    DocumentTypeEntity documentType = null;

    if (documentTypeCode != null)
    {
        documentType = GetDocumentType(documentTypeCode);
        satisfyingDocuments.Add(documentType);
    }

    alternativeDocumentIds.Select(GetDocumentType)
                          .ForEach(satisfyingDocuments.Add);

    return new RequiredDocumentEntity() { SatisfiedBy = satisfyingDocuments };
}


The issue the reviewer had with this method was that the initialization of the list's capacity made the code difficult to read. It was also stated that it was a micro-optimization to save the list from doubling upon resize. This I agree with, to an extent. The list will have, at most 6, elements in it upon return. My argument against that would be that while it is a micro-optimization, if you can calculate how many elements will be in a list, why wouldn't you simply initialize the list's capacity?

Therefore, my question is:  Does the initialization as written make this code difficult to read?
You don't have to initialize the list's capacity and with only 6 items you won't notice any difference anyway so why making the code overcomplicated? As a matter of fact you need only a single query to get what you need so it's not only the capacity that makes it unecessarily more difficult to read by the entire implementation.

You can just Concat the alternativeDocumentIds to the optional documentTypeCode:

private RequiredDocumentEntity GenerateRequiredDocumentMapping(string documentTypeCode, params string[] alternativeDocumentIds)
{
    var satisfyingDocuments =
        from docId in 
            string.IsNullOrEmpty(documentTypeCode) 
                ? Enumerable.Empty&lt;string&gt;()
                : new string[] { documentTypeCode }).Concat(alternativeDocumentIds)

        select GetDocumentType(docId);

    return new RequiredDocumentEntity()
    {
        SatisfiedBy = satisfyingDocuments.ToList()
    };
}


but if the the documentTypeCode is optional anyway why don't you simply include it in the params array? It doesn't make any difference for the result and the method becomes really simple:

private RequiredDocumentEntity GenerateRequiredDocumentMapping(params string[] documentIds)
{
    var satisfyingDocuments =
        from docId in documentIds
        where !string.IsNullOrEmpty(docId)
        select GetDocumentType(docId);

    return new RequiredDocumentEntity
    {
        SatisfiedBy = satisfyingDocuments.ToList()
    };
}


or

private RequiredDocumentEntity GenerateRequiredDocumentMapping(params string[] documentIds)
{
    return new RequiredDocumentEntity
    {
        SatisfiedBy = 
            documentIds
                .Where(docId =&gt; !string.IsNullOrEmpty(docId))
                .Select(GetDocumentType).ToList()
    };
}


whatever you prefer.