Code introspection
Most developers end up working in code they did not write.
One way to learn what it does is to ask the running program. That is introspection. These notes explain it and show where it lives in several common languages.
What it means
Introspection is a program looking at its own parts while it runs. It can ask what type a value has, what methods an object offers or what arguments a function takes.
Many languages call the fuller version reflection. The Go blog describes reflection as the ability of a program to examine its own structure, particularly through types. Each language's model is different, and many languages do not support it at all.
Python
Python has introspection built in. type() returns the type of an object. isinstance() checks whether an object belongs to a class or one of its subclasses. dir() lists an object's attributes, and help() opens the built-in help.
The inspect module goes further. It can examine the contents of a class, return a function's signature and retrieve the source code of a method. inspect.getsource() raises an error for built-in modules, classes and functions.
Java and .NET
Java's reflection lets a program examine classes at run time. Oracle's tutorial lists the common uses. Class browsers list the members of a class. Debuggers examine private members. Test tools call a discoverable set of methods to cover more code.
In .NET, the System.Reflection classes and System.Type give information about loaded assemblies and the types in them. A program can find a method's name, return type and parameters, then invoke it.
Go and JavaScript
Go's reflect package works from a value's dynamic type. reflect.TypeOf returns its type and reflect.ValueOf returns its run-time data. A reflected value can only be changed if it is settable.
JavaScript's Reflect object holds static methods with the same names as proxy handler methods. Its main use is to give default behaviour inside a Proxy.
What it costs
Oracle's tutorial says to avoid reflection when an operation can be done without it. Reflective calls run more slowly, because the virtual machine cannot apply some optimisations. Reading private fields breaks abstractions, so the code may behave differently after a platform upgrade.
The Go blog gives similar advice. Reflection is a powerful tool that should be used with care and avoided unless strictly necessary.
For learning a codebase, that advice still leaves plenty of room. Calling help() or inspect.signature() in an interactive session reads the program without changing it.