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.
Join the DZone community and get the full member experience.
Join For FreeDataWeave (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. :)
Opinions expressed by DZone contributors are their own.
Comments