Placeholder Image

字幕列表 影片播放

  • Should you be using comments?

  • It may surprise you, but the answer is most likely know.

  • And in today's video, I'm going to explain exactly why you shouldn't be using comments in your coat.

  • Let's get started now before we get started.

  • I want to talk about today's video sponsor, which is atlantic dot net hosting.

  • And this is an incredible hosting company, which is giving you an entire year of hosting completely for free.

  • If you sign up with the link down the description below and this isn't some cheapo server, this is actually an incredibly powerful server, which is more powerful than the server.

  • I even host my own website on, so you know that this is gonna be powerful enough to handle any of your needs.

  • On top of that, they have incredible reliability and redundancy on their servers.

  • So your Web page is always going to be up and always available, which is a great thing to know.

  • On top of that, you're gonna get an additional $50 of free credit if you sign up using the code, Kyle.

  • So make sure you check that out, using the link down in the description below and with that out of the way, let's jump into the video you came here for.

  • So I know this idea of not using comments probably sounds incredibly foreign and strange, especially because when you were first learning to develop, I'm sure that people told you.

  • Write comments, right, comments, right comments.

  • It's gonna make your coat easier to understand.

  • You're gonna know what your code is doing when you come back to it in a week or a month or a year, and it's just going to make it easier to know what's happening.

  • And they have a little bit of truth behind their statement.

  • When your very first started out programming, Ah, four loop is pretty difficult to understand exactly what it's doing, and certain complex operations are just difficult to understand because it's your first time being exposed to them.

  • So writing out a comment that says loop over all of the elements and add one is really useful when you're first learning.

  • But after you kind of learned what four loops do and you've learned a little bit about programming, when you look at that for Lou, you could just look at it and know that it's going to add one to every element, and the comet is just kind of bloated on there.

  • It's extra information that you don't even really need, because just reading the code tells you exactly what that comment says.

  • And this is kind of my problem with comments is a lot of times when people are first starting out with programming, they write all these comments that help them understand the code that they don't understand.

  • But then they get into the habit of doing that.

  • So then, even after you understand what a four loop is, you still put that comment in there saying, Loop over every element at one, even though you already know what is happening.

  • And the comment is just making your code more difficult to understand because it's extra information that you need to read, especially because if you change your four loop toe, add to in the future instead of one.

  • Now you need to make sure you go back, update your comment and make sure it says add to toe every element instead of add one.

  • And I promise you that almost always gets for gotten all the time when you have comments in your code, they're going to get left behind.

  • They're going to get ignored.

  • They're not going to get updated with the code they're associated with.

  • So often times you have comments that are out of sync with the code, and that is really confusing.

  • When you have a comment that says adds to and the actual code is adding one, you don't really know what's going on.

  • And it takes extra mental energy to figure out exactly what's happening now this.

  • Most people would agree with that.

  • You shouldn't use comments to explain simple code or to explain things that just kind of makes sense as you start learning.

  • And that is a good idea to use those comments when you're very, very first started out learning as a way to possibly help you understand what's going on.

  • But something that a lot of people don't agree on is the fact that you shouldn't use comments in complicated code unless you absolutely need to.

  • Most times when you comment your code, it's because your code is hard to read.

  • So you make a comment, which is easy to read.

  • That explains what the difficult code does, but I think in most cases when you have complex, difficult to read code.

  • The problem is not with the code itself being difficult, but the fact that the code is not written well, you could maybe rewrite this code in a way that is more explanatory on its own.

  • Because if you come in later and update the code So instead of doing this, it now does something else.

  • You need to make sure you update the comment to say that it now does something else instead of doing what it did before.

  • And if you just use good variable names and good function names to make the code easy to understand, you don't have to update any comments.

  • Nothing will ever get out of sync.

  • Now I'm gonna show just a little bit of code here that's going to explain this really well.

  • This is just really simple code that has a money variable.

  • It's multiplying that money very bold.

  • Times two and then now you have a new money variable with that new value and just looking at this code on its own, it's really confusing what's actually happening.

  • So to make it easy to understand, we just add in a comment that says that the two is the conversion rate between euros $2.

  • So we're converting our original money value, which is in euros to a new U.

  • S.

  • Dollar amount by multiplying by the conversion rate of two.

  • And you may think, Okay, that's great.

  • We explain what's happening in this difficult to understand code because no one really knows what the money variable is.

  • No one knows what to actually means, but I think that that comment is bad.

  • That is just a bad sign of bad code, because instead you should use variable names that makes sense that money variable should be changed to a euros, variable or money in euros or something that explains that it's a euro dollar amount, and then you're too should be set to a variable, a variable that says this is the conversion rate between euros $2 then, lastly, you should have your new money variable after you multiply by, the conversion rate be set to a variable that has also named something like US dollar amount or U.

  • S.

  • Dollar money, something that makes sense with what the actual code is doing.

  • Then you don't need comments to explain this code.

  • You have good variable names, you have good, constant names.

  • And that alone is enough to understand what's going on if you want a really good example of me going through and changing code.

  • That's comment heavy to code that actually uses functions.

  • Invariable names that make sense Make sure you check out my code review Siri's because many of the projects that I've done code reviews for have that problem where they're very, very comment heavy.

  • And I changed it to use almost no comments.

  • And it's full of variable names and function names.

  • That makes sense.

  • And the code is just a Z Z, if not easier to read with.

  • These variable names thin with all the comments that easily get out of date, easily become difficult to read since they don't actually match the code anymore.

  • And I think a lot of this problem actually stems from schools themselves.

  • For example, when I went to college, part of my grade on most of my assignments was actually based on the comments that I wrote in my code.

  • There was actually a couple of classes where they said that you had to have a certain amount of comment lines for every single line of code that you had, so you have to have a lot of comments and I mean a ton of comments, some of my projects that I had to do.

  • I had to write a comment for almost every single line of code that I had, which was just absurd.

  • Even back then I thought it was ridiculous and I was very new to programming.

  • But adding a comment to add two numbers together just felt absolutely ridiculous to me.

  • But they really preach it in a lot of schools and boot camps and other places.

  • And I think that gets pushed on people so much that they think I just need to use comments everywhere.

  • Comments are good.

  • They're always good there, never bad.

  • But really comments have such a darker side to them because they become added eight so easily as I've mentioned already.

  • So the next time that you're going through making some code changes and you're ready to put in a comment to explain something that's difficult to understand, I really implore you to try to figure out if there's a better way to write that code.

  • away where the comment is no longer needed, where your function names and variable names just do the commenting for you because most of the time 95% of the time there is going to be a solution with just variable names and function names that is going to solve the problem that you're running into and you won't need comments.

  • The main reason I see for putting in comments into your code is when you have code that normally you wouldn't write that way.

  • Let's say there's some kind of bug you're working around, so you wrote some code that really doesn't make sense.

  • Normally, for example, maybe you added two numbers together, but you didn't do it.

  • In a normal addition way.

  • Maybe you used some kind of weird funky addition function you made yourself because there was some bug related to adding those two numbers together.

  • That is when a comments really useful, because then people in the future look at that code and I like Why did you make your own ad function?

  • Then they'll see your comment that says, Hey, there was a bug.

  • I had it to do this because of the bug with adding these numbers the normal way that is.

  • One comments are useful when you have to do things that don't make sense, and you have to deliberately do them that way.

  • You can't do it any other way.

  • Then you need the comment to explain your decision.

  • And that, in my opinion, is pretty much the only use case for comments.

  • Other than that, they just become out of date, difficult to follow, and I just cumbersome overall.

  • So I hope when you work on your next project, you use bless comments that you did on the last time and make your code easier to read and understand through variable and function naming properties.

  • If you enjoyed this video, make sure to check on my other videos linked over here and subscribe to my channel for more videos just like this one.

Should you be using comments?

字幕與單字

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

A2 初級

你應該寫評論嗎? (Should You Write Comments?)

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