How can I further reduce how to write routes?

Asked

Viewed 64 times

2

I’m still new on these sides and I’m making an application in Ode (I think...). I’ve written my routes this way example:

routes.get('/produtos', mostrarProdutos)
routes.get('/produtos/:id', mostrarPorId)
routes.post('/produtos', novoProduto)

So I was hanging around and I did a test that worked out that way:

routes.get('/produtos', mostrarProdutos)
      .get('/produtos/:id', mostrarPorId)
      .post('/produtos', novoProduto)

Is it good practice this last way? Does it give any problem? Is there any way to decrease further?

  • 2

    Why further reduce? Smaller code is not necessarily better, I suggest you read the first paragraph of this answer :-)

  • Yeah, hkotsubo’s right, I heard something like that a while back. I just asked this to see if I could optimize my time and not write similar code in a much larger application.

1 answer

2


There is no problem in implementing in this way.

What happens is that this Object follows a pattern called Fluent Builder Pattern, or Fluent Constructor Standard, where each method returns the current instance of the object after performing the actions. So you can invoke a new method to follow, in a single row.

As for size, I believe this is the most compact functional format possible.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.