As a Xoogler who worked mostly in Go/Python, I recently moved to another MAANG. The codebase liberally uses the following type of expression: "iterable.forEach(x -> multiple lines of imperative code)". which I'm fairly certain is a bad practice (large block of imperative code with side-effects in a lambda block). But I'm keen to know what Google's style/best practices guide recommendations in the usage of .forEach construct in Java. Would anyone be willing to share a gist of it? Edit: Refactoring it to be more functional/declarative would be straightforward. I wanted to know about the style's guide reason behind why this practice is not good (to compare with my reasoning). Especially because Ruby has a similar ".each" construct and it's actually considered more idiomatic than loops (at least in Ruby). #blindoverflow #google #xoogler #googler #tech
Do you regret leaving
Based on this qn I am sure google will have no regret with him leaving
@Hoodz, Early to say. Eng standards-wise, it does seem like a steep downgrade (Google was way better), but I feel like I could get more things done here. Also looking at loogly's abuse for no reason, the culture seems to be going downhill.
Likely see if you can refactor it to a chain of functional methods. map/filter/collect etc. can also pull the block into a function to reference to make it more readable if the above isn’t possible. 🤷♂️ you can probably benefit from reading the more recent effective Java books.
Refactoring it to be more functional/declarative would be straightforward. I wanted to know about the style's guide reason behind why this practice is not good. Especially because Ruby has a similar ".each" construct and it's actually considered more idiomatic than loops (at least in Ruby).
But thanks! I would indeed benefit from reading it.
Looks like you joined Meta.
Bro read the Joshua Bloch book, Effective Java. He’s also xoogler it’s better than a style guide
Thanks for sharing. Anyone else care to chime in on best books to become a better software engineer?
Thanks bro!. I know much of Google's style often references his book. I was just looking for a shortcut (which I probably shouldn't).
Extract out imperative code in a static function. use iterable.foreach(Classname::staticFunction)
Thanks, AarBt, but... Refactoring it to be more functional/declarative would be straightforward. I wanted to know about the style's guide reason behind why this practice is not good (to compare with my reasoning). Especially because Ruby has a similar ".each" construct and it's actually considered more idiomatic than loops (at least in Ruby).
yes it's idiomatic, but only helps if tgere are few lines with straight forward logic. In case of multiole lines yiu need to have unit tests for these lines, and with functions testability increases. Furthermore, I find it more readable. With proper design you can even mock the function's behavior
Why don’t we write this as a good old for-loop so that it is readable and an exception stack trace that points to it is cleaner? Why the love for lambda?
Immutability + no side effects + less boilerplate + concurrency + thread safety. Though it's possible in iterative code. It's easier and less error-prone using functional.
for (int x : list) { do(x); }
Yeah, if the code is doing just forEach then it's better to write imperative. But if there are operations going on before and after it then ..... I believe later would be the case.
Asking people to share trade and industrial secrets. Wtf
Give us a break please. This is just a best practice.
Also the Google Java style guide is publicly available at https://google.github.io/styleguide/javaguide.html so I would hazard to say that this is far from trying to get company secrets.