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:

  1. 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.
  2. 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?

  • one
    Links to methods in Java there! - Olexiy Morenets
  • Oleksiy Morenets, I corrected the question. - user308670 pm

1 answer 1

The same click handler is a callback pattern (callback) whose meaning is to perform a delayed action on an event. At the same time, the action is not defined at the stage of creating the API (it is not known in advance which actions will be required when pressing a particular button - this is on the issue of an alien code). The mechanism itself is based on the implementation of interfaces in Java, which itself implies the absence of the method body with the subsequent implementation (at the stage of creating a specific program) of an arbitrary algorithm of actions.

An anonymous interface implementation is not a necessary condition at all, but an opportunity that is used when we do not need reuse . If the reaction response code has to be reused in other classes or places of the code, then nothing prevents you from making a full implementation of the interface (both implementation into a class and a separate class) and reusing it an unlimited number of times, for example - one click handler for multiple buttons.

You may not use anonymous classes at all (in favor of a full-fledged implementation) if you see this as a problem for yourself. By themselves, anonymous implementations are no attempts to circumvent non-existent flaws, but one of the possibilities that you can use, but you can not. There is no need to look for great meaning and special reasons where there are none. Using these mechanisms is justified in all cases where you, as a programmer, find their use appropriate in your code.

  • Callback is simply implemented through a link (pointer) to the function. It is enough to declare a function and then assign a link to it. - user308670
  • Button1.OnClick = myButtonClick; And through the implementation of the interface in Java, you have to write a lot of unnecessary - to declare a class, and even as a rule, to declare in it a reference to the parent in order to access its fields and methods. A lot of syntactic garbage instead of a single line of code. An anonymous class partly solves the problem (less to write, you can refer to the fields and methods of the object in which it is created), but it creates new ones (see the text message) - user308670
  • What you write is similar to wpf (and sharpe in general). If this is so, then behind the external simplicity lies a large amount of code generated by the compiler without your participation, this is exactly synthetic sugar in its pure form, and not what you write about Java, but it is verbose, but transparent and flexible due to of this. In general, each language and platform is its own rules and it is necessary to “play” according to the rules of the language in which you write, without projecting the Sharp technique on Java - pavlofff
  • If you need a large amount of synthetic sugar, as in Sharp, when developing on Android, then you should pay attention to the Kotlin language, so setting the listener to the button there looks like: button1.setOnClickListener{// действия при клике} , but behind this external simplicity as well as Sharpe costs a large amount of auto - generated code - pavlofff
  • I do not know what lies in Kotlin, but in classical programming languages, assigning a pointer to a function is simply copying the address. In the case of a pointer to a method, as in Delphi - copying two addresses - the addresses of the method and the address of the object. This is a very simple and effective operation. In Java, they did not at first realize the opportunity, created a problem for themselves, and then began to look for workarounds and wind up difficulties. But really, in Java 8, function pointers are not implemented in the obvious way, but also through ... sugar? - user308670 1:19 pm