{"id":10617,"date":"2014-02-13T11:30:06","date_gmt":"2014-02-13T16:30:06","guid":{"rendered":"https:\/\/simpleprogrammer.com\/?p=10617"},"modified":"2020-12-30T22:49:35","modified_gmt":"2020-12-31T03:49:35","slug":"comment-code","status":"publish","type":"post","link":"https:\/\/simpleprogrammer.com\/comment-code\/","title":{"rendered":"Should You Comment Your Code?"},"content":{"rendered":"<p>Most beginning software developers think it is a good idea to put lots of comments in their code. When I first started out in software development, I probably wrote 3 lines of comments for each line of real code. But, the problem with this approach is that comments aren&#8217;t living and code is. One changes the other grows stale.<br \/>\nInstead, it is better to write clear expressive code that describes itself so well that it doesn&#8217;t need comments.<br \/>\nIn this video I talk about why I try to avoid using comments in my code and why I think you should too.<\/p>\n<p>[responsive_video]http:\/\/www.youtube.com\/watch?v=ErW6fEvulAc[\/responsive_video]<br \/>\n[blank_space height=&#8217;3em&#8217;]<\/p>\n<p>Full transcription below<\/p>\n<p>[spoiler]<\/p>\n<p><b>John<\/b>:\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 Hey it\u2019s John Sonmez from simpleprogrammer.com, and today I&#8217;m actually going to bring you something a little bit different.\u00a0 I&#8217;m going to mix in some technical content in here.\u00a0 I realized that in these videos, I&#8217;m not really providing very much technical content and mostly inspirational stuff.\u00a0 I&#8217;m mixing things up a little bit here and you\u2019re going to see a little bit more technical content, and perhaps some tutorials and some interviews later on here.\u00a0 Let me know what you think, by the way, if you like this or not and what kind of stuff you\u2019d like to see on this channel.\u00a0 I\u2019ll try to do that for you.<\/p>\n<p>Today, what I want to talk to you about is comments in your code.\u00a0 This is something that I feel pretty passionately about, and I think is an area that a lot of developers, especially beginning developers, are confused about and they don\u2019t understand really how to apply comments to their code and when they should use them and when they shouldn\u2019t.<\/p>\n<p>Let me tell you my stance on here.\u00a0 It\u2019s a little bit of an extreme stance, but I stand by this and I\u2019ll give you some reasons why, which is I say that you should avoid using comments in your code as much as possible.\u00a0 Now, this might seem really weird.\u00a0 I know when I started out in software development I thought that good code was code that had lots of comments because it seemed like good code to me, because, hey, if there are a lot of comment, a comment on every single line explaining what that code did I could easily understand that code.\u00a0 Even if I didn\u2019t understand the programming language, I could understand it by reading the comments.<\/p>\n<p>This seemed like a good idea to me.\u00a0 I wrote a lot of code that was just full of comments, and I thought I was doing a great job.\u00a0 I was being very thorough.\u00a0 A lot of software developers, I think, have that same feeling.\u00a0 Hey, that\u2019s okay.\u00a0 I felt that way too but here\u2019s the thing.\u00a0 This is what I found out a little bit later on.\u00a0 As I found out that I could achieve the same effect of explaining what something does by making the names of my variables and the names of my functions and methods more descriptive and not putting comments.<\/p>\n<p>Almost everywhere in my early code when I go back and look at it where I had a comment, I could replace that comment with a descriptive variable, name, or a method or function.\u00a0 In fact, in some place I could create a method or function and for no other purpose then creating a description and breaking my code up to make it easier to understand and digest.\u00a0 One of the important parts of writing a good code is writing code that\u2019s highly readable, that\u2019s easy for someone to understand.<\/p>\n<p>Now, the reason why comments don\u2019t do this effectively is because comments are dead.\u00a0 They\u2019re not living.\u00a0 How many times have you gone through an old code base and you have looked at the comments there and you\u2019ve had to ignore the comments because they\u2019re just plain wrong, because someone updated the code.\u00a0 They updated the functionality of the system but they didn\u2019t update the comments.\u00a0 This happens all the time and you get to this point where you start to be a cynic.\u00a0 You start to be skeptical of comments because they often lie to you, and they can mislead you and put you down the wrong path.\u00a0 What happens?\u00a0 You start to ignore those comments and those comments don\u2019t make sense.<\/p>\n<p>Now, developers don\u2019t mean to do this, but the fact of the matter is that the code itself doesn\u2019t lie.\u00a0 It\u2019s what actually gets executed.\u00a0 The comments don\u2019t have to be updated with the code.\u00a0 That\u2019s the problem.\u00a0 There\u2019s a big disconnect there.<\/p>\n<p>The other part of this is that a lot of times beginning programmers will make comments that are descriptive that explain what the language does.\u00a0 If you\u2019re going to add a comment to your code and there are some places where it does make sense to add comment, it should be something non-obvious that can\u2019t be communicated by the variable, names, or the functions and the methods or the structure of the code itself.\u00a0 Something like a secret formula for calculating some kind of mathematical equation that it just would not make sense to try and spell out in the code itself, because it\u2019s complicated.\u00a0 It\u2019s not something that\u2019s going to be easily digested.\u00a0 You could put in a comment.\u00a0 There are even a paragraph of comments that explains that this is someone steer your equation, and this is why we\u2019re using this here.<\/p>\n<p>Other places for this would be a comment that explains some kind of weird or strange thing that you\u2019re doing.\u00a0 Now, obviously, you want to avoid that as much as possible.\u00a0 Let\u2019s say that there\u2019s some kind of weird bug or some kind of memory type of management thing that you need to do something strange in your code.\u00a0 You add a comment that explains why you\u2019re doing this action there.<\/p>\n<p>Really, when you look at a really good developer\u2019s code, at least most good developers, you\u2019ll find that there are not very many comments but you\u2019ll find that it\u2019s easy to read their code because their method names, their functions, their variable names, tell the story.\u00a0 They have the information that\u2019s in the comment.<\/p>\n<p>Let me give you a tip here before I wrap this up, which is if you\u2019re in the habit of writing a lot of comments in your code, that\u2019s good.\u00a0 It means you\u2019re trying to be thorough.\u00a0 I&#8217;m not trying to offend you or is it that you\u2019re doing a bad job, because it probably means that you\u2019re a good programmer and you\u2019re trying to do a good job but try this out for a change.\u00a0 Go through some of your code where you have a lot of comments.\u00a0 Replace the comments and see if you can replace them with a good variable name.\u00a0 If you could break up the code that\u2019s beneath that comment in a way that makes it self-explanatory that makes it so that the code communicates that what\u2019s in the comment.<\/p>\n<p>If you do this, I think you\u2019ll find that you\u2019ll be able to write a better and more descriptive and you won\u2019t have this problem of syncing up code with comments.\u00a0 They\u2019ll just improve your ability to describe things in your code overall, which is a very, very important thing to learn as a software developer.<\/p>\n<p>I hope this video has helped you.\u00a0 If you like these type of videos, if you\u2019d like to see more of this type of things, first of all, let me know.\u00a0 Let me know what you\u2019d like to see on this channel and subscribe.\u00a0 Subscribe to my channel and you\u2019ll get an update.\u00a0 I post a video every week.\u00a0 Also, check out my blog at simpleprogrammer.com.\u00a0 You can check on my <a href=\"https:\/\/simpleprogrammer.com\/pluralsight\" target=\"_blank\" rel=\"noopener\">Pluralsight courses at pluralsight.com<\/a>.\u00a0 I have 54 courses on a wide range of technologies.\u00a0 I just thank you for watching this video.\u00a0 I really like the comments and positive feedback I\u2019ve gotten from doing these videos.\u00a0 You guys make it all worth it so thank you and I will talk to you again next week.<\/p>\n<p>[\/spoiler]<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Most beginning software developers think it is a good idea to put lots of comments in their code. When I first started out in software development, I probably wrote 3 lines of comments for each line of real code. But, the problem with this approach is that comments aren&#8217;t living and code is. One changes&#8230;<\/p>\n","protected":false},"author":2,"featured_media":10619,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_kad_post_transparent":"","_kad_post_title":"","_kad_post_layout":"","_kad_post_sidebar_id":"","_kad_post_content_style":"","_kad_post_vertical_padding":"","_kad_post_feature":"","_kad_post_feature_position":"","_kad_post_header":false,"_kad_post_footer":false,"_kad_post_classname":"","footnotes":""},"categories":[162229169],"tags":[],"class_list":["post-10617","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-youtube"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.6 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Should You Comment Your Code? - Simple Programmer<\/title>\n<meta name=\"description\" content=\"Should you comment your code? In this video, I give you my reasons why I think it is better to write descriptive code than to comment it.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/simpleprogrammer.com\/products\/careerguide\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Should You Comment Your Code? - Simple Programmer\" \/>\n<meta property=\"og:description\" content=\"Should you comment your code? In this video, I give you my reasons why I think it is better to write descriptive code than to comment it.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/simpleprogrammer.com\/products\/careerguide\" \/>\n<meta property=\"og:site_name\" content=\"Simple Programmer\" \/>\n<meta property=\"article:published_time\" content=\"2014-02-13T16:30:06+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2020-12-31T03:49:35+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/simpleprogrammer.com\/wp-content\/uploads\/2014\/02\/2014-02-10_16-54-49.png\" \/>\n\t<meta property=\"og:image:width\" content=\"648\" \/>\n\t<meta property=\"og:image:height\" content=\"397\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"John Sonmez\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"John Sonmez\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"7 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/simpleprogrammer.com\\\/products\\\/careerguide#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/simpleprogrammer.com\\\/comment-code\\\/\"},\"author\":{\"name\":\"John Sonmez\",\"@id\":\"https:\\\/\\\/simpleprogrammer.com\\\/#\\\/schema\\\/person\\\/1abc70edfb189184827740310672de67\"},\"headline\":\"Should You Comment Your Code?\",\"datePublished\":\"2014-02-13T16:30:06+00:00\",\"dateModified\":\"2020-12-31T03:49:35+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/simpleprogrammer.com\\\/comment-code\\\/\"},\"wordCount\":1433,\"commentCount\":33,\"image\":{\"@id\":\"https:\\\/\\\/simpleprogrammer.com\\\/products\\\/careerguide#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/simpleprogrammer.com\\\/wp-content\\\/uploads\\\/2014\\\/02\\\/2014-02-10_16-54-49.png\",\"articleSection\":[\"YouTube\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/simpleprogrammer.com\\\/products\\\/careerguide#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/simpleprogrammer.com\\\/comment-code\\\/\",\"url\":\"https:\\\/\\\/simpleprogrammer.com\\\/products\\\/careerguide\",\"name\":\"Should You Comment Your Code? - Simple Programmer\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/simpleprogrammer.com\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/simpleprogrammer.com\\\/products\\\/careerguide#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/simpleprogrammer.com\\\/products\\\/careerguide#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/simpleprogrammer.com\\\/wp-content\\\/uploads\\\/2014\\\/02\\\/2014-02-10_16-54-49.png\",\"datePublished\":\"2014-02-13T16:30:06+00:00\",\"dateModified\":\"2020-12-31T03:49:35+00:00\",\"author\":{\"@id\":\"https:\\\/\\\/simpleprogrammer.com\\\/#\\\/schema\\\/person\\\/1abc70edfb189184827740310672de67\"},\"description\":\"Should you comment your code? In this video, I give you my reasons why I think it is better to write descriptive code than to comment it.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/simpleprogrammer.com\\\/products\\\/careerguide#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/simpleprogrammer.com\\\/products\\\/careerguide\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/simpleprogrammer.com\\\/products\\\/careerguide#primaryimage\",\"url\":\"https:\\\/\\\/simpleprogrammer.com\\\/wp-content\\\/uploads\\\/2014\\\/02\\\/2014-02-10_16-54-49.png\",\"contentUrl\":\"https:\\\/\\\/simpleprogrammer.com\\\/wp-content\\\/uploads\\\/2014\\\/02\\\/2014-02-10_16-54-49.png\",\"width\":648,\"height\":397},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/simpleprogrammer.com\\\/products\\\/careerguide#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/simpleprogrammer.com\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Should You Comment Your Code?\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/simpleprogrammer.com\\\/#website\",\"url\":\"https:\\\/\\\/simpleprogrammer.com\\\/\",\"name\":\"Simple Programmer\",\"description\":\"\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/simpleprogrammer.com\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/simpleprogrammer.com\\\/#\\\/schema\\\/person\\\/1abc70edfb189184827740310672de67\",\"name\":\"John Sonmez\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/dc2f18dfa76e017566ce607de002d7abe4acfd5ec19a6db82c180b00867b8d94?s=96&d=mm&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/dc2f18dfa76e017566ce607de002d7abe4acfd5ec19a6db82c180b00867b8d94?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/dc2f18dfa76e017566ce607de002d7abe4acfd5ec19a6db82c180b00867b8d94?s=96&d=mm&r=g\",\"caption\":\"John Sonmez\"},\"url\":\"https:\\\/\\\/simpleprogrammer.com\\\/author\\\/jsonmez\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Should You Comment Your Code? - Simple Programmer","description":"Should you comment your code? In this video, I give you my reasons why I think it is better to write descriptive code than to comment it.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/simpleprogrammer.com\/products\/careerguide","og_locale":"en_US","og_type":"article","og_title":"Should You Comment Your Code? - Simple Programmer","og_description":"Should you comment your code? In this video, I give you my reasons why I think it is better to write descriptive code than to comment it.","og_url":"https:\/\/simpleprogrammer.com\/products\/careerguide","og_site_name":"Simple Programmer","article_published_time":"2014-02-13T16:30:06+00:00","article_modified_time":"2020-12-31T03:49:35+00:00","og_image":[{"width":648,"height":397,"url":"https:\/\/simpleprogrammer.com\/wp-content\/uploads\/2014\/02\/2014-02-10_16-54-49.png","type":"image\/png"}],"author":"John Sonmez","twitter_card":"summary_large_image","twitter_misc":{"Written by":"John Sonmez","Est. reading time":"7 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/simpleprogrammer.com\/products\/careerguide#article","isPartOf":{"@id":"https:\/\/simpleprogrammer.com\/comment-code\/"},"author":{"name":"John Sonmez","@id":"https:\/\/simpleprogrammer.com\/#\/schema\/person\/1abc70edfb189184827740310672de67"},"headline":"Should You Comment Your Code?","datePublished":"2014-02-13T16:30:06+00:00","dateModified":"2020-12-31T03:49:35+00:00","mainEntityOfPage":{"@id":"https:\/\/simpleprogrammer.com\/comment-code\/"},"wordCount":1433,"commentCount":33,"image":{"@id":"https:\/\/simpleprogrammer.com\/products\/careerguide#primaryimage"},"thumbnailUrl":"https:\/\/simpleprogrammer.com\/wp-content\/uploads\/2014\/02\/2014-02-10_16-54-49.png","articleSection":["YouTube"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/simpleprogrammer.com\/products\/careerguide#respond"]}]},{"@type":"WebPage","@id":"https:\/\/simpleprogrammer.com\/comment-code\/","url":"https:\/\/simpleprogrammer.com\/products\/careerguide","name":"Should You Comment Your Code? - Simple Programmer","isPartOf":{"@id":"https:\/\/simpleprogrammer.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/simpleprogrammer.com\/products\/careerguide#primaryimage"},"image":{"@id":"https:\/\/simpleprogrammer.com\/products\/careerguide#primaryimage"},"thumbnailUrl":"https:\/\/simpleprogrammer.com\/wp-content\/uploads\/2014\/02\/2014-02-10_16-54-49.png","datePublished":"2014-02-13T16:30:06+00:00","dateModified":"2020-12-31T03:49:35+00:00","author":{"@id":"https:\/\/simpleprogrammer.com\/#\/schema\/person\/1abc70edfb189184827740310672de67"},"description":"Should you comment your code? In this video, I give you my reasons why I think it is better to write descriptive code than to comment it.","breadcrumb":{"@id":"https:\/\/simpleprogrammer.com\/products\/careerguide#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/simpleprogrammer.com\/products\/careerguide"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/simpleprogrammer.com\/products\/careerguide#primaryimage","url":"https:\/\/simpleprogrammer.com\/wp-content\/uploads\/2014\/02\/2014-02-10_16-54-49.png","contentUrl":"https:\/\/simpleprogrammer.com\/wp-content\/uploads\/2014\/02\/2014-02-10_16-54-49.png","width":648,"height":397},{"@type":"BreadcrumbList","@id":"https:\/\/simpleprogrammer.com\/products\/careerguide#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/simpleprogrammer.com\/"},{"@type":"ListItem","position":2,"name":"Should You Comment Your Code?"}]},{"@type":"WebSite","@id":"https:\/\/simpleprogrammer.com\/#website","url":"https:\/\/simpleprogrammer.com\/","name":"Simple Programmer","description":"","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/simpleprogrammer.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/simpleprogrammer.com\/#\/schema\/person\/1abc70edfb189184827740310672de67","name":"John Sonmez","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/dc2f18dfa76e017566ce607de002d7abe4acfd5ec19a6db82c180b00867b8d94?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/dc2f18dfa76e017566ce607de002d7abe4acfd5ec19a6db82c180b00867b8d94?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/dc2f18dfa76e017566ce607de002d7abe4acfd5ec19a6db82c180b00867b8d94?s=96&d=mm&r=g","caption":"John Sonmez"},"url":"https:\/\/simpleprogrammer.com\/author\/jsonmez\/"}]}},"_links":{"self":[{"href":"https:\/\/simpleprogrammer.com\/wp-json\/wp\/v2\/posts\/10617","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/simpleprogrammer.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/simpleprogrammer.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/simpleprogrammer.com\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/simpleprogrammer.com\/wp-json\/wp\/v2\/comments?post=10617"}],"version-history":[{"count":0,"href":"https:\/\/simpleprogrammer.com\/wp-json\/wp\/v2\/posts\/10617\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/simpleprogrammer.com\/wp-json\/wp\/v2\/media\/10619"}],"wp:attachment":[{"href":"https:\/\/simpleprogrammer.com\/wp-json\/wp\/v2\/media?parent=10617"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/simpleprogrammer.com\/wp-json\/wp\/v2\/categories?post=10617"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/simpleprogrammer.com\/wp-json\/wp\/v2\/tags?post=10617"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}