Maintenable js - null or undefined for primitive types?

15 views
Skip to first unread message

Алексей Струков

unread,
Jun 23, 2017, 9:35:49 AM6/23/17
to Zakas Books
Hi
I have a little question about primitive types, that were not fully considered in the book.
Your recommendation is use NULL in four cases. One of which is:
"To return from a function where an object is expected".
What do you think about functions, where primitive return value is expected, but can't be returned for some reasons? For example, functions interface @param is a {string}, and @return is a {number} (index in some text for example). What if searched string was not found? What you suggest to return in this case?

Thanks and sorry for my English)

Nicholas Zakas

unread,
Jun 23, 2017, 12:04:52 PM6/23/17
to Zakas Books
I'd return -1 in that case.
--
You received this message because you are subscribed to the Google Groups "Zakas Books" group.
To unsubscribe from this group and stop receiving emails from it, send an email to zakasbooks+...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Алексей Струков

unread,
Jun 23, 2017, 12:50:29 PM6/23/17
to Zakas Books
Thanks. It is reasonable, but not universal solution((
What if we need to find closest number in array that less then input value, but this input is bigger then each value in the array?
-1 will be represent the closest value or default "no value" value?

PS:
In my humble opinion, NULL should be universal "no value" value for reference and primitive types and I was hoping that you changed your mind, as it already happened in programming history, for example with Frederick Phillips Brooks :)

Reply all
Reply to author
Forward
0 new messages