Code Best Practices Checker | DAX
Power Automate Expression Refactor
This document analyzes and refactors a Power Automate expression, transforming a deeply nested conditional structure into a cleaner, more maintainable format by utilizing variables and array membership checks for improved performance and
Prompt
Answer
Language/Technology Identification
The provided code snippet appears to be written in Power Automate (previously known as Microsoft Flow) Expressions, typically used in conditional logic within workflows.
Code Assessment
The current implementation is highly nested and difficult to read, making it prone to errors and challenging to maintain. The nested structure doesn’t scale well, and the repeated calls to triggerOutputs()?['body/Gen']
and variables('MIR162')
can lead to performance issues.
Code Refactoring
Original Code
If(And(equals(triggerOutputs()?['body/Gen'], 'F2'), Equals(variables('MIR162'), '1')), true, If(And(equals(triggerOutputs()?['body/Gen'], 'F3'), Equals(variables('MIR162'), '1')), true, If(And(equals(triggerOutputs()?['body/Gen'], 'F4'), Equals(variables('MIR162'), '1')), true, If(And(equals(triggerOutputs()?['body/Gen'], 'B2'), Equals(variables('MIR162'), '1')), true, If(And(equals(triggerOutputs()?['body/Gen'], 'B3'), Equals(variables('MIR162'), '1')), true, If(And(equals(triggerOutputs()?['body/Gen'], 'B4'), Equals(variables('MIR162'), '1')), true, If(And(equals(triggerOutputs()?['body/Gen'], 'B5'), Equals(variables('MIR162'), '1')), true, If(And(equals(triggerOutputs()?['body/Gen'], 'B4'), Equals(variables('MIR162'), '1')), true, false))))))))
Refactored Code
@{ // Store values for easier referencing $genValue = triggerOutputs()?['body/Gen']; $mirValue = variables('MIR162');
// Define the accepted values
$acceptedValues = ['F2', 'F3', 'F4', 'B2', 'B3', 'B4', 'B5'];
// Check if conditions are met
If(And(contains($acceptedValues, $genValue), Equals($mirValue, '1')), true, false)
}
Documentation of Changes
Storage of Repeated Values:
- Original: The values
triggerOutputs()?['body/Gen']
andvariables('MIR162')
were repeatedly called. - Refactored: Created variables
$genValue
and$mirValue
to store these outputs and reduce function calls. This improves performance and reduces redundancy.
- Original: The values
Use of Array and
contains
function:- Original: Multiple nested
If
andAnd
statements checked for multiple values. - Refactored: Introduced an array
$acceptedValues
containing all valid conditions, and usedcontains
to check membership within this array. This approach enhances readability and lessens the complexity of conditions, allowing easier updates in the future.
- Original: Multiple nested
Simplified Boolean Evaluation:
- Original: The final outcome had unnecessary complexity with multiple
true
statements. - Refactored: The outcome now directly checks if either conditions are satisfied and returns
true
orfalse
accordingly, simplifying the entire logical structure.
- Original: The final outcome had unnecessary complexity with multiple
Conclusion
The refactored code significantly improves readability, maintainability, and performance by eliminating redundant function calls and complex conditional nesting. Such restructuring is fundamental in aligning with best practices in workflow logic development within Power Automate. Adopting these practices contributes to a cleaner and more efficient workflow implementation, allowing for easier adjustments and lower chances of introducing errors in complex mutually exclusive conditions.
Description
This document analyzes and refactors a Power Automate expression, transforming a deeply nested conditional structure into a cleaner, more maintainable format by utilizing variables and array membership checks for improved performance and readability.