3
\$\begingroup\$

I have two strings (aggregatePath and pathFromParent), either of which might be null or an empty string. I want to concatenate the two strings with a separator only if neither is null or empty. This is what I've ended up with:

FullPath =
    aggregatePath +
    aggregatePath is not (null or "") && pathFromParent is not (null or "") ? "." : "" +
    pathFromParent;

but it seems overly complex for what I'm trying to do.

How can I better clarify the intent of this code?

I'm not asking about performance. I've just come back to this code after a while, and it took me a few moments to figure out the intent. Is there perhaps some better idiom for this?

Using C# 9.

\$\endgroup\$
4
  • 1
    \$\begingroup\$ string.Join(".", paths.Where(x => x != null)) where paths = new List<string>{aggregatePath , pathFromParent}. \$\endgroup\$
    – BCdotWEB
    Commented Jul 6, 2021 at 8:03
  • \$\begingroup\$ @BCdotWEB I was thinking to use an array, but it still doesn't feel clear -- why am I creating this new instance of array/List<string>? (NB. I'm checking for both null and "".) \$\endgroup\$
    – Zev Spitz
    Commented Jul 6, 2021 at 8:05
  • \$\begingroup\$ If you want more clarity why not simply write things down explicitly, e.g if (string.IsNullOrEmpty(first) || string.IsNullOrEmpty(second)) { seperator = string.Empty }. You can use string.Join (separator, first, second) afterwards. \$\endgroup\$
    – devsmn
    Commented Jul 6, 2021 at 8:11
  • \$\begingroup\$ FullPath = $"{aggregatePath}{(aggregatePath?.Length > 0 && pathFromParent?.Length > 0 ? "." : "")}{pathFromParent}"; \$\endgroup\$
    – aepot
    Commented Jul 6, 2021 at 13:40

2 Answers 2

5
\$\begingroup\$

In my opinion, the best way to clarify your intention is to write down things explicitly.

Start by setting the separator to . by default.

string separator = ".";

Then, use string.IsNullOrEmpty to check whether one of the two strings is either null or string.Empty and set the separator to string.Empty if that's the case.

if (string.IsNullOrEmpty(aggregatePath) || string.IsNullOrEmpty(pathFromParent)) {
    separator = string.Empty;
}

Lastly, join the two string using string.Join.

string.Join (separator, aggregatePath, pathFromParent);

Might not be that fancy but it's quite easy to understand.

Try it out yourself.

\$\endgroup\$
1
  • \$\begingroup\$ My subjective feeling about this is that pulling out the separator into a separate variable is both clear and concise. But I'll accept your answer because it does answer the question. \$\endgroup\$
    – Zev Spitz
    Commented Jul 6, 2021 at 8:28
1
\$\begingroup\$

The following avoids temporary variables and unnecessary concatenation.

return string.IsNullOrEmpty(aggregatePath)
    ? pathFromParent
    : string.IsNullOrEmpty(pathFromParent)
        ? aggregatePath
        : aggregatePath + "." + pathFromParent;

If you don't find ternary expressions clear, you could expand it to if-statements, but I don't think that's necessary.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.