June 24, 2011

Reflection

Now, to most people reflection would mean looking back at yourself and thinking about where you have been and where you want to go. For me... It means a way of programming. Reflection is an exceptionally powerful tool for when you want to do things that OOP (Object-orientated programming) doesn't normally allow.

First of all, reflection gives the programmer unlimited power. (This is when you would hear an evil laugh in the background). To be fair, it basically completely breaks OOP at some of its basic levels. So what am I saying? It allows for you to directly access any part of any class at will. Private and protect contructors, methods, fields, etc. are all at your finger tips. As you can imagine that's pretty insane, you can even define classes dynamically on the fly. Say you want method y on class x, well you can do that. AOP (aspect-orientated programming) uses this concept and defines an entire programming paradigm around using reflection.

Reflection is very useful in the following scenarios:

  • You are testing a class and want to validate private/protected parts of it
  • You are creating a framework that needs to load plugins on the fly
  • You want to try some AOP
I'm sure there are many more reasons, but those are plenty to learn about Reflection. Reflection also allows you to define new test cases that you never thought were possible. Like testing the class structure itself. You can test which constructor signatures exist, which ones are accessible or not. You can test for methods or fields existing as well. Or test those which do not have public access too and even set the private fields prior to running the test. This means you can not only test the external but also the internal behavior of your classes. This opens up a huge new door to testing, one that is neglected in academics. 

So all this great stuff? Whats the catch?

Simple, performance. When you use reflection you take huge performance hits. So why would you use it? Well, if you need to access something in a non-standard OOP way you can do it. As long as you don't do it often that's perfectly fine. Loading an external set of plugins is basically effortless with reflection. You don't exactly do that very often but now you can. Another aspect that most people wouldn't want is the fact you can load and unload classes during run-time so you can use multiple versions of the same class in your program. Not, that you should do that, but the possibility that you can is a very interesting one.

Why am I talking about Reflection? Well, I plan to use it a little bit to make my developments significantly easier. Since I'm creating a framework, being able to load up various games into the framework would provide me with a very effective way to isolate the framework from the games. I also plan to use to help assist with testing. This way I can modulate testing of the framework from any actual games as well which is pretty sweet.

No comments :

Post a Comment