views:

420

answers:

7

I have a friend who is interested in getting into programming and is asking about a lot of different concepts. Classes, interfaces and things like polymorphism / inheritence have been easy to explain, but I'm struggling a bit with analogies for reflection.

How would you explain what it is and how it works in practice?

+18  A: 

Reflection is simply the ability of an object to tell you about itself, its methods, instance variable, type etc. To use the metaphor its named after its like looking in a mirror and seeing yourself. In that way you can describe yourself to someone else. In the same way reflection is the ability for an object (or even program) to describe itself.

ennuikiller
Reflection is a misnomer - it's not about seeing oneself in a reflected image, it's all about dynamic vs. static.
Eamon Nerbonne
@Eamon: please reflect on the fact that there is more than one meaning for the word "reflect" in the English language.
Stephen C
+3  A: 

"In most programming, the data and the operations are two separate things (operations work on data). With reflection, the operation becomes data, allowing the program to know about, and even modify, itself."

Although that may be too simplistic?

Edit: Also, if you want an analogy, the name reflection provides a good one. Reflection is like the program can look in a mirror and see itself. So it can call methods while only knowing their names as a string, and get a list of method names in strings, etc.

Sean Nyman
+1  A: 

Before you explain reflection, it would be a good idea to explain the difference between statically typed languages vs. dynamic typing. It would be a challenge to explain the need for something like reflection to a non-programmer however. If they ask enough questions, you'll inevitably end up having to explain the disadvantages of using reflection...

Thorarin
Yeah, this is a MUCH better analogy than the mirror concept. Any static object can call it's own methods even without Reflection, and reflection can be used to call things on others. Reflection is _not_ about seeing an image of oneself, it's about the static/dynamic divide.
Eamon Nerbonne
+1  A: 

Imagine a class being a car. It has some properties like:

  • steering wheel
  • tyres
  • engine
  • etc.

It also has some methods:

  • shift(gear)
  • turn(side)
  • etc.

Imagine you get some new car (latest and greatest), that you don't know anything about. Nobody told you anything about it. So you turn to reflection. Mechanic. He'll look under the hood and tell you what kind of engine does it have. What other properties are there for you to use and methods to call...

In the end you'll be able to drive this new car, because you know what properties/methods does it have.

Robert Koritnik
A: 

If your friend knows a dynamic language like Python, it might be helpful to explain how reflection is implemented; i.e. that objects are just dictionaries, and that reflection allows you to look at an object in its dictionary form rather than as an object is normally treated.

Imagist
+3  A: 

I wouldn't. Reflection is a programming concept, and it is only relevant to a programmer. If you're not a programmer, you don't need, or want, to know about it any more than I need to know the technical implementation details of building a nuclear reactor.

Since the person is interested in learning programming anyway, I don't really see the problem. Help him learn programming, and sooner or later he'll have the vocabulary necessary to discuss reflection, and then you can tell him what it is if he needs it.

jalf
A: 

The mirror analogy is imperfect as it implies reflection is something an object does to itself. Any object or function can reflect on another object. Furthermore, in some implementations there are separate classes or functions for reflection; you don't call methods or access properties of an abject to reflect on it.

A competing analogy is consciousness. A conscious entity can examine an object in the real world and describe its properties, which is what programmatic reflection is all about. The problem with this analogy is that consciousness implies cognitive awareness on the part of the entity while reflection doesn't. As long as you include this caveat, it isn't misleading.

Rather than (misleading) analogies, how about some illustrative uses? Reflection is an easy way of implementing one of the central tasks of a debugger: examining objects stored in variables. Storing and retrieving objects from an external datastore is made easier with reflection because you don't have to write a serializer for each class. A universal serializer can instead use reflection to examine the properties of an object and use that to create a database schema.

outis
It's rather helpful to know why an answer is downvoted.
outis