
You see computer programs becoming ever more complex. Hello World in C# involves millions of lines of code. Developers need to reuse old code, have ways to add new features to old code without changing other code that also uses it. You need to manage allocations and associate behaviors with data. And with all this, you want good syntax, fair performance, and maintainability. Here we examine OOP.
Most examples show Car objects and Employee classes. But what are the real benefits of object-oriented design, and why is it important? OOP has very little to do with Car objects or Employee classes.
The solution is normally called object-oriented programming, but it is also component-oriented programming. Good OOP is bound to design patterns. These patterns are basically the blueprints of quality object design.
Objects provide syntax clues to how you can use them. To make an object, you use new. Objects provide accessor methods that manage access to their internals. With native OOP languages, the compiler and build environments are designed for this.
You may have custom code that you need to use in a new project. In the new project, have three additional requirements. In old languages, you would need change the existing code. With C# and OOP, you can simply inherit the old code into a new class.
It improves reusability. You haven't changed any of the old code—you have just added new code on top of it. This means you haven't introduced any new bugs in old code.
Old callers don't need changes. Your old, reliable code doesn't need to be cloned just to add some functionality to it. The existing callers can still function as-is. No changes were made to them or the libraries they use.
It causes less breakage. In older languages, you can add functionality, but when the code you are changing doesn't have a clear calling contract, it will break other parts of your project. With OOP, changing the implementation of one part is safer, because it won't affect the way the code is called.
We can use exceptions to improve error handling. These allow us to separate errors from what the main intent of the program is. When you write code to list objects in an array, checking for null or invalid numbers may not be its primary purpose. With exception handling, we can separate the error handling from the really important thrust of the program. Clearer code makes maintenance, and future improvements, easier.
OOP can both make development faster, and your programs run faster. We are living in a world in which every person is rushed and frantic in life. As developers, we are always learning and trying new things. OOP provides separation of various objects.
You can use the proxy design pattern to meter access to expensive parts of your code. Now, you can just call into the proxy to access a resource. If there is a problem, the proxy can reject the request. This can be used to eliminate needless requests to databases or files.
See Protection Proxy Design Pattern.
It allows single-instance code. In old languages, you can use global variables and initialize them when you need them. But the singleton design pattern offers a way to simplify the code that does that. Singletons manage their state, can be lazily instantiated, and prevent memory wasting.
It has better IDEs. Object orientation can provide many clues to your development environment. A program like Visual Studio 2008 can make smarter guesses about what you intended to do, and what you may want to do next. This is because OOP has solid contracts between objects with interfaces.
See Visual Studio 2008 Review.
It speeds development and execution. Almost always, these two are dependent on one another. With a language like C#, a programmer of equal capabilities can spend more time on designing a better algorithm, rather than writing lower-level code.
Interfaces are a concession to the limitations of programmers. We can't remember every detail of how to use other people's code. They provide organization and sanity to large projects. Interfaces help you create a blueprint of a module, and then others can help build it to specification. They don't provide anything beyond syntax or organization, and certainly nothing that can't be done in C.
Yes. In fact, the whole point of OOP is that it is practical. It is really not a new paradigm or a revolutionary new thing. Everything on your Core 2 Quad processor is still running machine code. Above that there is still assembly language.
Also, object-orientation provides us with more organization and simpler syntax. Every part of this design is about practicality, and some parts have proven to be more practical than others.
C# is special because it eliminates the hard stuff. It has a compiler that basically rejects any code that looks like it could be buggy, whether it is or not. It takes several approaches to prevent programmers from making mistakes.
Further, code quality is improved. The C# language enforces that every method is in a class, which results in better organization. It is a practical language based on organization and helping programmers write the code that they intend to write.
As you know, Linus is the guy who wrote the original code for Linux. He is outspoken about C++ and newer object-oriented languages. However, the software industry that gets most stuff done is totally committed to OOP. The difference between the two viewpoints is that objects are useful for getting programs written and working well fast. Linus didn't have a deadline to ship his operating system, but almost every commercial project does.
Software developers are not perfect. We make mistakes and almost every program has flaws and could be done better. But we are people who have to work and support our families and pay rent. Object-orientation is a compromise between exacting design and practicality.
It's not. If you are using a C# program that is slow, it is almost positively not the fault of the C# language. New languages like Java and C# are extremely fast and often faster than their C equivalents. When you take a talented developer, he will be free to focus on more important algorithmic problems than lower-level stuff.
If you want to program embedded systems or OS kernels, you probably won't want to work with C# or even OOP in general. For the rest of us, 90% of the software community, OOP is the most practical way to write programs. OOP practices are a sturdy compromise between performance and clarity. It enables those of us who are not computer science geniuses to write programs that are very good and that fill the needs of the market.