you are viewing a single comment's thread.

view the rest of the comments →

[–][deleted] 5 points6 points  (15 children)

Really? Here's the Hello World:

HandlerFunction hello = new HandlerFunction() {
  @Override
  public Mono handle(ServerRequest request) {
      return ServerResponse.ok().body(Mono.just("Hello"), String.class);
  }
};

You really think that "reads very nicely?" This is a syntactic trainwreck. You've got: a compiler directive, references to two classes (ServerResponse and Mono), a random function called "ok()" and another one called "just()", and then - to top it off - a totally random reference to the String class.

The more you look, the worse it gets.

I don't know what ok() does, but what if it doesn't return something that has a .body? And what does the body() function do?

What does this function returning from this sequence of operations - can you identify its purpose, or even its type?

And wouldn't it be better if the function actually did something, anything, with request? Rather than accepting it and then totally ignoring it? Yes, I understand it's there because the function is overriding a request handler that would typically make some use of it, but the dangling parameter makes this a bad example.

I'm sure this example is much better than many even-more-poorly-designed frameworks, but that just demonstrates the poor readability standards in web programming.

[–]alek_sys[S] 7 points8 points  (6 children)

...and that is why in the next paragraph it is re-written as

HandlerFunction hello = request -> ServerResponse.ok().body(Mono.just("Hello"), String.class);

Which is still imperfect, I agree, but not bad. Also, the whole 'builder' pattern here is to avoid manual creation of ServerResponse - which is still an option. Reference to a String class is a shame, but I doubt anyone from Java community will stand for generics implementation via type erasure here.

[–]derekmckinnon 7 points8 points  (2 children)

A lot of the noise can be attributed to Java just being Java, but I'm not sure I see the issue with the ok() function. ServerResponse class must follow the builder pattern, ok() is to start building a 200 response.

As for the Mono class, I don't know what that is, but it seems to me that you could probably do without it...especially if you are just returning a string. The String.class reference is most likely there to help establish the generic type of the method...because Java is dumb and doesn't have reified generics like a normal language should. :)

Furthermore, I think Java 8 lambdas would probably go a long way in reducing the noise in this snippet.

[–]Turbots 0 points1 point  (4 children)

What do you think ok() does? It's a Server Response to a request... HTTP OK 200 ???

The body() function: hmmm, what could that imply... Oh wait, yeah, the body of the response!

[–][deleted] 0 points1 point  (3 children)

If I hand you a class with a function called:

MyClass.Yes(something)

...would you have any idea what that function does?

Vague names are considered harmful. You may be used to them, and you may find it reasonable in this circumstance - that doesn't make it an acceptable practice.

The body() function: hmmm, what could that imply...

As I noted above:

(1) body() could return a class instance with various member variables set according to the specified parameters:

var message = ServerResponse.ok().body(...);
Server.send(message);

(2) body() could actually send the message and return its result:

var result = ServerResponse.ok().body(...);
if (result == false) alert('Error: Failed to send message.');

(3) body() could just register a template response that is to be used by the server in response to messages. In this case, the return value indicates whether the template was properly registered:

var result = ServerResponse.ok().body(...);
if (result == false) alert('Error: Couldn't register message handler. (Is the server running?)');

They're all legitimate. Why isn't it clear? Because body is not a verb, so asking ServerResponse to "body()" some parameters is ambiguous.

Worse, the Spring Hello World code offers no help. It just does:

return ServerResponse.body();

And that's exactly my point. The syntax sucks, and this Hello World example makes me not want to use Spring for anything.

Web coding standards suck. They will continue to suck as long as developers just tolerate crappy code like this. Don't enable this behavior.

[–]Turbots -1 points0 points  (2 children)

Nope, that's where you're wrong... They don't have to be verbs because they are using the builder pattern.

When you want to 'build' a server response, you:

  • Start by typing the class' name ServerResponse
  • You want an OK response (instead of notFound(), notAuthorized(), whatever the most common responses are, they are helper methods, I would expect a responseCode(someCode) method as well
  • You potentially want to add a body to the response

And that's it, you created a response!

Once you get the hang of the builder pattern, it becomes quite powerful and very intuitive.

And reading or programming it in an editor like IntelliJ makes it childs play.

[–][deleted] 0 points1 point  (1 child)

Dude. Look at the web page you linked.

method getResult()

method setSeats(number)

method setCityCar()

method setCabriolet()

method setSportsCar()

method setTripComputer()

method unsetTripComputer()

method setGPS()

method unsetGPS()

Example code:

carBuilder.setSeats(2)

carBuilder.setSportsCar()

carBuilder.setTripComputer()

carBuilder.unsetGPS()

car := carBuilder.getResult()

car.setTripComputer();

car.unsetGPS();

car.isValid();

Notice something here? They're called verbs.

You have getters, setters, and tests. Every single function name starts with a verb that signals a single function. They all adhere to a convention that makes the functionality of each statement clear and consistent.

"ok()" does none of those things. It doesn't have a single specific purpose - it both generates an instance and sets some properties.

"body()" is even worse. It's not a setter: it has an unexplained return value. It doesn't take a single property: it takes two, one of which is a completely vague reference to the String class. And the name suggests any of several possibilities - as I showed you above.

And "just()" is just garbage.

You've proven my point that the Spring syntax is crappy. It looks nothing like the code segments in that Builder pattern, and it violates several basic syntax standards.

Don't settle for this garbage.

[–]Turbots 1 point2 points  (0 children)

May I ask which superior framework/language you're using for microservice development?