Home >

Comments versus Well written code

16. September 2009

So the conversation started today about a guy leaving our team and needing to knowledge transfer his part of the code base to the other team members. The lead programmer on the project told him to stop working on new development and start commenting his code so that the people coming in after him would understand what he had been doing.

Personally the first thing I do when opening a code base that I am not familiar with is to press CTRL-M, CTRL-O and start looking around. ( CTRL-M, CTRL-O is the default visual studio short cut for minimize all outlining. You get to see method declarations, and that is about it.) This will collapse blocks of comments so that they are not intrusive. I never re-expand them. The idea that you need comments for understanding code to me is a big red flag for bad code. I feel well named variables are much more intuitive to learning a new code base than using comments.

I have never sat down to learn an entire code base at one sitting. I am normally working on a small part or a bug fix and need to find something that is very specific. To that end a well named class/method/property is much more helpful with you use the ?Edit->Find? code parser than having to sort through and search all the comments for the set of information that you need.

Also, I am sure that almost everyone has opened a piece of code that had a form1, or class1 class right? Those names that MS graces us with are not supposed to stay named that. Here is the way you avoid this.

Step 1: Go to add class

Step 2: DON?T CLICK OK!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

Step 3: Look down at the bottom of the dialog and type in something intuitive.

image

Step 4: Go drink something with an umbrella and retire to the happy place of understandable class name land.

To prove my point.

   1:  //This is a class that handles 
   2:  //calculating tax for sales in 
   3:  //several states
   4:  public class class1
   5:  {
   6:          //Calc sales total for a couple states
   7:          //AR - tax 9.5%
   8:          //NM - tax 5.5%
   9:          //TX - tax 8.5%    
  10:          public decimal calc(decimal a, string n)
  11:         {
  12:               //The quick red fox jumped over the lazy brown dog.
  13:               //The quick red fox jumped over the lazy brown dog.
  14:               //The quick red fox jumped over the lazy brown dog.
  15:               //The quick red fox jumped over the lazy brown dog.
  16:               switch(n)
  17:               {
  18:                     case "AR":
  19:                            {
  20:                                  return  a * 1.095;
  21:                             }
  22:                             break;
  23:                      case "TX":
  24:                              {
  25:                                  return a * 1.0875;
  26:                              }
  27:                              break;
  28:                      case "NM":
  29:                             {
  30:                                  return a * 1.055;
  31:                              }
  32:                              break;
  33:                }    
  34:         }
  35:  }

versus my way

   1:  public class MultiStateSalesTaxCalculator
   2:  {
   3:          public decimal ARKANSAS_STATE_SALES_TAX = 0.095;
   4:          public decimal TEXAS_STATE_SALES_TAX = 0.095;
   5:          public decimal NEW_MEXICO_STATE_SALES_TAX = 0.095;
   6:   
   7:          public decimal CalculateSalesTotalWithTaxByState(decimal SubTotal, string stateAbreveation)
   8:         {
   9:               switch(stateAbreveation)
  10:               {
  11:                     case "AR":
  12:                            {
  13:                                  return  SubTotal + ( SubTotal * ARKANSAS_STATE_SALES_TAX)  ;
  14:                             }
  15:                             break;
  16:                      case "TX":
  17:                             {
  18:                                  return SubTotal + ( SubTotal * TEXAS_STATE_SALES_TAX);
  19:                              }
  20:                              break;
  21:                      case "NM":
  22:                             {
  23:                                  return SubTotal + ( SubTotal * NEW_MEXICO_STATE_SALES_TAX);
  24:                              }
  25:                              break;
  26:                }    
  27:         }
  28:  }

Comments

Mike
Mike
1/6/2010 5:19:46 PM #
Even though I'm the lead that suggested this, I actually agree with you.  I'd like to see well named classes/methods/variables far more than a bunch of comments (which become obsolete much more easily).  

However, in a project of any size there will almost certainly be areas where comments are useful to explain something uncommon, novel, or downright hokey - we're all guilty of those occasional hacks Smile  

In addition, if you're writing a core class (an object that's likely to be used by multiple areas of the program), I find it very helpful to use the triple / "summarizing" comments.  I not only find that the Intellisense it provides is great, but it also helps me to better name my methods and parameters once I actually try to describe them.

On a totally unrelated note, this appears to be a great place to get a payday advance!
Shades
Shades
3/2/2010 5:37:24 PM #
Good info, thanks for posting that up.
3/12/2010 6:58:02 PM #
Thanx for the effort, keep up the good work Great work, I am going to start a small Blog Engine course work using your site I hope you enjoy blogging with the popular BlogEngine.net.Thethoughts you express are really awesome. Hope you will right some more posts.
3/15/2010 3:09:26 AM #
Hi, I don't agree with everything in this write-up, but you do make some very good points. I'm very interested in this matter and I myself do alot of research as well. Either way it was a well thoughtout and nice read so I figured I would leave you a comment.
3/17/2010 5:36:23 AM #
Great blog it's not often that I comment but I felt you deserve it.
3/17/2010 10:41:47 AM #
Easily, the post is really the sweetest on this worthwhile topic. I fit in with your conclusions and will eagerly look forward to your approaching updates. Saying thanks will not just be sufficient, for the exceptional clarity in your writing. I will immediately grab your rss feed to stay privy of any updates.
3/17/2010 4:52:19 PM #
Howdy, I noticed this weblog on Yahoo Weblogs and think that this is definitely incredibly intriguing and gives good subject matter. Thanks with regard to this excellent post, I will certainly promote this on Twitter. Have a nice day.
3/17/2010 10:14:12 PM #
There is obviously a lot to know about this. I think you have made a lot of good points in your posting.
3/17/2010 10:30:56 PM #
i think you have a great blog here... today was my first time coming here.. i just happened to find it doing a google search. anyway, great post.. i'll be bookmarking this site for sure.
3/18/2010 1:32:07 AM #
That is nice to definitely find a site where the blogger knows what they are talking about.
3/18/2010 2:40:31 AM #
There are a lot of online pharmaceutical companies that sell Viagra pill with no prescription to boot.
3/18/2010 2:58:12 AM #
Nice post, great work. I have recently started my own blog so its good to pickup tips from what you have going here. Many Thanks.
3/18/2010 4:56:06 AM #
I was very pleased to find this site.I wanted to thank you for this great read!! I definitely enjoyed every little bit of it and I have you bookmarked to check out new stuff you post.
3/18/2010 9:30:14 AM #
i think you have a nice blog here... today was my first time coming here.. i just happened to find it doing a google search. anyway, good post.. i'll be bookmarking this page for sure.
3/18/2010 12:24:09 PM #
hopefully this post doesn't appear several times (it seems to freeze once i try to post my comment.. not sure if it's actually posting), but all I really wanted to say was great post and thanks for sharing.
3/19/2010 2:16:15 AM #
i think you have a great site here... today was my first time coming here.. i just happened to find it doing a google search. anyway, good post.. i'll be bookmarking this page for sure.
3/19/2010 3:00:45 AM #
I'm so glad to have found your web page. My pal mentioned it to me before, yet never got around to checking it out until now. I must express, I'm floored. I really enjoyed reading through your posts and will absolutely be back to get more.
3/19/2010 9:24:00 AM #
Hate to be off topic but I just wanted to find out on what is the difference between blogenenigne and wordpress blogs? Is it easier to use or more efficient? I amseeing a lot of blogs powered by this software popping up lately and wondering if it is better or not? Thanks...
3/19/2010 11:38:35 AM #
Excellent site, keep up the good work my colleagues would love this. I read a lot of blogs on a daily basis and for the most part, people lack substance but, I just wanted to make a quick comment to say I’m glad I found your blog. Thanks

zadoc
3/19/2010 11:39:22 AM #
also, I'll be subscribing to your feed and I hope you post again soon
3/19/2010 2:50:10 PM #
Wasn't sure where to post this but I would like to add your website in one of my directories. Please contact me at your convience because I only list webistes with the owners consent - thank you.
3/19/2010 2:51:50 PM #
Who else believes Corey Feldman's new tattoo might be, well...."totally different". I want to know just what made him get that artwork?
3/19/2010 5:34:37 PM #
I like the blog, but could not find how to subscribe to receive the updates by email. Can you please let me know?
3/19/2010 10:18:20 PM #
I like the blog, but could not find how to subscribe to receive the updates by email. Can you please let me know?
3/19/2010 11:18:03 PM #
Have your thought about adding some social bookmark buttons to your blog posts. At least add one for Digg so we can digg you up!
3/20/2010 8:46:50 AM #
I must say this blog has keep me coming back for a while.. http://www.Rare-Jordans.com the informatioon keeps getting better
3/20/2010 10:06:25 AM #
You should add some social bookmark buttons to your website site? You should at least add one for Digg so we can digg you up!
3/20/2010 1:37:20 PM #
I like the blog, but could not find how to subscribe to receive the updates by email.
3/20/2010 9:50:45 PM #
I like the blog, but could not find how to subscribe to receive the updates by email.
3/21/2010 6:36:33 PM #
Wow!, this was a real quality post. In theory I'd like to write like this too - taking time and real effort to make a good article... but what can I say... I procrastinate a lot and never seem to achieve anything
3/21/2010 7:57:18 PM #
I know this is really boring and you are skipping to the next comment, but I just wanted to throw you a big thanks - you cleared up some things for me!
Don
3/21/2010 11:25:14 PM #
A few days ago I discussed that topic with just the same result. It s a topic challenging for discussions anyway. Nice issues you took into account for argumentation
Tom
3/23/2010 1:07:25 AM #
Congratulations, that was  a excellent article. In theory I would like to write like this too. It takes time to creat that informative and additionally lots of effort to write  a brilliant article.
3/23/2010 2:49:03 AM #
Comfortably, the post is in reality the freshest on this notable topic. I agree with your conclusions and will eagerly look forward to your next updates. Saying thanks will not just be sufficient, for the phenomenal clarity in your writing. I will directly grab your rss feed to stay informed of any updates.
3/23/2010 4:40:48 AM #
This is my very first time i visit here. I found a lot of interesting things in your blog particularly it's discussion. From the tons of remarks on your articles, I guess I'm not the only one having all of the enjoyment here! keep up the good work.
3/23/2010 7:29:01 AM #
It gets obvious that their is a lot to experience around this. I think u have established a lot of strong points in ur article.
3/23/2010 4:16:18 PM #
Great article I've just added to my bookmark list.
3/23/2010 10:28:34 PM #
Many thanks for your exciting blog posting! I found your post very interesting, you are a great author. I just bookmarked your blog and come back soonto your blog. I want to encourage you to continue the brilliant job, I wish you a nice day!
3/23/2010 11:53:00 PM #
Hey, I found your blog in a new directory of blogs. I dont know how your blog came up, must have been a typo, anyway cool blog, I bookmarked you.
3/24/2010 12:48:14 AM #
I must say this blog has keep me coming back for a while.. the informatioon keeps getting better
3/24/2010 2:57:30 AM #
You guys must spend alot of time working on this blog. im impressed
3/24/2010 3:48:05 AM #
Pretty good post. I just stumbled upon your blog and wanted to say that I have really enjoyed reading your blog posts. Any way I'll be subscribing to your feed and I hope you post again soon.
3/24/2010 10:33:18 AM #
For some reason my browser doesn't display this page correctly...That said, it was a really interesting article, keep up the good work and I'll be back soon Smile
3/24/2010 11:22:57 AM #
i think you have a great site here... today was my first time coming here.. i just happened to find it doing a google search. anyway, great post.. i'll be bookmarking this blog for sure.
3/25/2010 12:42:53 AM #
Well id say your entirely right but not on everything you pointed out Keeped me entertained for ages
3/25/2010 6:03:02 AM #
Of course, what a great site and advisory posts, Can I add backlink - import your rss feed? Regards, Reader.
3/25/2010 12:21:26 PM #
i know i'm a little off topic, but i just wanted to say i like the layout of your blog. i'm new to the blogegine platform, so any tips on getting my blog looking good would be appreciated.
Tie
3/25/2010 9:22:55 PM #
interesting share, thanks Smile
3/26/2010 1:53:23 AM #
Thanks for the relevante information.  I have to agree and disagree about some points here
3/26/2010 6:35:31 AM #
If you're interested in having a guest blog poster please reply and let me know. I will provide you with unique content for your blog, thanks.
3/26/2010 9:38:17 AM #
Terrific post, good job. It Couldn't be composed any greater. Reading this post reminds me of my recent teacher! He always kept lecturing about this. I will forward this article to him. Pretty sure he will have a good read. Thanks for posting!
3/26/2010 3:43:09 PM #
I am not really sure if best practices have emerged around things like that, but I am sure that your great job is clearly identified. I was wondering if you offer any subscription to your RSS feeds as I would be very interested and can?t find any link to subscribe here.
3/27/2010 5:09:16 AM #
maybe you can help me get my blog set up?
3/29/2010 2:10:53 PM #
Very interesting post - Might be old news, but it was news to me.
3/30/2010 2:39:14 AM #
Took me time to read all the comments, but I really enjoyed the article. It proved to be Very helpful to me and I am sure to all the commenters here! It's always nice when you can not only be informed, but also entertained! I'm sure you had fun writing this article.
3/30/2010 9:59:56 AM #
Hi, Are you looking for any guest writers currently because I am willing to provide you with high quality articles written specfically for your website.
3/30/2010 3:35:39 PM #
It seems that you are maintaining a steady blogging pace. Well done! Looking for more updates from your end. Thanks a lot!
3/31/2010 4:07:39 AM #
Yes, I think the blog is pleasant, I found it in Yahoo and I will note it.
Modesto Isa surfing in Great Britain.
3/31/2010 6:13:10 PM #
This is a really good read for me, Must admit that you are one of the best bloggers I ever saw.Thanks for posting this informative article.
4/1/2010 5:41:56 AM #
Thanks for the relevante information.  I have to agree and disagree about some points here
4/3/2010 1:48:25 AM #
I like the blog, but could not find how to subscribe to receive the updates by email.
4/3/2010 9:58:58 AM #
Pretty good post. I just stumbled upon your blog and wanted to say that I have really enjoyed reading your blog posts. Any way I'll be subscribing to your feed and I hope you post again soon.
4/5/2010 8:53:41 AM #
This is a really nice article. I am sure it will help me in the future. Thanks for sharing!
4/5/2010 12:22:24 PM #
You've made some good arguments, but I'm sure there are people who will disagree with some points.
4/5/2010 10:22:39 PM #
Many thanks for your marvelous blog post! I really enjoyed reading it, I think you are a great writer.  I actually added your blog to my favorites and will return in the future. I want to encourage you to continue the great work, have a great day!
4/5/2010 11:47:10 PM #
This article is quite amazing. I love your blogging style, I hope you keep up the amazing writing, I'll definitly come back
4/6/2010 8:27:06 AM #
A few days ago I discussed that topic with just the same result. It s a topic challenging for discussions always. Nice elements you used for argumentation
4/6/2010 1:47:47 PM #
This webpage is quite amazing. I enjoy you're blogging style, I pray you keep up the great blogging, I will definitly check back
4/7/2010 7:30:17 AM #
Hello - I must say, I’m impressed with your site. I had no trouble navigating through all the tabs and information was very easy to access. I found what I wanted in no time at all. Pretty awesome. Would appreciate it if you add forums or something, it would be a perfect way for your clients to interact. Great job
4/7/2010 10:56:58 AM #
I just realized your blog is on a BlogEngine platform. I like the look and feel of this platform. I might even switch my blog to a BE blog!
4/8/2010 4:41:25 AM #
Super-Duper web site! I am loving it!! Be back again - taking your feeds also, Thanks.
4/8/2010 12:02:22 PM #
Zune and iPod: Most people compare the Zune to the Touch, but after seeing how slim and surprisingly small and light it is, I consider it to be a rather unique hybrid that combines qualities of both the Touch and the Nano. It's very colorful and lovely OLED screen is slightly smaller than the touch screen, but the player itself feels quite a bit smaller and lighter. It weighs about 2/3 as much, and is noticeably smaller in width and height, while being just a hair thicker.
4/8/2010 3:48:53 PM #
Yeah ive read this elsewhere, very interesting
4/13/2010 10:00:23 AM #
i am subscribing. thanks
4/14/2010 11:08:19 PM #
Thank you very much for the excellent blog posting! I really enjoyed reading it, I think you are a excellent author. I added your blog to my bookmarks and come back in the future. I want to encourage you to continue that brilliant job, I wish you a nice daytime!
4/14/2010 11:37:59 PM #
We are a group of volunteers and starting a new initiative in our neighborhood. Your site provided us with valuable information to help us get started|.You have done an impressive job!
4/15/2010 10:29:22 PM #
I hope that it might give an easier way for it ...
4/16/2010 8:17:18 AM #
Super-Duper website! I am loving it!! Will come back again - getting your feeds too, Thanks.
4/16/2010 11:47:37 AM #
I happen to enter your blog with the help of google search. To my sheer luck I got what I was searching for. Thanks
4/17/2010 5:55:52 AM #
Heard about this site from my friend. He pointed me here and told me I’d find what I need. He was right! I got all the questions I had, answered. Didn’t even take long to find it. Love the fact that you made it so easy for people like me. More power
4/17/2010 7:48:39 AM #
Pretty insightful post. Never thought that it was this simple after all. I had spent a good deal of my time looking for someone to explain this subject clearly and you’re the only one that ever did that. Kudos to you! Keep it up
4/17/2010 5:48:29 PM #
Im not gonna lie, youve lost me here.  I know that you meant well and you obviously know what youre talking about, but I cant say that I get where youre coming from.  If you want people to understand your points, you should think about the other side of the argument, too.  Youve got a wealth of knowledge, Ill give you that.  But, quite frankly, you turned me off with your tone.
4/18/2010 2:05:33 AM #
Howdy, i read your blog occasionally and i own a similar one and i was just wondering if you get a lot of spam comments? If so how do you prevent it, any plugin or anything you can advise? I get so much lately it's driving me mad so any assistance is very much appreciated.
4/20/2010 3:05:27 PM #
Really appreciate your writings and keen knowledge you have shared. I'll be back to see more of your post soon. Keep on sharing! Good Luck!
4/20/2010 11:33:23 PM #
Stumbled into this site by chance but I’m sure glad I clicked on that link. You definitely answered all the questions I’ve been dying to answer for some time now.  Will definitely come back for more of this. Thank you so much
4/21/2010 10:43:44 AM #
Hi, You have some really Great articles on your blog. I also like the theme and the layout as well. excellent work!
4/21/2010 11:16:59 PM #
Pretty insightful post. Never thought that it was this simple after all. I had spent a good deal of my time looking for someone to explain this subject clearly and you’re the only one that ever did that. Kudos to you! Keep it up
4/22/2010 1:10:03 AM #
It does seem that everybody is into this kind of stuff lately. Don’t really understand it though, but thanks for trying to explain it. Appreciate you shedding light into this matter. Keep it up
4/22/2010 3:37:49 AM #
How could I have missed this blog!  Its incredible.  Your design is flawless, like you know exactly what to do to do make people flock to your page!  I also like the perspective you brought to this subject.  Its like you have an insight that most people havent seen before.  So great to read a blog like this.
4/22/2010 9:55:24 AM #
Nice to be visiting your blog again, it has been months for me. Well this article that i've been waited for so long. I need this article to complete my assignment in the college, and it has same topic with your article. Thanks, great share.
4/22/2010 7:21:39 PM #
There are certainly a lot of details like that to take into consideration. That is a great point to bring up. I offer the thoughts above as general inspiration but clearly there are questions like the one you bring up where the most important thing will be working in honest good faith. I don?t know if best practices have emerged around things like that, but I am sure that your job is clearly identified as a fair game.
4/23/2010 9:51:19 PM #
Easily, the post is really the sweetest on this worthwhile topic. I fit in with your conclusions and will eagerly look forward to your approaching updates. Saying thanks will not just be sufficient, for the exceptional clarity in your writing. I will immediately grab your rss feed to stay privy of any updates.
4/24/2010 6:38:48 PM #
Can I make a suggestion?  I think youve got something good here.  But what if you added a couple links to a page that backs up what youre saying?  Or maybe you could give us something to look at, something that would connect what youre saying to something tangible?  Just a suggestion.
4/24/2010 8:38:57 PM #
Hi there.  I really appreciate the points you made.  I dont think Ive actually thought about it in that way.  I can really appreciate how you approached the subject matter and what you said really gave me a new perspective.  Thanks for taking the time to write this all out.
4/26/2010 5:32:15 PM #
can anyone tell me how to get those little avatars to appear in my comments section? thanx!
4/27/2010 7:25:23 PM #
Easily, the post is actually the best on this deserving topic. I fit in with your conclusions and will eagerly look forward to your upcoming updates. Just saying thanks will not just be sufficient, for the extraordinary lucidity in your writing.
4/28/2010 2:48:20 AM #
I enjoy reading your posts, keep up the amazing work!
4/28/2010 1:45:42 PM #
I thought it was going to be some boring old post, but it really compensated for my time. I will post a link to this page on my blog. I am sure my visitors will find that very useful
4/29/2010 1:07:21 PM #
I find myself returning to your blog site only because you have numerous wonderful insights and you are actually at this a very long time, and that is very exciting and tells me you know a lot.