So we're starting to use Typescript for a new project at work. Part of the application allows a user to retrieve their future availabilities (present/absent/possibly present), for planning purposes.
This is the get function of the service right now.
getMyAvailabilities(startDate: Moment, endDate : Moment): Observable<Map<Moment, Availability>>
{
let params = new URLSearchParams();
params.set('startDate', startDate.format(this.formatDateString));
params.set('endDate', endDate.format(this.formatDateString));
return this.http.get(this.resourceUrl, {search: params})
.map((res: Response) =>
{
let serializedResult = new Map<Moment, Availability>();
let result = res.json();
for (let key of Object.keys(result))
{
let availability = result[key];
serializedResult.set(moment(key),
availability);
}
return serializedResult;
});
}
You provide a startDate, and an endDate to the API, and the API will give you a map of availabilities with the date as a key.
I wrote a functional variant because it seemed suitable to functional code. We're using a lot of functional-style code in our project, because it's very suited for transforming lists of objects.
getMyAvailabilities(startDate: Moment, endDate: Moment): Observable<Map<Moment, Availability>>
{
let params = new URLSearchParams();
params.set('startDate', startDate.format(this.formatDateString));
params.set('endDate', endDate.format(this.formatDateString));
return this.http.get(this.resourceUrl, { search: params }).map((res: Response) => {
let result = res.json();
return Object.keys(result)
.reduce((map: Map<Moment, Availability>, key: string) => {
map.set(moment(key), result[key]);
return map;
}, new Map<Moment, Availability>());
});
}
So, the functional variant is shorter. It's not likely that this function will need to map a large amount of objects - right now, we're estimating about 10-60 objects.
Most of us have a Java background - we're used to programming in Java, we're new or new-ish to the functional style of programming, so when presented with a procedural-style snippet of code and a functional-style snippet of code which do the same, perform the same and are of the same length, we tend to pick the procedural-style code.
But for this new project, making heavy use of procedural code would clash with the functional style used throughout the rest of the project.
Thus, the dilemma:
If we're not sure whether a piece of code is better as functional or procedural, should we go with the procedural code, which is easier to understand and probably more performant, even although we don't need that performance? Or should we go with the functional code, which is harder to understand, but follows the style of the rest of the project and is shorter in LoC?
And for the bonus question:
Is that a proper use of .reduce
? Usually reduce gives a single value back. In this case, it returns... a collection. Is there another way by which we can go from Object (which has string keys and Availability values) to Map<Moment, Availability>
?