Сейчас весь в поисках работы, ну и по тихой грусти слегка обновляю свои сайтики. Сегодня чёт психанул и запостил в LinkedIn свой взрыв мозга.
How to create a tiny dynamic information bar? In my projects for administrative mode I often use information panels and on-page tools. I used StringBuilder before, but it is not the most convenient approach, now I like the 4th option - to use List<string> and string.Join.
• • •
❌ 1. Crrraazy way
var crazyBar = "";
if (item.Title.Length != 0)
crazyBar = item.Title;
if (item.CommentsCount > 0)
{
if (crazyBar.Length != 0)
crazyBar += " • Comments (" + item.CommentsCount + ")";
else
crazyBar += "Comments (" + item.CommentsCount + ")";
}
if (crazyBar.Length != 0)
crazyBar += " • " + item.Date;
else
crazyBar += item.Date;
Console.WriteLine(crazyBar);
❌ 2. Stupid way
var stupidBar = "";
if (!string.IsNullOrEmpty(item.Title))
stupidBar = item.Title;
if (item.CommentsCount > 0)
stupidBar += !string.IsNullOrEmpty(stupidBar) ? stupidBar + " • Comments (" + item.CommentsCount + ")" : "";
stupidBar += !string.IsNullOrEmpty(stupidBar) ? stupidBar + " • " + item.Date : item.Date;
Console.WriteLine(stupidBar);
❌ 3. Dumb way
var separator = "•";
var dumbBar = new StringBuilder();
if (!string.IsNullOrEmpty(item.Title))
dumbBar.Append(item.Title);
if (item.CommentsCount > 0)
dumbBar.Append($"{(dumbBar.Length > 0 ? $" {separator} " : "")}Comments ({item.CommentsCount})");
dumbBar.Append($"{(dumbBar.Length > 0 ? $" {separator} " : "")}{item.Date}");
Console.WriteLine(dumbBar);
✅ 4. I prefer one of the perfect styles. Yeah!
var perfectBar = "";
var infoData = new List<string>();
if (!string.IsNullOrEmpty(item.Title))
infoData.Add(item.Title);
if (item.CommentsCount > 0)
infoData.Add($"Comments ({item.CommentsCount})");
infoData.Add(item.Date);
perfectBar = string.Join(" • ", infoData).Trim();
Console.WriteLine(perfectBar);