Multi statement lambda and for each anti patterns

When I do a code review of lambda/stream code, I am immediately suspicious of two things – block statement lambdas and forEach().

What’s wrong with this? It’s functional programming right?

List<Integer> list = Arrays.asList(1,2,3,4,5,6,7,8,9,10);
		
AtomicInteger sum = new AtomicInteger();
List<Integer> odds = new ArrayList<>();
List<Integer> evens = new ArrayList<>();
		
list.forEach(n -> {
	sum.addAndGet(n);
	if (n % 2 == 0) {
		evens.add(n);
	} else {
		odds.add(n);
	}
});
		
odds.forEach(System.out::println);
System.out.println();
evens.forEach(System.out::println);
System.out.println();
System.out.println(sum);

Well? Not really. It does have a lambda. It doesn’t have a stream, but that’s easy enough to fix: list.stream().forEach(…).

All better? No. Just because you are using a stream doesn’t mean you are doing functional programming. I would much rather see this code as:

List<Integer> list = Arrays.asList(1,2,3,4,5,6,7,8,9,10);
		
		
list.stream()
   .filter(x -> x % 2 == 1)
   .forEach(System.out::println);

System.out.println();

list.stream()
   .filter(x -> x % 2 == 0)
   .forEach(System.out::println);

System.out.println();

list.stream()
   .mapToInt(x -> x)
   .sum();

Yes, I’m still using forEach(). But now I’m using it for one purpose (printing) rather than sticking logic in it.

Whenever I see a forEach() or lambda with more than one statement, my first thought is “could this be clearer or more functional.” Often the answer is yes. Filter(), map() and collect() are you friends.

And if I did need that List?

list.stream()
   .filter(x -> x % 2 == 1)
   .collect(Collectors.toList());

Scheduling Social Media Posts

I recently became responsible for managing the NYJavaSig social media. I’ve been using TweetDeck for scheduling Twitter posts. Today I researched scheduling Facebook and Linked In posts. Here’s what I learend

Buffer

Buffer.com was recommended to me. I like it. It is free for up to three channels/connections. There’s a limit on how many posts you can have in the queue at the same time, but it is way higher than I would need.

Once I registered, I immediately dropped the Pro trial so I could see what it would look like for me. Connecting Facebook was pretty easy. I had to sign in with Facebook to connect. It got dropped once and I reconnected. But after that, it seemed happy.

LinkedIn was tricker and I encountered two problems:

  • On the dashboard, there were buttons to connect instagram and twitter. So I though the limit of three social media channels was only those three.
  • I later learned you can click “manage social channels” and get access to your LinkedIn Account or a LinkedIn Page. However, it is not possible to schedule posts into a Group because LinkedIn has disabled that API

HootSuite

I learned about HootSuite from a linked in article about scheduling posts. I had two problems

  • HootSuite wanted permission to see my contacts. Why? This is not needed in order to post on my behalf. I didn’t want to give that.
  • It is no longer possible to schedule posts into a Group anyway because LinkedIn has disabled that API. (Yes the same problem as Buffer.)

Conclusion

I decided to split and use TweetDeck for Twitter and Buffer for Facebook. I may consolidate later. But I might not. I really like TweetDeck for Twitter!

blogging from JDConf online

I blogged a few sessions from Microsoft’s JDConf. I’m referring to it as synchronous blogging rather than live blogging. Because online with no live Q&A doesn’t feel live to me.

This is the first time I’ve tried to live/synchronous blog from home. I decided to try it because sessions were only 25 minutes and I wasn’t attending a lot of them. Also, some were topics I’d heard before which made it easier.

Like many things, I found it harder to do it at home. While I have two screens, they aren’t ideally positioned for this. And I found myself more distracted than at a conference. More importantly, blogging helps me process/network. I felt like that was lacking because the backchannel (twitter) was so quiet.

Blog posts