Photo by HalGatewood.com on Unsplash

Daily-used Extensions

Kiran Pande
3 min readMar 14, 2021

--

In our daily coding, we mostly use these data types String, Boolean and List. There are many checks, we need on these data types but it is not present in their respective classes.

Checks, we use mostly like null, ifTrue, ifFalse, hasElements etc. We generally use objects, when there is some data in it. There is no use of Null object or an empty list except showing error message.

So, we will create some or few extension functions for these datatypes and can easily be called on their respective objects even on Null.

Extension function has Very Important Feature as function can be called on NULL object as well.

Features

  • Repeated null / empty checks can be reduced.
  • Code will be more readable.
  • Execution of extension function can have if-part or both if-else-part
  • Function can be executed on NULL object as well.

List

While using list of anything, we mostly check if list has any elements in it. Even, any null list is also considered as empty. So, there are many places same or different list is used, at every place we need to add both checks. To reduce burden of repeated code, hasElements() function is created. This function will execute given function, when list is not null and has at least one element. hasElements() returns the same list for further processing.

There is also case when we need to show some error message when list is Null or Empty. isNullOrEmpty() will execute given function, when list is Null or Empty.

Below example shows detailed use of extension function on lists.

First example shows hasElements() is executed on source ArrayList. If elements are present in source list, add it to destination list otherwise ignore. Block of code is executed if and only if, source list has at least one element.

Second / Third example is used when both parts need to be handled ie. if source list has elements or source list is null or empty. Here, scope function run{} is used to separate code. Code is more readable now.

Fourth is to simplify previous example. Here, hasElements is called directly on source object. hasElements function returns same list for further processing. So, isNullOrEmpty function is executed after hasElements.

Only one block of code will be execute here. hasElements or isNullOrEmpty

Boolean

Other very common data type is Boolean. We use Boolean value to check if data is available, if dialog is displayed etc. We can create extension functions to execute block of code if variable value isTrue or isFalse. Here, we can reduce null check, if-else block and value check by one word functions. Same Boolean is returned for further processing.

Following are some examples to use Boolean extensions like Lists.

String

String objects are handled same as Lists. String shall not be null and empty.

Other common check on string is contains. If given string is present in source string. So, while executing extension, we pass data.

Following are examples for Extensions for String.

We can add / create / update as many extension function as we need or as per frequency of usage.

That’s All, Folks….!!!!

--

--