Heads up! To view this whole video, sign in with your Courses Plus account or enroll in your free 7-day trial. Sign In Enroll
Preview
Start a free Courses trial
to watch this video
Let's look at some legacy code, and spot the problems.
The code to this project can be found on GitHub.
Changes are made to the live
branch.
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign upRelated Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
[MUSIC]
0:00
Now, I realize that this
might cause some trauma.
0:04
But work with me here.
0:08
So imagine, if you will, that this code
here to connect to our legacy database
0:10
is repeated hundreds of times.
0:15
Every single time we need
to connect the database
0:17
this same code dance happens over and
over.
0:20
And it's a bit of a hassle.
0:23
So let's take a look at it really quick.
0:25
So we get a database and then we open
up a try block because this connection
0:26
could definitely throw an IO exception,
and
0:31
sometimes this connection for
some reason it comes back closed.
0:34
So what we've done as a team is we've
made sure that everybody knows that,
0:38
hey make sure that the connection
is open before you do it.
0:42
So it's become part of our dance,
it's become part of our ritual,
0:46
we need to do this.
0:48
So we open it up then we go ahead and
we execute and we get some stuff back.
0:50
This is kind of really the meat of what
the whole thing is trying to do, but
0:54
look at all this code surrounding it.
0:57
So we're going to catch that exception,
right,
0:59
because maybe this connection opens up and
it doesn't work or whatever.
1:01
So we want to catch that, we need
to catch it because it throws that.
1:05
And then finally we need to make
sure that we clean up, right?
1:07
We don't want to leave these
things dangling out there but
1:10
there's a problem there too, right?
1:13
So the cleanUp also might
throw an IO exception.
1:14
Now this is a pretty intense stance and
because it's so
1:17
intense and there's all these places
where it might happen, we've copied and
1:21
pasted this all over our code base.
1:24
It's nobody's fault really because
they were told this is the dance
1:26
that we need to do.
1:29
So let's go ahead and let's run it and
let's just see what happens.
1:30
Now I have gone ahead and
I mocked all the stuff out.
1:32
So basically,
I let us know when things are happening.
1:36
So we know the new database was called,
the connection was opened.
1:39
Then we run our query and then we call
cleanUp which closes the connection.
1:42
So that's what this cleanUp method here
does is it kind of closes the connection
1:46
that, any connection that was open.
1:49
So let's take a look really
quick at our legacy database.
1:51
That's interesting,
it implements AutoCloseable.
1:57
Now this is part of,
Java 7 did this really cool feature so
1:59
that you could kind of help resources that
need to be closed afterwards can do this.
2:03
Now I notice that ours is not using
this try with resources block,
2:07
it's still doing this this finally move
here so let's go ahead and let's fix this.
2:12
Now what I want to point
out here is that we evolved
2:18
our legacy database here to meet the Java
7 standards, but people aren't using it,
2:22
people are copying and pasting and that's
because it's still a bit of a dance.
2:26
Let's go ahead and I'm gonna make this,
so the try with resources block,
2:29
you open it up like this.
2:34
And then go ahead and put that in there
and we're going to lose our finally here.
2:35
I'm gonna go ahead and, we need to
catch an exception now though, right?
2:42
Cuz the autocloseable will
throw an exception so.
2:47
This is kind of what we look like here.
2:53
Actually, guess we can probably
lose this catch, right?
2:56
And we'll just catch any sort
of exception that comes through.
2:59
All right so, what happens is
anything that's inside of this,
3:03
this try with resources,
now it will automatically close.
3:05
Now this is still a dance, right?
3:08
We still need to, we're still making
sure this connection is open.
3:10
We're still going through here.
3:13
We could say, hey everybody on staff,
look, this is how you do it now.
3:15
Don't worry about that finally block
anymore now that we have this try with
3:20
resources.
3:23
But I really think there's a lot
of code that's duplicated here.
3:24
Now, not only is the code duplicated and
that's a problem,
3:28
that you're typing these things over and
over again and you're doing the copy and
3:31
pasta, basically you're just copying and
pasting this, right?
3:34
Let's be honest, you're copying and
pasting this block of code and
3:37
people are copying and
pasting the older block of code.
3:39
So it's hard to shift everybody's
mind to the new way of doing things.
3:41
This is pretty much better,
it's not the best, right?
3:45
One of the things that's really horrible
is that innovation has stalled right?
3:50
Like let's say that we wanted
to do connection pools, right?
3:53
It'd be kind of hard to do that
in each one of these patterns,
3:56
right, you'd have to change the way
that this getConnection comes out here.
3:59
What if we wanted to do transactions,
right,
4:03
like let's make sure that you only want
to have this happen if it was successful.
4:05
That'd be really hard to
do because of this dance,
4:09
is there a better way to do this?
4:12
There is, let's take a look at consumers.
4:14
You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up