I have the following piece of code
public class direction do(direction)
if(istrue) {
left = do(left);
} else {
right = do(right);
}
}
I was wondering if there is anyway to shorten this. I tried using ternary operators, but had some difficulty. suggestions?
You might use a ternary expression if the other programmers on your team don't mind. So if your example were in valid Java:
You could omit the braces in the
if
:Or you even could have a one liner:
Above all, choose a style that's clear and not too clever. Use more lines if they make your code easier to read and understand.
One-liners generally aren't very readable, though in some cases they make sense (to me), especially if you have families of very similar small methods: