- Another good programming practice
we will talk about in this video
is the need for simple things,
simple solutions that are clear.
So let's start with simplicity.
What do we mean by a simplicity concept
in the context of programming?
First of all, we don't like complex code
that's so hard to read and so convoluted
even though there's some trickery there
that's the envy of the world.
Even though complex code might be
more efficient and harder to steal,
simpler code it's easier to read,
easier to maintain, easier to debug,
potentially less efficient,
but it's still worth considering
once you have all the information necessary.
Let's look at a little program
and try to come up with some advice.
That's all the program does,
other than printing something.
And if you haven't noticed yet,
it calculates the square root equation.
So we have the values of A, B, and C,
which means we have formula AX square
plus BX plus C equals zero.
That's the formula we're solving.
And we know that the solution to that formula
is minus B plus square root of B square
minus four AC, all of that in paren,
and square root divided by two A.
So minus B, minus square root of,
B square minus four AC
divided by two A, and so on.
What I would like to do here
is to show two alternative ways
to write those calculations.
Look at line 18 and 19.
The formula looks very cluttered.
It's not clear with all those parentheses
where they open and where they close.
On the other hand, line 24,
we are calculating separately the discriminant,
taking B square minus four AC.
If the discriminant is bigger than zero,
only then I can perform any kind of calculation about it.
So if it's bigger than zero
and I have another qualifier, four AC,
if that A is also not equal zero,
then I can proceed because recall
the qualifier equation is B square
minus four AC, square root of all that,
then we add either the negative of it or the positive of it
and divide everything by two A.
If A is zero, it will imply
that you are dividing by zero,
and we have to handle that separately.
So what we're doing is
before we're calculating anything,
so we don't just calculate for no reason,
I'm asking, is the discriminant bigger than zero?
And is A not equal zero?
If that's the case, then we can now proceed
and do it in a slightly different way.
We have the discriminant already computed separately.
And now I have numerator one and numerator two.
So the numerator is minus B
plus the square root of the discriminant
or minus B minus the square root of the experiment.
One other reason to break those into those chunks
is that I have an opportunity now
to test those values separately.
I can test the value of numerator one.
I can do numerator one divided by two A and so on,
so I'm not that dependent on an external source.
And that's all there is about this video.
All we need to do now
is actually print the results.
And here they are.
In both instances where we print root one and root two,
we get the same exact result.
So root one is minus 0450807
and negative two minus 3549193.