< Return to Video

Robert C. Martin: Principles of Component Design.

  • 0:04 - 0:08
    Ready? Is Sound On?
  • 0:09 - 0:11
    Cool. Alright. So.
  • 0:23 - 0:25
    Do you see that red dot?
  • 0:30 - 0:32
    Why is it red?
  • 0:35 - 0:37
    It's dangerous.
  • 0:38 - 0:41
    I like that answer. Watch out! Take me to the Cuba.
  • 0:43 - 0:52
    Yeah, I've had the security people restricted me from going in the airports, because I'm carrying lasers with me.
  • 0:52 - 0:55
    They have, you know, sign on and it says dangerous.
  • 0:57 - 0:59
    But, why is that red?
  • 1:01 - 1:02
    I have a green laser.
  • 1:02 - 1:05
    Hey, you know, you gotta have lasers.
  • 1:06 - 1:09
    Alright? I carry three of them with me.
  • 1:11 - 1:16
    One of them is red. I always keep them in my back pack with the batteries (??).
  • 1:16 - 1:18
    Or turned around actually.
  • 1:18 - 1:23
    So this is my red laser. You can get a red laser for twelve dollars.
  • 1:24 - 1:25
    There it is. Red laser.
  • 1:25 - 1:29
    Nice. And I got a green laser.
  • 1:30 - 1:31
    This one is nice one.
  • 1:33 - 1:35
    A pretty bright one too.
  • 1:35 - 1:39
    Not bright enough to do anything interesting, but bright.
  • 1:39 - 1:41
    There is little adjustment in here.
  • 1:41 - 1:45
    You can cut a component out and put a different register in here.
  • 1:45 - 1:48
    Then you can get almost of quarter of Watt out of it.
  • 1:48 - 1:51
    The battery drains pretty fast. You can pop a balloon with it.
  • 1:52 - 1:55
    I mean that's cool. Boom.
  • 1:57 - 2:01
    But this is my favorite laser.
  • 2:02 - 2:05
    And I like this one because
  • 2:07 - 2:13
    Well, can you see that?
  • 2:15 - 2:19
    Little tiny violet dot. Do you see that up there?
  • 2:21 - 2:25
    See if I can show it to you here. Oh! That’s nice and bright.
  • 2:25 - 2:28
    On my thing here. That's nice and bright.
  • 2:28 - 2:31
    But over there, you can hardly see it.
  • 2:31 - 2:34
    Can you see this thing? No.
  • 2:34 - 2:36
    Can you see it here? No, not too much.
  • 2:36 - 2:39
    No, can't see it there much.
  • 2:39 - 2:49
    But, I found this once.
  • 2:51 - 2:57
    Just a marker. Litter laser doesn't do too much to the marker.
  • 2:57 - 2:59
    But the lid!
  • 3:02 - 3:05
    And look! It's yellow!
  • 3:06 - 3:07
    It's not blue!
  • 3:08 - 3:11
    And then see that little orange thing there?
  • 3:11 - 3:14
    Watch this! I gotta aim it carefully.
  • 3:14 - 3:18
    Wow! It's orange.
  • 3:18 - 3:20
    What kind of laser is this?
  • 3:22 - 3:26
    This is a laser of some other color.
  • 3:34 - 3:41
    My glasses are the kind that turn dark in the sun light.
  • 3:45 - 3:48
    I don't know if you can tell. But now there are all dark.
  • 3:48 - 3:51
    Alright? And I can't see any of you.
  • 3:51 - 3:53
    This is an ultra violet laser.
  • 3:54 - 3:58
    They don't bill it as such. You can buy them on amazon. 17$.
  • 3:58 - 4:01
    They say it's a violet laser. They lie.
  • 4:01 - 4:07
    It's much cooler than violet laser. It's an ultra violet laser.
  • 4:07 - 4:10
    Everybody has to have an ultraviolet laser.
  • 4:10 - 4:13
    It's completely useless as a laser pointer.
  • 4:13 - 4:17
    But you can draw nice little pictures on you glasses with it.
  • 4:19 - 4:22
    Oh~ I got my name.
  • 4:25 - 4:27
    Alright. Whatever.
  • 4:29 - 4:31
    Look at the code on the screen.
  • 4:33 - 4:34
    Anybody recognize that code?
  • 4:36 - 4:38
    I can't see it because my glasses are all dark.
  • 4:39 - 4:41
    Anybody recognize that code?
  • 4:42 - 4:44
    That's PDP-8 code.
  • 4:45 - 4:49
    That what code looked like in 1970s.
  • 4:49 - 4:55
    That's kind of code I was writing when I was slightly older teenager.
  • 4:56 - 4:59
    And then on into my twenties as well.
  • 5:00 - 5:04
    This statement right there.
  • 5:04 - 5:06
    Anybody know what that means?
  • 5:08 - 5:14
    That's the memory address which this program would be loaded.
  • 5:15 - 5:20
    We used to write the address our program into our program.
  • 5:21 - 5:24
    This program would be loaded at address 200.
  • 5:24 - 5:27
    And we put the data at address 300.
  • 5:27 - 5:28
    How about that?
  • 5:29 - 5:33
    And it makes perfect sense. Of course you would know where programs getting get loaded.
  • 5:33 - 5:35
    I mean who else is gonna decide that for you?
  • 5:35 - 5:38
    You are the programmer. You have control over memory.
  • 5:39 - 5:41
    Now, this works fine.
  • 5:43 - 5:46
    I'll show you a sequence of pictures here.
  • 5:46 - 5:50
    Let's see. Yeah, that's a nice one.
  • 5:50 - 5:55
    Hello. No, that's not the one what I wanted.
  • 5:56 - 6:02
    That's because nowadays when you open up documents
  • 6:02 - 6:10
    It opens up every document that's been opened by that application.
  • 6:11 - 6:12
    Don't save that.
  • 6:12 - 6:15
    That's the one I wanted, right there.
  • 6:17 - 6:20
    Imagine you are programmer who's writing this kind of code.
  • 6:21 - 6:24
    And you've got a program like this one.
  • 6:25 - 6:28
    My program lives here. It starts at address 200.
  • 6:29 - 6:32
    And there's a subroutine library that somebody else has written.
  • 6:33 - 6:35
    By the way subroutine libraries were not real common.
  • 6:35 - 6:38
    You usually wrote your own subroutine back in those days.
  • 6:38 - 6:46
    But after a while a few guys write some useful subroutines. You think "You know, I should have those in my program too."
  • 6:46 - 6:50
    And you think, "Well, just compile them in with my code"
  • 6:50 - 6:52
    That's what we used to do.
  • 6:52 - 6:53
    You just take the source code and jam it together.
  • 6:54 - 6:55
    What was the problem with that?
  • 6:59 - 7:02
    We were talking about the 1970s here. Right?
  • 7:02 - 7:06
    Those programs were contained on paper tape.
  • 7:06 - 7:10
    Paper tapes is being 50 characters per second, if you were lucky.
  • 7:11 - 7:20
    And so increasing the size of your source code lengthened the size of your compile by minutes.
  • 7:20 - 7:27
    So after a while these subroutine libraries got too long to continue to add your source code.
  • 7:27 - 7:35
    So what we did is we would compiled the subroutine library and we would loaded at location 1200.
  • 7:36 - 7:42
    Then what we could do is we could have a binary file which got loaded at 1200.
  • 7:42 - 7:45
    And we could have our program at 200.
  • 7:45 - 7:49
    And we did have a little file that all the symbols in it.
  • 7:49 - 7:55
    So the symbols would tell us which subroutine was loaded where.
  • 7:55 - 8:03
    So we knew the Get subroutine was at 205 and the Put subroutine was at 210 and so on.
  • 8:04 - 8:07
    And our symbols would get compiled in with the program
  • 8:07 - 8:10
    The subroutine would be loaded separately. And everything worked fine.
  • 8:12 - 8:14
    What's the problem with this?
  • 8:16 - 8:19
    When is the last time you saw a program that stayed small?
  • 8:21 - 8:22
    They grow!
  • 8:23 - 8:26
    And after a while, well let's see.
  • 8:28 - 8:30
    I got another picture here.
  • 8:35 - 8:36
    Yeah, that's that one.
  • 8:41 - 8:44
    Oh yeah, a program that got too big.
  • 8:44 - 8:47
    It overwrites the subroutines. This doesn't work.
  • 8:49 - 8:56
    When your program overwrites the subroutines, your program still thinks that the subroutines are there.
  • 8:56 - 9:02
    So when it calls the location 1205 it actually jumps into some arbitrary part of you code.
  • 9:03 - 9:04
    You didn't know this happened of course.
  • 9:05 - 9:08
    Until finally you debugged it and realized. "Oh, my programs got too big!"
  • 9:09 - 9:11
    What's the solution to this?
  • 9:14 - 9:18
    You are programmers, you can come up with the solution to this.
  • 9:21 - 9:23
    Jump around the subroutine library.
  • 9:24 - 9:27
    Right? You put it Jump right there to jump over here.
  • 9:28 - 9:31
    Of course the subroutine library gets bigger too.
  • 9:32 - 9:40
    And so after a while. Let's see if I've got that picture right.
  • 9:40 - 9:42
    How about that one? Yeah, that's the one.
  • 9:43 - 9:45
    After a while you get that problem.
  • 9:48 - 9:51
    We actually faced to this problems.
  • 9:51 - 9:54
    We actually had to deal with this stuff.
  • 9:54 - 9:56
    And you know how we solved it?
  • 10:00 - 10:05
    We came up with relocatable code.
  • 10:05 - 10:10
    We said "You know what? This idea of putting absolute address in the program is killing us."
  • 10:11 - 10:17
    "What we really like to do is compile our programs without telling them where they're going to be loaded."
  • 10:18 - 10:21
    "And then we'll tell the loader where to load them"
  • 10:21 - 10:23
    Now there's problem with that.
  • 10:24 - 10:30
    Because that means that your binary files cannot really be binary.
  • 10:30 - 10:39
    They have to have codes in them to tell you that certain address are not actually addresses. They are offsets.
  • 10:39 - 10:48
    And the loader has to find every address that's marked as an offset and add the start address to it.
  • 10:49 - 10:52
    But that works. We had relocatable loaders.
  • 10:52 - 10:55
    We made this relocatable binaries and loaders.
  • 10:55 - 10:57
    But now you've got another problem.
  • 10:57 - 11:00
    Because how do you know where the subroutines are.
  • 11:00 - 11:06
    If the subroutines are moving all over the place, how do you know where the subroutines are?
  • 11:06 - 11:11
    So now you have to add more information to the binary file.
  • 11:11 - 11:20
    This binary file you thought just binary of your program is now becoming very bizarre files.
  • 11:20 - 11:21
    It's got all kinds of gunky in it.
  • 11:21 - 11:28
    Now you have to put the names of the subroutine library into the binary file of the subroutines.
  • 11:28 - 11:32
    And you have to show what address they will get loaded at
  • 11:33 - 11:38
    And relocatable loader has to remember where it put those addresses
  • 11:38 - 11:43
    And then in the program you have to have another thing in there.
  • 11:43 - 11:47
    That says "Hey, I need to know where this programs gonna be loaded"
  • 11:47 - 11:53
    "And the loader has to link the subroutine library of the program."
  • 11:57 - 12:01
    Computers were slow in those days.
  • 12:01 - 12:03
    Disk drivers were very slow.
  • 12:03 - 12:05
    And there wasn't a lot of disk memory.
  • 12:05 - 12:08
    You were lucky if you had megabytes of disk.
  • 12:08 - 12:15
    And the seek arm took a long time and the rotational latency was high.
  • 12:15 - 12:18
    So link times took a long time.
  • 12:18 - 12:22
    Especially as we got more and more libraries.
  • 12:22 - 12:24
    More and bigger program.
  • 12:24 - 12:26
    Link time could take an hour.
  • 12:26 - 12:28
    Anybody have a link that took an hour?
  • 12:28 - 12:31
    Anybody here working in the 80s?
  • 12:31 - 12:33
    Oh no! You got one now!
  • 12:33 - 12:35
    You must be C++ programmer.
  • 12:37 - 12:39
    That says so, right on your shirt.
  • 12:39 - 12:41
    OSLO C++ Users Group.
  • 12:41 - 12:43
    Long link times.
  • 12:45 - 12:50
    Even in the 70s we had that problem. Although it was much smaller programs.
  • 12:50 - 12:58
    So the solution to that was to separate the link pace from the load pace.
  • 12:58 - 13:02
    We would link as a second compile step.
  • 13:02 - 13:07
    And that would produce a final relocatable file that had all the linkage were resolved.
  • 13:07 - 13:10
    And then we could load it at run time.
  • 13:10 - 13:12
    Ant that was relatively fast enough.
  • 13:13 - 13:18
    And for years and years and years. We lived with this 2 step process.
  • 13:18 - 13:24
    Compile down to binary files then link all the binary files into an executable.
  • 13:24 - 13:26
    And then you can load the executable.
  • 13:27 - 13:30
    And that solved problem until the 90s.
  • 13:30 - 13:34
    In the 90s something happened.
  • 13:34 - 13:36
    It was called Moore's Law.
  • 13:37 - 13:38
    What's Moore's law?
  • 13:39 - 13:44
    The speed of processors will double every 18 month.
  • 13:44 - 13:53
    Now apply that from about 1970 when the speed of our processors were half a million instructions per second.
  • 13:54 - 13:57
    And keep that going forward until the 1990s.
  • 13:57 - 14:00
    Well that's 20 years, how many 18 cycles is that?
  • 14:00 - 14:02
    Well, 18 month cycle is that?
  • 14:02 - 14:04
    That's about 15 cycles.
  • 14:04 - 14:07
    So we have 2^15 increase in speed.
  • 14:08 - 14:09
    Think about that.
  • 14:09 - 14:12
    2^15 increase in speed. What is that?
  • 14:12 - 14:14
    That's increase about 32000!
  • 14:14 - 14:15
    That's about right too.
  • 14:15 - 14:19
    Cause we went from about a half MHz to 2.8 GHz.
  • 14:19 - 14:22
    Well, maybe 1 GHz by the late 90s.
  • 14:22 - 14:25
    By that time disks were going faster too.
  • 14:25 - 14:27
    We spun up a lot faster in the road.
  • 14:27 - 14:32
    The heads weren't moving as far, we were getting a lot more bits around rim too.
  • 14:32 - 14:39
    We could get hundreds of MBs on a disk. Oh!
  • 14:41 - 14:44
    And somebody made a bright thought.
  • 14:44 - 14:46
    Somebody had a bright thought that
  • 14:47 - 14:50
    "We don't have to do the link separately anymore."
  • 14:51 - 14:54
    "We can do the link at the same time we load."
  • 14:54 - 14:57
    Anybody remember the ActiveX?
  • 14:57 - 14:59
    Anybody remember OLE?
  • 15:00 - 15:02
    What does DLL stands for?
  • 15:04 - 15:08
    Dynamically linked library.
  • 15:08 - 15:11
    Which means it's linked at load time.
  • 15:11 - 15:15
    The link step got moved back into the loader.
  • 15:16 - 15:19
    And we have the situation we have today.
  • 15:21 - 15:23
    Who's .NET programmer, here?
  • 15:24 - 15:25
    Look at that. A lot of people.
  • 15:25 - 15:28
    Java programmer! Raise your hands.
  • 15:28 - 15:30
    Not so many. How come?
  • 15:31 - 15:35
    How come it's all .NET? Oh maybe because it's a .NET kind of conference? Huh?
  • 15:36 - 15:42
    So, in .NET you got DLLs.
  • 15:42 - 15:44
    Dynamically Linked Libraries.
  • 15:44 - 15:46
    In Java, you got Jar files.
  • 15:46 - 15:51
    But they are still dynamically linked libraries. Same idea. Same purpose.
  • 15:52 - 15:56
    In C++, if you do in the Microsoft thing you still got DLLs.
  • 15:56 - 15:59
    If you don in UNIX thing you've got shared libraries.
  • 15:59 - 16:01
    They are still dynamically linked libraries.
  • 16:01 - 16:05
    Our mode of operation now a days is too dynamic linked libraries.
  • 16:05 - 16:08
    That's how we got here.
  • 16:12 - 16:14
    How many DLLs do you have?
  • 16:15 - 16:19
    Guys with the solutions, Visual Studio solutions.
  • 16:19 - 16:20
    How many projects?
  • 16:21 - 16:24
    60? That's not bad.
  • 16:24 - 16:25
    Who's got more than 60?
  • 16:25 - 16:27
    Oh, look at that.
  • 16:27 - 16:28
    Who's got more than 200?
  • 16:31 - 16:33
    And do you know why?
  • 16:33 - 16:38
    You separate your code into different DLLs?
  • 16:39 - 16:41
    Let me ask that question differently.
  • 16:41 - 16:44
    When you deploy your application,
  • 16:45 - 16:50
    Do you gather up all the DLLs and just ship the wad?
  • 16:53 - 16:58
    If you do then why are you dynamically linking them?
  • 16:58 - 16:59
    Statically link them.
  • 17:00 - 17:05
    Why would you bother with dynamic linking? If you just gonna take all those DLLs.
  • 17:05 - 17:07
    Gather them up into one gigantic wad.
  • 17:07 - 17:10
    Throw the big wad into directory. And says, "It's my system."
  • 17:12 - 17:17
    Why dynamically link if you not going to dynamically deploy?
  • 17:18 - 17:22
    Why did we come up with dynamically linked library?
  • 17:22 - 17:29
    We came up with dynamically linked library so that we could dynamically deploy our applications.
  • 17:29 - 17:30
    Why?
  • 17:33 - 17:36
    Well, we gonna get to network speed. Hang on in a minute.
  • 17:36 - 17:39
    Cause network speed has huge impact on this all thing.
  • 17:39 - 17:44
    Mid 90s I've got a client. He's got a 250MB executable.
  • 17:44 - 17:47
    In the mid-90s that was big program. Now it' not.
  • 17:47 - 17:52
    But then 250MB was big deal, you could not fit it on a CD.
  • 17:52 - 17:57
    This was CAD system. He shipped it to the company like Ford.
  • 17:57 - 18:02
    Ford would use it to design gears and levers. Stuff like that.
  • 18:03 - 18:04
    He statically linked it.
  • 18:05 - 18:11
    He would deploy the executable to his clients by burning it on several CDs.
  • 18:12 - 18:15
    If he changed one line of code,
  • 18:17 - 18:26
    He had to recompile, re-link, re-burn all the CDs and deploy all the CDs to all his clients.
  • 18:27 - 18:30
    And you can imagine how it cost him a fair bit of money.
  • 18:32 - 18:34
    I went there in the mid-90s.
  • 18:34 - 18:37
    And encountered them at a time
  • 18:37 - 18:44
    when they were trying to chop up their executable into this new Idea, DLLs.
  • 18:44 - 18:49
    Because they realized that if they had dynamically linked libraries
  • 18:49 - 18:52
    Then they could change a line of code
  • 18:52 - 18:57
    And ideally you would only have to ship that DLL.
  • 18:58 - 19:01
    You could e-mail it!. Back in those days that was a big deal.
  • 19:01 - 19:03
    You couldn't e-mail 250MB in those days.
  • 19:03 - 19:08
    Now a days you can as long as the guy you're talking to got a reasonable e-mail server.
  • 19:08 - 19:17
    But back in those days e-mailing 250MB was impossible, so they could e-mail the 100KB of DLL.
  • 19:17 - 19:20
    And so that was very very desirable for them.
  • 19:20 - 19:22
    They worked for month and month and month.
  • 19:22 - 19:25
    Chopping their application up in the little tiny bits,
  • 19:25 - 19:27
    turning them all into bunch of DLLs
  • 19:27 - 19:29
    And that when they realized their critical mistake.
  • 19:31 - 19:35
    The critical mistake was that chopping your system up into bunch of arbitrary DLLs
  • 19:35 - 19:39
    doesn’t do you a damn bit of good if they all depend on each other.
  • 19:40 - 19:43
    If they are all depend on each other then you can...
  • 19:52 - 19:56
    Was anybody just in Scott Meyer's talk here?
  • 19:56 - 19:58
    The last talk he just gave an hour ago?
  • 19:58 - 20:01
    He was talking about the problem of keyholes.
  • 20:01 - 20:08
    Problem of keyholes is that we arbitrarily constrain someone for no good reason.
  • 20:08 - 20:10
    Just arbitrary constrain them.
  • 20:10 - 20:15
    So for example, have you ever seen a text box on a GUI?
  • 20:15 - 20:16
    That was just too short?
  • 20:17 - 20:22
    And you had typed bunch of stuffs in it and it wouldn't let you resize the window?
  • 20:22 - 20:26
    It wouldn't let you scroll and you just had to type blind?
  • 20:26 - 20:30
    Or maybe the text with scroll but you wouldn't be able to see the beginning of it?
  • 20:30 - 20:35
    He was mentioning the keyhole problem.
  • 20:35 - 20:38
    And I know that something just happened here.
  • 20:39 - 20:43
    Apparently I'm not allowed not touch my computer for more than 5 minutes.
  • 20:43 - 20:48
    I must apparently touch my computer otherwise I'll be punished.
  • 20:52 - 20:55
    What was I talking about? Oh yeah, DLLs.
  • 20:55 - 20:58
    So this guy, he put all his DLLs together.
  • 20:58 - 21:00
    He forgot that DLLs depend upon each other.
  • 21:01 - 21:04
    His goal was to be able to touch a line of code
  • 21:04 - 21:08
    And just ship that DLL that was affected.
  • 21:08 - 21:12
    But he found that all the '#includes', C++ guys know what I'm talking about
  • 21:12 - 21:15
    All the '#includes' form the horrible network.
  • 21:15 - 21:18
    And he had to recompile and redeploy everything anyway.
  • 21:18 - 21:20
    They went out of business.
  • 21:23 - 21:28
    The purpose of my talk today, now that I'm getting around to it
  • 21:29 - 21:31
    is to talk about components.
  • 21:31 - 21:33
    The problem of component design.
  • 21:33 - 21:36
    And the first thing we gonna do is to define a component.
  • 21:36 - 21:39
    What's a component? Component is DLL.
  • 21:39 - 21:43
    When I talk about the word component what I mean is DLL.
  • 21:43 - 21:47
    Very particular kind of DLL. A dynamically deployable DLL.
  • 21:49 - 21:53
    A dynamically deployable DLL.
  • 21:53 - 21:56
    Why would we want dynamically deploy?
  • 21:57 - 22:00
    Well, because we would like to be able to change one line of code
  • 22:00 - 22:03
    Just ship the one DLL that changed and ignore all the others.
  • 22:03 - 22:05
    What's DLL hell?
  • 22:05 - 22:09
    A term invented, I believe, by Microsoft
  • 22:10 - 22:12
    to describe their own situation.
  • 22:13 - 22:18
    And was to be completely cured by .NET.
  • 22:18 - 22:22
    Anybody remember that line? "Dear .NET cures DLL hell."
  • 22:22 - 22:28
    Hu Hu Hu. No, it doesn’t cure DLL hell.
  • 22:28 - 22:32
    DLL hell is the problem that we've got all these little components with different version numbers
  • 22:32 - 22:35
    And nobody hell knows which one go should together
  • 22:35 - 22:39
    So we invented this module maintenance tools like Maven.
  • 22:39 - 22:41
    What are you guys using in .NET?
  • 22:41 - 22:46
    What's the tool that lets you keep all of your DLLs are in line.
  • 22:46 - 22:50
    So you know that download version 1 of that one and version 3 of that one.
  • 22:50 - 22:53
    Do you have tool for that? What?
  • 22:54 - 22:55
    NuGet?
  • 22:55 - 22:57
    Like the soft chewy (??)
  • 22:59 - 23:01
    Never mind, I'm not going there.
  • 23:10 - 23:18
    The graph you see on the screen is a graph of x^2.
  • 23:18 - 23:20
    That's just an x^2 graph.
  • 23:21 - 23:23
    But it's also something else.
  • 23:23 - 23:29
    It's the number of dependencies in a system.
  • 23:29 - 23:34
    Theoretical maximum number of dependencies, given a certain number of modules.
  • 23:34 - 23:38
    And you can see that the number of modules increases linearly
  • 23:38 - 23:41
    And the number of dependency increases with square.
  • 23:44 - 23:49
    The theoretical maximum number of coupling, which I show here
  • 23:51 - 23:55
    is proportional to the square of the number of modules.
  • 23:55 - 24:01
    Now of course we would never create a system that has every possible dependency in it.
  • 24:03 - 24:04
    Or would we?
  • 24:06 - 24:08
    Look at this curve.
  • 24:09 - 24:20
    This curve is the productivity of a team in comparison to the number of modules.
  • 24:20 - 24:25
    By the way this is completely arbitrary I just generated one of x squared curve here.
  • 24:25 - 24:28
    This is not me collecting actual data.
  • 24:29 - 24:34
    This is just me recollecting my experience with development teams.
  • 24:34 - 24:38
    They go slower and slower and slower over time.
  • 24:38 - 24:39
    Who's had this happened to them?
  • 24:40 - 24:42
    You start outgoing really fast. You can conquer the world.
  • 24:42 - 24:46
    A year later you slogging through some kind of horrible wet lands.
  • 24:46 - 24:49
    And you don't know what the heck has gone wrong.
  • 24:49 - 24:53
    But estimates that used to be on the order of one week are now three month long.
  • 24:53 - 24:58
    And by the way you blow all those estimates anyway and introduces more bugs than you fix.
  • 24:58 - 25:04
    That's the kind of problem that we have as we proceed a long development path.
  • 25:04 - 25:10
    And one of the reasons for that is accumulation of dependencies.
  • 25:11 - 25:12
    Why?
  • 25:12 - 25:21
    Well, that's the theoretical maximum dependency between modules.
  • 25:21 - 25:23
    This is the theoretical minimum.
  • 25:23 - 25:29
    If you're going to have an interconnected set of modules, there has to be some dependencies.
  • 25:29 - 25:32
    And the minimum set of dependencies is a tree structure.
  • 25:32 - 25:36
    Oh you can do some better with dynamically linking if you want to.
  • 25:36 - 25:40
    But for the most part, you gonna have a small number of dependencies.
  • 25:40 - 25:42
    How many do we have? 1. 2. 3. 4. 5. 6.
  • 25:42 - 25:45
    6 out of 7 modules.
  • 25:45 - 25:47
    Whereas here you've got
  • 25:47 - 25:51
    Well, I think that's half of 49.
  • 25:51 - 25:54
    It can't be half of 49 because that would be happy half dependency.
  • 25:54 - 25:58
    Maybe it's just a plain 49. I don't know what it is. It's a large number.
  • 25:58 - 26:01
    It's some relative of N squared.
  • 26:01 - 26:03
    Maybe it's one half n squared plus one half n.
  • 26:03 - 26:05
    Something like that.
  • 26:05 - 26:08
    It's a very large number of dependencies.
  • 26:08 - 26:11
    We don't want this. We do want that.
  • 26:11 - 26:13
    We strive very hard to get here.
  • 26:13 - 26:21
    But then, some shmuck does that.
  • 26:24 - 26:26
    Visual Studio doesn't allow this.
  • 26:27 - 26:29
    Uh! Inside solution.
  • 26:29 - 26:34
    Inside solution. The DLLs cannot have cycles between their graphs.
  • 26:34 - 26:35
    That's good.
  • 26:35 - 26:36
    Don't want cycles.
  • 26:36 - 26:39
    Between separate solutions there's no guarantee.
  • 26:39 - 26:40
    So you have multiple solutions.
  • 26:40 - 26:45
    If you're linking with things that come out of different solutions, you can still have cycles in the graph.
  • 26:45 - 26:51
    If you get cycles in the graph, it looks like it adds only one extra dependency.
  • 26:51 - 26:53
    But actually it adds more.
  • 26:53 - 26:57
    Because 6 now depends upon 2. Because 1 depends upon 2.
  • 26:57 - 26:59
    And dependency is transitive.
  • 26:59 - 27:05
    So 6 also depends upon 4 and 5. And 6 depends upon 3 and 7.
  • 27:05 - 27:07
    In fact, 6 depends upon all of them.
  • 27:08 - 27:14
    So number of dependencies multiplies dramatically as soon as you have a cycle.
  • 27:22 - 27:24
    This is the n squared graph again.
  • 27:25 - 27:29
    This is also a graph of C++ compile time.
  • 27:29 - 27:31
    As you add modules.
  • 27:31 - 27:34
    Compile time and the link time if you're doing static linking
  • 27:34 - 27:36
    But even if you're not static linking,
  • 27:36 - 27:40
    just compile time grows with the square of the number of modules,
  • 27:40 - 27:45
    if you have a fully connected network of modules.
  • 27:45 - 27:52
    What that mean is that your '#include' or your import statements or your using statements can be traced in a cycle.
  • 27:53 - 27:59
    And if you have that, then you gonna wind up with this big increase in compile time.
  • 27:59 - 28:01
    C++ in particular would do this.
  • 28:01 - 28:03
    Java and .NET don't.
  • 28:04 - 28:06
    Their compile time is based on a different metric.
  • 28:06 - 28:12
    They don't go reading source files. The way C++ does.
  • 28:12 - 28:15
    Java and .NET read binary files to get their declarations.
  • 28:15 - 28:19
    C++ reads source files to get his declarations.
  • 28:19 - 28:24
    And so if you had a cycle in C++, you got punished.
  • 28:25 - 28:27
    By a big compile time.
  • 28:27 - 28:29
    And a massive one.
  • 28:29 - 28:30
    It would go up with square.
  • 28:30 - 28:33
    So you would had couple of modules your compile time will double.
  • 28:33 - 28:36
    And that made us do something about it.
  • 28:36 - 28:39
    Who knows who Ward Cunningham is?
  • 28:40 - 28:40
    Oh, a few of you do.
  • 28:40 - 28:42
    Good. And the rest of you.
  • 28:42 - 28:43
    He's the guy who invented Wikis.
  • 28:43 - 28:47
    Ward Cunningham invented Wikis.
  • 28:47 - 28:52
    He's the guy who helped Kent Beck invent pair programming, test-driven development,
  • 28:52 - 28:53
    And most of the agile stuff.
  • 28:53 - 28:57
    Get to know who Ward Cunningham. He is very interesting fellow.
  • 28:57 - 28:59
    Smalltalk Programmer from a long ago.
  • 28:59 - 29:01
    And I asked Ward once.
  • 29:01 - 29:03
    "Why did Smalltalk died, Ward?"
  • 29:03 - 29:10
    And he said, "Smalltalk died because it was so easy to make a mess."
  • 29:11 - 29:13
    "You, C++ programmer,”
  • 29:13 - 29:14
    I was a C++ programmer at that time.
  • 29:14 - 29:17
    "You, C++ programmers are lucky."
  • 29:17 - 29:19
    "Your language punishes you if make a mess."
  • 29:20 - 29:22
    "Smalltalk didn't punish you."
  • 29:23 - 29:27
    Well, neither does Java. Neither does C#.
  • 29:27 - 29:29
    They don't punish you anymore.
  • 29:29 - 29:31
    It's very easy to make a very large mess.
  • 29:31 - 29:35
    And get a very tangled structure and not know you're doing it.
  • 29:35 - 29:39
    Fortunately Visual Studio keeps some of the cycles out of your graph.
  • 29:44 - 29:49
    We would like that level of productivity, which is n Log N.
  • 29:49 - 29:52
    Rather than this level of productivity. That's an n squared.
  • 29:53 - 30:00
    And one of the ways to help with that is to manage the dependencies between our components.
  • 30:02 - 30:07
    Then something happened to us in the late 90s and into the 2000s.
  • 30:07 - 30:10
    Network speed started increasing dramatically.
  • 30:11 - 30:16
    Now a days, it's not hard at all to download a GB in a couple of seconds,
  • 30:16 - 30:18
    And to upload a GB in 10 seconds.
  • 30:18 - 30:19
    That's pretty easy now a days.
  • 30:19 - 30:22
    Back in the early days it was much harder.
  • 30:22 - 30:28
    So, back in the early days we thought shipping individual DLL was going to be a benefit.
  • 30:29 - 30:32
    Now a days we just kind of gather them all together and ship the one big wad.
  • 30:32 - 30:36
    Why? Well because network speed is fast enough. We can do it.
  • 30:36 - 30:40
    We can treat our batch of DLLs just like it was statically linked.
  • 30:42 - 30:43
    But there's another issue.
  • 30:44 - 30:46
    How many of you work in teams?
  • 30:47 - 30:50
    Oh look at that. Everybody works in teams.
  • 30:51 - 30:54
    So, you come in at 8 in the morning.
  • 30:56 - 30:59
    You got a test to perform.
  • 30:59 - 31:02
    You work all day to get all your stuff working.
  • 31:02 - 31:04
    All works by end of the day.
  • 31:04 - 31:05
    You check it in and go home.
  • 31:05 - 31:07
    Come back the next day all your stuff is broken.
  • 31:07 - 31:08
    Why?
  • 31:10 - 31:12
    Somebody stayed later than you.
  • 31:13 - 31:16
    And changed something you depend upon.
  • 31:17 - 31:22
    And so you work all day long to fix whatever the problem was.
  • 31:22 - 31:24
    You go home and come back the next day and your stuff is broken again.
  • 31:25 - 31:26
    How many times can you go around that loop?
  • 31:27 - 31:31
    Lots of time. This is a problem of large teams.
  • 31:31 - 31:35
    Large teams will start to step on each other.
  • 31:35 - 31:37
    And of course we've invented tools to help us.
  • 31:37 - 31:39
    We've got source code control systems.
  • 31:39 - 31:42
    And we've got all kinds of good stuff to help us with this.
  • 31:42 - 31:47
    But we still can step all over each other, unless we manage our projects well.
  • 31:48 - 31:52
    So, how can we manage our projects well?
  • 31:56 - 31:58
    There is a principle.
  • 32:07 - 32:11
    A principle called Acyclic Dependencies Principle.
  • 32:13 - 32:19
    The Acyclic dependencies principle says, "If you have a set of components"
  • 32:19 - 32:23
    "You would like to be able to arrange without any cycles in the dependency graph."
  • 32:23 - 32:26
    Now, for whole bunch of reasons we've already talked about one of them.
  • 32:26 - 32:30
    Which is compile time. We've talked about another which is just dependency load.
  • 32:30 - 32:32
    Here is another.
  • 32:34 - 32:39
    Alarm would like to release its version of alarm.
  • 32:39 - 32:43
    The team that's working on the Alarm would like to make release 1.0.
  • 32:43 - 32:49
    They've got nobody that they depend upon.
  • 32:49 - 32:52
    So they are completely free to make any release they want.
  • 32:52 - 32:56
    So they release their version of 1.0. Alarm 1.0.
  • 32:56 - 32:58
    They start working on alarm 1.1.
  • 32:59 - 33:02
    But now Alarm 1.0 has been released.
  • 33:02 - 33:06
    Which means that Elevator and Conveyor can make their release.
  • 33:07 - 33:12
    And once Elevator and Conveyor made their releases they can start to work on 1.1.
  • 33:12 - 33:16
    But now Transport can make its release.
  • 33:16 - 33:18
    You can see what's happening here. Right?
  • 33:18 - 33:21
    The version numbers bubble up from the bottom.
  • 33:21 - 33:27
    1.0 gets created here. Then there. Then there. Here. Here. And finally there.
  • 33:27 - 33:30
    The version numbers bubble up from the bottom.
  • 33:30 - 33:33
    If you look at it closely, you'll realize that
  • 33:33 - 33:38
    the version numbers follows exact same path as the build. The compile.
  • 33:38 - 33:42
    Dependencies are running in that direction.
  • 33:43 - 33:50
    And now, some poor shmuck does this.
  • 33:50 - 33:53
    Who's this?
  • 33:53 - 33:54
    Well that was me. I did that.
  • 33:54 - 33:59
    I had an alarm subsystem I was working on.
  • 33:59 - 34:02
    And I needed it to put a message on the display and the control panel.
  • 34:02 - 34:07
    There happened to be a class up here that had a function called Display.
  • 34:07 - 34:10
    And I thought, "Oh! I should just call it." So I called it.
  • 34:10 - 34:13
    This was not in a language that restrict me from cycles.
  • 34:13 - 34:17
    And so it compiled and everything was fine and all worked okay.
  • 34:18 - 34:23
    And then the next day I had a group of angry developers come to my cubicles with club
  • 34:24 - 34:25
    And tell me "What the hell did you do?"
  • 34:26 - 34:33
    I said, "Well, I just called the control panel and display class that has display function in it."
  • 34:33 - 34:35
    "I needed to put this message on the screen."
  • 34:35 - 34:36
    And they said, "You can't do that."
  • 34:36 - 34:38
    Why can't we do that?
  • 34:40 - 34:41
    First of all,
  • 34:44 - 34:48
    what order should we build those modules in?
  • 34:50 - 34:53
    You'd like to build them bottom up.
  • 34:54 - 34:56
    But now there's no bottom.
  • 34:56 - 34:58
    So there's no correct build order.
  • 34:58 - 35:01
    If you have cycles in the component graph,
  • 35:02 - 35:06
    there is no correct build order for the modules in that system.
  • 35:07 - 35:11
    And therefore the execution of that system is undefined.
  • 35:12 - 35:15
    What does undefined mean? What's the definition of undefined?
  • 35:16 - 35:18
    Works in the lab.
  • 35:20 - 35:24
    Anything undefined will work until you actually deploy it somewhere. Then it will fail.
  • 35:25 - 35:29
    You can get systems to fail rather badly by having this cycles.
  • 35:29 - 35:31
    This is pretty common in Java.
  • 35:31 - 35:36
    If you have a system of Java that has cycles in it.
  • 35:36 - 35:37
    You can build it.
  • 35:37 - 35:39
    Although there is no correct build order.
  • 35:39 - 35:41
    Then you run your test. And test will fail.
  • 35:42 - 35:45
    Then you build it again. That will choose different build order.
  • 35:45 - 35:46
    And maybe the test will pass.
  • 35:46 - 35:49
    I know of companies that put their build in a loop until test pass.
  • 35:55 - 35:56
    But the problem is worse than that.
  • 35:57 - 36:04
    Because conveyor would like to make their release.
  • 36:04 - 36:06
    They want to release 1.1.
  • 36:06 - 36:11
    Now for them to release 1.1, they have to test with alarm 1.1.
  • 36:12 - 36:18
    But alarm 1.1 is waiting for control panel 1.1, which is waiting for transport 1.1
  • 36:18 - 36:22
    which is waiting for conveyor 1.1, which is the one trying to release.
  • 36:22 - 36:28
    So there's no way to make the release without checking all of that source code out into one place.
  • 36:28 - 36:31
    Integrating. Anybody remember integration.
  • 36:31 - 36:32
    The joys of integration.
  • 36:32 - 36:37
    Integrate the whole system and then make it work.
  • 36:38 - 36:41
    And while you're doing that you gonna be stepping all over each other.
  • 36:41 - 36:50
    So, if this cycle will bring back the problem of coming in 8 in the morning and find everything doesn't work.
  • 36:50 - 36:52
    But it's worse than that.
  • 36:53 - 36:58
    Because in order to test conveyor I need alarm.
  • 36:58 - 37:02
    Which needs control panel which needs revenue, which need the database.
  • 37:02 - 37:06
    The database takes 45 minutes to load and then it crashes.
  • 37:07 - 37:09
    I can't run my tests.
  • 37:09 - 37:14
    Ant the guys in conveyor saying, "Why the heck do I need the database?"
  • 37:14 - 37:17
    "Well, you need the database because of this strange dependency structure."
  • 37:18 - 37:22
    Anybody ever look at the number of DLLs that got loaded and scratch head and say,
  • 37:22 - 37:24
    "How come I need those?"
  • 37:24 - 37:27
    Anybody in C++ world have ever link line?
  • 37:27 - 37:29
    And wonder "what's all this stuff in the link line?"
  • 37:29 - 37:31
    "How come I need all that stuff?"
  • 37:31 - 37:33
    Right? You got cycles in the dependency graphs.
  • 37:33 - 37:35
    It's bringing in all kinds of gunk.
  • 37:35 - 37:43
    So the first principle of components is no cycles in the components.
  • 37:43 - 37:45
    What if you want to do this?
  • 37:46 - 37:52
    What if you really want to call some function from down here that lives up there?
  • 37:52 - 37:53
    How you gonna do it?
  • 37:55 - 37:57
    Well, you can put out another component.
  • 37:58 - 38:00
    That's one way to do it.
  • 38:00 - 38:03
    Here I took that class out of the control panel.
  • 38:03 - 38:05
    I put it in the display component.
  • 38:05 - 38:08
    Then the alarm component can talk to the display component.
  • 38:08 - 38:10
    The control panel can talk to the display component.
  • 38:11 - 38:12
    I can keep the cycles out.
  • 38:12 - 38:14
    That's common enough technique.
  • 38:14 - 38:16
    Remember, these are all DLLs.
  • 38:16 - 38:23
    So number of DLLs in your system will start to grow as people want to add cycles to the dependency graph.
  • 38:23 - 38:25
    Maybe.
  • 38:25 - 38:28
    Although there's another way to resolve the cycle.
  • 38:30 - 38:33
    You can use dependency inversion.
  • 38:33 - 38:40
    I could put an interface. A display interface.
  • 38:40 - 38:42
    In the alarm subsystem.
  • 38:42 - 38:45
    And have the control panel implemented it.
  • 38:46 - 38:48
    That turns the dependency around.
  • 38:50 - 38:53
    And changes a cycle into a straight tree.
  • 39:00 - 39:01
    What's OO?
  • 39:04 - 39:06
    What is Object Orientation?
  • 39:09 - 39:10
    Why do we like it?
  • 39:11 - 39:15
    How come all of our languages are object oriented languages?
  • 39:15 - 39:17
    We've been doing this for 30 years. We gotta know.
  • 39:17 - 39:19
    (Someone says) "Models the real world."
  • 39:19 - 39:20
    Models the real world! Thank you!
  • 39:20 - 39:23
    I planted him here so that he would say that.
  • 39:23 - 39:25
    So then I could rip him to shred.
  • 39:25 - 39:27
    No, that's absurd.
  • 39:27 - 39:32
    And that whole idea that OO has better way to model real world is plain non-sense.
  • 39:32 - 39:35
    It's something. Some guy concocted long time ago
  • 39:35 - 39:39
    To convince his manager to spend 12,000$ on C++ compiler.
  • 39:40 - 39:42
    Cause he couldn't figure out any other ways to get his manager to do it.
  • 39:42 - 39:46
    12,000$. Early C++ compiler costed a lot of money.
  • 39:46 - 39:48
    "12,000$. I'm not spending that for a compiler."
  • 39:48 - 39:50
    "Well, it will help me model the real world!"
  • 39:50 - 39:53
    "Oh. Okay then."
  • 39:53 - 39:58
    This whole notion of modeling the real world is downright silly.
  • 39:58 - 40:05
    What in the world is OO other than a bunch of functions using a bunch of data structure? Right?
  • 40:05 - 40:07
    Encapsulated. Okay fine. Encapsulated.
  • 40:07 - 40:09
    But bunch of functions using bunch of data structures.
  • 40:09 - 40:12
    How is that different from Non OO?
  • 40:13 - 40:15
    The answer to that is not easy to describe.
  • 40:16 - 40:18
    How that's different?
  • 40:18 - 40:22
    Well, okay. We kind of put the data structures in the functions together.
  • 40:22 - 40:24
    But we always used to do that.
  • 40:24 - 40:27
    Old C programmers used to that all the time.
  • 40:27 - 40:30
    Data structures in programs always went together.
  • 40:30 - 40:34
    There is a famous book called 'Algorithms plus data structures equals programs.'
  • 40:34 - 40:37
    Data structures now (??) working together.
  • 40:37 - 40:39
    So nothing really fancy about OO there.
  • 40:39 - 40:44
    There is one thing that OO gave us that we did not have before.
  • 40:44 - 40:46
    Because it wasn't safe.
  • 40:46 - 40:47
    And that's polymorphism.
  • 40:47 - 40:51
    Very very convenient polymorphism.
  • 40:51 - 40:53
    We used to have it in C.
  • 40:54 - 40:58
    Device independence in any operating system is an example of polymorphism.
  • 40:58 - 41:03
    If you can write a program, you don't need to know what device that program is gonna use.
  • 41:03 - 41:05
    It's a clearly polymorphic interface.
  • 41:06 - 41:09
    But that's dangerous in most languages.
  • 41:09 - 41:10
    Or it was back in those days.
  • 41:10 - 41:12
    Because you have to fiddle with pointers to functions.
  • 41:12 - 41:14
    And that was always dangerous.
  • 41:15 - 41:19
    What OO gave us was very very convenient polymorphism.
  • 41:19 - 41:21
    Polymorphism without thinking about it.
  • 41:21 - 41:24
    Java in particular all the methods are polymorphic.
  • 41:24 - 41:25
    There's no choice.
  • 41:25 - 41:29
    C#, you have choice. You can use that funny virtual keyword.
  • 41:29 - 41:35
    C++ programmers you've got a choice. You better use that virtual keyword. Especially on your destructors.
  • 41:36 - 41:39
    But most of us we don't even pay attention anymore.
  • 41:39 - 41:42
    All our functions are polymorphic. We don't even think about it.
  • 41:42 - 41:43
    Why?
  • 41:44 - 41:49
    Because when a function is polymorphic something amazing happens.
  • 41:50 - 41:55
    The flow of control goes down towards to derivatives.
  • 41:55 - 41:59
    But the source code dependency goes back towards base.
  • 42:00 - 42:08
    We can take a source code dependency and turn it around without changing the run time dependency.
  • 42:08 - 42:10
    How do you get DLLs?
  • 42:10 - 42:12
    How do you get components?
  • 42:12 - 42:14
    You isolate them.
  • 42:14 - 42:17
    But you have to maintain the run time dependency.
  • 42:21 - 42:23
    Visual Studio people
  • 42:25 - 42:28
    Are you using ReSharper?
  • 42:29 - 42:30
    Who is? ReSharper?
  • 42:30 - 42:32
    Look at that. Everybody.
  • 42:33 - 42:36
    Does Visual Studio know about ReSharper?
  • 42:38 - 42:39
    No.
  • 42:39 - 42:42
    Does Visual Studio call ReSharper?
  • 42:43 - 42:45
    Yes.
  • 42:45 - 42:49
    The flow of control goes from Visual Studio into ReSharper.
  • 42:49 - 42:54
    There are function calls in Visual Studio that their way into ReSharper.
  • 42:54 - 43:01
    But there is no source code dependency that moves from Visual Studio into ReSharper.
  • 43:01 - 43:03
    Because they've been inverted the dependencies.
  • 43:04 - 43:10
    You can create DLLs that your application will call.
  • 43:10 - 43:12
    But your application doesn't know it exist.
  • 43:13 - 43:16
    And you do that by inverting dependencies.
  • 43:16 - 43:17
    Turn them around.
  • 43:17 - 43:19
    So this is one nice way to do that.
  • 43:20 - 43:29
    Now the control panel is a plug-in to the alarm system.
  • 43:29 - 43:32
    The alarm system doesn't know the control panel exists.
  • 43:32 - 43:36
    The control panel is a plug-in.
  • 43:36 - 43:39
    And the alarm system would accept any kind of plug-in.
  • 43:39 - 43:43
    A lot of different things to do implement this display function here.
  • 43:43 - 43:45
    So we could have a lot of different things that we alarmed.
  • 43:53 - 43:55
    What would rather depend upon?
  • 44:02 - 44:05
    A component that was stable?
  • 44:07 - 44:09
    Or a component that was unstable?
  • 44:11 - 44:12
    Trick questions.
  • 44:12 - 44:18
    Right? Everybody knows that you want to depend on something that's stable.
  • 44:23 - 44:25
    But now let me define stability.
  • 44:29 - 44:31
    Is my laser stable?
  • 44:34 - 44:36
    It's not changing.
  • 44:39 - 44:41
    But is it stable?
  • 44:41 - 44:44
    Stability is not a Boolean.
  • 44:44 - 44:46
    Stability is a continuous variable.
  • 44:46 - 44:51
    And it is defined as the amount of work required to make a change.
  • 44:51 - 44:53
    If it takes a lot of work to make a change, it's stable.
  • 44:53 - 44:56
    If it takes very little work to make a change, it's unstable.
  • 44:56 - 45:00
    That is unstable.
  • 45:00 - 45:04
    Cause it doesn't take much work to make a change to its state.
  • 45:04 - 45:09
    This. Well I won't say that it's stable.
  • 45:09 - 45:12
    Cause it would take much work to upset it.
  • 45:12 - 45:15
    And this whole stage doesn't feel very stable to me.
  • 45:16 - 45:19
    So I may have to be careful about the way I move.
  • 45:19 - 45:22
    So let me ask the question again.
  • 45:22 - 45:25
    What would you rather depend upon?
  • 45:25 - 45:27
    Something easy to change?
  • 45:27 - 45:29
    Or something hard to change?
  • 45:29 - 45:33
    Modify the source code.
  • 45:34 - 45:35
    What would you rather depend upon?
  • 45:35 - 45:40
    A module whose source code was hard to change or a module whose source code was easy to change?
  • 45:42 - 45:43
    Same answer.
  • 45:43 - 45:46
    You wanna depend upon thing that hard to change.
  • 45:46 - 45:49
    And the reason behind that is very simple.
  • 45:49 - 45:52
    Do you mind depending on string?
  • 45:52 - 45:53
    No.
  • 45:53 - 45:55
    Why don't you mind depending on string?
  • 45:55 - 45:58
    Cause if those guys ever change string, they'll be held to pay.
  • 46:00 - 46:01
    They would suffer more than you.
  • 46:02 - 46:04
    That's the equation that we're talking about here.
  • 46:04 - 46:06
    You are happy to do depend upon something,
  • 46:07 - 46:12
    if it will hurt the authors of that thing more to change it than hurts you.
  • 46:12 - 46:14
    That's the equation.
  • 46:14 - 46:16
    You are happy to depend upon things
  • 46:16 - 46:19
    as long as you're not likely to change or at least the bastards gonna pay if they change.
  • 46:19 - 46:25
    So alright, we don't want to depend on things that are easy to change.
  • 46:27 - 46:29
    Think about that very carefully.
  • 46:29 - 46:33
    I don't want to depend on something easy to change.
  • 46:35 - 46:40
    Do we design parts of our system so that they will be easy to change?
  • 46:43 - 46:44
    Yes.
  • 46:44 - 46:50
    What parts of our system do we most want to be easy to change?
  • 46:53 - 46:54
    The GUI.
  • 46:57 - 46:58
    GUIs are volatile.
  • 46:58 - 47:02
    They change for no good reason at all.
  • 47:02 - 47:05
    People will change GUI just because they feel like it.
  • 47:05 - 47:08
    There will be some committee (??) to say,
  • 47:08 - 47:10
    "You know, our system look so old."
  • 47:10 - 47:12
    What the hell does that mean?
  • 47:12 - 47:15
    "Our system look so old, we need to give it a face lift."
  • 47:15 - 47:19
    "Marketing people have decided, we’ve gotta have whole new look and feel."
  • 47:19 - 47:21
    "Not gonna change any behavior."
  • 47:21 - 47:23
    "Just look and feel of the GUI."
  • 47:23 - 47:25
    GUI has to be easy to change.
  • 47:25 - 47:29
    The modules that hold that source code have to be easy to change
  • 47:29 - 47:33
    And that means that none of you modules should depend on the GUI.
  • 47:34 - 47:37
    No source code dependency should land on the GUI.
  • 47:38 - 47:42
    Your components should not have dependencies that land on the GUI.
  • 47:42 - 47:45
    GUI components have to depend upon the application.
  • 47:45 - 47:49
    Application component cannot depend upon the GUI.
  • 47:49 - 47:53
    Otherwise you wind up with the systems that wear the GUIs hard to change.
  • 47:53 - 47:57
    How many of you test your systems?
  • 47:57 - 47:58
    Automated test.
  • 47:59 - 48:00
    Through the GUI.
  • 48:03 - 48:04
    Got a couple of people.
  • 48:05 - 48:07
    You write test code through the GUI.
  • 48:07 - 48:11
    You are depending upon something that supposed to be easy to change.
  • 48:11 - 48:13
    You'll make it hard to change.
  • 48:13 - 48:16
    If you test your system through the GUI.
  • 48:16 - 48:19
    I have a client who have 15,000 tests all go through the GUI.
  • 48:19 - 48:21
    Same client, by the way.
  • 48:21 - 48:22
    Same one went out of the business.
  • 48:22 - 48:26
    15,000 tests through the GUI.
  • 48:26 - 48:29
    He had so many tests he didn't know what they were anymore.
  • 48:29 - 48:31
    He just knew they all had to pass.
  • 48:31 - 48:33
    If somebody touch the GUI
  • 48:33 - 48:35
    a thousand of those test would break.
  • 48:35 - 48:37
    He couldn't find the time to fix them.
  • 48:37 - 48:39
    So he came up with real simple rule.
  • 48:39 - 48:41
    What do you think that rule was?
  • 48:41 - 48:44
    No body touches the GUI.
  • 48:44 - 48:47
    They made the GUI hard to change.
  • 48:48 - 48:52
    The other thing of course could happened there is that you could lose your tests.
  • 48:53 - 48:58
    You spend a man year putting together nice automated test suite that go through the GUI.
  • 48:58 - 49:02
    And then somebody decides, "We need a face lift on our site."
  • 49:02 - 49:04
    "Pull all out the old GUI and put a brand new GUI"
  • 49:04 - 49:06
    And all those tests are gone.
  • 49:06 - 49:10
    And you get to rewrite them again, but you've got other systems you gotta test.
  • 49:10 - 49:12
    Don't test through the GUI.
  • 49:12 - 49:14
    Don't do anything through the GUI.
  • 49:14 - 49:19
    All dependencies point away from the GUI.
  • 49:19 - 49:23
    What other things do we want to be easy to change?
  • 49:23 - 49:26
    The database.
  • 49:26 - 49:28
    We want the database to be easy to change.
  • 49:28 - 49:32
    We want to be able to make changes to database without rippling through the whole applications.
  • 49:34 - 49:38
    All dependencies you should point away from the database.
  • 49:38 - 49:41
    Put the database in a component with all the dependencies pointing outwards.
  • 49:41 - 49:44
    Put the GUI into a component with all the dependencies pointing outwards.
  • 49:46 - 49:54
    I don't want to depend on anything that is unstable.
  • 49:55 - 49:58
    How can we measure instability?
  • 50:02 - 50:04
    See this guy up here?
  • 50:05 - 50:06
    That component up there?
  • 50:07 - 50:09
    Is he Stable? Or Unstable?
  • 50:09 - 50:13
    He's got lots of incoming dependencies.
  • 50:13 - 50:16
    No outgoing dependencies.
  • 50:16 - 50:17
    He's hard to change.
  • 50:17 - 50:20
    If I make a change to him, it impacts all those guys.
  • 50:20 - 50:25
    So this component here is responsible to those guys.
  • 50:25 - 50:29
    Oh and this component here is independent.
  • 50:29 - 50:31
    It doesn't depend on anybody.
  • 50:31 - 50:33
    It's responsible and independent.
  • 50:33 - 50:34
    I's an adult.
  • 50:37 - 50:39
    Stable. Adult.
  • 50:40 - 50:41
    This guy.
  • 50:42 - 50:44
    He depends on lots of other components.
  • 50:45 - 50:47
    No body depends upon him.
  • 50:47 - 50:48
    Is he responsible?
  • 50:48 - 50:50
    And dependent?
  • 50:50 - 50:51
    He's teenager.
  • 50:52 - 50:54
    And he's unstable.
  • 50:54 - 50:58
    These are the two extreme kinds of components.
  • 50:58 - 51:00
    At the two sides of component spectrum.
  • 51:00 - 51:03
    You've got the adults that highly stable.
  • 51:03 - 51:07
    And you’ve got the teenager who are highly unstable.
  • 51:07 - 51:09
    The unstable ones are easy to change.
  • 51:09 - 51:12
    That's what we wanna put all of our little code.
  • 51:12 - 51:15
    The stable ones are hard to change.
  • 51:16 - 51:23
    We can measure that stability by creating a metric.
  • 51:23 - 51:26
    That metric is a metric that I call "I"
  • 51:27 - 51:34
    'I' is equal to the fan-out. Number of outgoing dependencies.
  • 51:34 - 51:37
    Divided by the fan-in plus the fan-out.
  • 51:37 - 51:40
    And if you think about that for very long.
  • 51:40 - 51:44
    You'll realize that 'I' is a metric that goes from 0 to 1.
  • 51:44 - 51:47
    0 being stable. 1 being unstable.
  • 51:47 - 51:50
    0 being an adult. 1 being a teenager.
  • 51:50 - 51:53
    It’s all about the dependencies.
  • 51:53 - 51:59
    And now we can re-phrase the principle to say this.
  • 52:06 - 52:14
    Every dependency in a component graph should point at something more stable than it is.
  • 52:14 - 52:22
    Or, if use the metric, these arrow should point in the direction of decreasing 'I'.
  • 52:22 - 52:25
    Decreasing instability. Increasing stability.
  • 52:25 - 52:31
    And you can do the math on just the fan-ins and fan-outs and verify that's correct.
  • 52:32 - 52:37
    Why was the cycle bad idea?
  • 52:38 - 52:41
    Something stable depended on something unstable.
  • 52:45 - 52:48
    But that leaves us with a problem.
  • 52:48 - 52:50
    And the problem is this.
  • 52:55 - 52:56
    What's that guy?
  • 52:58 - 52:59
    Stable? Or unstable?
  • 53:01 - 53:03
    He's really stable.
  • 53:03 - 53:05
    He's sitting down here the bottom of the graph.
  • 53:05 - 53:08
    Everything over here depends on him.
  • 53:09 - 53:10
    He's very hard to change.
  • 53:11 - 53:15
    If I touch that component, all these other components will be affected by.
  • 53:15 - 53:18
    For no other reason than the release number changes.
  • 53:24 - 53:29
    That means the stuffs down here at the bottom of graph is very difficult to work with.
  • 53:31 - 53:33
    But there's an escape to that.
  • 53:34 - 53:35
    Back to polymorphism.
  • 53:37 - 53:43
    How can you make something easy to extend?
  • 53:44 - 53:46
    Even though it's hard to modify.
  • 53:49 - 53:50
    You make it abstract.
  • 53:51 - 53:54
    Abstract classes can be extended trivially.
  • 53:55 - 53:57
    Without having it modified.
  • 53:57 - 54:00
    You can add new features to a system
  • 54:00 - 54:06
    if those new features live inside the derivatives of base classes.
  • 54:06 - 54:09
    So the final principle is this.
  • 54:10 - 54:16
    We would like abstractness to increase as we go down the zeros. (??)
  • 54:16 - 54:18
    Stuff up here concrete.
  • 54:19 - 54:20
    Unstable and concrete.
  • 54:20 - 54:23
    Stuff down here abstract and stable.
  • 54:23 - 54:30
    Abstractness becomes more and more prevalent as we go down this tree.
  • 54:32 - 54:39
    In fact, we could say that abstractness is a number
  • 54:39 - 54:46
    which is the number of abstract classes divided by the total number of classes in a component.
  • 54:47 - 54:50
    And if you did that, you get this number 'A' which goes 0 to 1.
  • 54:50 - 54:52
    0 being concrete.
  • 54:52 - 54:55
    1 being entirely abstract. Composed of nothing but interfaces.
  • 54:55 - 54:59
    And then we can do this very interesting thing.
  • 54:59 - 55:09
    We can say that for any particular component 'A' plus 'I' should equal 1.
  • 55:09 - 55:13
    Either it's abstract where 'A' is 1.
  • 55:13 - 55:16
    And stable where 'I' is 0.
  • 55:16 - 55:20
    Or it is instable where 'I' is 1.
  • 55:20 - 55:21
    And concrete where 'A' is 0.
  • 55:21 - 55:23
    'A' plus 'I' equals 1.
  • 55:23 - 55:27
    The magic formula of components. 'A' plus 'I' equals 1.
  • 55:27 - 55:31
    Now you've got the adults up here which are abstract
  • 55:31 - 55:33
    And everybody depends upon them. So they are stable.
  • 55:33 - 55:36
    You've got the teenagers down here.
  • 55:37 - 55:42
    Everybody that's got no incoming dependencies but they're very concrete.
  • 55:42 - 55:44
    What do you got here?
  • 55:44 - 55:47
    This is the line 'A' plus 'I' equals 1.
  • 55:48 - 55:53
    This is where we would like our components to sit if they can sit. One of those two end points.
  • 55:53 - 55:54
    Why?
  • 55:56 - 55:59
    Well, what's up here?
  • 56:01 - 56:04
    Highly abstract. Nobody depends upon on it.
  • 56:05 - 56:08
    An interface that nobody implements. Useless.
  • 56:09 - 56:11
    This is the zone of uselessness.
  • 56:11 - 56:14
    We don't want our components going that way.
  • 56:14 - 56:16
    What's down here?
  • 56:17 - 56:20
    Very concrete. Everybody depends upon on it.
  • 56:20 - 56:22
    Database schemas.
  • 56:23 - 56:25
    Concrete. Everybody depends upon them.
  • 56:25 - 56:27
    Fun to change.
  • 56:27 - 56:30
    Right? We don't want things down here.
  • 56:30 - 56:31
    This is the zone of pain.
  • 56:32 - 56:36
    We would like our components to be as far from those 2 points as possible.
  • 56:36 - 56:39
    Ideally if we could get them here and here that would be best.
  • 56:39 - 56:42
    But it turns out the components are persnickety that way.
  • 56:42 - 56:45
    So at least we'd like them to be sitting along this line.
  • 56:46 - 56:48
    Or close to the line.
  • 56:49 - 56:51
    Which leaves us the one last metric.
  • 56:53 - 56:54
    'D'
  • 56:54 - 56:57
    How far away is a component from the line?
  • 56:58 - 57:02
    Well, 'D' could be the absolute value of 'A' plus 'I' minus 1.
  • 57:02 - 57:05
    You can do the math on this. It's not very difficult.
  • 57:05 - 57:07
    'D' is the metric that goes from 0 to 1.
  • 57:07 - 57:10
    0 means that right on the line.
  • 57:10 - 57:12
    1 means that one of the two bad end points.
  • 57:12 - 57:15
    If you wanna know which end point you can take the absolute value signs off.
  • 57:15 - 57:16
    But I don't care.
  • 57:16 - 57:22
    You can measure 'D' by looking at the fan-in and the fan-out of the component.
  • 57:22 - 57:24
    Measuring its abstractness.
  • 57:24 - 57:28
    Doing the math is not very difficult math to do.
  • 57:28 - 57:34
    And find out whether your component sits nicely on that line.
  • 57:34 - 57:40
    If it does then it is abstract as it is depended upon.
  • 57:41 - 57:47
    If it doesn't that means either it's very abstract and not depended on
  • 57:47 - 57:49
    or very concrete and heavily depended upon.
  • 57:49 - 57:50
    Both of which are very bad ideas.
  • 57:51 - 57:55
    There are lots of tools that automatically calculate these metrics for you.
  • 57:55 - 57:59
    If you ever used NDepend. That will calculate the metrics for you.
  • 57:59 - 58:03
    If you ever used any of the other static analysis tools,
  • 58:03 - 58:06
    they can generate all these metrics. I. D. All of them for you.
  • 58:06 - 58:10
    So that you can look at your components and see if they fit.
  • 58:10 - 58:14
    And then think about what should be easy to change.
  • 58:15 - 58:18
    Things that are easy to change should be in teenagers.
  • 58:19 - 58:24
    Things that are hard to change should be in adults that are abstract.
  • 58:24 - 58:28
    Concrete teenagers hold the stuff that's easy to change.
  • 58:28 - 58:32
    Abstract adults hold the stuff that's hard to change.
  • 58:33 - 58:34
    Any questions?
  • 58:36 - 58:40
    We started with PDP-8 assembly code and got here.
  • 58:43 - 58:45
    Any questions? No?
  • 58:45 - 58:46
    Good.
  • 58:47 - 58:48
    Oh, damn.
  • 58:50 - 58:51
    It's alright. It's okay.
  • 58:51 - 58:57
    (Someone is questioning.)
  • 58:57 - 58:57
    Yep.
  • 58:57 - 59:03
    (Someone is questioning)
  • 59:03 - 59:05
    2 versions.
  • 59:05 - 59:07
    (Someone is questioning)
  • 59:07 - 59:10
    Yeah, 2 versions. Same component. You kill the programmers.
  • 59:11 - 59:11
    Yeah.
  • 59:12 - 59:15
    Don't have multiple versions of same component in you system please.
  • 59:15 - 59:17
    That's DLL Hell.
  • 59:18 - 59:20
    Anybody else? Yeah.
  • 59:21 - 59:24
    Where do you place...? Very very good question.
  • 59:24 - 59:26
    String class sits right there.
  • 59:27 - 59:32
    It's in the worst possible place but nobody ever changes it. So we don't care.
  • 59:32 - 59:36
    This is all about the stuff we are actively developing.
  • 59:36 - 59:38
    The stuff that is being changed.
  • 59:38 - 59:43
    So we pay very close attention to the stuff that's in our libraries that are changing.
  • 59:43 - 59:46
    The stuff that we get from other libraries that's not changing.
  • 59:46 - 59:49
    Or the stuff that's in our old libraries that's not changing.
  • 59:49 - 59:50
    We not gonna pay attention to that here.
  • 59:51 - 59:52
    A lot of that stuff may live here.
  • 59:52 - 59:53
    And that's okay.
  • 59:53 - 59:55
    None of it's gonna live here.
  • 59:56 - 59:58
    Well, some of it might.
  • 59:58 - 60:00
    Anybody pull out any dead code?
  • 60:00 - 60:02
    Abstract classes that nobody implements?
  • 60:02 - 60:05
    Yeah, okay. So maybe you see some stuffs there.
  • 60:05 - 60:12
    But String, Vector, a lot of libraries live down here we don't care because they're not volatile.
  • 60:12 - 60:16
    Think of another axis is coming out from this graph, right, towards you.
  • 60:16 - 60:18
    That's the axis of volatility.
  • 60:18 - 60:22
    This is a slice where volatility is close to 1.
  • 60:22 - 60:25
    The stuff where volatility is 0, we don't care about it.
  • 60:26 - 60:27
    Anybody else?
  • 60:28 - 60:29
    Yo! In the back.
Title:
Robert C. Martin: Principles of Component Design.
Description:

How do you manage the structure of large agile and object oriented systems? What is the best way to break that structure into components? What should the components contain, and how should they be interrelated? How can we effectively partition a system into independently deployable components? This course teaches the 6 principles of object oriented component design. The first three are principles of component cohesion, which discuss the rules for how classes should be allocated to components. The second three are the principle of component coupling, which discuss how components should be related to one another. Principles include “The Common Closure Principle, the Stable Dependencies Principle, and the Acyclic Dependencies Principle, among others.

more » « less
Video Language:
English

English subtitles

Revisions