How could Reflection not lead to code smells?

后端 未结 18 2077
失恋的感觉
失恋的感觉 2020-12-12 15:02

I come from low level languages - C++ is the highest level I program in.

Recently I came across Reflection, and I just cannot fathom how it could be used without cod

18条回答
  •  粉色の甜心
    2020-12-12 15:20

    Without reflection you often have to repeat yourself a lot.

    Consider these scenarios:

    • Run a set of methods e.g. the testXXX() methods in a test case
    • Generate a list of properties in a gui builder
    • Make your classes scriptable
    • Implement a serialization scheme

    You can't typically do these things in C/C++ without repeating the whole list of affected methods and properties somewhere else in the code.

    In fact C/C++ programmers often use an Interface description language to expose interfaces at runtime (providing a form of reflection).

    Judicious use of reflection and annotations combined with well defined coding conventions can avoids rampant code repetition and increase maintainability.

提交回复
热议问题