This code uses an API for whom a default value must be specified. Unspecified default values can cause your application to crash.
1private Channel createChannelNonCompliant(URI uri) {
2 // Noncompliant: default value for the port number is not specified.
3 return NettyChannelBuilder.forAddress(uri.getHost(), uri.getPort())
4 .negotiationType(NegotiationType.PLAINTEXT)
5 .build();
6}
1private Channel createChannelCompliant(URI uri) {
2 int port = uri.getPort();
3 // Compliant: default value for the port number is specified.
4 if (port == -1) {
5 port = 11984;
6 }
7 return NettyChannelBuilder.forAddress(uri.getHost(), port)
8 .negotiationType(NegotiationType.PLAINTEXT)
9 .build();
10}