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
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(consoleWorkerInterval));
}
/// <summary>
/// Handles the timers callback once elapsed.
/// </summary>
/// <param name="timerObject">ConsoleWorker timer's object.</param>
private void timerElapsed(object timerObject)
{
var serverInfo = Faze.getServer().getServerInformation();
if (serverInfo.hasStarted())
{
if (StaticSettings.advancedLoggingWhenDebugging && Debugger.IsAttached)
classLogger.Info("ConsoleWorker has elapsed -> " + DateTime.Now.ToLongTimeString());
TimeSpan serverUptime = DateTime.Now - serverInfo.getStartedTime();
var projectName = serverInfo.getProjectName();
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" : "")
);
System.Console.Title = consoleTitleString;
}
}
public void Dispose()
{
consoleWorkerTimer.Dispose();
}
}
}
One final thing, Someone on the old post said use CamalCase because I would seem like I'm coding JavaScript otherwise, is this a do do and a don't don't go against thing or is that just his personal preference?