Functional-Programming

Either Disjoint or Union Types

Many languages support union types, and it is high time Scala did. Union types are coming in upcoming version of Scala - Dotty. Union types (|) are already being compared with Either and Option (disjoint unions).

Know Thy Option

Avoid .get at all costs. Forget there is even a .get function on Option. There is always a way out - better one, than using .get. Same applies to .head

If you are going to have access the value in an Option in a test class, prefer extending your test class from OptionValues. Then you can use .value on an Option. Doing so establishes the presence of value as verification with meaningful error if value is not defined.

Pattern for Saner Mocking

It is common to see mocks being setup this way in unit tests.

scenario("Test Case 1") {
	...
	when(addressResolutionService.resolve(...)).thenReturn(...)
	when(vendorInventoryService.checkInventory(...)).thenReturn(...)
	...
	.... another bunch of when and then returns
	when(shipmentService.schedule(...)).thenReturn(...)

	...thisIsTheActualCalltoTest(...)

	verify(vendorInventoryService, 1).checkInventory(...)
	... other such verifications
}

scenario("Test Case 2") {
	...
	when(addressResolutionService.resolve(...)).thenReturn(...)
	when(vendorInventoryService.checkInventory(...)).thenReturn(...)
	...
	.... another bunch of when and then returns ...give or take one or more mocks compared to the previous test ...
	when(shipmentService.schedule(...)).thenReturn(...)

	...thisIsTheActualCalltoTest(...)

	verify(vendorInventoryService, 1).checkInventory(...)
	... other such verifications
}

... other such test cases
More ...

Non-FP to FP Conversion Caveat

Sometimes you learn the best from others; by watching. This post is based on such an instance. A fellow engineer on my team was investigating a nagging issue - partially-successful operations or rather operations that left data in an inconsistent state. It goes without saying that I take no credit for the time and effort spent on the investigation nor for the fix. I am just the messenger. And as a responsible programmer 🤓, I am sharing it with the rest of the world. More ...

Article hero image

A Rambling on Error Handling

In the early years, software applications were tiny, compared to what we build today. In any given application, one could say, there were only a handful of error scenarios to deal with. Besides, error reporting, if not error handling, lacked finesse. Just slap the user with something red enough, and just say An error occurred. More ...

JS Programming in C# - Immutability

Enough! JavaScript had us in its grip for long with its foot guns. The first time I heard the term Hoisting, I had no idea about it and misheard as hosting. You declare variables using var happily, and you have to come to peace with yourself that it is okay to hoist the vars (lift’em all to the top-most scope). I can’t believe JS convinced the rest of us that it was okay. Then came ES6 and saved us. let fixed the scoping. const provided immutability. At least now, you can say JavaScript supports functional programming. More ...

Article hero image

Facets of Immutability

Immutability, the cornerstone of functional programming, has many facets.

Not every (mainstream) language supports all the facets; at least not per what each facet stands for. That’s what I will talk about today. The various facets of immutability from a theoretical perspective, and briefly show how some of the mainstream languages have adopted and support these facets in their own way.

More ...