In general when we are developing software were

Info icon This preview shows pages 99–102. Sign up to view the full content.

View Full Document Right Arrow Icon
In general, when we are developing software, we’re trying to make it as easy as possible for other developers (including our “future selves”) to do the right thing, almost by accident. You’ll often hear this approach called “designing for the pit of success.” The idea is that people will fall into doing the right things because of the choices you’ve made. Some aspects of an object don’t fit well as either a normal modifiable field or a constant value. Take the plane’s identifier, for example—that’s fixed, in the sense that it never changes after construction, but it’s not a constant value like kilometersPerMile . Dif- ferent planes have different identifiers. .NET supports this sort of information through read-only properties and fields, which aren’t quite the same as const . Defining Classes | 75
Image of page 99

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

View Full Document Right Arrow Icon
Read-only Fields and Properties In Example 3-5 , we made our Plane class’s Identifier property private . This prevented users of our class from setting the property, but our class is still free to shoot itself in the foot. Suppose a careless developer added some code like that in Example 3-15 , which prints out messages in the SpeedInMilesPerHour property perhaps in order to debug some problem he was investigating. Example 3-15. Badly written debugging code public double SpeedInMilesPerHour { get { return SpeedInKilometersPerHour / kilometersPerMile; } set { Identifier += ": speed modified to " + value; Console.WriteLine(Identifier); SpeedInKilometersPerHour = value * kilometersPerMile; } } The first time someone tries to modify a plane’s SpeedInMilesPerHour this will print out a message that includes the identifier, for example: BA0048: speed modified to 400 Unfortunately, the developer who wrote this clearly wasn’t the sharpest tool in the box—he used the += operator to build that debug string, which will end up modifying the Identifier property. So, the plane now thinks its identifier is that whole text, in- cluding the part about the speed. And if we modified the speed again, we’d see: BA0048: speed modified to 400: speed modified to 380 While it might be interesting to see the entire modification history, the fact that we’ve messed up the Identifier is bad. Example 3-15 was able to do this because the SpeedInMilesPerHour property is part of the Plane class, so it can still use the private setter. We can fix this (up to a point) by making the property read-only—rather than merely making the setter private , we can leave it out entirely. However, we can’t just write the code in Example 3-16 . Example 3-16. The wrong way to define a read-only property class Plane { // Wrong! public string Identifier { get; } 76 | Chapter 3: Abstracting Ideas with Classes and Structs
Image of page 100
... } That won’t work because there’s no way we could ever set Identifier —not even in the constructor. Auto properties cannot be read-only, so we must write a getter with code.
Image of page 101

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

View Full Document Right Arrow Icon
Image of page 102
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