Rock Solid Python with Python Typing Transcripts
Chapter: Static vs. Dynamic Languages
Lecture: C# Motorcycle and Why Types Can Detract from Readability

Login or purchase this course to watch this video and the rest of the course contents.
0:00 Next up in the list here is C# and dotnet used to be just a Windows thing, but now it's a cross platform thing with dotnet cores. That's what we're using. So we can install it here on the Mac or whatever it is you're running. There's a preview for eight, but we don't want that we want to run the stable one. So kind of make sure you go over here and get the right version. And then we can install that real quick. All right, that's good. Let's open up a new shell and type dotnet. See what happens. Awesome.
0:30 So it says what we got to do is run dotnet new console app called MC on this version. There's a whole bunch of stuff that happens.
0:42 I don't want to mix it into our source code here. So let's just go to the desktop. Run that doing all the things.
0:55 You can see it's got the various CS prods and the program. We're going to copy this over here. Let's just give it the name program like that.
1:10 It says, ""Okay. Then we're going to go into the directory and call this right here, which is this one. We'll just say .NET run. Hey, look at that.
1:24 First time it's kind of slow because that's a compile. Then it should be a tiny bit faster.
1:29 That's how you run it just so you can play with it yourself. Let's look at the code again in enumeration, sport, naked, terrain, adventure, and so on.
1:37 We've got our public class. So in C#, you have not just the type information, but you have visibility.
1:44 This one, motorcycle, is available to be exported out of this application. Whoever wants to use this library, they can use motorcycle.
1:51 It could be internal. It could be private. But it's public. Doesn't really matter. No one's using it, but that's how it works. So you've got the types.
2:03 This time the types appear in front, not behind. That's less like Python, more like C, as well as the type modifiers.
2:11 But here we've got the constructor. Same thing comes in. These are all getters and setters like our canJump.
2:17 These are properties, not actual variables. So capital one there, a little bit funky instead of just being the type, which otherwise you
2:24 would put like that. Can jump is a little lambda expression over to just whether or not it's off road.
2:34 Creative Venture, right, you can see they're pretty, pretty similar again.
2:38 Over here we also have everything lives in a namespace explicitly not just the module
2:43 file name and within a class right even down here the public static void is part of a class. So that's kind of interesting, right.
2:52 Right, so this is the C# version. Decent, but look at how many more words appear on the screen. Let's put it side by side again real quick
3:01 with our Python one here. If I can get it to go to the side, there we go. So let's just look at some of these sections.
3:08 Here's some of the drawbacks of type information. Right, I've been talking about how it's good. If it's good, why don't we just want it everywhere?
3:18 Right, what was wrong with Python? Why didn't they make the right choice? Well, we'll see. So for example, look at all of that stuff there.
3:26 Public string model curly get semicolon private set semicolon curly. Ooh, and then down here, you've got like your model
3:35 and then your model is equal to your model. And this, this is the same thing, right? There's the model, there's that.
3:43 I know it's not a property, but I put that aside, right? 'Cause we're not using anything interesting about the property.
3:50 Look at how much cleaner and simpler this is. Then all of that information is all captured right there. So that's pretty cool. This property can jump.
4:00 You know, it's kind of on par with our property, maybe even a little cleaner, but I don't know, it's also a little funky. Create Adventure.
4:07 Let's go down there and look at that. Class method, Create Adventure. I'm gonna have to scroll for this one. Public, Static, Motorcycle.
4:16 Whew, Create Adventure. String model, string engine size. And then new motorcycle. Okay, so the internals are pretty similar.
4:24 All right, once we get down to actually using it though, like our create bikes, pretty straightforward, other than you've got a list of motorcycle
4:34 is what this method returns. Interesting, you can see that the type information, while helpful, is definitely cluttering up the readability
4:43 and the understand, the ability to have just concise readable code. And we saw right in the open in the Wikipedia,
4:50 Python is all about trying to make things as readable, easy and straightforward as possible to take the code in as a developer.
4:58 I think here's an interesting contrast that we can see here.


Talk Python's Mastodon Michael Kennedy's Mastodon