Ron and Ella Wiki Page

Extremely Serious

Page 19 of 33

Set the SSL Backend to use with Git

To provide which ssl backend to use with git use the following syntax:

git config --global http.sslBackend <SSL_BACKEND>

The SSL_BACKEND token can potentially be openssl or schannel.

schannel uses the windows certicate store.

Example

git config --global http.sslBackend schannel

Verbatim String Literal

A double quoted string prefixed with an at (@) sign symbol will be interpreted verbatim.

Syntax

@"<STRING>"

The STRING token can have simple escaped sequences (e.g. \\ for backslash), hexadecimal escape sequence (i.e. \x<HEX_NUMBER>) and unicode escape sequences (i.e. \u<HEX_NUMBER>) that are interpreted literally. However, the "" will still be escaped an will produce a single double quote mark.

Verbatim string literal and string interpolation can be combined with the syntax as $@"<STRING>". Moreover, the {{ or }} will still be escaped.

Example

var string1 = "C:\\Windows\\System32"
var string2 = @"C:\Windows\System32"

Console.WriteLine(string1)
Console.WriteLine(string2)

The writelines will produce the same output.

String Interpolation

A double quoted string prefixed with a dollar ($) sign symbol can includes interpolation expression.

Syntax

$"<STRING>"

The STRING token can have interpolation expression and has the following syntax:

{<INTERPOLATION_EXPRESSION>[,<ALIGNMENT>][:<FORMAT_STRING>]}
Token Description
INTERPOLATION_EXPRESSION The part that will produce a formatted output.
ALIGNMENT Imposed a minimum number of character. If positive it is right-aligned and if negative it is left-aligned.
FORMAT_STRING A format that is supported by the expression. Find more on here.

If you needed to include { or } in your string output, escape it as {{ or }} respectively on which ever you need.

Example

var name = "World"
var greeting = $"Hello {name}"

Sample Usage of Teeing Collector

//A Stream of Integers
var ints = Stream.of(10, 20, 30 , 40);

//Calculate the average using the Collectors.teeing method.
long average = ints.collect(
        Collectors.teeing(
                
                //Sum all of the integers in the stream.
                Collectors.summingInt(Integer::valueOf),
                
                //Count the content of the stream.
                Collectors.counting(),
                
                //Calculate the average.
                ( sum, count) -> sum / count
        )
);

System.out.println(average);

Sample HttpClient.sendAsync() Method Usage

//Create an instance of HttpClient using its builder.
HttpClient httpClient = HttpClient.newBuilder()
        .version(HttpClient.Version.HTTP_2)
        .build();

//Create an instance of HttpRequest using its builder.
HttpRequest req = HttpRequest.newBuilder(URI.create("https://www.google.com"))
            .GET()
            .build();

/* Use the httpClient.sendAsync() method and encapsulate the response
in the CompletableFuture. */
CompletableFuture<HttpResponse> resFuture =
        httpClient.sendAsync(req, HttpResponse.BodyHandlers.ofString());

//Use the resFuture.thenAccept to wait for the async response.
resFuture.thenAccept(res -> System.out.println(res.version()));

//Wait for the resFuture to to complete.
resFuture.join();

Java Switch Expression

Syntax

switch(VARIABLE) {
	case MATCH_1 [, MATCH_2][, MATCH_N] -> EXPRESSION; | THROW-STATEMENT; | BLOCK
	default -> EXPRESSION; | THROW-STATEMENT; | BLOCK
}
Token Description
VARIABLE The variable to test.
MATCH_1, MATCH_2, MATCH_N The value or values that can match the VARIABLE.
EXPRESSION A expression to execute.
THROW-STATEMENT A expression that throws an exception.
BLOCK A block of statements to execute.

If a return value is required use the yield keyword instead of return.

Example

public enum Day { 
	SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY; 
}

Day day = Day.WEDNESDAY;

int numLetters = switch (day) {
    case MONDAY, FRIDAY, SUNDAY -> 6;
    case TUESDAY -> 7;
    case THURSDAY, SATURDAY -> {
        System.out.println(8);
        yield 8;
    }
    case WEDNESDAY -> {
        System.out.println(9);
        yield 9;
    }
    default -> {
        throw new IllegalStateException("Invalid day: " + day);
    }
};

System.out.println(numLetters);

Testable Codes

  • Favor composition over inheritance.
  • Block of codes must not be more than 60 lines.
  • Cyclomatic complexity of 1 to 10 as long as you can still write unit tests.
  • Idempotent implementation.

ShowCodeDetailsInExceptionMessages Java Option

Use the following java option to have a more verbose exception message especially for NullPointerException:

-XX:+ShowCodeDetailsInExceptionMessages

For example, if we have the following snippet:

var a = null;
a.b = 1;

Running the above snippet will have the following exception:

Exception in thread "main" java.lang.NullPointerException: Cannot assign field "b" because "a" is null

Note: This is more useful if the classes were compiled with debugging info (i.e. javac -g )

« Older posts Newer posts »