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

Morse code translator in C#

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

Problem

I'm writing a Morse code translator for homework in C#. It takes an input from the user and returns the Morse code version of their input. I understand that this code may look horrible, so how could I improve it in terms of efficiency and readability?

Program.cs

```
using System;
using System.Collections.Generic;

namespace MorseCodeTranslator
{
class Program
{
static Dictionary translator;

static void Main(string[] args)
{
InitialiseDictionary();
getUserInput();
}

private static void InitialiseDictionary()
{
char dot = '.';
char dash = '−';

translator = new Dictionary()
{
{'a', string.Concat(dot, dash)},
{'b', string.Concat(dash, dot, dot, dot)},
{'c', string.Concat(dash, dot, dash, dot)},
{'d', string.Concat(dash, dot, dot)},
{'e', dot.ToString()},
{'f', string.Concat(dot, dot, dash, dot)},
{'g', string.Concat(dash, dash, dot)},
{'h', string.Concat(dot, dot, dot, dot)},
{'i', string.Concat(dot, dot)},
{'j', string.Concat(dot, dash, dash, dash)},
{'k', string.Concat(dash, dot, dash)},
{'l', string.Concat(dot, dash, dot, dot)},
{'m', string.Concat(dash, dash)},
{'n', string.Concat(dash, dot)},
{'o', string.Concat(dash, dash, dash)},
{'p', string.Concat(dot, dash, dash, dot)},
{'q', string.Concat(dash, dash, dot, dash)},
{'r', string.Concat(dot, dash, dot)},
{'s', string.Concat(dot, dot, dot)},
{'t', string.Concat(dash)},
{'u', string.Concat(dot, dot, dash)},
{'v', string.Concat(dot, dot, dot, dash)},
{'w', string.Concat(dot, dash, dash)},
{'x', string.Concat(

Solution

Is translator really a translator? IMO a better name could be dictionary.

I also don't understand an idea to create variables like dot and dash.
{'a', ".-"} would be much clearer.

Don't use misleading function names. The function getUserInput does much more than getting something from user! It also translates the text and prints it. So, in Main, I'd write:

input = getUserInput();
morse = translate(input);
PrintOnScreen(morse);


Be consistent in naming: now some functions have names starting with capital letter, some not.

Code Snippets

input = getUserInput();
morse = translate(input);
PrintOnScreen(morse);

Context

StackExchange Code Review Q#122480, answer score: 7

Revisions (0)

No revisions yet.