Sunday, August 21, 2016

What is this method doing?

One-liner method passes its parameters untouched to anther method doesn't usually add value. Let me explain why.

I came across this method the other day, and wanted to look into it.
private String base64EncodedFrom(byte[] bytes) {
   if (bytes == null) {
       return null;
   return Base64.encodeBase64String(bytes);

I know this is not a one-liner. Sure it checks if bytes array is null beforehand, its name is a bit more human readable, because of the From, than the one that it'll pass on. To make it even better, do you think the method name should be something like base64EncodedFromByteArrayNullSafe? Well, that's not my point, if you've read this post.

Let's check
public static String encodeBase64String(final byte[] binaryData) {
   return StringUtils.newStringUtf8(encodeBase64(binaryData, false));

public static byte[] encodeBase64(final byte[] binaryData, final boolean isChunked) {
   return encodeBase64(binaryData, isChunked, false);

public static byte[] encodeBase64(final byte[] binaryData, final boolean isChunked, final boolean urlSafe) {
   return encodeBase64(binaryData, isChunked, urlSafe, Integer.MAX_VALUE);

public static byte[] encodeBase64(final byte[] binaryData, final boolean isChunked, final boolean urlSafe, final int maxResultSize) {
   if (binaryData == null || binaryData.length == 0) {
       return binaryData;

Now we know encodeBase64() is null-safe. Let's then check
public static String newStringUtf8(final byte[] bytes) {
   return newString(bytes, Charsets.UTF_8);

private static String newString(final byte[] bytes, final Charset charset) {
   return bytes == null ? null : new String(bytes, charset);

So newStringUtf8() is null-safe too. Now it's safe to rewrite the first method this way
private String base64EncodedFrom(byte[] bytes) {
   return Base64.encodeBase64String(bytes);
Do you still think base64EncodedFrom() is a method that adds value?

Friday, December 25, 2015

30th Coding Anniversary

I was selected into a interest group learning programming in BASIC (using Apple IIe and Laser-310) in December 1985, when I was in year 8. It was fun. I learned coding so hard that I got first prize award (equal 1st, Year 7-9) in programming competition in my home city few months later in 1986. One question in the competition was swapping the values in 2 variables without using a 3rd variable. My name was published on newspaper and I told my parents I'm famous.
Admission ticket, 1986

I got three more first prize awards later, 4th place (Y7-9) in 1987, 3rd place (Y10-12) in 1988 and 2nd place (Y10-12) in 1989. The last one in June 4th 1989 was the most difficult. I got mumps below my left ear and had been stayed at home and almost couldn't eat anything for a week just before the competition, which I sit in No. 15 Middle School, very close to my then home.
First prize certificate, 1986

These are few things happened before I went to university studying computer science and software engineering. I have come a long way and these things always remind me of where I came from, and where the determined boy wanted to go three decades ago.

Merry Christmas and Happy Coding!