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
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Last call! Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

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

  • Start Coding With Google Cloud Workstations
  • Measuring the Impact of AI on Software Engineering Productivity
  • How Large Tech Companies Architect Resilient Systems for Millions of Users
  • Artificial Intelligence, Real Consequences: Balancing Good vs Evil AI [Infographic]
  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.0K 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
Oops! Something Went Wrong

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

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

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 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!