Thank you for stopping by! I am a full-stack developer that combines the power of entrepreneurship and programming to make the lives of programmers easier.
That's actually an interesting solution. Abstracting that array to just a generic wrapper let's say (like the Nullable in C# or Optional in Java) it could work.
A few questions though:
What do you do with arrays? Do you just .forEach(...).forEach(...)? Isn't that really difficult to read?
What about properties on objects? let counter = {} and have to check for counter.elements to be instantiated.
My rule of thumb here is to never use null and simply check for undefined as my sole "none" value. So that if statement would be reduced to:
if(counter!==undefined){...}
And, if I know that counterhas to be an object then I can simply do
I'm definitely for Generic Wrappers and these can be easily implemented with the above list strategies.
As for forEach(...).forEach(...), that will actually fail since forEach(...) is a consumer function; it doesn't return anything. Instead use map() which is the same as forEach() but it returns a value. I personally like to do stack method chains like so:
That style blends well with list method chains, Promise chains and Streams.
I like the if (counter) style but this isn't going to work in every language and my employer doesn't allow it in our JS code. We have to always use the double check unfortunately.
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
That's actually an interesting solution. Abstracting that array to just a generic wrapper let's say (like the
Nullable
in C# orOptional
in Java) it could work.A few questions though:
.forEach(...).forEach(...)
? Isn't that really difficult to read?let counter = {}
and have to check forcounter.elements
to be instantiated.My rule of thumb here is to never use
null
and simply check forundefined
as my sole "none" value. So that if statement would be reduced to:And, if I know that
counter
has to be an object then I can simply doMost JS programmers would recognize this pattern.
I'm definitely for Generic Wrappers and these can be easily implemented with the above list strategies.
As for
forEach(...).forEach(...)
, that will actually fail sinceforEach(...)
is a consumer function; it doesn't return anything. Instead usemap()
which is the same asforEach()
but it returns a value. I personally like to do stack method chains like so:That style blends well with list method chains, Promise chains and Streams.
I like the
if (counter)
style but this isn't going to work in every language and my employer doesn't allow it in our JS code. We have to always use the double check unfortunately.