Eliminate Comments with Lambda Expressions

If you have been converting your code comments into code by creating small functions, I found an interesting alternative that uses lambda expressions.

This starts with a post on a forum run by John Somnez. One reader made a comment that your functions should not be too short. John replied that short functions are desirable because you can avoid comments by using small functions. The name of the function essentially takes the place of the comment. I follow this coding practice but I thought that the reader did have a point.

Now there is an alternative if you program with a language that supports lambda expressions. The idea is to convert code sections into lambda expressions instead of creating separate functions. I experimented with the technique in Java 8. The technique works and it works even better in Scala.

Scoped Comments

Imagine having the ability to have a comment that is part of the language, checked by the compiler, that delimits the scope of the comment. This technique actually allows the programmer to delimit the start of a block of code which is not unusual. What is different is the the end of the block of code is also delimited.

This sounds bizarre but it is possible because Java 8 and Scala allow lambda expressions to be passed as arguments to functions.

Here is the entire program.

    public void test() {
        long number;
        P041_PandigitalPrime pandigitalPrime = new P041_PandigitalPrime();
        number = pandigitalPrime.getNumber();
        // print messages
        System.out.printf( "1. number = %d%n", number );
        System.out.printf( "2. number = %d%n", number );
        printMessages( () -> 
            System.out.printf( "3. number = %d%n", number );
            System.out.printf( "4. number = %d%n", number);
        } );
        assertEquals( 7652413, number );

Here is the helper function that takes the lambda, executes it, and returns nothing:

   private void printMessages( Runnable function ) {

Advantages of Morphing a Comment into a Lambda Expression

Use a Lambda Expressionns to Improve Code Structure

Use Lambda Expressions to Improve Code Structure

In the example above, there are two lines of code preceded by a comment.

The next two print statements in the lambda passed to the printMessages() function perform the same operations, convey the meaning of the statements, and essentially convert the comment into a function call.

It’s as if the code that’s in the printMessages() function migrated back from that function into the main section of code.

Also note that, the commented code has no scope unless you add yet another comment to mark the end of the code block. If you use a lambda expression, the curly braces mark the exact section of code that is passed to the helper function. That’s one advantage.

Have a look at this example that uses a Supplier class to return a value from a code block.

This is not earth shattering but it demonstrates the power of functional programmer techniques that can improve the readability of your code and help reduce long term maintenance costs. That’s a second advantage.

If you have a comment, please leave a message below. For computer science videos, please subscribe to my Translation Data YouTube channel.

You can follow me on twitter @HaroldAlmon or @TranslationData.