Placeholder Image

字幕列表 影片播放

  • All right.

  • Hello, world.

  • This is CS 50 on Twitch.

  • My name is Colton Ogden, and I'm joined today by Bryan and David for a part to code review Stream.

  • Yeah, I did a lot of fun the first time, and now we brought Brian one of our best teaching fellows here on campus.

  • Good to be here.

  • Yeah, I wasn't on sometime last time.

  • We had a ton of viewership, and it's super interesting getting the test.

  • See everybody's projects, a wide range of programming, languages.

  • Last time I looked at some python programming languages are projects programming projects on.

  • And I think we looked at, um, html page.

  • Actually, night.

  • Nate was 10 years old indeed.

  • Broad audience out there.

  • So it's good to see some familiar names and faces and emojis here.

  • And welcome to reveal who just followed as well.

  • Yeah, and we had another follower.

  • Actually, we're adjusting our view here a little bit in real time so that we can actually see not only the comments nice and wide, but also our followers here.

  • So sayyed 055 as well.

  • Thank you very much for the follow Lita to G and D Manda.

  • Oh, and we have a few, actually.

  • And see Miss Sweden.

  • Hugh, thank you very much for the follows.

  • We really should just say hello to people's numbers.

  • Yeah, maybe it might be a little easier to pronounce you, but Yeah.

  • So we have a bunch of awesome project today.

  • And also thanks to Brian for joining us for the first time.

  • Absolutely.

  • And many more going forward.

  • Why don't we?

  • Why don't we dive right into our first product roll?

  • Sure.

  • So if you're new to the stream, we via Colton solicited some code that folks had worked on other for personal projects or otherwise by a Google form a few weeks ago.

  • And we got a number of submissions python, html, c and other languages.

  • And so what Brian and I thought we would do today with Colton here is just provide some feedback, much like our teaching fellows here on campus Would, based on the design thing, efficiency, the quality of the code that we're seeing and certainly take all this with a grain of salt.

  • Reasonable people will disagree, so you don't need to take our thoughts as cannon.

  • But hopefully it's helpful food for thought whether it's your coat we're looking at or code, that's of interest.

  • Some of some of us might even disagree, We shall say Probably not.

  • Probably not with a bunch of regulars in the chat with ST Nate.

  • Uh, we just actually mentioned has submitted a thing last week.

  • And actually, is he saying that they submitted more code and they hope that they contribute again?

  • Well, thank you.

  • And, uh, we should probably preface this by plugging the actual you, Earl for the form for people that want to submit us projects in the future for more episodes after this one.

  • This is part two.

  • We didn't another one.

  • You can watch the video on YouTube.

  • If you goto billy slash CS 50 twitch code review, you can submit a get up or a Google form with your gift every but we're just and then you could be in any language.

  • Can be a small project, be an advanced project.

  • But we'd be happy to take a look at it.

  • Graded on a correctness, not practice on design and style.

  • Not correctness will not be bug testing.

  • You know, I think we need your else order for our u R l sort.

  • Yeah.

  • We probably should go to a C study that.

  • But what we have here a CZ the first project, and we sort of looked these a little bit in advance and cure.

  • At least, um, sort curated.

  • Want them in kind of an order.

  • This is a C library, which is kind of cool because we didn't actually look at any C code in the last video, but sees 50 teachers see a sort of the first language on ramps.

  • Everybody to the more dynamic languages.

  • It's kind of cool that maybe look at a real world project in C.

  • Yeah, It looks like we have a project here from Noah.

  • 1101 to live picks map on for the top of this.

  • It get hub.

  • You're able to put a description for every project he put on.

  • Get home.

  • This is a simple to use library to read and write P p.

  • M.

  • Or portable picks, map images.

  • Frankly, I was not familiar too much with p p.

  • M.

  • Just a few minutes ago.

  • Yeah, I think so.

  • But I think that's okay.

  • So let's actually go ahead.

  • And Google, for instance, on Wikipedia is probably a great source of information here, and indeed, there's our file format.

  • But in a different number of contexts here, uh, let's see pages per minutes.

  • It's probably this portable picks map format.

  • So Annette's PBM format is any graphics format used and defined by the Nets PBM Project.

  • Uh, this is the, uh, these air image file formats designed to be easily exchanged between platforms.

  • And they are with us since, like the 19 eighties, it seems.

  • Yeah, it looks like in the 19 eighties, there were first developed in order to make it easier to transmit images via e mails where you wanted to be able to send an email, that an image in it.

  • And it looks like this format.

  • The Net PBM format would allow you to take an image, turn it into a plane, ask you text, according to this article, in order to just send it more easily from one person to another over the Internet.

  • So no, if you're tuning in or if you tune and after certainly let us know why or what motivated this project because it's quite interesting, it seems, but as we did last time, shall we just dive right into the gate, huh?

  • Yeah.

  • And I should also mention that there get abusing Was actually from there.

  • Twitch.

  • User name, which we did solicit.

  • Twitch.

  • Every release is the talk of an eye out for every release other today or in some future stream.

  • So the way I only start by diving into the students code or colleagues code is just to look at, like, read me.

  • And I think if we scroll down here on, get Hub will indeed see, read me from Noah.

  • So here we have a nice description.

  • Frankly, we should just roll before I guess, what is live picks?

  • Map.

  • It's a library and impure.

  • See the names to provide an easy and convenient A P I for reading and writing.

  • PPM files four features opening ppm images creating blank ppm images, setting and retrieving the color values for specific pixels.

  • Not unlike what we do in CS 50 on dhe set and get all pixels.

  • This extra sounds reminiscent of what we do with bit map files, which is another relatively simple image format.

  • What I like is that this library is very simple, but very flexible, like you could use this to dynamically generate any image data based at least when I'm reading here.

  • But the fact that you create a blink image and then senator chief color values yeah, that's great.

  • That's good way to start because other formats that are more recent like J.

  • Peg are actually very highly optimized for compression and use much fancier algorithms to store the information.

  • It's not quite as easy, therefore, to get at individual pixel values.

  • All right, So should we take a look at an example, for instance, and get a sense of how this all works?

  • You wanna walk us through what you're seeing here and how you would go about reading someone's code for the first time?

  • Yeah, sure.

  • So we have the main function there, So it seems like well up at the top before the main function there, including Standard Out H, which you might be familiar with a few chickens.

  • 50.

  • We use that in a lot of see programs and then there, including picks map dot each, which looks like it's probably the header file for the library that Noah has created here, and that's gonna give him access to some of the types that we're going to see in just a moment, probably in some of the functions.

  • Meanwhile, so inside the main function we're defining a something of type picks WMAP image.

  • So not an image that's built in deceased's probably have some sort of definition that will take a look at just the moment on.

  • We're fighting a pointer to it called Image, and then we're running a function that looks like he's defined called picks WMAP Image open.

  • I presume this is gonna be some file path to the image that we're going to try and open, and we're gonna store some sort of data about it inside of what's pointed to by the Pointer image.

  • And we looks like there's an air checking here, checking to make sure that we're able to open the image successfully.

  • If we weren't able to open the image successfully, we print in error, return a non zero error code, And what would it mean for image to be not image like?

  • What is this?

  • Checking for C.

  • U S o N C.

  • When you have ah pointer value like image and you have exclamation point image or bang image that's checking to see whether or not image is equal to know.

  • And so no stands for the no pointer, meaning image is not pointing to anything.

  • And so it's a convention and a lot of functions.

  • If we try and run, pick snap image open, for instance, If we couldn't open the image, for instance, if the file didn't exist at the particular file path, then what we meant to get back is rather than a pointer to an actual picks me up image.

  • You might get a pointer.

  • That's just know.

  • And so if bang images checking to see if images know in which case something went wrong and we print that air.

  • So it's really like checking for zero really or fall.

  • So it's not false.

  • Then go ahead, right?

  • Do this.

  • So I'm a little nervous and we wouldn't know really until we dive into the code.

  • But because images declared is a pointer.

  • As per this asterisk right here, a pointer to a picks mapped image.

  • I'm a little nervous that were then in the sample code, getting the address of the pointer unless this image is actually gonna do a lot of our memory allocation for us and then change what is at what point changed the contents of that pointer itself as opposed to what it's pointing it.

  • But we'll see.

  • Yeah, that's a good place.

  • Is appointed to a pointer, which feels a little a little worrisome.

  • But maybe it's correct.

  • Yeah, all right.

  • And then down here, Do you want to fight on for what's going on a little further with these prints?

  • Okay, so next it looks like we're trying to print the images within height.

  • And in order to do that, looks like Noah's to find some functions, picks my image, picks WMAP image, get with and picks me up.

  • Image get height, each of which is taking that pointer a pointer to a pixel image.

  • And presumably those functions are able to take that image and somehow where inside the data stored within the image get access to the width and the height of that image and maybe a couple points year.

  • If you've been taking CS 50 you might know that in recent years we actually introduce students 2% I for integer well percent D for decimal into Juries almost identical, and it is in fact, with the context of print depth.

  • It's a little different if you're using scanning for actually getting user input.

  • But that's otherwise.

  • The same thing is percent I and I think it's worth noting.

  • For those of you who have heard about or familiar with object oriented programming, this is a convention.

  • And see that, frankly starts to get messy pretty quickly and see there is no notion of object orientation.

  • And so even though image kind of sounds like an object that represents an actual image, and you'd like to just, like, look inside of it for its with or its height, you could do that if it's a struck.

  • But if there's more to it, you sometimes need to pass these structures into other functions and let them figure out what the height the width is.

  • So this is a common procedural paradigm.

  • I've seen this in certain, like game code bases to and see they have to exact gonna pass everything around his inputs.

  • All right, so just a few more lines here, let's see if we can glean what's going on here.

  • Maybe, All right, so it looks like we're defining a variable of a new type.

  • We saw the type picks that image before.

  • And now we have picks matte r B R g b, which was probably red, green blue, standing in place for a single pixel that has a bunch of color values.

  • And now we've got this function picks WMAP image Get pixel.

  • Looks like it takes four arguments.

  • One is the image that we're trying to get the pixel from, and then we have two numbers and I'm assuming those air like an X and Y coordinate of where in the image we want to try and extract a pixel from.

  • And then the remaining is okay.

  • We've gotten the pixel from the image and that we probably need to tell the function.

  • Where do you want to store the resulting information about the pixel?

  • And so we're passing in a pointer to that picks up RGB variable that we just to find a couple lines earlier.

  • Mainly, it looks like also, if we do happen to accidentally pass in 1000 images say, 500 pixels wide, it does return negative one.

  • So there is some error checking that we can cleanly do.

  • That's true.

  • We're not actually using the return value in this example of you could imagine you might want to add some air checking here.

  • We said it equal to an end to then check to see if that result is negative one, because that would mean that the coordinates supplied or not in the bounds of the image on based on the next three lines, it looks like pixel is probably a structure of some kind because we're using this dot notation to say pixel has a whole bunch of properties within it a red, green and blue property.

  • And the dot notation and see is how you access a member of instruct and get access to you.

  • Just the red value of the pixel, the green value of the excellent value that exult looks like those air integers because we're printing them out using percent D.

  • Um, and then we're creating a new pixel again.

  • Creating a new pics map are RGB, and this time, rather than reading it from the image we know it's instruct.

  • We know it has members called red, green and blue, so we could also create a pixel just by defining what we want the red, green and blue values to be for ourselves.

  • We want to decide what we want.

  • The pixels color to be This has a red value of 50 green of 1 50 blue of 200 which is probably gonna be a greenish blue color, I would guess.

  • And then where it looks like we're setting the pixel, we're calling a function called picks WMAP image set pixel, which falls a similar format to get pixel.

  • But it looks like instead of we're going to be providing the image and the coordinates.

  • And rather than reading a pixel from those coordinates were taking a pixel of our own, namely new pixel, and saying we would liketo update the image most likely and set the books of pixel it coordinate 100 comma 100 to be this new pixel that we've just created again.

  • I like the fact that you could just do this dynamically and generate textures this way because this has applications in game development.

  • If you want to randomly generate textures for stuff, that's pretty cool to just change the colors suit of randomly as opposed to heart.

  • Exactly.

  • If you wanted to make Mario, for example, have a red outfit, you could do that by there are wasted and hardware in the olden days.

  • But now, generally you just have ah, to the image is Mario.

  • You can just choose.

  • You can either choose a layer on top of Mario or just re color Mario Sprite.

  • But this way you could actually handset however you want it or just make him a new question from scratch using algorithms.

  • Well, you know, it's worth noting here, and I'm just inferring this from how this functions being used.

  • The fact that new pixels being passed in by pointer or by reference so to speak, is actually an interesting technique.

  • Because, strictly speaking, I'm gonna guess that when you set a pixels color to be whatever this pixel represents at Location 100 come a 100.

  • Strictly speaking, you don't intend to change anything inside this picture.

  • You're just passing it in for It's red, it's green, it's blue value.

  • But the interesting thing is, if each of these values red, green and blue is each using like an integer to store it, that might be 32 bits plus 32 bits plus 32 beds.

  • So three times the number three ends in total.

  • Well, you can actually get away by passing in and therefore copying fewer bids by just passing in that entire new pixel with those three fields and anything else that's tucked inside of the struck by just one end, which is gonna be into dress.

  • So this is a common technique in C for efficiency.

  • You can pass in a very large data structure to another function is input, but you do it by way of a single value.

  • It's a dress, so it's not waste time copying an entire structure you can imagine the same thing applies to, like, pick picks, Matt Image get with two images, pointer.

  • In this case, you will be the same exact thing.

  • Now you wanna be careful.

  • There are techniques that we kind of sort of showing CS 50 where you can declare variables or parameters to functions is being constant constant because the danger in passing and something as a dress like this is that that means the function you're calling theoretically could change the contents.

  • So you try to at least put a barrier of defense between you and some accidental code.

  • It was the constant, a fairly recent scything Has it been with sea since, Like the seven?

  • Pretty sure that's a pretty old yet.

  • Okay, And then I could like this one off.

  • I'm guessing here this is what's freeing the image and bring up any memory that was dynamically allocated by the original function that actually created this image in the first place.

  • So should we dive into the library itself and see if we can't provide some helpful feedback accordingly?

  • A saying.

  • In the olden days, I'm guessing a sort of tongue in cheek yes, when I was a kid.

  • But all right, so let's see where.

  • So when you are looking at a report for the first time, you've read, Ah, high level English description of it.

  • Where do you where your eyes go to figure out what's going on with the code itself?

  • Well, it looks like there's a There's a folder called SRC, which usually just stands for a source, as in the source code of the of this particular project.

  • So it's most likely that the code for the library is inside of that directory and noticed no has been good here eight days ago, did he add an extra comments for clarification.

  • That's his Get hub commits, All right, right around the time we started our soliciting the U.

  • S.

  • So it looks like there's a test image here, so that'll be interesting to passing his input.

  • If you're playing with the code itself, Which of these would you start with picks mapped out, a chair picks mapped out.

  • See?

  • Yes, This is a library that has but the header file picks mapped out each, which is probably going to just declare what the functions may be.

  • The types are for this pick snap library, but not actually going to give us the code yet for how it is that those functions are implemented.

  • Most likely.

  • And so it probably makes sense to start by taking a look at pick snapped out each, I'd say, Just see sort of what's inside of this library, and then we can take a look at the implementation and see how it actually goes about achieving those things.

  • I like that idea.

  • I mean, we're talking to see Swifty about the idea of abstraction, which is kind of what a header file is.

  • It really just simplifies the contents of something, and then you would go into the sea file for the so called implementation details.

  • All right, so here is a whole bunch of code.

  • Let's just kind of like to kind of skim everything to get a sense of how much work I have ahead of myself, but it doesn't look like all that much.

  • It looks like it's mostly type definitions as well as function declarations here, so I think we're okay.

  • It looks like we have some familiar libraries up here now.

  • This is really nitpicky, but I tend to be a little obsessive when it comes to even the simplest of details in code, like I would actually alphabetize this.

  • It's not a big deal when you have just a few lines here.

  • But personally, whenever I see a list of things, whether it's includes or a Norden list of anything, it's just convenient to know that.

  • Well, if c type dot H or something else that starts with A C is in this list, it's toward the top, not toward the bottom.

  • Otherwise, you're using on Big O of End to search your own code or someone else's code.

  • So super minor point.

  • But I do think it's a useful heuristic in the absence of other rules of thumb agree.

  • So, up here it looks like here we're declaring an actual structure called picks mapped image.

  • That's gonna be of type this struck here and now struck pick WMAP image.

  • Where is that probably coming from?

  • Do we think struck picks?

  • Math doesn't it would have to be to find above.

  • I'm not sure where that's coming from.

  • Coming from one of the header files by chance, a visible extended library actually struck picks mapped image.

  • So the reason we're struggling here is normally this is a data type that we seem to be using.

  • It's called Struck picks WMAP image, but we're not seeing it actually used.

  • So you know what?

  • Let's see if we go back for a moment and go, Well, that's the only file.

  • So let's go into the sea file real quick.

  • Little digression.

  • Interesting.

  • So picks matt dot age.

  • So that is interesting.

  • And I'm not sure that would compile, as is, that things were out of order.

  • Now, um, I'd have toe have to Look, I know that I think you can do weird stuff like can you type definite type before the structures to find.

  • I didn't think you could Maybe you can, uh, let me go ahead and open both of these up.

  • So here's my C file on Here is my header file.

  • Clean this up a little bit.

  • So the reason we're struggling just be clear is that we're seeing used, uh, we're declaring I mean, maybe this might actually be valid in, so far as it's just declaring of mapping an alias of this to this string, even though the string is not actually present yet.

  • So maybe that is, actually, maybe I didn't know you could do that.

  • I think maybe it functions like prototypes in that sense where you can declare the function signature prior to actually finding function.

  • But it's this symbol that doesn't exist.

  • So, um, we don't really have a good you know what can we try this real quick sand?

  • But we actually have technology by which we can answer this question.

  • Oh, I love the fact that now, now a uh that's cool.

  • So what a nice tool this is.

  • All right, let's say over loved in his cognitive.

  • So that's our open Oscar P here.

  • So that's who's gonna own this.

  • So the reason we're struggling is we're actually both curious like, Is this possible?

  • And frankly, in a class I might normally say, for time's sake, You know what?

  • I'll try this later on and get back to you, But why don't we just do it now?

  • If you have a compiler available to you, Weaken, simply test this out.

  • So I've gone ahead and opened up sandbox.

  • Let's see a 50 dot io.

  • I'm gonna go ahead and create a file called Test dot See, for instance.

  • And now my go to program, Any time I'm doing something in sea is like, oh, gimme and includes standard io and then into main avoid and then do something like print F.

  • But we'll just come back to that.

  • But I think the gist of what I'm trying to do here is I just want to declare a type of my own and let's just type declare something like it ends to be called David's type, and this would be that the work.

  • That's fine.

  • That's fine.

  • You know, What we want to do is we want a typed up that struck before we defined the structure was to find a structure like a coordinate point or something like typed obstruct point and just typed out at the point, which is the equivalent of what no is doing and then down below defined struck point and say a struck point has an index innit?

  • Entwined, for instance, so unfamiliar type toughing something is just a way of taking one type in, giving it another name to it, and so that makes it easy to refer to something, usually by a simpler name.

  • So if you've gone through CS 51 thing that we type death, for instance, in the CIA's 50 library is char star, which in the initial weeks of the class, just to simplify things, we typed up to strength so that you can use a string and that literally gits interpreted by the compilers charge star.

  • But the type nothing just creates a mapping between two names, so you can use the ladder one instead of the former one.

  • All right, so we'll see if this compile successfully, then I think we have our answer and that it is okay so long as the structures eventually defined somewhere else.

  • I'm gonna go ahead and do make tests enter.

  • Oh, apparently it works.

  • We both learned something today.

  • Nice.

  • Very nice.

  • All right, so thanks for that digression.

  • Um, here we go.

  • So also check out the state's 50 sandbox.

  • Yes.

  • Good plug for the CS 50 sandbox.

  • Notice that we slipped that in there.

  • Okay, so let's come back to where we were.

  • So this is declared.

  • Okay, So even so, I'm gonna still find fault here.

  • So it should not have been that ambiguous to us whether or not that's possible.

  • Can I dare say that declaring something?

  • Well, do I want to say this?

  • I'm really uncomfortable with this because I'm declaring in my header file in Alias for Picks Mapped image to be of type struck picks WMAP image.

  • But that data type is defined in my sea file.

  • Frankly, if I want to reuse this data type and if I have a dot header header file at all Frankly, I really wanted to be part of the header file because I want to make sure that this header file works independent even of my C code.

  • When it comes to my types declaration.

  • Right.

  • If picks up image has members that we want to make sure If you're including this header file that you know what the members of a picks map image are, which is what they do successfully with picks, map RGB tell us in the header file that the RGB pixel has a red and a green than a blue component to them.

  • Exactly.

  • So what I would probably do here is something like this.

  • Take that same code that we have go up to my text editor.

  • I'll use the same file for now, and you're gonna go ahead and type deaf it all in one fell swoop.

  • We can go ahead and say, Oops.

  • We can go ahead and say we can't.

  • Apparently, insert mode is a thing on this computer.

  • Oh, yeah.

  • Welcome to windows type Depths struck.

  • Lost the character there.

  • And then if I'm remembering my syntax correctly, we don't have anything nested inside.

  • We actually don't need that.

  • Strictly speaking, pretty sure we can do this right?

  • So now we have both declared the structure as well as typed, and we haven't.

  • You haven't introduced the symbol struck picks WMAP image because we've immediately typed Upton otherwise anonymous structure here, so that would get the job done.

  • And you could put this then in your actual header file.

  • All right, so let's go back to our header file.

  • Indeed, and see what more we have.

  • So here's same idea.

  • Type death struck picks, Matt rgb But here, too.

  • If you want to declare a type you can notice, you don't have to call it struck something.

  • You could just give it a name at the very end, just as we did as well.

  • Stylistically, this is perfectly fine.

  • And CS 50 we tend to put the symbols name on the next line, but I think that's okay.

  • Here.

  • What would you glean is going on with blind 17.

  • 20 here?

  • Yeah, so 17 through 20 or a couple more type deaths.

  • Just giving a new name to some of the existing types and these existing types.

  • I believe they're introduced in the standard into dot h Better file on dhe.

  • They're just different data types for representing specific types of integers.

  • So namely, we have into 30 to underscore t the underscore t just means it's a type and the type is going to be a 32 bit integer where 32 bit insecure is most of our systems by default.

  • Now, if you're using, like, pfft idea years ago 50 sandbox and you just say and it will be a 32 bit integer, but that's not necessarily the case.

  • They're older machines on which might be 16 bits, for instance, that they're not very common anymore on.

  • So this is just a way of being absolutely sure, telling the system that rather than just use your default value of into which might be in 32 to definitely use a 32 bit integer, which is important if you're doing something like reading and writing images because you really care about there being a certain number of pixels in a certain place, because the specifications for what the image file format means probably prescribes the number of bits that certain values need to be like the Arctic and the values.

  • Now, given that these types already exist in 32 t, you in eight underscore tea and so forth.

  • Are you convinced that we should be bothering to declare the synonyms I 32 you ate?

  • I've seen it done before.

  • I personally don't think it's too necessary that it's not all that much more work to just say in 30 to underscore TIA.

  • But if you're using it a lot, I can see why they're doing it for the sake of just being able to use fewer characters.

  • To say I 32 to stand in for a 32 bit integer is very popular.

  • More so like microcontroller embedded programming.

  • Things like that, like game development and see for embedded systems, tends to use this a lot because there is there.

  • You do see this a lot because they're its architecture specific for the most part of those.

  • Okay, these cases And now what about the style here?

  • I see all of these functions are very nicely aligned.

  • Such the return types and functions.

  • Names all fall in the same vertical access, and even the Noah has very cleanly put the pointer the Astros to the left off that very nicely left in mind.

  • Although who in the last two and we got a ding, you minus one here.

  • It's not quite a line, but what do you think about this?

  • And all of the argument lists are lined up on the left here, too.

  • Actually, I think It's pretty clean.

  • I'm inclined to e ignoring the last two lines of inclined to say This is pretty good.

  • It lets you very clearly see it here, the names of the functions and hear the arguments.

  • And it tells me that all of almost all of them take a pointer to a picks.

  • WMAP image as their first argument, although a couple of them seem to take a pointer to a pointer.

  • So I think that very readable.

  • OK, I would say there's definitely an opportunity for some comments here so far, his header files and then to be used by other people using your library.

  • That's where I'm gonna go for my documentation, ideally.

  • So, just pre fixing one or more of these blocks of functions with actual comments, just with a description of what's going on, I think would help.

  • I do like how Noah has clustered together seemingly related functions, thes air about getting values from the image.

  • This is about creating new stuff.

  • This is about setting or getting pixels.

  • So I think the layout here is nice, but it would benefit from just telling me how these things were used so I don't have to infer absolutely everything.

  • Yeah, uh, yeah.

  • They used really great function names.

  • I think they didn't Awesome job there.

  • And the style looks great self documenting.

  • But I agree.

  • I think having doc strings or at least some kind of comment system here would really take it to the next level.

  • Yeah, I agree.

  • All right.

  • Should we take a look at the sea file, then?

  • Yeah, let's take a look.

  • All right, So when the sea file he knows seems to be including picks map dot age.

  • And that's a common convention to anything you want to expose publicly to users of Europe, e I go in the header file, everything else stays internal to this file.

  • So I think regard to discuss how this really should probably go in the header file and it doesn't even need this name.

  • You just typed up it immediately.

  • Then let's stroll down here horses and number from a file.

  • So I'm just reading some comments here.

  • You know, I don't like Thio.

  • I feel like now we really do need some comments because I'm just starting to see code.

  • I'm seeing prototypes of functions that presumably come later.

  • I think now I'd like to get a little more organization in the file is to what I should be looking at what?

  • In fact, I don't see any comments actually within the code itself.

  • And what's the price to be paid?

  • I mean, if someone like me is reading the code, trying to find a bug trying to add a feature, I literally have to read every line of code.

  • Think back to how this is working.

  • What is it doing when you could just tell me that?

  • Because Noah, whoever's written code, knows it best and just save your readers time by just giving them the gist of whatever things doing agreed.

  • I think it's reasonably common thio, unfortunately that I think I think, folks, I think, know what did an awesome job of writing.

  • Great clean coach is in very great identify your names, but I think no matter how good your identifier names, and I think it was even Andres this before, we don't necessarily know the why or that we might be able to infer that how.

  • But the why I think is important nothing that necessarily comes across Yeah, despite how clean your commenting or your symbols are.

  • And I think a good example of this is this line here.

  • So it's clearly the case that we're allocating some number of unsigned in its upsides.

  • Eight.

  • But high times with I get that makes sense to multiply.

  • It gives you the resolution.

  • But the three again, If I'm coming into this a little unfamiliar with P p m.

  • I'm not exactly sure what that is.

  • Maybe that's because it's one bite for red, one for green, one for Blue so I can kind of in for But that's the kind of detail that you just tell your reader what's going on there.

  • Yeah, it actually felt like back to the header file, though.

  • Bella cures raise an interesting question, Which was what was the hash crack?

  • Emma wants directive at the top of the file.

  • Do you know what that one is?

  • I don't know.

  • I don't recall offhand so that anytime you see something, begin with a hashtag again.

  • See, that's what we would call a pre processor directive.

  • So when you compile a C programming undergoes a number of different steps, of which the very first step before the actual compilation is called pre processing on Dad.

  • It's interesting.

  • So this It's very common if you have a larger program that multiple files might include the same header file, and so far as each of them depend on it.

  • So it looks like what we're seeing both for the first time.

  • Hash fragment ones literally is just a hint to the compiler, assuming it supports it, that it should only compile this file once.

  • It should not copy and paste its contents effectively every time you see include lead pick picks mapped out H include picks, map dot h and so forth and multiple fire.

  • Kind of similar to like the hash if and deaf.

  • Indeed.

  • So you might have seen this in some of the city's files.

  • If I open up the sandbox here, just have a little scratch pad.

  • You might see something like, uh, what we say.

  • If end f fu, then we say deaf fu.

  • Then you put your code and then what is it?

  • And NBS evidence.

  • And, uh, and if I'm not dead, I'm not present.

  • See if death I actually don't remember our own Oh, and if and if and if and so in this way, too It's kind of a hack whereby if fu is not defined and I think you do the pound sign Oh, yes, of course.

  • Thank you.

  • If Fu is not defined, go ahead and define it in the second line.

  • Then you have your code and then ended.

  • Then the next time the same files included, it will be the case that who was already defined So none of those lines get executed again.

  • Hash.

  • Pragmatic once.

  • Just a nice shorthand way of achieving that exact same results.

  • All right, back to the sea code.

  • Yep.

  • Anything you that strikes your eyes here?

  • Um, yeah.

  • I was looking at that Malik line as well.

  • In fact, can we scroll up and take a look at the definition of the structure picks WMAP image?

  • I just wanted to take a closer look and see what's included there.

  • Okay, so it looks like it, including a file pointer to whatever it is.

  • It was the file that was opened and then, eh, pointer Thio unsigned integer an eight bit on son integer for pixels.

  • Um, that's an interesting choice because it seems like we're trying to store like, an array of pixels here where I would interpret this to mean.

  • Like each individual pixel is an eight bit unsigned.

  • But maybe what you really want there is like an array of the red green blue values could be defined destruct for a pixel already, but we're not.

  • We're not using that here we start components would be more accurate in this case.

  • Interesting.

  • Yeah, And actually see Andres timed in with a similar answer to what pregnant once is.

  • And I think I've been typing too much python.

  • Let me actually correct myself.

  • It should be hashed.

  • Fine.

  • Not half deaf.

  • I was accidentally defining fight on function there.

  • Okay, Other thoughts that jump out at you here.

  • It's storing a width and a height, and it's also storing a maximum color value, which is interesting because I remember in the header file there was also a function for, like, get maximum color values their story out there, too.

  • It's nice that they cashed the width and height so it doesn't have to compute it.

  • When you do get with their height, just stores it and then, you know, process.

  • Yeah, you would have to store some count there either the pixels or the size could see.

  • You can't just say, how big are you?

  • You would need to source something around.

  • So I agree.

  • I like that approach to scroll down a little further capture.

  • So if we go down to the latter half of the code here picks WMAP image free.

  • I mean, now, I would start to comment really on error checking too.

  • So, strictly speaking, if you're using your library correctly, you're never gonna pass in an invalid pointer or like a Noel pointer.

  • But if you're writing a library, you should assume defensively that other people are.

  • Somehow someone is gonna air.

  • And it's a little obnoxious.

  • If your library code that you wrote crashes because someone else has misused it, the onus is really on you, the library author, to defend your code against accidental or incorrect usage.

  • So I would have some simple checks.

  • And I don't like the one Brian pointed out earlier whereby between line 44 45 here, just check and make sure that image is not know and on Lee.

  • If it's not know to do, proceed to try to close the image file, free the pixels and free the image.

  • And even then I would further make sure that you're checking that image file is not know so that you don't accidentally close.

  • No point there, There.

  • Now, back in the day, years ago when space was incredibly scarce as well as time was very precious when it came to see views.

  • You don't want to do unnecessary checks.

  • And so checking for no all over the place if you're writing most of the code, strictly speaking wasn't necessary.

  • But you have cycles despair these days, and I think it's much better practice to code defensively.

  • Other thoughts that jump out at you here, um, taking a look at picks WMAP image open now and seeing that, Okay, it does indeed.

  • Take a pointer, toe a pointer.

  • So this is one of the questions that we had earlier in the original example code of All right, we defined a pointer to a picks up image, and then we're passing a point to it as the argument, and so here would probably be a place where I would again like to see some documentation of how it is that this actually works.

  • But it seems like, Okay, if the file path that we pass in is no by a line 52 there that we're taking the pointer to the pics WMAP image and setting that equal to zero or no and returning.

  • But that behavior at least I haven't seen it documented anywhere yet on dhe.

  • Whether it was in a comment here somewhere else might be helpful.

  • Yeah.

  • I mean, what I presume is happening is that there's gonna be memory being allocated in here somehow that's actually storing some value at that point, right?

  • Which is why, if I keep scrolling and scrolling scrolling okay, these comments All right, so we appreciate the comments here, but at some point, they need to be attached to the actual code.

  • I think so.

  • Little opportunity for cleaning up there.

  • And also the comment with by itself in height by itself.

  • Those air, interestingly, sort of terse comment, indeed.

  • And there's the allocation on my 40 year.

  • Interesting.

  • Okay, so this is interesting to Noah.

  • Seems to be assigning to image, really know which is what's happening here, even though he's not doing it symbolically, because if image is a pointer to a pointer than star image itself is a pointer, so you could just write this as null.

  • And frankly, you probably should.

  • It leaves to make clear to the user that this is not the number zero, but it's indeed a no pointer.

  • The casting happens implicitly to an actual point value here.

  • But I think that's one opportunity for reminder improvements as well, even though not technically necessary.

  • So there was one thing I was gonna point out up here if I can throw up and up and up.

  • Uh, where are we?

  • No, I think it's okay.

  • Oh, here.

  • I was gonna ask.

  • This is an interesting Syntex that some folks online might not have seen before On Line 67.

  • You see these curly braces with a zero?

  • You want to speak to what that is doing?

  • Oh, I think And correct me if I'm wrong.

  • This just became, I think, the curly brace in Texas that defining an array that contains only zero elements is that the way of defining an array that has multiple things with the same value elements that are zero that are zero.

  • Yeah.

  • So this is a fancy way of just clearing out all of the memory.

  • An array by storing the value zero there again and again and again as many times is appropriate.

  • You could use a catalog to allocate that much memory.

  • Or in this case, you could use mem set, Remember, clear to achieve the same thing.

  • I wouldn't want to do it by hand with 10,000 sighs.

  • All right?

  • No, no.

  • This is a nice little syntactic trick.

  • Which girl?

  • Down toward the bottom before Maybe moving on to the next.

  • Yeah, let's do that.

  • Let's see if there's anything juicy down here that we might have met logic there.

  • All right, So yeah.

  • Now, at this point, I really just want you to tell me what's going on.

  • I mean, we could probably figure this out logically, reasoning through it.

  • But there's enough complexity going on now with the code, especially down here with all the parenthesis ization.

  • Just tell me what I'm looking at.

  • I think.

  • Let me think.

  • It also looks like there's some repeated calculation here.

  • That is something you might wanna factor out.

  • It seems just by taking a guess at it that where and you've got an array of what's called pixels, but each really three elements in this pixels are right together look like they're combining to form one pixel.

  • We're storing the red value at the first pixel in the green value with the second pixel in the blue value with the third, um, so that might not be immediately intuitive.

  • But definitely the calculation of where the red pixels should go is a calculation you could do once in the green pixel is just one offset from not in the blue pixels to offset from that point.

  • Very pretty.

  • This is an example of point arithmetic, right?

  • For those unfamiliar, dauntless through is asking, What's the difference?

  • Stream the bracket zero and then just empty brackets, huh?

  • I don't actually know.

  • Is the empty bracket syntax sighing?

  • See empty curly braces?

  • So this is what I would do if unsure.

  • Well, actually, first I would ask Brian.

  • Then I would ask me Colton, and then I would ask Google a an undefined contents in the array.

  • All right, well, let's a C plus plus, but that might be close enough.

  • Default initialization direct list.

  • I think this is for classes that that's too c++ specific.

  • Well, I did say say type array in the in the chat during the search.

  • All right.

  • All right, Ray, I think I don't want to take too much time this month.

  • It might be a suit possibles on Lee thing.

  • I've not seen it have infinity.

  • That in which case, maybe this one We should kick afterward.

  • Although C programming course notes, let me search for two curly braces.

  • No, but place your initializing data in the races.

  • Yeah, Multi dimensional.

  • They're just saying you can define it around using the curly braces on something called something.

  • I'm gonna go out on a limb and say, You can't do that.

  • Anybody wants to try those sandbox.

  • We do.

  • Let's keep that isn't at home exercise.

  • I think just that we can move on to the next programme.

  • Good.

  • Could be wrong.

  • So that's just our hunch.

  • But I think that the instincts here all right, we don't want to take too much time by trying out every little thing in the sandbox.

  • But when in doubt, if you have a compiler, just try it out.

  • All right, Go.

  • And here we go.

  • So puppet for 411 empty brackets and c++ is default.

  • Initialization and Brackett zero bracket is setting equal to zero.

  • Okay.

  • All right.

  • So we can pick up another one.

  • I'm going to go ahead and switches to this other screen so we could get us on the next.

  • Let's go to the next one tux, man, 29.

  • This is a little one.

  • My all right.

  • Strength.

  • I have never seen any Leuco before in my life.

  • Says it's like Java script in Python.

  • Had a baby.

  • Okay, so please take toe heart.

  • Any feedback Brian has here on your code, then, please?

  • Okay, so here we go.

  • I'm overwhelmed by the files in the file extensions.

  • But when in doubt, start with the read me.

  • So this is by tux man open blackjack, which is a game in lieu.

  • Uh ah.

  • Language the Colton actually uses in his game development class.

  • Maybe you want to share the link of that?

  • Oh, yeah.

  • Let's do that course to be used on open os in the Minecraft mod open computers.

  • I see a lot of to do is up ahead, but if you think I'm done, and that's fine too.

  • I looked through the code base and it was pretty.

  • It looked like those pretty future complete, so Okay, I see.

  • I see more done than to do.

  • So I got a little nervous that the Jews were at the top there.

  • Long term goals.

  • So I think none of this is something.

  • We've been the code, and that's fine. 00:3

All right.

字幕與單字

單字即點即查 點擊單字可以查詢單字解釋

B1 中級

我們再次審查你的代碼!- CS50 on Twitch, EP.35 (WE REVIEW YOUR CODE AGAIN! - CS50 on Twitch, EP. 35)

  • 1 1
    林宜悉 發佈於 2021 年 01 月 14 日
影片單字