-3

So, I am a beginner level Java guy and I was following the java 2D game development tutorial on YouTube by CodeNMore. in the States video (https://www.youtube.com/watch?v=871zoXsYrbI&index=11&list=PLah6faXAgguMnTBs3JnEJY0shAc18XYQZ), he had this line of code:

if(State.getState() != null) 
    State.getState().render(g);

Firstly, why doesnt the if statement need to have curly brackets ({})? Secondly, is it fluent interface thats why you put State.getState().render(g);? or is it something else?

1
  • It's not a fluent interface just because it has two periods in it. Aug 4, 2018 at 22:24

1 Answer 1

0

Firstly, why doesn't the if statement need to have curly brackets ({})?

No need for {} if there is only one statement after the "if" clause. Usually its better to use {} to avoid confusion, even though its not in your example.

Secondly, is it fluent interface that's why you put State.getState().render(g);? or is it something else?

I would not worry about the term 'fluent' because there is no real agreed definition. This is an example of method chaining where State.getState() returns a State object, and then the render(g) method is invoked on the returned state. (I looked at the video in case anyone is wondering).

This is what the State.getState().render(g) looks like in two steps and does the same thing.

State tempState = State.getState(); tempState.render(g);

5
  • I am confused, because the way you do fluent interface is by the same syntax. Im guessing that if the method you are calling does not return something then it is deemed as fluent interface?
    – lpydawa
    Aug 4, 2018 at 9:03
  • @lpydawa. The example above is method chaining. This helps to achieve "fluent" interfaces. The aim of fluent interfaces is to make the code look readable like English for example. Read this <en.wikipedia.org/wiki/Fluent_interface>
    – bcperth
    Aug 4, 2018 at 9:15
  • So there are a few types of "fluent" interfaces?
    – lpydawa
    Aug 4, 2018 at 9:57
  • "Fluent" is not a technical thing. Its just making the code look more readable, and is often based on method chaining as per wiki, but may be expressed differently in different computer languages. Its not something you need to learn how to do.
    – bcperth
    Aug 4, 2018 at 12:26
  • I see, thanks for your time @bcperth, very much appreciated!
    – lpydawa
    Aug 4, 2018 at 13:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.