DEV Community

Alex Martinez
Alex Martinez

Posted on • Edited on

2

Main difference between 'do' and 'using' operators in DataWeave

Which one would you use??

I wrote this post with my own views and wider explanations, but I wanted to compile here the main difference in syntax.

do

%dw 2.0
output application/json
var age = 21
---
{
  person: do { 
    var user = "Robin"
    var age = 5
    ---
    {
        name: user, 
        age: age
    }
  }
}
Enter fullscreen mode Exit fullscreen mode

using

%dw 2.0
output application/json
var age = 21
---
{
  person: using (user = "Robin", age = "5") { 
    name: user, 
    age: age
  }
}
Enter fullscreen mode Exit fullscreen mode

Besides the fact that using is deprecated in DataWeave 2.0, I prefer to use do way more because of the syntax. For me it is more intuitive to know where to define local functions or variables. I think you can't define named functions with using, but you can create lambda expressions (which is a whole can of worms on its own!).

What do you think though?

Image of Datadog

The Essential Toolkit for Front-end Developers

Take a user-centric approach to front-end monitoring that evolves alongside increasingly complex frameworks and single-page applications.

Get The Kit

Top comments (1)

Collapse
 
gauthierplm profile image
Gauthier POGAM--LE MONTAGNER

I think comparing do and using may not be the best approach to explain the difference since using is deprecated, and its use shouldn't be advertised at all. It was kept for backward compatibility, as mentioned in the official doc and thus no new code should be written using it.

To avoid confusing learners / discorage the use of a deprecated and (now) poorly documented operator, reworking the blog post to explain how to move from using to do, and providing a more clear warning about not using using would be a better approach.

It is sometimes hard for junior developers to understand why something should not be used when they have not yet experienced deprecation (I did the mistake too when learning programming!).

The Most Contextual AI Development Assistant

Pieces.app image

Our centralized storage agent works on-device, unifying various developer tools to proactively capture and enrich useful materials, streamline collaboration, and solve complex problems through a contextual understanding of your unique workflow.

👥 Ideal for solo developers, teams, and cross-company projects

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay