Placeholder Image

字幕列表 影片播放

  • Hey guys, I'm starting a new series called Code Like a Pro, and what we're gonna basically teach you is how to code professionally.

  • It's pretty apparent when you open up somebody's project and it's a hobby or they're just starting out and they don't know how to improve their code.

  • Make it so that it looks professional.

  • It looks readable, maintainable.

  • These are all going to be things that I've picked up as my years as a developer, but also that I, you know, I can't take credit for most of them because, you know, I read books and I do courses, and I'm going these from people with many more years of experience than I have.

  • So what does it mean to have professional code like what I mean by that?

  • Well, I want to give ah sort of analogy to explain it.

  • Ah, la times when you're getting started, all you really care about is does your code run and does it deliver the features that you're you know, the client wants, does it work?

  • And it can.

  • And I'm assuming it does in both situations where you're writing professional level code and then you just writing some code we're getting getting started and learning.

  • Um, it's sort of like comparing a treehouse to the homeless.

  • I feel like it like there is something that's going to last the test of time because we have written it in such a way that it's going to be maintainable and easily updated.

  • And then you have a tree house, which is going to slowly get worn down because we didn't build it in the proper way.

  • Um, both our shelters, one's going to outlast the other.

  • So what makes clean maintainable code?

  • Well, we're gonna be talking about a whole slew of things in this series, and we're actually going to be giving specific examples, not just like, hey, you should do it this way.

  • We are going to tell you how to do this way and why, But we're also gonna give coding.

  • Examples were going to say OK when I say variable names need to be descriptive and how what does that mean exactly?

  • And how can we showcase that our code will be now more readable by using something like object or a raid e structuring, for instance, to get us going here and see actually see in our in real time how we can take this set of code.

  • And now we make it this set of code and it makes a whole lot more sense.

  • So we're gonna talk about code readability.

  • You don't write code for yourself or the client even.

  • I mean, you do because they're cutting the checks, right?

  • So But the reason the not the reason.

  • But one of the reasons that you write code is you write it for the next first asked to maintain it, right, the average deficit job, two years or something like that.

  • And chances are that project's gonna outlive you for a very long time.

  • And so you need to make sure that you're writing the cleanest code possible, the most maintainable coat.

  • And to do that, you have to write readable code.

  • So what is limping to those of us who are new?

  • The concept?

  • Well, lynching really is two things, and these they're always Whenever you're incorporating a tool, you have to answer what I call the Big Two.

  • The what and the why.

  • So what is a letter?

  • It is a tool that you set up essentially one time, usually per project that will flag Ares programming.

  • Ares bugs die elastic Ares Ah, and suspicious constructs.

  • What does that mean?

  • It's basically gonna catch Syntax airs.

  • That's it.

  • It's going to find out if you're making simple mistakes in your code where Hey, you're declaring this here outside of its scope and that's gonna break job script is one those languages where sometimes we don't find if that happens until we work our way through the application.

  • So lynchers give us the opportunity to find those airs earlier and more often.

  • It also adds the readability.

  • What that means is we're going to make sure that we are code is formatted in such a way that is the industry standard or is a standard for that language that programming items.

  • And that's why so it's gonna catch Ares syntax there, sometimes more than syntax Ares.

  • It's gonna have trouble catching logic ares, But But, ah, that's that's usually more so.

  • Developer issue won't talk about howto catch those later on this, saying it's something that can also be automated so you don't have to run it every time it's just there, and we'll start picking up good habits by just sort of being trained by the winter to a degree, um, and this will lead to more readable code.

  • Ah, less prone, less bug buggy coat.

  • And I will be writing in better standards because of it.

  • So let's go ahead and jump into an example.

  • All right, So what we have here is a typed her file, which is just javascript.

  • It's a super set of JavaScript.

  • And what we're going to be looking at is, you know, immediately because he didn't intend this file to be typescript.

  • We have some air, so I'm not too worried about that right now.

  • Um, we should have some worries, but, uh, you'll see here, this is before return the lint err on, by the way.

  • And so what's gonna happen here is we're gonna go ahead and turn the lynch around and see Hey, let's take this file.

  • Um, from someone just getting started and see how we could just clean up our code to follow the standards.

  • And, uh, don't worry about the setup of letters.

  • I will include, as I mentioned to videos, but I'm gonna go ahead and turn the letter back on and reload the app.

  • And what we're going to see here is quite a bit of difference.

  • Uh, supposed todo there guess just a little bit of a hiccup so you can see that now there's something wrong with pretty much everything in this file.

  • And, uh, this is so where lynchers excel at is catching those small heirs that are semantic in nature or that are, you know, we want to anything that we can compare that they're not logic based areas.

  • There are structure airs.

  • They are formatting ares items like that.

  • And so what you'll see here is that the standard for comments, for instance, this have a space between them and so that's something we can fix.

  • You'll see here that in modern Java script, you know, the VAR keyword is essentially forbidden.

  • And that's what the word they use and you'll say, OK, this is something that we have decided that, you know, use letter constant.

  • So for the time being will go ahead and change all the bars here and there's quite a bit right.

  • There's quite a bit to let so change that let go ahead and replace all of those far, so we may have to change some of them The cons.

  • You also see that we have her over this, That she s Lane is saying, Hey, on average, you can use you should be using double coats.

  • This includes That's kind of a silly one.

  • But, um, you can change that, depending on your preference.

  • But I think the more important point here is that you see double quotes here sing quotes.

  • Here is we want to have a set standard of what it is we're doing.

  • You shouldn't have multiple ways of how you do things like this in your code.

  • So let's go ahead and replace all the single quotes with double quotes.

  • You can see how we're slowly going to make this file.

  • Just look a little bit cleaner.

  • Um, this isn't something that's there that winter would do.

  • But from a personal standpoint, I would say that that, um, you know, you want your coats faced out evenly as well.

  • When we're getting started here, your coat needs to look like you cared.

  • That's really what code readability means as we're going through here.

  • So we'll go ahead and put a space here, but its face here and there's gonna be other things that we're gonna want to do this file as we progress.

  • But we just want to make it a little bit more readable.

  • And we could do this in just a few more steps.

  • Um, any time you'll have commenters code in here as well, you go ahead and remove that.

  • There's no reason to have that.

  • So right now you're going to the fire.

  • We're doing these minor changes.

  • A lot of this will be syntax stuff.

  • Some of it's important, right?

  • We're using a lead instead of a concert.

  • And there's there's hundreds of rules when it comes to this.

  • And so what you end up picking up are these good conventions, these conventions that say, Hey, what makes a good?

  • What makes a what makes a good clean code?

  • Because that's really what a letter is there for, um, for what it could do in that aspect, and you'll pick these up.

  • Just her habit, your habit.

  • So what we've done is now we have made our code just using the winter, and there's still some areas in here and that we'll talk about those in later section, but we have been able to make our code much more readable, much more understandable And to the latest standards and in terms off just basic t s lint, things and winters to get going so laters air here to stop those syntax mistakes lynchers air here to get you to code in a certain manner in a cleaner manner in a more readable manner.

  • And they're the first step to writing cleaner, more maintainable code.

  • This will be the more of the hard cut like this is the only tool the rest of the stuff we're gonna be talking about.

  • This is stuff that you have to learn that you have to implement and one thing about letters, and in our case, we're just using T s lent.

  • But one thing is that you have the ability override rules because they're not absolutes, right?

  • Don't live in a world of absolutes, but these always help to get you going.

  • These always help to get you started, especially when you're just getting started to writing cleaner code and in especially in a job script environment, where this is kind of a newer idea when you're getting it going and no one maybe has told you about it, which is kind of occurred.

  • Me, like this is where we should start.

  • We're gonna be talking about how we can define our variables and use them better.

  • Thio accomplish a couple objectors, right?

  • We all want to be ableto write software like a professional.

  • And these were some of the items that you need to keep in mind when you're using variables as well as defining names for variables.

  • So what is that gonna accomplish?

  • We're gonna have three objectives.

  • By the end of this course, you're gonna write code that it's more searchable, readable and understandable.

  • There's a lot of bubbles there, but this is what our objective is here.

  • And we're gonna talk a little bit.

  • About what?

  • Those How we're gonna accomplish those objectives now, these are just some of them, but we're gonna be diving into other things.

  • Well, we're talking about falling language standards, limited scope of variables.

  • We're talking about creating new variables, verse, reusing them, eliminating magic numbers and magic strings and items like that in general, we're talking about naming burials, spent a good portion of time talking about that, about choosing a name that describes what a variable does or what its purpose is not how it does it.

  • And then we're gonna talk about what I call the, um this relates to naming the variables what I call the parent or significant other test will dive into that in the code examples we're also gonna talk about when we can use long names for variables and when verse short names and you know, some of the you know how we can incorporate that into her coat and more.

  • Some of the and more sections we're gonna talk about is the practice is that a lot of people do that we need to break right.

  • Um, you're actually causing more harm than good.

  • And those are some of the examples we're gonna give.

  • So I'm not one that really like looking at at slides.

  • I learned from the code.

  • Let's go ahead and dive in.

  • All right, so let's start with a couple different things here.

  • Here we have a simple algorithm to return a factorial of a number.

  • It's not important about the code.

  • It's not important that's in Java script.

  • But the important point here is there's a couple things that we can fix immediately with our variables.

  • One we can start following the language standards, right?

  • This is something that's very simple and immediately going to be a parent in Java script, for instance, you would never ever have something in all caps unless you're dealing with a constant and an actual constant meaning and stuntmen object during a ray that you're gonna be modifying.

  • But this is something that we need to keep in mind at the very least because before we can get get into the higher level things off.

  • Hey, we need to be able to thio understand how to write good variable names this week.

  • We have to just understand how to write variables.

  • We have to write this to our standards.

  • So we're gonna use our camel case, lower case camel case, which is the standard here, and go ahead and fix this.

  • All right.

  • The next thing that we need to start thinking about is can we eliminate variables?

  • The less variables, the better.

  • And this is actually some code I wrote from, like, three years ago.

  • So, um, you don't think that, you know, everyone writes bad code, and we all improve his welcome songs we put effort into, but can we eliminate things.

  • This is something that we need to start thinking about because it's rather important.

  • And the less variables we have to maintain, the better.

  • So in our example here, we can actually eliminate this count variable altogether.

  • There's really no reason to have it.

  • And you'll see here.

  • Okay.

  • What are we doing with it?

  • Were saying zero.

  • Okay.

  • Can we eliminate it?

  • Well, yeah.

  • Goes number here, which is just a number.

  • And this is typescript, right?

  • And we're expected return number here.

  • Just provide some context.

  • But count isn't counts.

  • Doing nothing.

  • Count is doing absolutely nothing for what?

  • We need to do it here.

  • Is it performing anything different than number?

  • No.

  • Then what we can do is we can't just eliminate it.

  • There's no shame in eliminating variables when needed.

  • You know, this is just clutter.

  • At the end of the day, you should if you can have this few variables as possible, because then there's less moving parts.

  • There's less things that we have to keep track of.

  • Now, you, sir, we start looking here where we have another No, no.

  • At the end of day.

  • Um, this this is a debatable But in my opinion, we need to.

  • Numb is not a descriptive enough word.

  • And we probably shouldn't be reassigning the values either, as we're going through here, as you can see, right, So we're modifying known, which is not gonna break this for Lou.

  • But let's go ahead and create a variable that actually has a good name.

  • Now, when we create a variable, we want to describe what it does or what it is and not how it does a lot of times when people start getting descriptive.

  • Um, you know, let me give you a very bad example of a variable name in this instance.

  • Um um, let factorial let total multiple occasions and, well, uh, you know, initialize it two No.

  • All right.

  • Total multiplication is technically true for a variable name, but it isn't what we need to do here.

  • And it's not what we should want to do either, huh?

  • So total multiplication is, um, not a good name.

  • And a better name is factorial.

  • Factorial total.

  • Now, what is this?

  • Well, it is the number that we're storing r factorial sze in our total factorial.

  • All right, now let's go ahead and set up here and we can do it just like so.

  • And now here we return R factorial total, not some random num that's coming in.

  • Let's give another example of some bad variable names.

  • All right, let's take a look at this function.

  • All this function supposed to do is return today.

  • State.

  • Now you'll see Here we have this D and there's a very simple function.

  • But we have this d here and we're creating it now.

  • If we didn't know what this was and we didn't know what this was, would we know what d is if we had a conversation so earlier?

  • I wreck?

  • I talked about the parent are significant Other test.

  • So if I said to you, baby, what do you think variable d is?

  • What would you say?

  • Okay.

  • Okay.

  • Uh, well, what would you say if we renamed it too?

  • I said, hey, what do you think today's date as a variable would would be excellent.

  • That is the significant other or parent test.

  • You should be ableto Hansel one a piece of paper and this is the title, really at the other day and it should be that readable when you get going, Let's go in and give some other examples.

  • Let's give a few more examples of how we might re factor our code in a relatively clean project where we want to make it more readable and more understandable when it comes to our variable names.

  • Here we have an algorithm that is going to take in some values a double nested array for when we last backed up Somehow items.

  • That's all we need to know about it now.

  • The first thing that we could do is we're gonna be returning back is a are the file ideas that we want to update.

  • But you might be thinking, Hey, we're just returning a file of these Another way that would get up that make it a little bit better is it's actually technically we only want the ones to update.

  • So go ahead and make that more descriptive is now making this this function more descriptive in the process, and you have an idea of what it is you're trying to accomplish.

  • Update this like so all right.

  • Next one thing I want I talked about in the intro was that there are times to use short variable names and there are times to use long variable names.

  • Now in here, when your scope is very short, meaning it's like a callback function, it's returning something.

  • It is all right to be using short names such as here.

  • So we're having file a diesel.

  • We're doing a sorting.

  • It is, I personally will still do file i D.

  • But this is an understandable situation, especially in typescript.

  • Where we might do we might drop that f put I d.

  • And then maybe we add a type of file onto here, right?

  • And then it's just us descriptive as before, Um, but now we can eliminate a little bit of clutter, but in terms of short scopes of our variables, flex.

  • So this is completely fine if we wanted a short in those a little bit.

  • But one thing I would highly suggest is that you start using tools that are built into language where if you are going to iterate through an entire ray, for instance, there's no reason to do a four loop like this.

  • Were iterating through the entire ray, and we have tools to make this more descriptive while still iterating through it.

  • So let's use a four of loop, for instance, and I'll talk about how we're gonna make it more descriptors.

  • So in our four of loop, we're gonna remove some variables.

  • We're gonna remove all these ugly eyes, and instead we're gonna replace changes I with a more descriptive word that allows us to write cleaner, more readable, searchable in maintainable code.

  • So how could we do this?

  • Well, what?

  • Which what we're gonna have is a change of changes.

  • And we could maybe, um um, listen, let's just riff actress one piece at a time.

  • So here we're now eliminating pieces, pieces of texture, piece by piece by piece, and it's a little bit cleaner Now.

  • What a reiterating through were clearly iterating through our array of changes, which is an excellent name.

  • And when you were talking about renaming variables, make sure that you take into consideration renaming functions and renaming parameters because they're equally as important when you're moving forward and we'll have a whole section on functions and parameters as well.

  • But that's nice neither here nor another there, but you'll notice that we have this change zero and change one and change one.

  • And you know, if you're super into the familiar with the changes array.

  • It may make sense, but we want to look at our code and not have to dive deeper.

  • We want to have the simplest ways that we could do that.

  • And we can use things in job, script and other languages as well, where we can give a more descriptive, descriptive, descript it descriptive, variable name and assignments here while making our code.

  • So what has changed their of change?

  • There is greater than last.

  • Backup time doesn't make anything.

  • But if you go in and credit const here and we say, Hey, let's do some well say Constance is equal to would have under change.

  • We'll create, Ah variable here.

  • So each one these changes are an array.

  • We'll do a little bit of a ray deconstruction and won't say last updated time, and we'll go ahead and also create a variable here called, um, File I D or updated file I D.

  • And we'll just set this equal to change.

  • And we have those variables.

  • Now that we have this, we can make our code here so much more readable.

  • Where saying hey is our code was our last updated time greater than the last backup time that makes more sense than change zero.

  • Right?

  • And was our updated file I d Do we already have?

  • Does our file ideas to update already include the updated file i d.

  • If it doesn't go ahead and add to file ideas to update our current file i d.

  • Now, with just a couple changes in our logic here not not changes in our logic changes in our structure.

  • We have eliminated some of the floating sort, enough loading but index values that aren't needed.

  • And we have instead done a little bit of a ray deconstructing to pass in values.

  • And now this reads very, very clear, at least in when it talks about are variable, define mints define.

  • Mintz was a weird word to use.

  • One thing I wanted to say is, don't be afraid to change variable names as well.

  • So the thing you have to remember when you're writing code is that it's always changing, right?

  • New features, air coming out, updates are coming out.

  • And even just sitting here, I was thinking about this variable name a little bit on, and I was gonna move on to the next section by sinking.

  • This isn't even completely correct.

  • It's not that file ideas we want to update.

  • We want to back up files.

  • That's what we're trying to get here now.

  • We are storing file ideas in there, but what is what is it we're trying to accomplish?

  • We're trying to back of files and so fine I'd used to back up.

  • We're not trying to update anything, and so these are the ideas to back it up.

  • So don't be afraid to jump in and change this.

  • And if you dive into the code and the variable names or bad or the variable names have slightly changed, don't be afraid to go and change.

  • A lot of times people are, especially if it's code they didn't originally right.

  • One thing that people do all the time, especially when they're getting start with object or oriented programming, is they start prefects ing property names with the name of the class.

  • It's a really bad item.

  • What I mean by that let's say we have this cat class and it is our friend's cat.

  • That's whose cat it is, and we want to update our friends cats age.

  • They just had a birthday, you would have properties which essentially are variables that the other day of an object, Um, say, hey, cat age is now 10 then our friend's cat dot name.

  • We're updating his named V Tito.

  • We do not need to prefects property names in our classes so we could go ahead and just remove that.

  • Like So this is something that people do all the time, all the time, all the time.

  • Um, and I've been guilty of this as well, so I don't think most of things that I'm telling you things that I've done and learn from, um, there's no reason to profess pre face perfect these items.

  • Um, you know, we haven't tell sense.

  • We can hover over.

  • We can see the type we can look through the code.

  • Um, all it does is create clutter and confusion.

  • So when you're dealing with objects, you don't want to be doing that.

  • Another thing to keep in mind is when you have Constance, when you have variables, older developers, although it's not too common nowadays.

  • Well, um, used was called Hungarian notation, where essentially you're going to include the type in your item.

  • So let's say we have a type here, and I remember our file ideas.

  • Ah, so we defy I d s.

  • Uh And then you would include number at the end or at the beginning, depending on the convention.

  • But this essentially Hungarian notation.

  • Now, this is a very bad practice and something I encourage you not to do for one.

  • If you're using typescript Joanie to and most programming languages that aren't that is not JavaScript will have a type that you can assign to it and to what else could find ideas be It could be a string.

  • I guess I could be a number, but this adds clutter and unnecessary, and it could be multiple things.

  • So what if What if I ladies is actually a dynamic type and so we get a number of strength, but sometimes a string number.

  • What do we D'oh?

  • All we're doing is adding confusion to our coats.

  • Those avoid Hungarian notation as much as possible.

  • All right, One thing that you need to avoid as well are what we call magic numbers and strings, for that matter, when, when when you are, that's a little bit different.

  • Magic strings refers to passing in a key into an object that you haven't defined.

  • You typically won't avoid that.

  • But hear magic numbers refers to passing in numbers that apparently came out of nowhere with no context.

  • Right.

  • So here you'll see 63,006.

  • 67.4447 Do you know what this is?

  • I I do just cause I had recently looked at it, but it took me a second figure out.

  • This is actually the earth radius to figure out the orbital period.

  • So let's go ahead and provide contacts on context.

  • And we'll make this a a big old constant in this case in all caps, and we're gonna set it equal to this value.

  • Now, we have a much better idea what this is, and we'll go ahead and pass in Earth Radius.

  • Actually, I don't I don't really like that.

  • We're gonna do it like this.

  • Um, if I was doing a global constant, I might have like that, but other way, So have Earth radius here.

  • And then what is this big old number?

  • Well, it's going to be the gravitational metric.

  • Um, we'll do grab metric and said it equal to this and insert crab metric here Now, our coaches much more readable.

  • We do have more variables.

  • That's all right.

  • But it's more readable, which is the objective.

  • Here s o avoid magic numbers win at all possible another thing to keep in mind when working with variables as you want to limit the scope to be as small as possible.

  • And when I mean by that is you know, here we have this beginner project that we've will be re factoring or we did every factor and upcoming litter video.

  • But, um, we we have a ton of of global variables.

  • Typically, you want to avoid global variables, if at all possible and when you can, such as, like, this answer buttons using to spot.

  • It's used in this function here.

  • Same thing with on Lee used in the suction.

  • Same thing I would imagine with this one and this one.

  • So what we can do is we can go ahead and take this jump down to here and put it into our where it belongs, which is in the aim or local scope to where is that?

  • The idea here with the variables is you want to keep as close to where they need to exist because it's gonna provide the most context for how much they are useful, right?

  • So you typically want to avoid the global scope if you can, and put put variables in the most limited scope possible so that you can get that additional contacts really variable.

  • Naming ah to consider is when we're thing working with bullying.

  • Specifically, there is a is for present tense or has for past tense in terms of the naming convention.

  • This sort of lets you flag it out here, and we can typically, we don't ever want to put the variable types in here if we can avoid it so we could do something like this.

  • Where instead of is box bullion is box, meaning it is a boxer, It is not a box or, if you prefer the half past tense, has box or does not have box.

  • In that sense, the only thing I will say that when you're naming this keep its singular, because part of our objective here is to make items readable and pronounceable, so his box would be how I would go about it, typically for me in the present tense so we could go out and replace box bowling with blocks everywhere and clean that up a little bit.

  • So that's something keeping my working with naming for bullying variables.

  • Now, this is a little bit of a personal preference, I would say.

  • But one thing toe for variables is default parameters.

  • A lot times you'll see people use this or, you know, if we have a invalid value here, a false see value.

  • But I would I would argue that if you need a default value for your parameters, they should be which essentially her valuables at the end of day, right?

  • We're passing data, anything that passes data.

  • We're considering a very well at this point, but we instead want to have this value here.

  • The reason for it is, uh, now the reason for it is it's it's default value.

  • Thus, you can assign the only the only caveat to that is that it does it work slightly different than our or operator.

  • So, um, this will only assign a default value if it is actually undefined.

  • While here, it'll do it for any falsity value.

  • So keep that in mind.

  • But in general, you want to.

  • Unless you have a good reason.

  • Want to put in your default parameters because it's in one location, 11 place to look at and it's it's ah, in my, in my opinion, a little bit cleaner than using the your operator.

  • Functions are objectives for functions.

  • They're gonna be pretty similar to what our objectives for or for variables we want to have readable, searchable and understandable code, which will lead to more maintainable code.

  • We could go ahead and put that in there as well if you want to, but three bubbles seems like enough.

  • So are still the ways that we're gonna consciousness.

  • That's where it's going to differ slightly.

  • We're gonna write smaller functions, and we're gonna say showcase how we might do that And why and when might we might do that.

  • And we're gonna also good and hide me for a second.

  • We'll move, Dylan No move, Dylan.

  • Dylan doesn't wanna move.

  • There were move Dylan over here.

  • And, uh, what we're gonna do is we're going to eliminate bullying flags.

  • We're gonna talk a little bit of all these things we talked about.

  • Why?

  • I just won't tell you.

  • Why do you think this is the right way.

  • Do it my way.

  • It's not Not always my way, but dude, um, but and tell you why I support.

  • So, uh, we're gonna talk about writing our function so they read top to bottom and making sure that's the correct way we're gonna talk about correct function naming.

  • How do we know we're naming functions?

  • I bring over about 34 things that we can do to Neymar functions properly.

  • Give some examples.

  • We're talking about limiting parameters and how we might go about doing that and why we need to do that.

  • We're gonna, of course, talk about the dry principle which hopefully everyone's familiar with.

  • Don't repeat yourself.

  • Why that's so important.

  • We're gonna talk about avoiding side effects in our code to the best of our ability, and we're going to be talking about why they should never be global functions.

  • These were some of the topics we're gonna hit on.

  • Let's go ahead and dive into some of the examples.

  • This file might look somewhat familiar.

  • We're going to continue to re factor it.

  • So we've talked about one concept per or at least I think we have one concept per action item.

  • Right.

  • So, um, if you were going to get some data, you might pre face that with the verb git.

  • You know, you're not gonna start using interchangeable words like retrieve and on up lifter.

  • I I don't know why you don't Typically you take it.

  • And that's the idea here and there goes the same for like, if you're going to use the word update for updating something, that would be what you would do, you wouldn't say set.

  • You won't say update you.

  • You'd choose a verb to stick with because you want your code to be readable.

  • And part of that process is renaming the very by renaming the function names as well as in the process.

  • Eliminating comments.

  • Now, uh, in future section, we're gonna talk about comments, but right now, we're not going to worry too much about.

  • It is it's more about functions, but the kind of are interchangeable at times.

  • So we have random number here.

  • For the most part, this is a This is a decent, decent, um, variable name some things that we could change about.

  • It is variable.

  • Names are supposed to be verbs.

  • Their actions that are happening.

  • So how do we change the super well We Atget we have get on here is more descriptive of what it actually is.

  • Uh, um, you'll see here we have a minute A max, I don't We couldn't maybe change random number here to be get random number from range If we really want to be specific, I think that's fine, because that that's that's as descriptive as we could probably get.

  • And, um, you know, this is typescript in Java script.

  • Typically, you don't have return types.

  • This is a personal preference.

  • I will say what I think makes more maintainable Coat is when we actually put explicit return types.

  • Typescript is optional and in job skirt doesn't exist.

  • But in most languages you have the ability to add an explicit return time as well as explicitly saying what these values will be, which I think is equally important.

  • You know, I think let's just go through this, uh, this project from the top to bottom and see if there's a way that we can update these functions just to make the function is a little bit more readable, a little bit more understandable.

  • We'll see here.

  • So this is our box sizing prop, add CSS selected buttons and changes answers in box.

  • I see.

  • So realistically, I think this should probably be broken out into different, different, different functions.

  • Whenever I see an if else, like this, I started thinking about how can I eliminate it now?

  • Because if an L statements are inherently bad or anything like that, but usually it's because they really are doing to separate things, and I want to treat it that way.

  • So in this instance, I would define a bought A function update.

  • Uh, see, we're gonna update this box.

  • So slight Changes answers in boxes update, um, Porter box sizing something like that and in here, huh?

  • It looks like we're not actually returning anything which slides an update.

  • But we gotta put avoid return type.

  • And instead, let's start by here, fasting this in here, that should all still be the same.

  • And then in here.

  • And I think we'll be a little bit more updating in the second this little this stop update Porter box sizing else.

  • Uh, this is gonna be contact box function, update content box sizing.

  • It looks like we're not going to be taking anything in else in here, Put a void on that.

  • And we're gonna go ahead and paste this in here like so and go ahead and, uh, just update content books.

  • I'm sorry, we don't need to this on here because we're not working within the classes.

  • Just a generic, uh, types, your job application.

  • So we've now put all the logic in two separate files, and if end, we're saying that the end of year reset, uh, so we have this stop border box and our else, and we've sort of put these into smaller functions.

  • That's really one of things.

  • If you're functions more than 10 lines long, you need to start reconsidering reconsidering how we might be able to how we might be able to go about and re factor that.

  • And, you know, that includes renaming these to provide additional context like we could probably have guessed based off the content that was in here.

  • That Hey, if border boxes true, update border box sizing, but and then reading through here to see Oh, well, we're updating content box, right?

  • Um, that's how we did it to name this, But a better way of doing that is the right cleaner, more maintainable code this weekend, and then that way also weaken test these functions individually as well.

  • We'll say, Hey, update content box sizing that makes sense and update border box sizing.

  • It's a more descriptive variable name, and it's easier to read here as well.

  • And and what happens on top of that, I guess, is that once you you know, if any of the codes breaking her out of this is gonna look at these functions.

  • But when you're looking at this sort of container, this public function public function mean, meaning the user is going to be dealing with it.

  • While these would probably be private functions, meaning that the user doesn't even know they exist, they just see that there's something that says update border box sizing, update content box, something they can't actually call these directly.

  • Then we have a month.

  • We really only have to maintain one function and be able to read one function to see what it does in the.

  • In the meantime, unless something breaks, there isn't working properly.

  • Okay, so this would be a a great example of how we might might modify a name here that so we have this ad points where were incriminating points, and you could make the argument that perhaps what we're actually doing here is updating the points.

  • Um, add is a little bit more descriptive, but update, um, update points by one by bye.

  • I, um this is a unique because it looks like we're always incriminating, So you might actually be able to say that.

  • Hey, when we're always in HK remaining at is a good name.

  • So I take it back.

  • I think this is a a good value here.

  • Um, and then we are calling great job after the fact.

  • Now, this is more of a form, anything, and we'll do it for manning video.

  • But typically you want, and we'll leave it.

  • But typically you're gonna win.

  • Wants to set your lot.

  • You want to separate blocks?

  • A code based off space is, um let's see here.

  • Uh, so this is probably a a name that we would re factor very quickly, and the reason for it is great.

  • Job isn't descriptive, right?

  • It doesn't really provide any context.

  • Um, it is it is sort of a funny name in the fact that like, Oh, great job.

  • What is what is this actually, um probably a completion and animation.

  • Um, this would be something like, um, trigger completion.

  • Something like that.

  • Like you have, uh, animation.

  • Probably on top of that here.

  • Were saying like, Hey, um, you completed it.

  • It's an animation in This triggers it.

  • Right?

  • So this is much more descriptive than great job.

  • We'll see if we have any more great jobs out there.

  • Don't just stolen in here.

  • All right?

  • So cool.

  • So we're triggering our completion animation, and I may have this slightly wrong, but triggering animation is right.

  • Um, there may be a sugar ad points animation.

  • That might even be something that makes sense.

  • We're now adding points.

  • Um, I'm making the assumption that hey, uh, this is this is something that is, uh, this is something that is, after they completed the example, we'll see here set styles.

  • So I definitely would change yourself, J.

  • Stiles, because we're trying to one concept, one verb per concept, right?

  • Um and you also noticed that Ah, good job was not a verb is well, functions and methods, these air verbs, and we need to treat them as such, right?

  • So, um, I'm not really stake.

  • So here, we're going to do update CSS display again because we're updating a ton of different values here.

  • Um, yeah, well, we'll leave it at that.

  • I think, um, so answer buttons A whole lot going on in this thing.

  • Let me take a quick look, quick, Isis.

  • Another great example of how we're going to set different values here.

  • There's a lot going on in this function here.

  • Um And to be honest, I'm not fully sure everything that's going on here we're gonna delete this long ago, Dick.

  • Uh, all right, so it sounds like this is updating the answer button.

  • So we're gonna update, answer.

  • And I mean, you could abbreviate Beatty.

  • And there's really no reason to Don't be afraid to have a long, variable names.

  • Long variable names are descriptive variable names.

  • Typically, we have three things here.

  • You know, we all use intel sense.

  • It doesn't matter that we're using extra extra buns, these air, all all the great things.

  • So, um, how would we So I think what we're going to do here is we're going to Yes.

  • This is incorrect.

  • One ever use it?

  • Incorrect willing.

  • Correct One thing.

  • Correct.

  • So CSS is it here is a global election.

  • So I'm going to and noticed, by the way, how when I'm writing these additional functions that we're gonna break out our code, it's always right next to it.

  • It's it's by convenience, but also by design.

  • Where we want functions are used by other functions, if possible, to be nested in the code where you would expect them to be, right.

  • So I'm defining a function that I'm going to use here.

  • Um and I want that I want it.

  • Thio, um, I wanted to be nearby, so I can see it and provide context.

  • So in this case, we're going to write, get, And this is going to be, um, on get all answers and this is going to take in What is this taking it?

  • This is taking in anything it is going to take in a, uh, nothing.

  • Uh, it's actually going to just return things.

  • So here, uh, we're going to return an array of I believe numbers cool.

  • And what we're gonna do is we're gonna take pretty much all of this, and we're gonna replace it with constant answers is equal to And then we'll say that, uh, get all answers.

  • And in here, we're just gonna go ahead and return.

  • Turn this like so it was the problem here.

  • Uh oh, yeah.

  • Let's go ahead and put this in here.

  • Very nice.

  • Okay, So I'm not worried about four Maddie and spacing.

  • That's why I'm not cleaning this up for some of you are losing their mind.

  • Um, when I'm what I'm worried about, it's cleaning up our function code.

  • So now we've put this all in 11 function where we're gonna handle that.

  • And then, um, the thing that he, too thought was sorting the answer buttons wasn't reminds will just leave this code while we're at it.

  • That my comments?

  • No, really.

  • I'll leave that in there for the common video.

  • Um, okay, so for each one day, she will, and even something like this, um, this is, uh this is actually so here we're updating answer buttons.

  • Uh, I guess this is part of this one.

  • We could break this out if we wanted to, but I think that's fine.

  • But now we've been able to take a lot of this logic slightly change it and, um, do something with it that that now makes sense, right?

  • We don't really need it all in here.

  • This is getting all the answers incorrect and correct.

  • Um, and, uh, if we wanted to make it more descriptive, we could even put incorrect and crashed because that's what I felt necessary described.

  • Right?

  • So I started going off.

  • You're your own thing.

  • It's like, What is this?

  • You gets all the answers.

  • What is all that's incorrect and and correct.

  • So maybe if, you know, I usually go with the verbal test for a lot of things, and it helps a lot when, when describe wouldn't describe the functions it won.

  • Everything we talked about was eliminating Boolean flags.

  • And that's sort of what I meant with this.

  • This if else because that's really what is it?

  • I mean, typically, bullying flag would be passed in here, but in our application, one thing that we could do is we could probably eliminate this this function altogether and just do our best to have these trigger the correct code.

  • We could go ahead and the reset into each one of these when they're called and we could just say, Hey, we need to make sure that this is on Lee on the one with the idea of border box And now we don't have to have this sort of bullying flags if else that goes in there, or or a flag that goes in into the event or the the parameter or anything like that.

  • So, typically if you can and it's not always possible.

  • But it's possible more often than people think.

  • It's great to do.

  • Try and eliminate those those are fully and flags so that you don't have to worry about them now.

  • Somehow, uh, this is not I mean, I think I talked about it when it comes to variables about not having global's um, but so in this in this project we have this CSS here, Um, which is, you know, we don't we don't even really do anything with it.

  • We just initialize it.

  • So we want to do our best to not have Global's.

  • And there's quite a bit of global's in here.

  • And the reason for that is, um, even object oriented program we Won has have asked few side effects as possible.

  • It's not always possible.

  • Um, you know this.

  • So you'll see this, like, CSS global variable here.

  • Honestly, when it when it's like this or even you.

  • Let me see if we've been using this anywhere.

  • Yeah.

  • Yeah, we are.

  • We're initializing values.

  • But you'd probably be better off doing something like this.

  • Where?

  • Let's just eliminate this variable altogether.

  • And in here, we are going to get the with the past fat in the margin and so we can eliminate this global variable by writing a function where Reese, today something get sues us blocks model properties.

  • And in here we will go ahead and take all this and put this in here and what we're going to return.

  • And it's just an array of those values right now with the pad the, um the r d r.

  • The, uh God, I've probably rename a lot of this stuff.

  • So, um, I'm going to guess this is border.

  • I'm also going to guess that this is margin and I'm going to guess that this is ah, order style.

  • So I start by renaming those.

  • Even though this isn't about variable rename, it's just as important, more urgent, and then order style.

  • All right.

  • So that's what I'm going to return here.

  • Uh, which is with pad, margin border.

  • We don't care about that right now.

  • Other places where we're going to use this, we're gonna update it down below.

  • So in this instance, we're going to say, const and I think what we'll do is we'll just do some array deconstructing here.

  • It will say, uh, this is equal to, um get Susic's small properties.

  • It's a constant.

  • All right, Cool.

  • What's the issue here?

  • Shadows.

  • I think I think with might be so sort of global variable or might be a reserved keyword.

  • Same with border.

  • No shadow.

  • Oh, maybe they want to see Use it.

  • No, we're using with So pad, it should be padding like Pat either.

  • All right, so here for Patty, we're going through it like so for border.

  • I don't like So, shadow.

  • What is a shadow?

  • Damn, no shadowed variable because it's not like that.

  • I know.

  • I think he's probably I'm naming thing in here, but instead of focusing on that on the video, you just focus on the idiot idea here.

  • So I'm just gonna throw one on this.

  • So we're just gonna do some.

  • So string population here and margin one and then we'll go ahead and dio order style one for Oh, it's not border.

  • So this is a box sizing soon.

  • Um, block size one.

  • Ah, do that.

  • And then

Hey guys, I'm starting a new series called Code Like a Pro, and what we're gonna basically teach you is how to code professionally.

字幕與單字

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

A2 初級

如何像專業人員一樣編寫代碼(與Dylan Israel一起編寫)。 (How to Code Like a Pro (with Dylan Israel))

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