DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Code Review That Matters: Tips and Best Practices
  • Fighting Climate Change One Line of Code at a Time
  • DataWeave: Play With Dates (Part 1)
  • Unlocking Game Development: A Review of ‘Learning C# By Developing Games With Unity'

Trending

  • Building Threat Intelligence Pipelines Using Python, APIs, and Elasticsearch
  • 5 AI Security Incidents That Broke Things in Production (and What They Have in Common)
  • Pragmatica Aether: Let Java Be Java
  • GenAI Implementation Isn't Magic — It’s a Lifecycle
  1. DZone
  2. Software Design and Architecture
  3. Integration
  4. Thoughts About Writing Clear Code

Thoughts About Writing Clear Code

In this article, I will try to uncover some of my thoughts based on my experience that can be useful when writing your code in DW.

By 
Alex Bogomol user avatar
Alex Bogomol
·
Sep. 07, 23 · Analysis
Likes (2)
Comment
Save
Tweet
Share
3.2K Views

Join the DZone community and get the full member experience.

Join For Free

DataWeave (DW) is a new functional programming language that emerged from Mulesoft technology. This is a simple, transparent, and powerful language for data transformation.

Since the language is new, I found there was no well-established practice on how to organize a DW code inside the Mulesoft app. I frequently ran into situations where the absence of a single DataWeave style or recommendations for a team can make the project difficult to understand and maintain. In this article, I will try to uncover some of my thoughts based on my experience that can be useful when writing your code in DW.

For the last seven years, I have been involved in projects and noticed that every developer/architect decided on his own how to tackle this problem, which is not bad unless the entire team follows it and a code review supports this process as well.

The benefits of some styles or recommendations are evident in the quick and fast onboarding of developers and the future maintenance of apps. It increases the code quality, reduces the risk of misunderstandings, benefits readability, and provides good documentation for the app.

My goal here is not to explain the syntax of DW or impose some rules, but instead, I would like to share my opinion or maybe start out a conversation on how adhering to some principles could help decrease the operation efforts in maintaining the project.

Let’s take a look at an example: 

 
{ “amount” : $.items map (($.amount * $.qauntity) + $$) reduce($+$$)}


The main idea is to make code readable and understandable, which the code above lacks. 

How about making it look like this by creating a self-explanatory function name:

 
"amount" : totalAmount($.items)


With respect to DW, we need also to keep in mind that the aim of this language is data transformation. So, the structure of the future new message must be easily seen.

I prefer the following in DW code: 

1. Do Not Overload Your Code With Complex Computations

Instead, put them into functions. Refer to this example:

 
%dw 2.0
output application/json
var vDate = "2023-10-01" as Date
---
payload.orders map( 
{
"amount" : $.items map (($.amount * $.qauntity) + $$) reduce($+$$),
"orderId" : $.id,
"orderDate": min($.items.created filter ((item, index) -> item >= vDate))
}
) 


It can be converted and understood easily:

 
%dw 2.0
output application/json
var vDate = "2023-10-01" as Date
 fun totalAmount(val) = val map (($.amount * $.qauntity) + $$) reduce($+$$)
 fun lastDeliveryDate(val) = min(val filter ((item, index) -> item >= vDate))
---
payload.orders map( 
{
"orderId" : $.id,
"amount" : totalAmount($.items),
"orderDate": lastDeliveryDate($.items.created)
}
) 


2. Remember the Data Structure First

I have seen something like this from time to time: 

 
%dw 2.0
output application/json
var vDate = "2023-10-01" as Date
 fun totalAmount(val) = "amount": val map (($.amount * $.qauntity) + $$) reduce($+$$)
 fun lastDeliveryDate(val) = "orderDate": min(val filter ((item, index) -> item >= vDate))
 fun orderId(val)= "orderId": val.id
---
payload.orders map( 
orderId($) ++ totalAmount($.items) ++ lastDeliveryDate($.items.created)
) 


It tells nothing about the resulting message. 

The data structure is important for DW.

3. Do Not Show Off Your Smartness

Sometimes, you run into such examples:

 
%dw 2.0
output application/json
var vDate = "2023-10-01" as Date
 fun totalAmount(val) = "amount": val map (($.amount * $.qauntity) + $$) reduce($+$$)
 fun lastDeliveryDate(val) = "orderDate": min(val filter ((item, index) -> item >= vDate))
 fun orderId(val)= "orderId": val.id
var result= payload.orders map( 
orderId($) ++ totalAmount($.items) ++ lastDeliveryDate($.items.created)
) 
---
result


It has a variable named "result" but tells nothing about the result.

Again, please show the entire message structure in DW. 

4. Keep Examples of the Original Payloads Before the Transformation Inside the App

This would be very handy for quick onboarding and understanding the app itself and the project.

This list can go on and on, and I believe if we follow these simple recommendations, at least this will make our life a little bit easier. :)

Data transformation MuleSoft Coding best practices Coding (social sciences)

Opinions expressed by DZone contributors are their own.

Related

  • Code Review That Matters: Tips and Best Practices
  • Fighting Climate Change One Line of Code at a Time
  • DataWeave: Play With Dates (Part 1)
  • Unlocking Game Development: A Review of ‘Learning C# By Developing Games With Unity'

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook