Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. C# Program Output??

C# Program Output??

Scheduled Pinned Locked Moved C#
questioncsharpdata-structurescode-review
8 Posts 7 Posters 1 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • U Offline
    U Offline
    User 12022308
    wrote on last edited by
    #1

    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?

    L N P P 4 Replies Last reply
    0
    • U User 12022308

      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?

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      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)

      R 1 Reply Last reply
      0
      • L Lost User

        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)

        R Offline
        R Offline
        Rob Philpott
        wrote on last edited by
        #3

        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.

        L 1 Reply Last reply
        0
        • U User 12022308

          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?

          N Offline
          N Offline
          Nelson Costa Inacio
          wrote on last edited by
          #4

          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.

          1 Reply Last reply
          0
          • U User 12022308

            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?

            P Offline
            P Offline
            Pete OHanlon
            wrote on last edited by
            #5

            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.

            1 Reply Last reply
            0
            • U User 12022308

              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?

              P Offline
              P Offline
              Paul Conrad
              wrote on last edited by
              #6

              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

              J 1 Reply Last reply
              0
              • R Rob Philpott

                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.

                L Offline
                L Offline
                Lost User
                wrote on last edited by
                #7

                Rob Philpott wrote:

                It's not all bad.

                That's true; it is indented correctly :-\

                Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^][](X-Clacks-Overhead: GNU Terry Pratchett)

                1 Reply Last reply
                0
                • P Paul Conrad

                  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

                  J Offline
                  J Offline
                  John Torjo
                  wrote on last edited by
                  #8

                  especially if you look at the poster's profile :D Best, John

                  -- LogWizard - Log Viewing can be a joy!

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups