03.01.2015 Views

C# 5.0 Programmer's Reference

Visual Studio 2013 C# 5.0 Programmer's Reference

Visual Studio 2013 C# 5.0 Programmer's Reference

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

250 ❘ CHAPTER 11 OOP Concepts<br />

You could probably replace all those methods with a single DrawShape method that draws and outlines<br />

or fills various kinds of shapes depending on the parameters that it received. That would make the code<br />

harder to understand because you would have to carefully study the parameters in a specific call to figure<br />

out what it was doing. By using separate methods, the class makes the code obvious. If the program<br />

calls DrawRectangle, it is drawing a rectangle.<br />

Don’t Overgeneralize<br />

Creating methods that try to do too much is sometimes called the “kitchen sink”<br />

approach because the method contains everything “including the kitchen sink.”<br />

Sometimes it’s easy to slip into this approach by trying to generalize a method<br />

unnecessarily.<br />

For example, suppose you need a method to print an invoice. Some other part of the<br />

program needs to print an employee timesheet. Because they both involve printing,<br />

you might try to make a single method to do both. That’s probably a mistake. The<br />

two tasks both involve printing and may require common printer setup code, but<br />

they don’t have much in common logically. (The fact that you might want to name<br />

the method something confusing like PrintInvoiceOrTimeSheet is a hint that the<br />

method is being asked to do too much.)<br />

A better approach would be to make separate PrintInvoice and PrintTimeSheet<br />

methods. If they share a lot of common code, then you can move that code into a<br />

PrintMethods class that handles printing details. For example, that class might<br />

provide SelectPrinter, InitializePrinter, and FinishPrinting methods.<br />

When you are working on the PrintInvoice method, you will be focused on<br />

printing, so calling SelectPrinter, InitializePrinter, and FinishPrinting<br />

makes sense.<br />

When other pieces of code need to print an invoice, they simply call PrintInvoice<br />

and the details of setting up the printer are hidden.<br />

Making methods perform a single, tightly focused task can be a difficult concept for beginning<br />

programmers. Adding more features seems like it would give developers more power, so you might<br />

think it would make their jobs easier. However, it often makes development more confusing and<br />

difficult. Instead of thinking in terms of giving the developer more power, you should think about<br />

the fact that this approach gives the developer more things to worry about and more ways to make<br />

mistakes. Ideally, you should not expose any more features than the developer actually needs.<br />

Inheritance<br />

Inheritance lets you derive a child class from a parent class. The child class inherits all the properties,<br />

methods, and events defined by the parent class. It can then modify, add to, or subtract from<br />

the parent class. Making a child class inherit from a parent class is also called deriving the child<br />

class from the parent, and subclassing the parent class to form the child class.<br />

www.it-ebooks.info

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!