Computer programs become ever more complex. Hello World in C# involves millions of lines of code. Developers need to reuse old code. They need ways to add new features without changing other code.
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.
OOP:It is normally called object-oriented programming, but it is also component-oriented programming.
Tip:Good OOP is bound to design patterns. These patterns are basically the blueprints of quality object design.Class, Design Patterns
Objects provide syntax clues. 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.NewConstructor
Sharing code. In a new project, you 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.Inheritance
Tip:It improves reusability. You haven't changed any of the old code. 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.
So:It causes less breakage. In older languages, you can add functionality, but this breaks other parts of your project.
However:With OOP, changing the implementation of one part is safer, because it won't affect the way the code is called.
Exceptions 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.
So:With exception handling, we can separate the error handling from the really important thrust of the program.Exception
OOP can both make development faster, and your programs run faster. We are living in a world in which every person is rushed. As developers we are always learning and trying new things. OOP provides separation of various objects.
Tip:You can use the proxy design pattern to meter access to expensive parts of your code. Just call into the proxy to access a resource.Protection Proxy
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.Singleton Class
It has better IDEs. Object orientation can provide many clues to your development environment. A program like Visual Studio can make smarter guesses about what you intended to do, and what you may want to do next.Visual Studio
So:It speeds development and execution. Almost always, these two are dependent on one another.
And:With a language like C#, we 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 developers' code. They provide organization and sanity to large projects. Interfaces help create a blueprint of a module.
Then:Others can help build it to specification. Interfaces don't provide anything beyond syntax or organization.Interface
OOP is practical. It is really not a new paradigm or a revolutionary new thing. Everything on your brand new 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.
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.
And:Code quality is improved. The C# language enforces that every method is in a class, which results in better organization.
Thus:It is a practical language based on organization and helping programmers write the code that they intend to write.
Linus Torvalds 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.
We are not perfect. We make mistakes. Almost every program has flaws. 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.
Therefore:If you are using a C# program that is slow, it is almost positively not the fault of the C# language.
Note:New languages like Java and C# are extremely fast and often faster than their C equivalents.Class, Java
And:When you take a talented developer, he or she is free to focus on more important algorithmic problems.
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 a practical way to write programs.
So:OOP enables those of us who are not computer science geniuses to write programs that are good and fill the needs of the market.