Lambdas with a single statement do not need the curly braces and can be written as an "expression lambda". Note that our IDE has a thin line that indicates how wide the line should be. When I was a little boy, my father was studying at university and one of the classes he took was Fortran programming. We had all these printouts and punched-cards that we used to play with, pretending they were some secret access cards. Punched-cards were typically 80 characters wide, and this number has stuck through decades of coding. Even though I am using a 4k Dell monitor, the default line length is still set to 80 characters. This does seem to be a reasonable human limit. Make it much longer, and it becomes difficult to read. You might want to increase it 120 if your team agrees.
We replace the SORTED_SCRAMBLER
statement lambda with an expression lambda.
In order to also change the RANDOM_SCRAMBLER
to an expression lambda, we need
to first inline the new ArrayShuffler()
to where the shuffler
variable is
currently used. We can do that manually by selecting the
new ArrayShuffler()
, cutting it, and pasting it
on top of the shuffler
in the line underneath. We
then delete the ArrayShuffler
variable declaration.
When we change the layout of our code, we need to remember that this is very much a matter of preference. It is a good idea to be able to comfortably read a lot of different coding styles. When we work in someone else's code base, it is polite to use whatever coding style they used for their code.
We hope you enjoyed this tutorial. If you did, you will also enjoy our courses. We suggest you start with Extreme Java - Advanced Java, followed by Extreme Java - Concurrency Performance for Java 8.
We deliver relevant courses, by top Java developers to produce more resourceful and efficient programmers within their organisations.
We can help make your Java application run faster and trouble-shoot concurrency and performance bugs...