Computer programs become ever more complex. Hello World in C# involves millions of lines of code. We need to reuse old code, have ways to add new features to old code without changing other code that also uses it. We need to manage allocations and associate behaviors with data. We want good syntax, fair performance, and maintainability.
The examples you find all talk of Car objects and Employee classes. But what are the real benefits of object-oriented design, and why is it important? Here I detail my impressions about object-oriented design, and why it has very little to do with Car objects or Employee classes.
I will name the solution object-oriented programming, but it is also called component-oriented programming. Good OOP is bound to design patterns. These patterns are basically the blueprints of quality object design. Here are some questions and how they are answered with OOP.
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. You can duplicate this, but with native OOP languages, the compiler and build environments are ready for it.
You may have a very polished and useful block of code that you need to use in a new project. However, you now have 3 additional requirements. In old languages, you would need change the main implementation of that good code. With C# and OOP, you can simply inherit the old code and make a new object from it.
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.
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. Object-oriented programming is, as I have said, mostly syntax. It provides separation of various objects. Let's look at some examples.
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. They can help a developer 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.
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. 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. It enforces every method to be in a class, which in the end will result 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.
Not perfect. Software developers are not perfect--not even close to 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.
If you are using a C# program that is slow, it is almost positively not the fault of C#. New languages like Java and C# are extremely fast and often faster than their C equivalents. This is because 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. It offers 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.