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

Parroting user input

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

Problem

The below may not look like much, but the ability to call methods in other classes, offloading all the work onto other classes from MainClass, and returning a variable to be passed to the next method is a rather large leap for me at least.

using System;

namespace CSharpTutoriel
{
    class Work
    {
        public static string TellMeWhatToSay(out string passTheQuote)
        {
            string whatIsWritten;
            Console.WriteLine("Tell Me What To Say");
            whatIsWritten = Console.ReadLine();
            passTheQuote = whatIsWritten;
            return passTheQuote;
        }
        public static void HelloWorld(string itWillBeSpoken)
        {
            Console.WriteLine(itWillBeSpoken);
        }
        public static void PressToContinue()
        {
            Console.WriteLine("Press any key to continue...");
            Console.ReadKey();
        }
    }

    class MainClass
    {

        public static void Main(string[] args)
        {
            string whatToSay;

            Work.TellMeWhatToSay(out whatToSay);
            Work.HelloWorld(whatToSay);
            Work.PressToContinue();
        }
    }
}

Solution

Redundancy of both out parameter and return

public static string TellMeWhatToSay(out string passTheQuote)
    {
        string whatIsWritten;
        Console.WriteLine("Tell Me What To Say");
        whatIsWritten = Console.ReadLine();
        passTheQuote = whatIsWritten;
        return passTheQuote;
    }


You both write the result to an out parameter and return it. A violation the DRY principle, this outputs the same information twice. As the out parameter does not feel appropriate for a modern language to me, I will leave only the return:

public static string TellMeWhatToSay()
    {
        string whatIsWritten;
        Console.WriteLine("Tell Me What To Say");
        whatIsWritten = Console.ReadLine();
        passTheQuote = whatIsWritten;
        return passTheQuote;
    }


Now that we eliminated that redundancy, we can simplify by returning directly:

public static string TellMeWhatToSay()
    {
        Console.WriteLine("Tell Me What To Say");
        return Console.ReadLine();
    }


Unecessary alias of a built-in Console.ReadLine

public static void HelloWorld(string itWillBeSpoken)
    {
        Console.WriteLine(itWillBeSpoken);
    }


HelloWorld is the same as Console.WriteLine so there is no need to write it.

Code Snippets

public static string TellMeWhatToSay(out string passTheQuote)
    {
        string whatIsWritten;
        Console.WriteLine("Tell Me What To Say");
        whatIsWritten = Console.ReadLine();
        passTheQuote = whatIsWritten;
        return passTheQuote;
    }
public static string TellMeWhatToSay()
    {
        string whatIsWritten;
        Console.WriteLine("Tell Me What To Say");
        whatIsWritten = Console.ReadLine();
        passTheQuote = whatIsWritten;
        return passTheQuote;
    }
public static string TellMeWhatToSay()
    {
        Console.WriteLine("Tell Me What To Say");
        return Console.ReadLine();
    }
public static void HelloWorld(string itWillBeSpoken)
    {
        Console.WriteLine(itWillBeSpoken);
    }

Context

StackExchange Code Review Q#145369, answer score: 6

Revisions (0)

No revisions yet.