Enterprise Architecture Patterns

Reducing Complexity Solution

Enterprise Architecture Patterns

Check out a free preview of the full Enterprise Architecture Patterns course

The "Reducing Complexity Solution" Lesson is part of the full, Enterprise Architecture Patterns course featured in this preview video. Here's what you'd learn in this lesson:

Lukas live codes the solution to the Reducing Complexity exercise.


Transcript from the "Reducing Complexity Solution" Lesson

>> Let's see the solution for this exercise. So, I'm gonna hop into the code and I'm going to start to Fix this monstrosity. Now, what I'm going to do as well, just as a reference. I'm going to look at the test. So I've gone ahead and I've kind of written test that should work, which in a way gives you an indication or a hint of what your code should look like.

And so you can see here just off right on the gate. You can see that we are looking for update widgets, get total price, add widget update widget and we have a pretty good idea of what we are looking for. So from here, let's go ahead and start to extract this.

So, what we need to do here is we'll go update widget. So this will be one method and then we'll do delete widget. And we're gonna go create widget. And then I believe as you can see here I already got this wrong, add widget. And there's one more I'm looking for get total price.

Add widget, get total price, Excel and then from here, what we can do is we can just copy this out and we can paste this in. Now, what I'm gonna do is a little bit of a kind of a mental thing is that in a method it's not always bad but when you find yourself referencing this dot something, just stop and check and see if you can possibly extract that out to a parameter.

And so in this case I can, so I don't need this. Instead, what I need to do is I need to go widgets, widget. All right, and so now this is a fairly pure function that takes a collection and return something and it's going to do the same thing every single time.

And from here, what we'll do is we're just going to go return this dot delete widget, and we'll go widgets, widget and so this is going to obviously break initially. But you can see here it's asking for widget. So let's update. Well, let's do these methods first and then we'll go through and continue.

I'm kind of coming from the bottom up, but you could also go from the top down. So from here to update, we're going to copy this right here. And We'll pass in widgets and widget. There we go. Clean this up. And now let's go to the next one.

And I can delete this instead of the return, return this dot update widget, widgets, widget. And from here, one more. And this one is a little funny in the sense of we're faking that we're pulling from a database. And so we're having to kind of hard code or not hardcore, but generate a UUID for it.

And so this is why you are seeing this little bit of gymnastics here, all right? And so now we have three methods that it takes widgets and it takes a widget and then it essentially performs that function and returns a new widgets collections. By the way, these are immutable operations.

We'll talk about these later. But I just wanted to point that out. And so from here, return this dot add widget, widget, widget. Okay, now let's go. Mode, widgets So we're gonna type this like so. And I believe mode is a string. There we go. And so now we are getting rid of mode here.

And let's also do this last one here Which will take just widgets. All right. So I've kind of broken it out but I still have the single responsibility principle violation here. And so let's see if I have any hints in here about what is going on. So here we have it, update widgets.

So let me go back to here and we're going to create a new method okay, widgets which takes Mode widgets and a widget. And I suppose I could have just renamed this one up here that, I wanted to show us actually moving this out. Oops, I'll paste this in here.

And then let's just pick this up. And we'll go here. So now we'll say this widgets equals this update widgets. Mode, widgets, widget, Widgets. There we go. And so just to walk through this real quick, I think I've done everything correctly. Other than I'm missing a mock widget.

No problem. You can copy this here, but let's actually let's look at the code. Let's run the test. I think we might have one little tiny issue. But we can check that. So notice in the recalculate total that I've extracted all of that functionality out into two methods.

And then I've taken the nested logic. I've extracted those out into standalone methods. And then from there, I've abstracted or extracted the mode from internal state or hidden state to a parameter that we provide via dependency injection. So let me save this. And let's go into the command line.

And let's run this. And let me real quick, let me make this bigger, there we go. So let's run this and see what happens. One failed, so right here, we need to make this a probably a mock widget. So let's hop back in our code. I thought that was going to happen.

Let's see if we can fix this. I'm just going to copy this. So we have a mock widget. Where is this angry? We'll just go here. So I've just defined a mock widget. So this is just me getting this test working. A small little hiccup and it's because I have strongly typed This, all right.

I'm not seeing any more. Let me set this right here. Save this. And so all I did is before I had not typed that as a widget and the tests were able to pass but since I went ahead and strongly typed it then it's got a little angry at me.

So back here, let's see if we can get this working. Every time I run unitest I start making deals with God like if this just passed I'll never eat another carbohydrate again, I promise to be good. But looks like I did eat pizza for lunch, but I'm glad my test pass.

All right.

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now