Home > Articles > Programming

Robert C. Martin's Clean Code Tip of the Week #5: Avoid Redundant Comments

We join "The Craftsman," Robert C. Martin's series on an interstellar spacecraft where programmers hone their coding skills. In this fifth tip in the series, the programmers discuss redundant comments, which describes something that adequately describes itself.
You can review additional articles from Robert C. Martin's series, "The Craftsman," on the ObjectMentor website.
This chapter is from the book

Feb 15, 1945, 08:00:00 GMT

Linus Pauling was on a conference call with Washington, waiting for the status meeting to begin.   He had very good news about the latest experiments for shaping the nuclear charges that propel the Orion crafts.   The next generation vehicle could be 20% more efficient as a result.

“Linus, are you there?” said Bill Davies, coordinator of the meeting.

“I’m (cough, cough) here.” he said. “Excuse me, I can’t seem to shake this awful cold.”

“I know what you mean!   We’ve got a dozen or so people out sick today.”

Jamie Carlson, also on the call, said: “Yeah, there’s a bug going around at Brookhaven too.   Don’t you just love winter?”

“Good Morning Folks.” Vice President Truman said, as he joined the call. “Shall we get…(cough)…excuse me… started?”


Monday, 11 Mar 2002, 15:00

I was staring at the code on my screen.   It was about three years old, and was pretty well written.   The thing I didn’t like about it was that it was littered with redundant comments.   I looked up and spotted Jerry pairing with Avery in the next cube.

“Jerry, could you come over here and look at this?”

“Jerry looked up and said, “I’ve only got another 10 minutes in the current tomato. Can it wait till then?”

“Tomato?”   But Jerry just started back expectantly. “Uh, yeah, sure. Whenever you’re ready.”

“Thanks.” And Jerry turned back to Avery and whatever it was they were working on.

So I scrolled through the code again:

 /**
 * QuickEntryMediator.   This class takes a JTextField and a JList.
 * It assumes that the user will type characters into the JTextField

 * that are prefixes of entries in the JList.   It automatically selects
 * the first item in the JList that matches the current prefix in the
 * JTextField.
 * <p/>
 * If the JTextField is null, or the prefix does not match any element
 * in the JList, then the JList selection is cleared.
 * <p/>
 * There are no methods to call for this object.   You simply create it,
 * and forget it.   (But don't let it be garbage collected...)

 * <p/>
 * Example:
 * <p/>
 * JTextField t = new JTextField();
 * JList l = new JList();
 * <p/>
 * QuickEntryMediator qem = new QuickEntryMediator(t, l);
 * // that's all folks.
 *
 * @param t The JTextField from which abbreviations will be gotten.
 * @param l The JList that will be automatically scrolled to match.

 * @author Jerry, Jeremy.
 * @date 30 Jun, 1999 2113 (DYSON)
 */
  
 public class QuickEntryMediator {
  private JTextField itsTextField;
  private JList itsList;
  
  public QuickEntryMediator(JTextField t, JList l) {
    itsTextField = t;
    itsList = l;
  

    itsTextField.getDocument().addDocumentListener
      (
        new DocumentListener() {
          public void changedUpdate(DocumentEvent e) {
            textFieldChanged();
          }
  
          public void insertUpdate(DocumentEvent e) {
            textFieldChanged();

          }
  
          public void removeUpdate(DocumentEvent e) {
            textFieldChanged();
          }
         } // new DocumentListener
      ); // addDocumentListener
  } // QuickEntryMediator()
  
  private void textFieldChanged() {

    String prefix = itsTextField.getText();
  
    if (prefix.length() == 0) {
      itsList.clearSelection();
      return;
    }
  
    ListModel m = itsList.getModel();
    boolean found = false;
    for (int i = 0; found == false && i < m.getSize(); i++) {

      Object o = m.getElementAt(i);
      String s = o.toString();
      if (s.startsWith(prefix)) {
        itsList.setSelectedValue(o, true);
        found = true;
      }
    }
  
    if (!found) {

      itsList.clearSelection();
    }
  } // textFieldChanged
 } // class QuickEntryMediator
 

A few minutes later I heard a small bell ringing. I’d heard it several times that day, but had ignored it. This time, however, it coincided perfectly with Jerry getting up and coming over to his workstation.

“I thought you were supposed to be working with Adelaide.” Jerry said.

“Oh, yeah I was, but Jean stepped in.”

Jerry rolled his eyes and nodded wryly. “Yeah, that happens.”

“What’s up with the bell?” I asked.

“Oh that? I’ll tell you later. What did you need me for?”

I heaved a sigh. “Did you write this code?”

Jerry looked it over for about half a minute and then said: “Oh, yeah, this is part of the Ice system that Jeremy and I wrote a few years back. Whoo, what a hoot that project was!”

I couldn’t imagine anything being a hoot where Jeremy was concerned, but I let that go. “I thought you told me that comments shouldn’t be redundant.”

“Right!” Jerry said, “That’s one of Mr. C’s rules.”

“Yeah, I know.”I said. “Its C3: ‘A comment is redundant if it describes something that adequately describes itself.

“Yep, that’s the one.”

“So how come you’ve got all this redundancy in the javadocs in this module?”

“What redundancy is that?”

“Well, look.” I said. “The statements, and the initial opening paragraph that describers the constructor arguments, and the comments on the closing braces, and…”

“Oh, yeah, that was back when Javadoc was brand new. We were so excited about all that automatically generated documentation that we kinda went overboard with it. But Mr. C. got wind of it (I think Jean told him) and he put a stop to it.”

I nodded. It was hard for me to internalize that things were so different just a couple of years ago. “I guess things change a lot around here.”

“You bet they do.” Jerry agreed. “We keep on learning new stuff all the time. I think that’s kind of what it means to be a software developer. God help us if we ever stop learning.”

I nodded. “So I shouldn’t use this code as a model for my own code, should I?”

“Well, at least not the comments.” Jerry smiled. “In fact, why don’t you refactor the comments in that module so that they conform to our current style. ”

“Uh, er, sure.” I hadn’t asked him over here to give me a new task.

“Was that all you needed?” He asked, clearly anxious to get back to working with Avery.

“Yeah, thanks. I’ll see you later at dinner?”

“Uh, No, not tonight Alphonse, I’ve got a date with Jazzy, er, Jasmine.”

“OK, well later then.”

Jerry nodded with a smile and then turned back to the cubicle with Avery. I heard him wind something up, and notice a ticking sound.   I started typing.

 /**

 * Selects the line in the list matched by the abbreviation in the text 
 * field.   If no line matches then the selection is cleared.
 */

InformIT Promotional Mailings & Special Offers

I would like to receive exclusive offers and hear about products from InformIT and its family of brands. I can unsubscribe at any time.

Overview


Pearson Education, Inc., 221 River Street, Hoboken, New Jersey 07030, (Pearson) presents this site to provide information about products and services that can be purchased through this site.

This privacy notice provides an overview of our commitment to privacy and describes how we collect, protect, use and share personal information collected through this site. Please note that other Pearson websites and online products and services have their own separate privacy policies.

Collection and Use of Information


To conduct business and deliver products and services, Pearson collects and uses personal information in several ways in connection with this site, including:

Questions and Inquiries

For inquiries and questions, we collect the inquiry or question, together with name, contact details (email address, phone number and mailing address) and any other additional information voluntarily submitted to us through a Contact Us form or an email. We use this information to address the inquiry and respond to the question.

Online Store

For orders and purchases placed through our online store on this site, we collect order details, name, institution name and address (if applicable), email address, phone number, shipping and billing addresses, credit/debit card information, shipping options and any instructions. We use this information to complete transactions, fulfill orders, communicate with individuals placing orders or visiting the online store, and for related purposes.

Surveys

Pearson may offer opportunities to provide feedback or participate in surveys, including surveys evaluating Pearson products, services or sites. Participation is voluntary. Pearson collects information requested in the survey questions and uses the information to evaluate, support, maintain and improve products, services or sites, develop new products and services, conduct educational research and for other purposes specified in the survey.

Contests and Drawings

Occasionally, we may sponsor a contest or drawing. Participation is optional. Pearson collects name, contact information and other information specified on the entry form for the contest or drawing to conduct the contest or drawing. Pearson may collect additional personal information from the winners of a contest or drawing in order to award the prize and for tax reporting purposes, as required by law.

Newsletters

If you have elected to receive email newsletters or promotional mailings and special offers but want to unsubscribe, simply email information@informit.com.

Service Announcements

On rare occasions it is necessary to send out a strictly service related announcement. For instance, if our service is temporarily suspended for maintenance we might send users an email. Generally, users may not opt-out of these communications, though they can deactivate their account information. However, these communications are not promotional in nature.

Customer Service

We communicate with users on a regular basis to provide requested services and in regard to issues relating to their account we reply via email or phone in accordance with the users' wishes when a user submits their information through our Contact Us form.

Other Collection and Use of Information


Application and System Logs

Pearson automatically collects log data to help ensure the delivery, availability and security of this site. Log data may include technical information about how a user or visitor connected to this site, such as browser type, type of computer/device, operating system, internet service provider and IP address. We use this information for support purposes and to monitor the health of the site, identify problems, improve service, detect unauthorized access and fraudulent activity, prevent and respond to security incidents and appropriately scale computing resources.

Web Analytics

Pearson may use third party web trend analytical services, including Google Analytics, to collect visitor information, such as IP addresses, browser types, referring pages, pages visited and time spent on a particular site. While these analytical services collect and report information on an anonymous basis, they may use cookies to gather web trend information. The information gathered may enable Pearson (but not the third party web trend services) to link information with application and system log data. Pearson uses this information for system administration and to identify problems, improve service, detect unauthorized access and fraudulent activity, prevent and respond to security incidents, appropriately scale computing resources and otherwise support and deliver this site and its services.

Cookies and Related Technologies

This site uses cookies and similar technologies to personalize content, measure traffic patterns, control security, track use and access of information on this site, and provide interest-based messages and advertising. Users can manage and block the use of cookies through their browser. Disabling or blocking certain cookies may limit the functionality of this site.

Do Not Track

This site currently does not respond to Do Not Track signals.

Security


Pearson uses appropriate physical, administrative and technical security measures to protect personal information from unauthorized access, use and disclosure.

Children


This site is not directed to children under the age of 13.

Marketing


Pearson may send or direct marketing communications to users, provided that

  • Pearson will not use personal information collected or processed as a K-12 school service provider for the purpose of directed or targeted advertising.
  • Such marketing is consistent with applicable law and Pearson's legal obligations.
  • Pearson will not knowingly direct or send marketing communications to an individual who has expressed a preference not to receive marketing.
  • Where required by applicable law, express or implied consent to marketing exists and has not been withdrawn.

Pearson may provide personal information to a third party service provider on a restricted basis to provide marketing solely on behalf of Pearson or an affiliate or customer for whom Pearson is a service provider. Marketing preferences may be changed at any time.

Correcting/Updating Personal Information


If a user's personally identifiable information changes (such as your postal address or email address), we provide a way to correct or update that user's personal data provided to us. This can be done on the Account page. If a user no longer desires our service and desires to delete his or her account, please contact us at customer-service@informit.com and we will process the deletion of a user's account.

Choice/Opt-out


Users can always make an informed choice as to whether they should proceed with certain services offered by InformIT. If you choose to remove yourself from our mailing list(s) simply visit the following page and uncheck any communication you no longer want to receive: www.informit.com/u.aspx.

Sale of Personal Information


Pearson does not rent or sell personal information in exchange for any payment of money.

While Pearson does not sell personal information, as defined in Nevada law, Nevada residents may email a request for no sale of their personal information to NevadaDesignatedRequest@pearson.com.

Supplemental Privacy Statement for California Residents


California residents should read our Supplemental privacy statement for California residents in conjunction with this Privacy Notice. The Supplemental privacy statement for California residents explains Pearson's commitment to comply with California law and applies to personal information of California residents collected in connection with this site and the Services.

Sharing and Disclosure


Pearson may disclose personal information, as follows:

  • As required by law.
  • With the consent of the individual (or their parent, if the individual is a minor)
  • In response to a subpoena, court order or legal process, to the extent permitted or required by law
  • To protect the security and safety of individuals, data, assets and systems, consistent with applicable law
  • In connection the sale, joint venture or other transfer of some or all of its company or assets, subject to the provisions of this Privacy Notice
  • To investigate or address actual or suspected fraud or other illegal activities
  • To exercise its legal rights, including enforcement of the Terms of Use for this site or another contract
  • To affiliated Pearson companies and other companies and organizations who perform work for Pearson and are obligated to protect the privacy of personal information consistent with this Privacy Notice
  • To a school, organization, company or government agency, where Pearson collects or processes the personal information in a school setting or on behalf of such organization, company or government agency.

Links


This web site contains links to other sites. Please be aware that we are not responsible for the privacy practices of such other sites. We encourage our users to be aware when they leave our site and to read the privacy statements of each and every web site that collects Personal Information. This privacy statement applies solely to information collected by this web site.

Requests and Contact


Please contact us about this Privacy Notice or if you have any requests or questions relating to the privacy of your personal information.

Changes to this Privacy Notice


We may revise this Privacy Notice through an updated posting. We will identify the effective date of the revision in the posting. Often, updates are made to provide greater clarity or to comply with changes in regulatory requirements. If the updates involve material changes to the collection, protection, use or disclosure of Personal Information, Pearson will provide notice of the change through a conspicuous notice on this site or other appropriate way. Continued use of the site after the effective date of a posted revision evidences acceptance. Please contact us if you have questions or concerns about the Privacy Notice or any objection to any revisions.

Last Update: November 17, 2020