Use early returns

Use early returns to avoid nested if conditions. Then the code will become more readable and maintainable.

Note: If both if and else contain similar kinds of logic, don’t return early.

Bad Code

function handleCSVBodyParts(http:Request request) returns string[][]|error {
    var bodyParts = request.getBodyParts();
    if bodyParts is mime:Entity[] {
        string[][] csvLines = [];
        foreach var bodyPart in bodyParts {
            var mediaType = mime:getMediaType(bodyPart.getContentType());
            if mediaType is mime:MediaType {
                string baseType = mediaType.getBaseType();
                if "text/csv" == baseType {
                    byte[] payload = check bodyPart.getByteArray();
                    csvLines = check getCSVData(payload);
                } else {
                    return error("Invalid base type, not text/csv");
                }
            } else {
                return error("Invalid media type");
            }
        }
        return csvLines;
    } else {
        return error("Error in decoding multiparts!");
    }
}
  • Actual logic resides after many depths.
  • Code is not readable.

Good Code

function handleCSVBodyParts2(http:Request request) returns string[][]|error {
    mime:Entity[]|error bodyParts = request.getBodyParts();
    if bodyParts is error {
        return error("Error in decoding multiparts!");
    }
    string[][] csvLines = [];
    foreach var bodyPart in bodyParts {
        mime:MediaType|error mediaType = mime:getMediaType(bodyPart.getContentType());
        if mediaType is error {
            return error("Invalid media type");
        }
        string baseType = mediaType.getBaseType();
        if "text/csv" != baseType {
            return error("Invalid base type, not text/csv");
        }
        byte[] payload = check bodyPart.getByteArray();
        csvLines = check getCSVData(payload);
    }
    return csvLines;
}

See Also: