Code review comment: This is done to prevent an if condition inside for loop. Since we know if rest bp exists, it will be at the last, we should get the advantage of that without checking the if for each loop.

Code before review:
private BLangMatchPattern transformMatchPattern(Node matchPattern) {
Location matchPatternPos = matchPattern.location();
SyntaxKind kind = matchPattern.kind();
if (kind == SyntaxKind.SIMPLE_NAME_REFERENCE &&
((SimpleNameReferenceNode) matchPattern).name().isMissing()) {
dlog.error(matchPatternPos, DiagnosticErrorCode.MATCH_PATTERN_NOT_SUPPORTED);
return null;
}
if (kind == SyntaxKind.SIMPLE_NAME_REFERENCE &&
((SimpleNameReferenceNode) matchPattern).name().text().equals("_")) {
BLangWildCardMatchPattern bLangWildCardMatchPattern =
(BLangWildCardMatchPattern) TreeBuilder.createWildCardMatchPattern();
bLangWildCardMatchPattern.pos = matchPatternPos;
return bLangWildCardMatchPattern;
}
if (kind == SyntaxKind.IDENTIFIER_TOKEN && ((IdentifierToken) matchPattern).text().equals("_")) {
BLangWildCardMatchPattern bLangWildCardMatchPattern =
(BLangWildCardMatchPattern) TreeBuilder.createWildCardMatchPattern();
bLangWildCardMatchPattern.pos = matchPatternPos;
return bLangWildCardMatchPattern;
}
if (kind == SyntaxKind.NUMERIC_LITERAL ||
kind == SyntaxKind.STRING_LITERAL ||
kind == SyntaxKind.SIMPLE_NAME_REFERENCE ||
kind == SyntaxKind.IDENTIFIER_TOKEN ||
kind == SyntaxKind.NULL_LITERAL ||
kind == SyntaxKind.NIL_LITERAL ||
kind == SyntaxKind.BOOLEAN_LITERAL) {
BLangConstPattern bLangConstMatchPattern =
(BLangConstPattern) TreeBuilder.createConstMatchPattern();
bLangConstMatchPattern.setExpression(createExpression(matchPattern));
bLangConstMatchPattern.pos = matchPatternPos;
return bLangConstMatchPattern;
}
if (kind == SyntaxKind.TYPED_BINDING_PATTERN) {
TypedBindingPatternNode typedBindingPatternNode = (TypedBindingPatternNode) matchPattern;
BLangVarBindingPatternMatchPattern bLangVarBindingPattern =
(BLangVarBindingPatternMatchPattern) TreeBuilder.createVarBindingPattern();
bLangVarBindingPattern.pos = matchPatternPos;
bLangVarBindingPattern.setBindingPattern(transformBindingPattern(typedBindingPatternNode.bindingPattern()));
return bLangVarBindingPattern;
}
if (kind == SyntaxKind.ERROR_MATCH_PATTERN) {
ErrorMatchPatternNode errorMatchPatternNode = (ErrorMatchPatternNode) matchPattern;
BLangErrorMatchPattern bLangErrorMatchPattern =
(BLangErrorMatchPattern) TreeBuilder.createErrorMatchPattern();
bLangErrorMatchPattern.pos = matchPatternPos;
NameReferenceNode nameReferenceNode;
if (errorMatchPatternNode.typeReference().isPresent()) {
nameReferenceNode = errorMatchPatternNode.typeReference().get();
bLangErrorMatchPattern.errorTypeReference = (BLangUserDefinedType) createTypeNode(nameReferenceNode);
}
if (errorMatchPatternNode.argListMatchPatternNode().size() == 0) {
return bLangErrorMatchPattern;
}
Node node = errorMatchPatternNode.argListMatchPatternNode().get(0);
if (isErrorFieldMatchPattern(node)) {
createErrorFieldMatchPatterns(0, errorMatchPatternNode, bLangErrorMatchPattern);
return bLangErrorMatchPattern;
}
bLangErrorMatchPattern.errorMessageMatchPattern = createErrorMessageMatchPattern(node);
if (errorMatchPatternNode.argListMatchPatternNode().size() == 1) {
return bLangErrorMatchPattern;
}
node = errorMatchPatternNode.argListMatchPatternNode().get(1);
if (isErrorFieldMatchPattern(node)) {
createErrorFieldMatchPatterns(1, errorMatchPatternNode, bLangErrorMatchPattern);
return bLangErrorMatchPattern;
}
bLangErrorMatchPattern.errorCauseMatchPattern = createErrorCauseMatchPattern(node);
createErrorFieldMatchPatterns(2, errorMatchPatternNode, bLangErrorMatchPattern);
return bLangErrorMatchPattern;
}
if (kind == SyntaxKind.NAMED_ARG_MATCH_PATTERN) {
NamedArgMatchPatternNode namedArgMatchPatternNode = (NamedArgMatchPatternNode) matchPattern;
BLangNamedArgMatchPattern bLangNamedArgMatchPattern =
(BLangNamedArgMatchPattern) TreeBuilder.createNamedArgMatchPattern();
bLangNamedArgMatchPattern.argName = createIdentifier(namedArgMatchPatternNode.identifier());
bLangNamedArgMatchPattern.matchPattern = transformMatchPattern(namedArgMatchPatternNode.matchPattern());
return bLangNamedArgMatchPattern;
}
if (kind == SyntaxKind.LIST_MATCH_PATTERN) {
ListMatchPatternNode listMatchPatternNode = (ListMatchPatternNode) matchPattern;
BLangListMatchPattern bLangListMatchPattern =
(BLangListMatchPattern) TreeBuilder.createListMatchPattern();
bLangListMatchPattern.pos = matchPatternPos;
SeparatedNodeList<Node> matchPatterns = listMatchPatternNode.matchPatterns();
int matchPatternListSize = matchPatterns.size();
for (int i = 0; i < matchPatternListSize - 1; i++) {
BLangMatchPattern bLangMemberMatchPattern = transformMatchPattern(matchPatterns.get(i));
if (bLangMemberMatchPattern == null) {
continue;
}
bLangListMatchPattern.addMatchPattern(bLangMemberMatchPattern);
}
BLangMatchPattern lastMember = transformMatchPattern(matchPatterns.get(matchPatternListSize - 1));
if (lastMember.getKind() == NodeKind.REST_MATCH_PATTERN) {
bLangListMatchPattern.setRestMatchPattern((BLangRestMatchPattern) lastMember);
} else {
bLangListMatchPattern.addMatchPattern(lastMember);
}
return bLangListMatchPattern;
}
if (kind == SyntaxKind.REST_MATCH_PATTERN) {
RestMatchPatternNode restMatchPatternNode = (RestMatchPatternNode) matchPattern;
BLangRestMatchPattern bLangRestMatchPattern = (BLangRestMatchPattern) TreeBuilder.createRestMatchPattern();
bLangRestMatchPattern.pos = matchPatternPos;
SimpleNameReferenceNode variableName = restMatchPatternNode.variableName();
bLangRestMatchPattern.setIdentifier(createIdentifier(getPosition(variableName), variableName.name()));
return bLangRestMatchPattern;
}
if (kind == SyntaxKind.MAPPING_MATCH_PATTERN) {
MappingMatchPatternNode mappingMatchPatternNode = (MappingMatchPatternNode) matchPattern;
BLangMappingMatchPattern bLangMappingMatchPattern =
(BLangMappingMatchPattern) TreeBuilder.createMappingMatchPattern();
bLangMappingMatchPattern.pos = matchPatternPos;
SeparatedNodeList<FieldMatchPatternNode> fieldMatchPatterns = mappingMatchPatternNode.fieldMatchPatterns();
int fieldMatchPatternListSize = fieldMatchPatterns.size();
for (int i = 0; i < fieldMatchPatternListSize - 1; i++) {
bLangMappingMatchPattern.fieldMatchPatterns.add((BLangFieldMatchPattern)
transformMatchPattern(fieldMatchPatterns.get(i)));
}
BLangMatchPattern lastMember = transformMatchPattern(fieldMatchPatterns.get(fieldMatchPatternListSize - 1));
if (lastMember.getKind() == NodeKind.REST_MATCH_PATTERN) {
bLangMappingMatchPattern.restMatchPattern = (BLangRestMatchPattern) lastMember;
} else {
bLangMappingMatchPattern.fieldMatchPatterns.add((BLangFieldMatchPattern) lastMember);
}
return bLangMappingMatchPattern;
}
if (kind == SyntaxKind.FIELD_MATCH_PATTERN) {
FieldMatchPatternNode fieldMatchPatternNode = (FieldMatchPatternNode) matchPattern;
BLangFieldMatchPattern bLangFieldMatchPattern =
(BLangFieldMatchPattern) TreeBuilder.createFieldMatchPattern();
bLangFieldMatchPattern.fieldName =
createIdentifier(fieldMatchPatternNode.fieldNameNode());
bLangFieldMatchPattern.matchPattern = transformMatchPattern(fieldMatchPatternNode.matchPattern());
return bLangFieldMatchPattern;
}
dlog.error(matchPatternPos, DiagnosticErrorCode.MATCH_PATTERN_NOT_SUPPORTED);
return null;
}

Line under review: }

Code after addressing the review:
private BLangMatchPattern transformMatchPattern(Node matchPattern) {
Location matchPatternPos = matchPattern.location();
SyntaxKind kind = matchPattern.kind();
if (kind == SyntaxKind.SIMPLE_NAME_REFERENCE &&
((SimpleNameReferenceNode) matchPattern).name().isMissing()) {
dlog.error(matchPatternPos, DiagnosticErrorCode.MATCH_PATTERN_NOT_SUPPORTED);
return null;
}
if (kind == SyntaxKind.SIMPLE_NAME_REFERENCE &&
((SimpleNameReferenceNode) matchPattern).name().text().equals("_")) {
BLangWildCardMatchPattern bLangWildCardMatchPattern =
(BLangWildCardMatchPattern) TreeBuilder.createWildCardMatchPattern();
bLangWildCardMatchPattern.pos = matchPatternPos;
return bLangWildCardMatchPattern;
}
if (kind == SyntaxKind.IDENTIFIER_TOKEN && ((IdentifierToken) matchPattern).text().equals("_")) {
BLangWildCardMatchPattern bLangWildCardMatchPattern =
(BLangWildCardMatchPattern) TreeBuilder.createWildCardMatchPattern();
bLangWildCardMatchPattern.pos = matchPatternPos;
return bLangWildCardMatchPattern;
}
if (kind == SyntaxKind.NUMERIC_LITERAL ||
kind == SyntaxKind.STRING_LITERAL ||
kind == SyntaxKind.SIMPLE_NAME_REFERENCE ||
kind == SyntaxKind.IDENTIFIER_TOKEN ||
kind == SyntaxKind.NULL_LITERAL ||
kind == SyntaxKind.NIL_LITERAL ||
kind == SyntaxKind.BOOLEAN_LITERAL) {
BLangConstPattern bLangConstMatchPattern =
(BLangConstPattern) TreeBuilder.createConstMatchPattern();
bLangConstMatchPattern.setExpression(createExpression(matchPattern));
bLangConstMatchPattern.pos = matchPatternPos;
return bLangConstMatchPattern;
}
if (kind == SyntaxKind.TYPED_BINDING_PATTERN) {
TypedBindingPatternNode typedBindingPatternNode = (TypedBindingPatternNode) matchPattern;
BLangVarBindingPatternMatchPattern bLangVarBindingPattern =
(BLangVarBindingPatternMatchPattern) TreeBuilder.createVarBindingPattern();
bLangVarBindingPattern.pos = matchPatternPos;
bLangVarBindingPattern.setBindingPattern(transformBindingPattern(typedBindingPatternNode.bindingPattern()));
return bLangVarBindingPattern;
}
if (kind == SyntaxKind.ERROR_MATCH_PATTERN) {
ErrorMatchPatternNode errorMatchPatternNode = (ErrorMatchPatternNode) matchPattern;
BLangErrorMatchPattern bLangErrorMatchPattern =
(BLangErrorMatchPattern) TreeBuilder.createErrorMatchPattern();
bLangErrorMatchPattern.pos = matchPatternPos;
NameReferenceNode nameReferenceNode;
if (errorMatchPatternNode.typeReference().isPresent()) {
nameReferenceNode = errorMatchPatternNode.typeReference().get();
bLangErrorMatchPattern.errorTypeReference = (BLangUserDefinedType) createTypeNode(nameReferenceNode);
}
if (errorMatchPatternNode.argListMatchPatternNode().size() == 0) {
return bLangErrorMatchPattern;
}
Node node = errorMatchPatternNode.argListMatchPatternNode().get(0);
if (isErrorFieldMatchPattern(node)) {
createErrorFieldMatchPatterns(0, errorMatchPatternNode, bLangErrorMatchPattern);
return bLangErrorMatchPattern;
}
bLangErrorMatchPattern.errorMessageMatchPattern = createErrorMessageMatchPattern(node);
if (errorMatchPatternNode.argListMatchPatternNode().size() == 1) {
return bLangErrorMatchPattern;
}
node = errorMatchPatternNode.argListMatchPatternNode().get(1);
if (isErrorFieldMatchPattern(node)) {
createErrorFieldMatchPatterns(1, errorMatchPatternNode, bLangErrorMatchPattern);
return bLangErrorMatchPattern;
}
bLangErrorMatchPattern.errorCauseMatchPattern = createErrorCauseMatchPattern(node);
createErrorFieldMatchPatterns(2, errorMatchPatternNode, bLangErrorMatchPattern);
return bLangErrorMatchPattern;
}
if (kind == SyntaxKind.NAMED_ARG_MATCH_PATTERN) {
NamedArgMatchPatternNode namedArgMatchPatternNode = (NamedArgMatchPatternNode) matchPattern;
BLangNamedArgMatchPattern bLangNamedArgMatchPattern =
(BLangNamedArgMatchPattern) TreeBuilder.createNamedArgMatchPattern();
bLangNamedArgMatchPattern.argName = createIdentifier(namedArgMatchPatternNode.identifier());
bLangNamedArgMatchPattern.matchPattern = transformMatchPattern(namedArgMatchPatternNode.matchPattern());
return bLangNamedArgMatchPattern;
}
if (kind == SyntaxKind.LIST_MATCH_PATTERN) {
ListMatchPatternNode listMatchPatternNode = (ListMatchPatternNode) matchPattern;
BLangListMatchPattern bLangListMatchPattern =
(BLangListMatchPattern) TreeBuilder.createListMatchPattern();
bLangListMatchPattern.pos = matchPatternPos;
SeparatedNodeList<Node> matchPatterns = listMatchPatternNode.matchPatterns();
int matchPatternListSize = matchPatterns.size();
if (matchPatternListSize == 0) {
return bLangListMatchPattern;
}
for (int i = 0; i < matchPatternListSize - 1; i++) {
BLangMatchPattern bLangMemberMatchPattern = transformMatchPattern(matchPatterns.get(i));
if (bLangMemberMatchPattern == null) {
continue;
}
bLangListMatchPattern.addMatchPattern(bLangMemberMatchPattern);
}
BLangMatchPattern lastMember = transformMatchPattern(matchPatterns.get(matchPatternListSize - 1));
if (lastMember.getKind() == NodeKind.REST_MATCH_PATTERN) {
bLangListMatchPattern.setRestMatchPattern((BLangRestMatchPattern) lastMember);
} else {
bLangListMatchPattern.addMatchPattern(lastMember);
}
return bLangListMatchPattern;
}
if (kind == SyntaxKind.REST_MATCH_PATTERN) {
RestMatchPatternNode restMatchPatternNode = (RestMatchPatternNode) matchPattern;
BLangRestMatchPattern bLangRestMatchPattern = (BLangRestMatchPattern) TreeBuilder.createRestMatchPattern();
bLangRestMatchPattern.pos = matchPatternPos;
SimpleNameReferenceNode variableName = restMatchPatternNode.variableName();
bLangRestMatchPattern.setIdentifier(createIdentifier(getPosition(variableName), variableName.name()));
return bLangRestMatchPattern;
}
if (kind == SyntaxKind.MAPPING_MATCH_PATTERN) {
MappingMatchPatternNode mappingMatchPatternNode = (MappingMatchPatternNode) matchPattern;
BLangMappingMatchPattern bLangMappingMatchPattern =
(BLangMappingMatchPattern) TreeBuilder.createMappingMatchPattern();
bLangMappingMatchPattern.pos = matchPatternPos;
SeparatedNodeList<Node> fieldMatchPatterns = mappingMatchPatternNode.fieldMatchPatterns();
int fieldMatchPatternListSize = fieldMatchPatterns.size();
if (fieldMatchPatternListSize == 0) {
return bLangMappingMatchPattern;
}
for (int i = 0; i < fieldMatchPatternListSize - 1; i++) {
bLangMappingMatchPattern.fieldMatchPatterns.add((BLangFieldMatchPattern)
transformMatchPattern(fieldMatchPatterns.get(i)));
}
BLangMatchPattern lastMember = transformMatchPattern(fieldMatchPatterns.get(fieldMatchPatternListSize - 1));
if (lastMember.getKind() == NodeKind.REST_MATCH_PATTERN) {
bLangMappingMatchPattern.setRestMatchPattern((BLangRestMatchPattern) lastMember);
} else {
bLangMappingMatchPattern.addFieldMatchPattern((BLangFieldMatchPattern) lastMember);
}
return bLangMappingMatchPattern;
}
if (kind == SyntaxKind.FIELD_MATCH_PATTERN) {
FieldMatchPatternNode fieldMatchPatternNode = (FieldMatchPatternNode) matchPattern;
BLangFieldMatchPattern bLangFieldMatchPattern =
(BLangFieldMatchPattern) TreeBuilder.createFieldMatchPattern();
bLangFieldMatchPattern.fieldName =
createIdentifier(fieldMatchPatternNode.fieldNameNode());
bLangFieldMatchPattern.matchPattern = transformMatchPattern(fieldMatchPatternNode.matchPattern());
return bLangFieldMatchPattern;
}
dlog.error(matchPatternPos, DiagnosticErrorCode.MATCH_PATTERN_NOT_SUPPORTED);
return null;
}