Context
In Clean Code, page 35, it says
This implies that the blocks within if statements, else statements, while statements, and so on should be one line long. Probably that line should be a function call. Not only does this keep the enclosing function small, but it also adds documentary value because the function called within the block can have a nicely descriptive name.
I completely concur, that makes a lot of sense.
Later on, on page 40, it says about function arguments
The ideal number of arguments for a function is zero (niladic). Next comes one (monadic), followed closely by two (dyadic). Three arguments (triadic) should be avoided where possible. More than three (polyadic) requires very special justification—and then shouldn’t be used anyway. Arguments are hard. They take a lot of conceptual power.
I completely concur, that makes a lot of sense.
Issue
However, rather often I find myself creating a list from another list and I will have to live with one of two evils.
Either I use two lines in the block, one for creating the thing, one for adding it to the result:
public List<Flurp> CreateFlurps(List<BadaBoom> badaBooms)
{
List<Flurp> flurps = new List<Flurp>();
foreach (BadaBoom badaBoom in badaBooms)
{
Flurp flurp = CreateFlurp(badaBoom);
flurps.Add(flurp);
}
return flurps;
}
Or I add an argument to the function for the list where the thing will be added to, making it "one argument worse".
public List<Flurp> CreateFlurps(List<BadaBoom> badaBooms)
{
List<Flurp> flurps = new List<Flurp>();
foreach (BadaBoom badaBoom in badaBooms)
{
CreateFlurpInList(badaBoom, flurps);
}
return flurps;
}
Question
Are there (dis-)advantages I am not seeing, which make one of them preferable in general? Or are there such advantages in certain situations; in that case, what should I look for when making a decision?
flurps.Add(CreateFlurp(badaBoom));
?f(g(x))
is against your style-guide, well, I can't fix your style-guide. I mean, you don't splitsqrt(x*x + y*y)
into four lines either, do you? And that's three(!) nested subexpressions on two(!) inner nesting levels (gasp!). Your goal should be readability, not single operator statements. If you want the later, well, I have the perfect language for you: Assembler.mov
instructions and a singlejmp toStart
at the end. Someone actually made a compiler that does exactly that :Drlwimi
instruction on the PPC. (That stands for Rotate Left Word Immediate Mask Insert.) This command took no less than five operands (two registers, and three immediate values), and it performed the following operations: One register contents was rotated by an immediate shift, a mask was created with a single run of 1 bits which was controlled by the two other immediate operands, and the bits that corresponded to 1 bits in that mask in the other register operand were replaced with the corresponding bits of the rotated register. Very cool instruction :-)