Code Review Stack Exchange is a question and answer site for peer programmer code reviews. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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

enter image description here

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

namespace GetBootstrap.Styles
{
    /// <summary>
    /// Project Name: GetBootstrap v1.0.0
    /// Language: Console C#
    /// 
    /// Developed by Leonel Sarmiento
    /// Email: [email protected]
    /// Website: http://stackoverflow.com/users/2575662/leo-sarmiento
    /// </summary>
    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.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;
            }
            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));
            }
            Console.ResetColor();
        }
        //Bootstrap.Write with params
        public static void Write(string style, string value, params object[] arg)
        {
            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, arg);
            Console.ResetColor();
        }

        public static void Write(string value, int min, int max, params object[] arg)
        {
            Random speed = new Random();
            value = string.Format(value, arg);
            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, params object[] arg)
        {
            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;
            }
            Random speed = new Random();
            value = string.Format(value, arg);
            for (int t = 0; t < value.Count(); t++)
            {
                Thread.Sleep(Convert.ToInt32(speed.Next(min, max)));
                Console.Write(value.Substring(t, 1));
            }
            Console.ResetColor();
        }
        //Bootstrap.WriteLine Basic
        public static void WriteLine(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.WriteLine(value);
            Console.ResetColor();
        }

        public static void WriteLine(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)));
                if (t != value.Count() - 1)
                {
                    Console.Write(value.Substring(t, 1));
                }
                else
                {
                    Console.WriteLine(value.Substring(t, 1));
                }
            }
        }

        public static void WriteLine(string style, string value, int min, int max)
        {
            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;
            }
            Random speed = new Random();
            for (int t = 0; t < value.Count(); t++)
            {
                Thread.Sleep(Convert.ToInt32(speed.Next(min, max)));
                if (t != value.Count() - 1)
                {
                    Console.Write(value.Substring(t, 1));
                }
                else
                {
                    Console.WriteLine(value.Substring(t, 1));
                }
            }
            Console.ResetColor();
        }
        //Bootstrap.WriteLine with params
        public static void WriteLine(string style, string value, params object[] arg)
        {
            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.WriteLine(value, arg);
            Console.ResetColor();
        }

        public static void WriteLine(string value, int min, int max, params object[] arg)
        {
            Random speed = new Random();
            value = string.Format(value, arg);
            for (int t = 0; t < value.Count(); t++)
            {
                Thread.Sleep(Convert.ToInt32(speed.Next(min, max)));
                if (t != value.Count() - 1)
                {
                    Console.Write(value.Substring(t, 1));
                }
                else
                {
                    Console.WriteLine(value.Substring(t, 1));
                }
            }
        }

        public static void WriteLine(string style, string value, int min, int max, params object[] arg)
        {
            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;
            }
            Random speed = new Random();
            value = string.Format(value, arg);
            for (int t = 0; t < value.Count(); t++)
            {
                Thread.Sleep(Convert.ToInt32(speed.Next(min, max)));
                if (t != value.Count() - 1)
                {
                    Console.Write(value.Substring(t, 1));
                }
                else
                {
                    Console.WriteLine(value.Substring(t, 1));
                }
            }
            Console.ResetColor();
        }
        //Bootstrap.Alert Basic
        public static void Alert(string style, string value)
        {
            switch (style)
            {
                case "Success":
                    Console.ForegroundColor = ConsoleColor.Green;
                    Console.BackgroundColor = ConsoleColor.DarkGreen;
                    break;
                case "Information":
                    Console.ForegroundColor = ConsoleColor.Cyan;
                    Console.BackgroundColor = ConsoleColor.DarkCyan;
                    break;
                case "Warning":
                    Console.ForegroundColor = ConsoleColor.Yellow;
                    Console.BackgroundColor = ConsoleColor.DarkYellow;
                    break;
                case "Danger":
                    Console.ForegroundColor = ConsoleColor.Red;
                    Console.BackgroundColor = ConsoleColor.DarkRed;
                    break;
                default:
                    Console.ForegroundColor = ConsoleColor.Gray;
                    Console.BackgroundColor = ConsoleColor.DarkGray;
                    break;
            }
            Console.Write(value);
            Console.ResetColor();
        }

        public static void Alert(string style, string value, int min, int max)
        {
            switch (style)
            {
                case "Success":
                    Console.ForegroundColor = ConsoleColor.Green;
                    Console.BackgroundColor = ConsoleColor.DarkGreen;
                    break;
                case "Information":
                    Console.ForegroundColor = ConsoleColor.Cyan;
                    Console.BackgroundColor = ConsoleColor.DarkCyan;
                    break;
                case "Warning":
                    Console.ForegroundColor = ConsoleColor.Yellow;
                    Console.BackgroundColor = ConsoleColor.DarkYellow;
                    break;
                case "Danger":
                    Console.ForegroundColor = ConsoleColor.Red;
                    Console.BackgroundColor = ConsoleColor.DarkRed;
                    break;
                default:
                    Console.ForegroundColor = ConsoleColor.Gray;
                    Console.BackgroundColor = ConsoleColor.DarkGray;
                    break;
            }
            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));
            }
            Console.ResetColor();
        }
        //Bootstrap.Alert with params
        public static void Alert(string style, string value, params object[] arg)
        {
            switch (style)
            {
                case "Success":
                    Console.ForegroundColor = ConsoleColor.Green;
                    Console.BackgroundColor = ConsoleColor.DarkGreen;
                    break;
                case "Information":
                    Console.ForegroundColor = ConsoleColor.Cyan;
                    Console.BackgroundColor = ConsoleColor.DarkCyan;
                    break;
                case "Warning":
                    Console.ForegroundColor = ConsoleColor.Yellow;
                    Console.BackgroundColor = ConsoleColor.DarkYellow;
                    break;
                case "Danger":
                    Console.ForegroundColor = ConsoleColor.Red;
                    Console.BackgroundColor = ConsoleColor.DarkRed;
                    break;
                default:
                    Console.ForegroundColor = ConsoleColor.Gray;
                    Console.BackgroundColor = ConsoleColor.DarkGray;
                    break;
            }
            Console.Write(value, arg);
            Console.ResetColor();
        }

        public static void Alert(string style, string value, int min, int max, params object[] arg)
        {
            switch (style)
            {
                case "Success":
                    Console.ForegroundColor = ConsoleColor.Green;
                    Console.BackgroundColor = ConsoleColor.DarkGreen;
                    break;
                case "Information":
                    Console.ForegroundColor = ConsoleColor.Cyan;
                    Console.BackgroundColor = ConsoleColor.DarkCyan;
                    break;
                case "Warning":
                    Console.ForegroundColor = ConsoleColor.Yellow;
                    Console.BackgroundColor = ConsoleColor.DarkYellow;
                    break;
                case "Danger":
                    Console.ForegroundColor = ConsoleColor.Red;
                    Console.BackgroundColor = ConsoleColor.DarkRed;
                    break;
                default:
                    Console.ForegroundColor = ConsoleColor.Gray;
                    Console.BackgroundColor = ConsoleColor.DarkGray;
                    break;
            }
            Random speed = new Random();
            value = string.Format(value, arg);
            for (int t = 0; t < value.Count(); t++)
            {
                Thread.Sleep(Convert.ToInt32(speed.Next(min, max)));
                Console.Write(value.Substring(t, 1));
            }
            Console.ResetColor();
        }
        //Bootstrap.AlertLineBasic
        public static void AlertLine(string style, string value)
        {
            switch (style)
            {
                case "Success":
                    Console.ForegroundColor = ConsoleColor.Green;
                    Console.BackgroundColor = ConsoleColor.DarkGreen;
                    break;
                case "Information":
                    Console.ForegroundColor = ConsoleColor.Cyan;
                    Console.BackgroundColor = ConsoleColor.DarkCyan;
                    break;
                case "Warning":
                    Console.ForegroundColor = ConsoleColor.Yellow;
                    Console.BackgroundColor = ConsoleColor.DarkYellow;
                    break;
                case "Danger":
                    Console.ForegroundColor = ConsoleColor.Red;
                    Console.BackgroundColor = ConsoleColor.DarkRed;
                    break;
                default:
                    Console.ForegroundColor = ConsoleColor.Gray;
                    Console.BackgroundColor = ConsoleColor.DarkGray;
                    break;
            }
            Console.WriteLine(value);
            Console.ResetColor();
        }

        public static void AlertLine(string style, string value, int min, int max)
        {
            switch (style)
            {
                case "Success":
                    Console.ForegroundColor = ConsoleColor.Green;
                    Console.BackgroundColor = ConsoleColor.DarkGreen;
                    break;
                case "Information":
                    Console.ForegroundColor = ConsoleColor.Cyan;
                    Console.BackgroundColor = ConsoleColor.DarkCyan;
                    break;
                case "Warning":
                    Console.ForegroundColor = ConsoleColor.Yellow;
                    Console.BackgroundColor = ConsoleColor.DarkYellow;
                    break;
                case "Danger":
                    Console.ForegroundColor = ConsoleColor.Red;
                    Console.BackgroundColor = ConsoleColor.DarkRed;
                    break;
                default:
                    Console.ForegroundColor = ConsoleColor.Gray;
                    Console.BackgroundColor = ConsoleColor.DarkGray;
                    break;
            }
            Random speed = new Random();
            for (int t = 0; t < value.Count(); t++)
            {
                Thread.Sleep(Convert.ToInt32(speed.Next(min, max)));
                if (t != value.Count() - 1)
                {
                    Console.Write(value.Substring(t, 1));
                }
                else
                {
                    Console.WriteLine(value.Substring(t, 1));
                }
            }
            Console.ResetColor();
        }
        //Bootstrap.AlertLine with params
        public static void AlertLine(string style, string value, params object[] arg)
        {
            switch (style)
            {
                case "Success":
                    Console.ForegroundColor = ConsoleColor.Green;
                    Console.BackgroundColor = ConsoleColor.DarkGreen;
                    break;
                case "Information":
                    Console.ForegroundColor = ConsoleColor.Cyan;
                    Console.BackgroundColor = ConsoleColor.DarkCyan;
                    break;
                case "Warning":
                    Console.ForegroundColor = ConsoleColor.Yellow;
                    Console.BackgroundColor = ConsoleColor.DarkYellow;
                    break;
                case "Danger":
                    Console.ForegroundColor = ConsoleColor.Red;
                    Console.BackgroundColor = ConsoleColor.DarkRed;
                    break;
                default:
                    Console.ForegroundColor = ConsoleColor.Gray;
                    Console.BackgroundColor = ConsoleColor.DarkGray;
                    break;
            }
            Console.WriteLine(value, arg);
            Console.ResetColor();
        }

        public static void AlertLine(string style, string value, int min, int max, params object[] arg)
        {
            switch (style)
            {
                case "Success":
                    Console.ForegroundColor = ConsoleColor.Green;
                    Console.BackgroundColor = ConsoleColor.DarkGreen;
                    break;
                case "Information":
                    Console.ForegroundColor = ConsoleColor.Cyan;
                    Console.BackgroundColor = ConsoleColor.DarkCyan;
                    break;
                case "Warning":
                    Console.ForegroundColor = ConsoleColor.Yellow;
                    Console.BackgroundColor = ConsoleColor.DarkYellow;
                    break;
                case "Danger":
                    Console.ForegroundColor = ConsoleColor.Red;
                    Console.BackgroundColor = ConsoleColor.DarkRed;
                    break;
                default:
                    Console.ForegroundColor = ConsoleColor.Gray;
                    Console.BackgroundColor = ConsoleColor.DarkGray;
                    break;
            }
            Random speed = new Random();
            value = string.Format(value, arg);
            for (int t = 0; t < value.Count(); t++)
            {
                Thread.Sleep(Convert.ToInt32(speed.Next(min, max)));
                if (t != value.Count() - 1)
                {
                    Console.Write(value.Substring(t, 1));
                }
                else
                {
                    Console.WriteLine(value.Substring(t, 1));
                }
            }
            Console.ResetColor();
        }
    }
}
share|improve this question
up vote 4 down vote accepted

The main issue I can see is that you have duplicated your switch statements which map 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 you have two type of mapping: One for Write/WriteLine and one for Alert/AlterLine - you should have two dedicated methods which translate your style into the colors you require and the ncall 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.

share|improve this answer
    
Thank you for your response, I'm going to base onto your answer in my next update. – Leonel Sarmiento Oct 11 '14 at 4:19
    
Where should I post my new update? Do I need to post new review? – Leonel Sarmiento Oct 22 '14 at 23:54
1  
@Leonel: Yes just another question (maybe linked back to this one saying that you have rewritten some of the code) – ChrisWue Oct 23 '14 at 0:39

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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