Review the solution to the improve code readability challenge activity

The following code is one possible solution for the challenge from the previous unit.

c#Copy

/*
   This code reverses a message, counts the number of times 
   a particular character appears, then prints the results
   to the console window.
 */

string originalMessage = "The quick brown fox jumps over the lazy dog.";

char[] message = originalMessage.ToCharArray();
Array.Reverse(message);

int letterCount = 0;

foreach (char letter in message)
{
    if (letter == 'o')
    {
        letterCount++;
    }
}

string newMessage = new String(message);

Console.WriteLine(newMessage);
Console.WriteLine($"'o' appears {letterCount} times.");

This code is merely “one possible solution“. You may have come up with some different variable names and different vertical spacing and tab indentation. Here’s a list of changes that were made.

  • The code includes a higher-level description of what the entire code listing is attempting to accomplish in a multi-line comment at the top. You could argue that this is a small improvement over the original code comments, however, given the challenge’s description of the code, there wasn’t much more context available.
  • The individual comments were removed because they weren’t providing any real insight into the code’s purpose or function.
  • Several blank lines were added to improve the phrasing of the code listing. Keep code lines together when they appear similar, or when they work with each other to accomplish a small task.
  • Line feeds and tabs were added to improve the appearance of the foreach statement and the if statement.
  • Local variable naming conventions were applied to better convey the purpose of each value.

If you identified the same issues and addressed them in a similar way, congratulations! Continue on to the knowledge check in the next unit.

https://lernix.com.my/ibm-cognos-bi-training-courses-malaysia

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *