top of page

different styles when coding #4

at the release of java 8 by oracle, the new lambda expressions were included. that feature was something so new, that i didn't dare to touch it. to be fair, the release was in the middle of my study and java 7 was already pretty new to me. but after my graduation, at my first job, i got in touch with these ominous lambdas. the beginning was hard, the syntax was new, but when time was passing i gained more and more confidence. today i can say that i love it and won't miss it.


however, the lambda expressions have some small downsides. you must learn the new syntax and sometimes its hard to identify the actual interface being used when writing anonymous implementations of one method interfaces.


of course there are also advantages. the new syntax is lightweight, because it reduces the boilerplate code of creating an anonymous class with one method and therefor reduces some of the complexity. the lambdas are constructed at runtime and are thread-safe. so what are your pros and cons of (not) using lambdas?


style 1:

public void translateLambadaStyle(final String locale) {
    final var input = new JTextField("place your text here.");
    final var submit = new JButton("submit");
    submit.addActionListener(event -> translate(locale, input.getText()));
}

style 2:

public void translateClassicStyle(final String locale) {
    final var input = new JTextField("place your text here.");
    final var submit = new JButton("submit");
    submit.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(final ActionEvent event) {
            translate(locale, input.getText());
        }
    });
}

style 3:

public void innerClassListener(final String locale) {
    final var input = new JTextField("place your text here.");
    final var submit = new JButton("submit");
    submit.addActionListener(new TranslateListener(locale, input.getText()));
}

so which style are you, 1, 2, 3 or even something else, tell me about it in the comments. 😄



Recent Posts

See All
bottom of page