Dot Net Perls

Object-Oriented Programming Benefits

by Sam Allen - Updated June 13, 2009
Object-oriented programming

Problem. 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.

Solution. 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.

1. Object-oriented programming

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.

2. How can I simplify syntax?

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.

3. Sharing code

You may have custom code that you need to use in a new project. In the new project, have 3 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.

4. How can I simplify error handling?

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.

5. How does OOP speed development?

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.

6. Why do I need interfaces?

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 us 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.

7. Is OOP practical?

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.

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.

8. Why is C# special?

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.

C# 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.

9. What about naysayers like Linus Torvalds?

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.

10. We all make mistakes

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.

11. Why is C# so slow?

It's not. 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.

When you take a talented developer, he will be free to focus on more important algorithmic problems than lower-level stuff.

12. Conclusion

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.

Dot Net Perls
Language Features | DllImport and Dllexport for... | Global Variable Use | Process.Start Command-Line Examples | Timer Tutorial | WebClient Tutorial
C# | Reflection Field Example | Validate Characters in String | Main Args Examples | Enum String Method
© 2009 Sam Allen. All rights reserved.