Optional vs if/else-if performance java 8

前端 未结 5 1788
我寻月下人不归
我寻月下人不归 2021-02-19 22:58

Hello i have two samples of code

if/else if/else statements

private Object getObj(message) {
        if (message         


        
5条回答
  •  花落未央
    2021-02-19 23:57

    In my opinion after around 20 years of commercial experience, I've formed a view that pursuing readability is absolute stupidity, and at the same time, intentionally writing convoluted code is evil.

    I know this goes totally against popular opinion.

    However, everyone needs to realize this...

    1. What is readable to one person is not necessarily readable to the next. Even in this thread, we have varying opinions of whether if or Optional are more readable or not. These kinds of debates will occur irrespective of what constructs or situations we are in.
    2. If we take the if option, which is more performant than the functional approach, each and every time, then the people reading that code will get used to it and find it MORE READABLE - because it is the style that they have now become accustomed to.
    3. Performant code does not have to be "hard to read"... but this loops back to points 1 and 2. It's a matter for developers to actually know the fundamentals of the language they are using, and writing the code appropriate for that language, rather than trying to form "english sentences" in their code.

    So, in essence: go with the if... do NOT use that Optional!

提交回复
热议问题