According to the basics of structured programming, the program should be broken down into small functions that can be reused.
If an anonymous object / function is used, an alien code is inserted into the function text that cannot be reused and that breaks the control flow. Moreover, it often has nothing to do with the function in which it is inserted. For example, the body of the button click handler in the OnCreate Activity.
At first glance, the following possible reasons for the existence of anonymous classes / functions are striking me:
- The lack of references to functions in Java (in versions less than 8), which is why event handlers have to be assigned with writing a bunch of extra characters and anonymous classes as an attempt to make it easier.
- The desire to simplify the writing of code as much as possible, while completely neglecting its readability and clarity.
But I am aware of the fact that I do not know everything, and there may be valid reasons for the existence of anonymous classes and functions.
What are these reasons?
In what cases is the use of these mechanisms justified?
Should we consider them only as a syntactic way to circumvent the lack of language and not use more than one operator in such functions?