HiveBrain v1.2.0
Get Started
← Back to all entries
patterncsharpMinor

Console application - customizer bootstrap style

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
customizerapplicationbootstrapstyleconsole

Problem

I've created a console customizer for a C# console application. This is not finished yet but I'm looking for tips, requests and suggestions so I can make some updates.

Sample Preview

The following code is use to create a customize Write() and WriteLine() and I also make some Typewriter effect on it.

```
namespace GetBootstrap.Styles
{
///
/// Project Name: GetBootstrap v1.0.0
/// Language: Console C#
///
/// Developed by Leonel Sarmiento
/// Email: Leonel@outlook.ph
/// Website: http://stackoverflow.com/users/2575662/leo-sarmiento
///
public static class Bootstrap
{
//Bootstrap.Write Basic
public static void Write(string style, string value)
{
switch(style)
{
case "Success":
Console.ForegroundColor = ConsoleColor.Green;
break;
case "Information":
Console.ForegroundColor = ConsoleColor.Cyan;
break;
case "Warning":
Console.ForegroundColor = ConsoleColor.Yellow;
break;
case "Danger":
Console.ForegroundColor = ConsoleColor.Red;
break;
default:
Console.ForegroundColor = ConsoleColor.Gray;
break;
}
Console.Write(value);
Console.ResetColor();
}

public static void Write(string value, int min, int max)
{
Random speed = new Random();
for (int t = 0; t < value.Count(); t++)
{
Thread.Sleep(Convert.ToInt32(speed.Next(min, max)));
Console.Write(value.Substring(t, 1));
}
}

public static void Write(string style, string value, int min, int max)
{
switch (style)
{
case "Success":
Console.Fo

Solution

The main issue I can see is that you have duplicated your switch statements mapping styles to colors about a gazillion times. This is not very maintainable. Imagine you add a new style - you now have to add this to a lot of places and hope you didn't miss anything.

As far as I can see there are two types of mappings: One for Write/WriteLine and one for Alert/AlterLine - you should have two dedicated methods which translate the style into the colors you require and then call these two methods in the appropriate places.

I would also consider making the style an enum rather than a string as there seem to be fixed values. At least define public string constants for the different default names - this way someone could just use those constants and doesn't have to remember the correct name to put in.

There are other code duplications as well which you should try to avoid and extract into common methods.

Context

StackExchange Code Review Q#65329, answer score: 5

Revisions (0)

No revisions yet.