Simplifying Java One Liners (Lambda Expressions) Debugging with Lightrun
Introduction
In Java programming, lambda expressions or Java one-liners have become widely adopted practices for writing concise and expressive code. These compact, anonymous functions introduce functional programming concepts to Java, streamlining operations on collections, simplifying data manipulation, and enhancing code readability.
Introduced in Java 8, lambda expressions are designed to represent blocks of executable code. These expressions can be treated as values, making them easy to pass as arguments or return from methods. Unlike traditional methods, lambdas eliminate the need for explicit names, return types, or access modifiers, offering a functional-style programming approach that reduces boilerplate and promotes clean, efficient code.
A Lambda expression consists of three main components as you can see in the snippet below (Fig 1):
Fig 1: A Sample Single Line Lambda Expression in Java
Parameter list, arrow operator that separates the parameter list from the lambda function, and lastly, the body which represents the implementation of the lambda function.
The benefits of the practice are wide and include code readability, coding flexibility, and conciseness.
Challenges in debugging Java One Liners
While the concept and practice of Lambda expressions is evolving and adopted by software developers, having the ability to debug Java code that uses such components at runtime directly from the IDE is very complex due to the nature of these functions.
Specifically, with Lambda expressions, since there is a lack of named context in the code, when obtaining a traditional debugging stack trace from a Lambda the output is less informative and often does not include sufficient context about where an error or exception occurs. Also, traditional debuggers struggle with stepping into lambda expressions or inline methods effectively (not to mention at runtime). Most importantly is the difficulty in adding logs and traces in the Lambda expression code due to the minimal room such implementation leaves for debugging data. Adding print statements, traces, logs and such debugging insights during development within the Lambda expressions are not a valid option and can break the concise structure of the Lambda.
How to debug Java One Liners with Lightrun
Starting with the Lightrun 1.48.1 version, Lightrun introduced to its customers an additional debugging capability that’s 100% aimed for Java One Liners (Lambda Expressions). This new capability allows developers to seamlessly add to their lambda expressions at runtime! Logs and Snapshots (virtual breakpoints) and examine their code incidents directly from the JetBrains IDE plugin. Customers who wish to start using this new capability would have to upgrade to version 1.48.1 (both plugin and agent).
Let’s briefly learn how to use the new Lightrun for Lambda expressions.
From the relevant Java line of code that consists of a Lambda expression, right click on that line and navigate to the Lightrun actions menu → If the line actually consists of such expression, a sub menu will appear allowing you to either add a dynamic log or a snapshot and start collecting observability data directly in the IDE.
In the below screenshot (Fig 2) we show the first action of adding a Lambda snapshot within a specific Java one liner. Once we right click on that line, we get 2 options: Add a snapshot on the entire line of code or specifically on the Lambda expression.
Fig 2: Selecting a Single Line Lambda Expression Snapshot within the Lightrun JetBrains IDE Plugin
Preview of Lightrun’s Single Line Lambda Snapshots
If we choose to place the snapshot on the Lambda expression, an “Insert a Lambda Snapshot” popup window opens allowing us to further configure the snapshot with parameters like number of snapshot hits, conditions, expiration settings, and more (see below an example of the configuration window – Fig 3).
Fig 3: Lightrun’s Configuration Window shown Upon Inserting a Java Lambda Expression Snapshot
Once you receive the snapshot hit response, you can easily dive into the stack trace and the variable within the Lambda expression as you can see in the below screenshot – Fig 4 (in our example, the “num” variable).
Fig 4: Lightrun’s Single Line Lambda Expression Snapshot Stack Trace View
Preview of Lightrun’s Lambda Expressions Logs
Similarly to Lambda snapshot, Lightrun supports the addition of Lambda logs on specific expressions. Here as well, a developer can right click the Java one liner and choose to add a Lambda log to any given expression within the line as you can see in the below screenshot (Fig 5).
Fig 5: Selecting a Single Line Lambda Expression Log within the Lightrun JetBrains IDE Plugin
Once a Lambda log action has been requested, an “Insert a Lambda Log” popup window appears within the IDE plugin allowing the developer to configure the log with conditions, target output of the log (STDOUT, IDE Plugin, Prometheus, etc.) – See below an example of such a Lambda log configuration screen (Fig 6).
Fig 6: Lightrun’s Configuration Window shown Upon Inserting a Java Lambda Expression Log
With the new addition to the Lightrun observability platform, Java developers are better equipped with tools to allow them to debug the most complex code that includes Lambda expressions at runtime, from their native IDE, and without the need to worry about telemetry data inclusion throughout the software development life cycle. Such streamlines debugging workflow simplifies developers lives, as well as shortens debugging time for incidents that are related to Lambda expressions in the application code.
Learn More About Lightrun’s Lambda Expressions Runtime Debugging
To get started with Lightrun and utilize Lambda expression runtime debugging, please go to the Lightrun website and get in touch with the Lightrun team for a demo. You can read more about Lightrun’s Lambda logs and snapshots as well as see end to end short demonstrations in the below references:
Getting Started with Debugging Lambda Expressions using Lightrun Logs: https://docs.lightrun.com/logs/#lambda
Getting Started with Debugging Single Line Lambda Expressions using Lightrun Snapshots : https://docs.lightrun.com/snapshots-plugin/#lambda
It’s Really not that Complicated.
You can actually understand what’s going on inside your live applications.