# Refactoring lambdas

**URL:** <https://bnd.discourse.group/t/refactoring-lambdas/404>\
**Category:** TIPS\
**Created:** [December 20, 2023, 8:13am UTC](https://bnd.discourse.group/t/refactoring-lambdas/404 "2023-12-20T08:13:08Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![pkriens](https://yyz2.discourse-cdn.com/free1/user_avatar/bnd.discourse.group/pkriens/32/3_2.png) [@pkriens](https://bnd.discourse.group/u/pkriens)\
**Post date:** [December 20, 2023, 8:13am UTC](https://bnd.discourse.group/t/refactoring-lambdas/404/1 "2023-12-20T08:13:08Z")

</div>

Somehow I quite often starter with a lambda that has a single statement, like:

```auto
   Supplier<String> c = ()-> "".substring(0);

```

However, then sometime later I need to convert this into a block because I need more than one statement.

```auto
   Supplier<String> c = ()-> { 
        return "".substring(0);
    };

```

I you use command-1 when the cursor is inside the statement, for example on the `substring`, you do not get an option to convert the single statement to a block.  
 ![image](https://global.discourse-cdn.com/free1/uploads/bnd/original/1X/8333023b0479adf1a7c181021c331673ea236302.png)

However, I just found out that if you put the cursor on the arrow of the lambda, you get lots of options, also the option to convert a single line lambda to a block!

 ![image](https://global.discourse-cdn.com/free1/uploads/bnd/original/1X/22f52ddee7ccc9970f00788ee3b2f106bb523107.png)

This seems to be true for many other refactorings of lambdas. I looked many times for a refactoring that would change something like `s -> s.length()` to `String::length` and it actually exists as long as the cursor is on the arrow.

Maybe the whole world knows this and I am the last. But found it very useful.

---

<div class="post-metadata">

**Author:** ![chrisrueger](https://yyz2.discourse-cdn.com/free1/user_avatar/bnd.discourse.group/chrisrueger/32/18_2.png) [@chrisrueger](https://bnd.discourse.group/u/chrisrueger)\
**Post date:** [December 20, 2023, 7:51pm UTC](https://bnd.discourse.group/t/refactoring-lambdas/404/2 "2023-12-20T19:51:55Z")

</div>

Thanks for sharing this. Yeah I also often go crazy putting the cursor in different kinds of places and press `Cmd + 1` just to see which refactorings I am offered. I it even sometimes it works with selecting a block of text - but not completely sure.  
E.g. I there is a way to convert an if - else if - else into a switch statement… and if you switch on an enum you can automatically create a switch on all the missing names of this enum.  
A lot seems to be there.

I never googled this before but I now found this:

- Quick Assists: [Help - Eclipse Platform](https://help.eclipse.org/latest/index.jsp?topic=%2Forg.eclipse.jdt.doc.user%2Freference%2Fref-java-editor-quickassist.htm&cp%3D1_4_14)
- Quick Fixes: [Help - Eclipse Platform](https://help.eclipse.org/latest/index.jsp?topic=%2Forg.eclipse.jdt.doc.user%2Freference%2Fref-java-editor-quickfix.htm)

---

<div class="post-metadata">

**Author:** ![pkriens](https://yyz2.discourse-cdn.com/free1/user_avatar/bnd.discourse.group/pkriens/32/3_2.png) [@pkriens](https://bnd.discourse.group/u/pkriens)\
**Post date:** [December 21, 2023, 1:35pm UTC](https://bnd.discourse.group/t/refactoring-lambdas/404/3 "2023-12-21T13:35:37Z")

</div>

That help page is really useful.

I am working on this for bndtools at the moment for OSGi specific stuff, will be mindblowing … 😉
