C# Program Output??
-
class Program
{
static void Main(string[] args)
{
if (args.Length > 0)
{
var v = File.ReadLines("dictionary.txt");var v1 = args\[2\]; var v2 = args\[3\]; var first = v.Where(w => w == v1).First(); var last = v.Where(w => w == v2).First(); var l = Array.IndexOf(v.ToArray(), first); var l2 = Array.IndexOf(v.ToArray(), last); var l3 = l2 - l; var results = v.Skip(l).Take(l3); foreach (var result in results) { Console.WriteLine(result); } } else { Console.ForegroundColor = ConsoleColor.DarkRed; Console.WriteLine("Arguments invalid"); } }
}
// Please provide written answers to the questions below.
// 1. What is this code doing?
// 2. Comment on the quality of this code / how could you improve this implementation?
// 3. How could you verify the behaviour is correct? -
class Program
{
static void Main(string[] args)
{
if (args.Length > 0)
{
var v = File.ReadLines("dictionary.txt");var v1 = args\[2\]; var v2 = args\[3\]; var first = v.Where(w => w == v1).First(); var last = v.Where(w => w == v2).First(); var l = Array.IndexOf(v.ToArray(), first); var l2 = Array.IndexOf(v.ToArray(), last); var l3 = l2 - l; var results = v.Skip(l).Take(l3); foreach (var result in results) { Console.WriteLine(result); } } else { Console.ForegroundColor = ConsoleColor.DarkRed; Console.WriteLine("Arguments invalid"); } }
}
// Please provide written answers to the questions below.
// 1. What is this code doing?
// 2. Comment on the quality of this code / how could you improve this implementation?
// 3. How could you verify the behaviour is correct?Member 12055749 wrote:
// 1. What is this code doing?
Bugging me on a forum.
Member 12055749 wrote:
// 2. Comment on the quality of this code
It is bad.
Member 12055749 wrote:
how could you improve this implementation?
Verify if there's a third argument on the commandline before accessing it, replace the "var" declarations with something descriptive, and add exception handling and logging. I would also check the validity of the arguments at the start, and throw an exception if they are invalid, as opposed to using an
else
statement. And get rid of variable names like "v1".Member 12055749 wrote:
// 3. How could you verify the behaviour is correct?
Using a unit-test?
Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^][](X-Clacks-Overhead: GNU Terry Pratchett)
-
Member 12055749 wrote:
// 1. What is this code doing?
Bugging me on a forum.
Member 12055749 wrote:
// 2. Comment on the quality of this code
It is bad.
Member 12055749 wrote:
how could you improve this implementation?
Verify if there's a third argument on the commandline before accessing it, replace the "var" declarations with something descriptive, and add exception handling and logging. I would also check the validity of the arguments at the start, and throw an exception if they are invalid, as opposed to using an
else
statement. And get rid of variable names like "v1".Member 12055749 wrote:
// 3. How could you verify the behaviour is correct?
Using a unit-test?
Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^][](X-Clacks-Overhead: GNU Terry Pratchett)
It's not all bad. I was looking at it thinking, surely that should be File.ReadAllLines, but in fact no, File.ReadLines is there too. Didn't know that and now I know the subtle difference. Something new, every day.
Regards, Rob Philpott.
-
class Program
{
static void Main(string[] args)
{
if (args.Length > 0)
{
var v = File.ReadLines("dictionary.txt");var v1 = args\[2\]; var v2 = args\[3\]; var first = v.Where(w => w == v1).First(); var last = v.Where(w => w == v2).First(); var l = Array.IndexOf(v.ToArray(), first); var l2 = Array.IndexOf(v.ToArray(), last); var l3 = l2 - l; var results = v.Skip(l).Take(l3); foreach (var result in results) { Console.WriteLine(result); } } else { Console.ForegroundColor = ConsoleColor.DarkRed; Console.WriteLine("Arguments invalid"); } }
}
// Please provide written answers to the questions below.
// 1. What is this code doing?
// 2. Comment on the quality of this code / how could you improve this implementation?
// 3. How could you verify the behaviour is correct?1. V = Extract dictionary.txt file content to a string V1 = Contains string[] 3rd element V2 = Contains string[] 4th element First = Extract from V the first string matching with V1 Last = Extract from V the first string matching with V2 l = Get from V the index of First l2 = Get from V the index of Last l3 = Length of Last - First Results = Skip first element and return next l3 elements Print Results The code is reading dictionary.txt and print all results between index of 3rd and 4th array element. 2. This code is hard to read. Variable names aren't intuitive. Doesn't seem to be necessary use var because some types aren't anonymous. ReadLines return a string[], V1 and V2 are strings, l, l2 and l3 are integer.
-
class Program
{
static void Main(string[] args)
{
if (args.Length > 0)
{
var v = File.ReadLines("dictionary.txt");var v1 = args\[2\]; var v2 = args\[3\]; var first = v.Where(w => w == v1).First(); var last = v.Where(w => w == v2).First(); var l = Array.IndexOf(v.ToArray(), first); var l2 = Array.IndexOf(v.ToArray(), last); var l3 = l2 - l; var results = v.Skip(l).Take(l3); foreach (var result in results) { Console.WriteLine(result); } } else { Console.ForegroundColor = ConsoleColor.DarkRed; Console.WriteLine("Arguments invalid"); } }
}
// Please provide written answers to the questions below.
// 1. What is this code doing?
// 2. Comment on the quality of this code / how could you improve this implementation?
// 3. How could you verify the behaviour is correct?Of course, the easy way for you to answer the questions would be to actually run the program and step through it. You'll be amazed at what you can learn.
-
class Program
{
static void Main(string[] args)
{
if (args.Length > 0)
{
var v = File.ReadLines("dictionary.txt");var v1 = args\[2\]; var v2 = args\[3\]; var first = v.Where(w => w == v1).First(); var last = v.Where(w => w == v2).First(); var l = Array.IndexOf(v.ToArray(), first); var l2 = Array.IndexOf(v.ToArray(), last); var l3 = l2 - l; var results = v.Skip(l).Take(l3); foreach (var result in results) { Console.WriteLine(result); } } else { Console.ForegroundColor = ConsoleColor.DarkRed; Console.WriteLine("Arguments invalid"); } }
}
// Please provide written answers to the questions below.
// 1. What is this code doing?
// 2. Comment on the quality of this code / how could you improve this implementation?
// 3. How could you verify the behaviour is correct?This smells like homework. What do you think it does? What do you think of the code quality?
"I've seen more information on a frickin' sticky note!" - Dave Kreskowiak
-
It's not all bad. I was looking at it thinking, surely that should be File.ReadAllLines, but in fact no, File.ReadLines is there too. Didn't know that and now I know the subtle difference. Something new, every day.
Regards, Rob Philpott.
-
This smells like homework. What do you think it does? What do you think of the code quality?
"I've seen more information on a frickin' sticky note!" - Dave Kreskowiak
especially if you look at the poster's profile :D Best, John
-- LogWizard - Log Viewing can be a joy!