With the readnumbersfromfile method in place we can

Info icon This preview shows pages 80–82. Sign up to view the full content.

View Full Document Right Arrow Icon
With the ReadNumbersFromFile method in place, we can now write this sort of code: double[] lapTimes = ReadNumbersFromFile("LapTimes.txt"); double[] fuelLevels = ReadNumbersFromFile("FuelRemainingByLap.txt"); 56 | Chapter 2: Basic Programming Techniques
Image of page 80

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
The Call Stack When you invoke a method, the CLR allocates some memory to keep track of that method’s state. This state includes incoming arguments and local variables. When a method calls out to another method, the method state also remembers where we were in the calling method’s code to be able to carry on later. If you have nested method calls—if a first method calls a second method which calls a third method, for example—you end up with a sequence of method states, and this sequence is often referred to as the call stack . In general, a stack is a sequence of items where you can add or remove items only at the end of the sequence; by convention, we use the terms push and pop to describe adding and removing stack items. So when C# code invokes a new method, it pushes a new method state record onto the call stack. When the method returns, either because execution reaches the end or because we’ve hit a return statement, the current method state is popped from the call stack, and then execution resumes from where the previous method state record says the calling method had reached. You can look at the call stack in the Visual Studio debugger. The Debug Win- dows Call Stack menu item displays a window showing a list of all the current methods in the call stack. You can double-click on any of these items to see the current location, and if you’ve opened any of the debug windows that show local variable state from the Debug Windows menu, these will show local variables for the method you select. It doesn’t take a lot of effort to understand that this code is reading in numbers for lap times and fuel levels from a couple of text files—the code makes this aspect of its behavior much clearer than, say, Example 2-12 . When code does what it says it does, you make life much easier for anyone who has to look at the code after you’ve written it. And since that probably includes you, you’ll make your life easier in the long run by moving functionality into carefully named methods. This idea of moving code out of the middle of one method and into a separate method is very common, and is an example of refactoring . Generally speaking, refactoring means restructuring code without changing its behavior, to either simplify it, make it easier to understand and maintain, or avoid duplication. There are so many ways to refactor code that whole books have been written on the topic, but this particular refactoring operation is so useful that Visual Studio can automate it. If you select some code and then right-click on the C# editor window, it offers a Refactor Extract Method menu item that does this for you. In practice, it’s not always that straightforward—you might need to re-
Image of page 81
Image of page 82
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern