what's up youtube
welcome to this next video on the series
let's talk
as usual with the let's talk series
there's not a whole lot of coder
sometimes never any code
so i hope you come in with that
expectation that
it's more of a talking series than is a
code series however i will be showing
you some code for this purpose
but unfortunately there's only one core
example even though
the problems are plentiful and there
should have been many more code examples
but i
only i want to show you because in the
real world for me i don't actually see
those kind of issues
anymore but i'll try and explain the
problems in the other code that i don't
have here to show you right so today's
video is about the null conditional
operator we talked a little bit on
this topic in my last video on the eye
disposable thing i'll sort of revisit
that a little bit
and then the code example i'll show you
is slightly different and then we'll
have some discussion on that so
the null conditional operator
is the question mark dot or n i think
kotlin they call it
safe call which
explains the the problem
the safe call and then you're doing it
because you want to feel safe so call
this in a safe manner
and
i think
not just with regards to null
conditional operator but just generally
speaking in programming i think we are
being unnecessarily
safe
i think the need to be safe is the
problem
because
at least in the way i program which is
more about the programming with intent
if you haven't seen any of that or what
you don't don't know what i'm talking
about
i'll put a link to the entire playlist
here
like 40 or maybe 50 videos now
in that playlist that's all encompassing
programming with intent which is a large
body of work
i don't know if it's because of that as
in the way i program that i don't ever
see the need for it or is the way i
think or the combination of the two all
right
so what is the problem with the null
conditional operator
i'm going to put it down to being abused
or overused
but i guess when you look at your own
code you probably get a better
understanding of why you overusing it or
you're abusing it
or you're just blindly using it without
thinking much about your code
and so the takeaway from this video
would be
if you are using the null conditional
operator which is sort of i'm not
recommending don't use it at all
what i'm saying is
just be mindful of when you use it don't
blindly use it and once you've used it
go back and look at that code and tell
and sort of think to yourself what is
this code saying to me
what is my intent if i would read this
code as somebody else
what is that code telling me
right what is the intent
being expressed by that code and is that
intent
what you were intending
in other words if you program with
intent and you're always thinking about
what am i really trying to say here by
writing this code what is this code
saying
code talks to you does it not
good
so we're on the same page there
so what is that code saying to me and is
that what i was intending to say in the
first place if not then you need to look
at and change the code
so use the null conditional operator
wisely
and especially when it comes to the a
dot
or a question mark dot b question mark
dot c and i've seen that turn on that
code it doesn't occur so much in
the code that you know i'm working on
the the teams with
but i do code reviews for other teams as
well and i see it a lot there and it's
definitely not being thought out
in many cases it has caused bugs because
it's an inadvertent thing
because you're not thinking about what
you're writing you don't get compiler
errors you're potentially not getting
any runtime issues because you want to
be safe
and that
excessive kind of desire to be safe
has then caused a bug
so i don't know if you've ever seen this
in your own code
but maybe you want to go back and look
at your code if you have too many
non-conditional operators in use
is that the intention or is it just
being safe right
so don't use it if you don't need it
so if you have a situation
if you remember the last video on
disposables
i think there are three places in which
we use like something to dispose dot
dispose only in one place did we
actually need to use a question mark dot
dispose
in the other two cases it was just not
required like
you could put it there
but that's not required because those
things would never be null by time the
disposal is called so why bother putting
a null
why confuse the issue how would you say
why bother
why confuse the issue by putting in a
null conditional operator
and now the next guy
said okay why is that going to be null
let me see look at the code i can't see
anywhere but this could be null
why is it called a question mark dot why
not just a dot right
so that's the basic takeaway all right
the null conditional operator is
safe call operator is
it's okay if you ask me
should you prefer that this
non-conditional operator was not in the
language i'd say yes
my in my opinion for all the benefits
that it seems to give you which is
hardly any
but all the pain it causes
i'd rather not have it
and that is a lot of features these days
in c sharp ever since you know probably
six or seven
ever since rosalind see once rosalind
came about
rosalind has given the c-sharp team an
easier way to add features synthetic
features
and
unfortunately that
kind of gets misused and that's when i
start to not like the feature i
personally like the feature except i
know that i only use it sometimes but
when
the problem i see day-to-day is that
it's being abused or misused all the
time
then i'd rather i think to myself you
know what i rather not have it because
that's causing more problems than it is
giving any benefit
all right okay
so
if you've understood the basic problem i
have with non-conditional operator and
the misuse or overuse or misguided use
of it and that you need to think about
what is your code saying after you've
used it like is that the intention i'm
going to show you this code here this
code borders on the null
nullable reference types
and the
null forgiving operator or the null
suppression operator even though that's
not the point of the topic here i'll be
using that so in case you don't know
what that is then
i don't intend to explain it here so you
might want to read up on that before
watching the rest of the code all right
so let's take a look at the code here
some of you might find this code
familiar from the logging
series so this is the sort of the
program.cs of that
project or the last of that project
and i have a an application logger here
a private member static member
and the variable for it
right now in my mind the way i
programmed this the way i've written the
code
this application logger is not nullable
now this project is not using the
non-nullable
reference types feature in cjrv yet
i'll convert it to do that soon
but i'm explaining to you here as the
code is in my mind this variable will
never be null
now this is program cs there's main here
so the first thing i do in main is i
initialize this
variable and so now this has been
initialized so from this point forward
for the rest of the lifetime of the
system this variable is not null
then i use it here
and then when i call this insert blog
post method
you can see here
it's using it here right and i have no
reason or desire
to verify that this is not null anywhere
because i know the way i'm writing this
code here and my intention
is that this will never be null even
though there's no constructor per se in
this
project i'm using it as a
static it's initialized sort of soon
enough or
higher up in the entry point
and from that point forward is not known
right so that is my intention
i'm going to turn on the feature of
um
nullable reference types all right
then if you don't know what
that feature is or what i just did in
the
the code there that's the way to turn on
the that feature for the entire solution
you can turn off by file you know file
by file if it's a lot project
and you don't want to turn it on for the
whole solution and one shot or one
project for the whole project in one
shot so you can do it file by file i'm
just turning it on
for this entire solution so i've just
uncommented that
all right
okay the moment i do that it gives me
this warning let me zoom in here and
show you what that warning is this is
non-nullable field application logger
must contain a non-nullable value when
exiting the constructor
consider declaring the field as nullable
simple enough there's no constructor
here so i really don't have an option
but to
exit the constructor
without assigning it so consider
declaring this field of valuables i'll
do that
yes that's the nos this is nullable
now right away
it has already altered my intention my
intention
was not that is a nullable field is not
in my mind that is a non-nullable field
i just don't have a way the language to
express that
and so i'm sort of falling prey to
my need to be safe and the guidances
that i get from you know from
the
court analysis or bh sonar or
whatever other ide tools and you know
how this help you you're trying to get
away from these squiggles in your code
blindly
and you could
inadvertently
alter your intention
and i've done that here thinking yep
that's going to clear up my
issue with that that warning so that's
gone cool
and then i look over here and i get
another
warning that says
application logger maybe null here the
reference of a possibly null reference
all right
why is that now
there is such a thing as flow analysis
in the c compiler with regards to this
nullable reference types feature
is a static it's a compile time static
analysis so it's looking at your code at
compile time
there's no runtime manifestation of that
in other words no
loading is occurring that supports that
this is a compile-time feature
it also disappears that question mark
dot the question mark number type
disappears the
if you see the exclamation the null
forgiving or null suppression operator
disappears right there's no
manifestation of that in actual compile
code is the static analysis only
and this flow analysis i'll show you
what that means what it looks like here
the idea with flow analysis is the
following
this variable has been initialized here
all right
so then i can use it here without
wondering or hoping that this is
not even those declared as nullable here
i can use it here because
it's been initialized here so you can
see so by doing the flow analysis you
can see that okay this was initialized
here so we use it here i don't have to
warn you about this
possibly being null
right
however when you come here
at that point
and you call this method
this method seeing that the same method
that is dependent on a member variable
the member variable has been marked as
nullable
so over here you're getting the one
thing but this could be null
right
because of these variables even though
they it could have done the flow
analysis and saying okay this is the
only place that's calling this method
it's not null for the time being because
the only one that's calling this method
is here and this over here this thing's
already been initialized before calling
the method so i can look at that i can
see that and doing the flow analysis
this should be
you should be able to see that
that could change in the in future
versions of c sharp compiler could
become smarter in the flow analysis
which is actually bound to happen over
time except at this point in time
it doesn't know that it's not doing that
so again given the
my mindset
i want to get rid of that
squiggle now
this
particular incident of this this code
base i actually was inspired by to do
this video because of again something
that happened in the real world
some discussion that took place on one
of my teams and
i said you know what this is a good
enough video to make it's short enough
but also conveys the point exactly
and so and this applies to i don't know
if you've already figured this out but
applies to pretty much every video i've
made
it all comes from my real world
experiences so
i'm not
making things up if you
and so this is one of those videos and
so the discussion here was you know well
let's just do you're going to get rid of
that right just do that
so you call the safe call or the null
conditional operator saying okay then
just do it conditionally call it if this
can't be null then
do that but
now i've altered the meaning of my code
way too much
right first of all i made the member
variable nullable which in my mind is
not so i'm already deviating from my
intent i'm not able to convey my intent
clearly
then over here
what i'm doing is um
see the code works and the
warning disappeared and most programs oh
cool i got rid of that warning i got rid
of the warning great i'm done right
i don't know if sometimes i feel so
frustrated with this sort of thing
because the thought process
is of course the initial problem
the fact that you didn't recognize that
you have now changed your intent of this
is your own code here
ends up being the other sort of
contentious bono contention of the
frustration
we need to think harder about the code
we write we need to stay away from
wanting to be safe
at least that's how i see that this deep
desire to be safe
stop that
don't be safe
you know just don't worry about that
stuff because that's what's going to get
you into trouble
right so the need to be safe is i think
is the genesis of the reason for why
some of the stuff happens
be true to your intent but think clearly
about the code you're writing and the
intention you have what is your intent
what would somebody else
that reads your code understand from the
code you just wrote
but the code is talking to you so what's
it saying
what's it saying to you what would you
say to somebody else who doesn't have
any idea what your intention is right
would it say the same thing
that's what you want to be focused on
no warnings here
but is it saying the same thing it's
saying to me here that this could be
potentially null so i'm just going to
call this with an unconditional operator
but
are you really saying that you don't
care if this gets logged
is it okay
that
that block will not take place
this logging is an example i mean this
happens everywhere in actual other sort
of code not stuff that is doing log but
actual code that's calling things and
setting values right
you're not that is not your intention
you're not really saying that the
logging in this case
is optional
right in the sense that
there's an actual method let's say over
here before this let's say you know
actually insert the data data into
your database and then log the fact
in my mind that logging is not an
optional thing you want to log each time
you do an insert let's say just as an
example
then logging is not not an optional
thing then why do you put the question
mark dot because that's what you're
saying to me with code and that that
it's okay that if that logger is null
just ignore it don't log i'm cool with
that
but yeah insert the data in the database
just don't log it
no that's not any attention your
intention really or it should be the way
i would say it is that this
logging over here is not an optional
thing so if this thing is null
well too bad
you can't do this the problem is
somewhere else
fix that like this this is not the
reason to
to make this optional you can't put the
question mark dot here even though it's
solving the problem or it's you know
it's not giving you the warning here
anymore
it's highlighting another problem
don't fix it here at the site where
you're seeing the symptom
fix the root cause
all right so if you agree with that then
it's okay then what do we do to fix this
now unfortunately with the way the
language feature is currently the
non-nullable reference lives feature is
in language there's no real way to
actually get around that
one way could be that we could use
define an operator this is not about the
non normal reference size by the way but
let me just show you what i'm thinking
or how i'm thinking about this
i would say okay you can just say not
now here there's a
attribute not none
it won't get rid of this warning here i
don't believe
yes it's not it's still saying it's not
initialized here but over here it's no
longer giving that warning so that's one
way to kind of do that
but this is useless this is not the
intention of this attribute
what you could do is you could suppress
the warning here
so now that that's been suppressed you
don't see this error here either
that's in many ways that is the my
intention as i'm saying yes i understand
this variable is not nullable
i'm not initialized in the construct and
i'm cool with that and i'm okay with
that
so
you can use that and i wish there was a
way in c sharp to actually do that with
the you know like the bank operator then
the null forgiving operator or the null
suppression operator
[Music]
which again i think is
not quite where it's supposed to be or
should be you know
i would have preferred to do something
like that
saying yes it is nullable it's not
nullable and i know it's not nullable so
just let it let me do this you know but
there's no option for that
the other option is to leave this like
that
and
well
you what you'll end up with here instead
of the null conditional operator is you
can have the null forgiving operator
and that's probably one other solution
in the scenario where he's still
altering in the meaning of what you're
doing here so i really have a problem
with this right this is where i have a
problem
i'm not intending to say this that this
is
nullable but i've now set it to be
nullable
that's not my intention
but i can still get around it using the
exclamation dot instead of the question
mark dot the question about doc dot was
altering my intent
but the null forgiving and all
suppression operators saying just
suppress the warning let me just do what
i want to do without you telling me
there's a warning here
so it's still
solving this problem as it's not
changing my intention here
but i am changing the intention here
this is not what i intended
so
that's all i had for this video i hope
this is conveyed to you
that the null conditional operator
is something to be
watchful of don't misuse it don't abuse
it don't overuse it use it only if and
when it is required and that means you
need to think about your code you need
to think about your intention
will this thing ever be now or am i just
being safe about it
and don't be safe there's no benefit to
being safe honestly it's just causing
more trouble is
passing the buck if you will downstream
it's hiding the true nature of the
problem and it's showing up as another
problem somewhere else
just don't do that sort of thing right
so and if you need to you can also watch
the
other videos on my programming but the
intent series start with the first one
that itself will probably blow your mind
and then go on to others
all right i hope you've enjoyed this
video and hope you've learned a few
things if you have
please give me a thumbs up
and i will see you
next time