patterncsharpMinor
Console Worker Follow-Up
Viewed 0 times
consolefollowworker
Problem
A few hours ago I posted a similar question but this is a follow-up, I have revised the code and added and removed certain lines of code and hopefully improved it a bit, I just brought it here for a final cleaning, can anyone notice anything than can be improved even more? Some of the code has been totally changed such as the way I wrote the uptime string
I feel its a bit squeezed in, is there a way to make it a bit more clean without the need of an if statement?
Just a few of the improvements I've made
```
using Faze.Other.App;
using log4net;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace Faze.Other.Util.Console
{
class ConsoleWorker : IDisposable
{
private readonly Timer consoleWorkerTimer;
private readonly int consoleWorkerInterval;
private readonly ILog classLogger;
public ConsoleWorker()
{
consoleWorkerInterval = 1000;
classLogger = LogManager.GetLogger(typeof(ConsoleWorker));
consoleWorkerTimer = new Timer(timerElapsed, null, TimeSpan.FromMilliseconds(consoleWorkerInterval), TimeSpan.FromMilliseconds(co
var consoleTitleString = string.Format("{0} / {1}, {2}, and {3}",
projectName,
serverUptime.Days + " day" + (serverUptime.Days != 1 ? "s" : ""),
serverUptime.Hours + " hour" + (serverUptime.Hours != 1 ? "s" : ""),
(StaticSettings.includeSecondsInUptimeString) ? serverUptime.Minutes +
" minute" + (serverUptime.Minutes != 1 ? "s" : "") + " and " +
serverUptime.Seconds + " second" + (serverUptime.Seconds != 1 ? "s" : "") :
serverUptime.Minutes + " and minute" + (serverUptime.Minutes != 1 ? "s" : "")
);I feel its a bit squeezed in, is there a way to make it a bit more clean without the need of an if statement?
Just a few of the improvements I've made
- Removed field comments
- Improved long-lined code
- Removed useless code
```
using Faze.Other.App;
using log4net;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace Faze.Other.Util.Console
{
class ConsoleWorker : IDisposable
{
private readonly Timer consoleWorkerTimer;
private readonly int consoleWorkerInterval;
private readonly ILog classLogger;
public ConsoleWorker()
{
consoleWorkerInterval = 1000;
classLogger = LogManager.GetLogger(typeof(ConsoleWorker));
consoleWorkerTimer = new Timer(timerElapsed, null, TimeSpan.FromMilliseconds(consoleWorkerInterval), TimeSpan.FromMilliseconds(co
Solution
I still find the string creation awful. If you cannot use C# 6 the I suggest the
and an extension method to get rid of conditionals:
StringBuilder:var consoleTitle = new StringBuilder()
.Append(projectName).Append(" / ")
.Append(serverUptime.Days).Append(" day".Pluralize(serverUptime.Days))
.Append(serverUptime.Hours).Append(" hour".Pluralize(serverUptime.Hours))
// ...
.ToString();and an extension method to get rid of conditionals:
static class StringExtensions
{
public static string Pluralize(this string text, int value)
{
return (value != 1 ? text + "s" : text)
}
}Code Snippets
var consoleTitle = new StringBuilder()
.Append(projectName).Append(" / ")
.Append(serverUptime.Days).Append(" day".Pluralize(serverUptime.Days))
.Append(serverUptime.Hours).Append(" hour".Pluralize(serverUptime.Hours))
// ...
.ToString();static class StringExtensions
{
public static string Pluralize(this string text, int value)
{
return (value != 1 ? text + "s" : text)
}
}Context
StackExchange Code Review Q#133976, answer score: 2
Revisions (0)
No revisions yet.