A Comment on Comments

In my first programming class in college, my teacher handed out two BASIC coding sheets. On the board, the assignment read "Write a program to input and average 10 bowling scores." Then the teacher left the room. How hard could this be? I don't remember my final solution but I'm sure it had a FOR/NEXT loop in it and couldn't have been more than 15 lines long in total. Coding sheets — for you kids reading this, yes, we used to write code out longhand before actually entering it into a computer — allowed for around 70 lines of code each. I was very confused as to why the teacher would have given us two sheets. Since my handwriting has always been atrocious, I used the second one to recopy my code very neatly, hoping to get a couple extra points for style.

Much to my surprise, when I received the assignment back at the start of the next class, I received a barely passing grade. (It was to be an omen to me for the rest of my time in college.) Scrawled across the top of my neatly copied code, "No comments?"

It was not enough that the teacher and I both knew what the program was supposed to do. Part of the point of the assignment was to teach me that my code should explain itself to the next programmer coming behind me. It's a lesson I've not forgotten.

Comments are not evil. They are as necessary to programming as basic branching or looping constructs. Most modern languages have a tool akin to javadoc that will parse properly formatted comments to automatically build an API document. This is a very good start, but not nearly enough. Inside your code should be explanations about what the code is supposed to be doing. Coding by the old adage, "If it was hard to write, it should be hard to read," does a disservice to your client, your employer, your colleagues, and your future self.

On the other hand, you can go too far in your commenting. Make sure that your comments clarify your code but do not obscure it. Sprinkle your code with relevant comments explaining what the code is supposed to accomplish. Your header comments should give any programmer enough information to use your code without having to read it, while your in-line comments should assist the next developer in fixing or extending it.

At one job, I disagreed with a design decision made by those above me. Feeling rather snarky, as young programmers often do, I pasted the text of the email instructing me to use their design into the header comment block of the file. It turns out that managers at this particular shop actually reviewed the code when it was committed. It was my first introduction to the term career-limiting move.

by Cal Evans

This work is licensed under a Creative Commons Attribution 3

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
这段代码是什么意思-- Create table create table CUX_WMS_PO_TRANSACTION_HEADER ( erp_message_no VARCHAR2(60) not null, po_number VARCHAR2(20) not null, erp_order_type VARCHAR2(20) not null, org_code VARCHAR2(20) not null, header_id NUMBER not null, supplier_code VARCHAR2(240) not null, asn_num VARCHAR2(200) not null, last_update_date DATE, last_updated_by NUMBER, creation_date DATE, created_by NUMBER, last_update_login NUMBER, operatives VARCHAR2(50) not null, return_code VARCHAR2(200), return_mesg VARCHAR2(400), isinwarehouse NUMBER, remark VARCHAR2(200), character5 VARCHAR2(200), isinspectionresults NUMBER, remark2 VARCHAR2(150), remark3 VARCHAR2(150), remark4 VARCHAR2(150) ) tablespace APPS_TS_TX_DATA pctfree 10 initrans 1 maxtrans 255 storage ( initial 16K next 8K minextents 1 maxextents unlimited pctincrease 0 ); -- Add comments to the columns comment on column CUX_WMS_PO_TRANSACTION_HEADER.erp_message_no is '接口编号'; comment on column CUX_WMS_PO_TRANSACTION_HEADER.po_number is '采购订单号'; comment on column CUX_WMS_PO_TRANSACTION_HEADER.erp_order_type is '事务处理类型'; comment on column CUX_WMS_PO_TRANSACTION_HEADER.org_code is 'OU-ID'; comment on column CUX_WMS_PO_TRANSACTION_HEADER.header_id is '采购订单头id'; comment on column CUX_WMS_PO_TRANSACTION_HEADER.supplier_code is '供应商编码'; comment on column CUX_WMS_PO_TRANSACTION_HEADER.asn_num is 'wms号'; comment on column CUX_WMS_PO_TRANSACTION_HEADER.operatives is '操作员'; comment on column CUX_WMS_PO_TRANSACTION_HEADER.return_code is '反馈代码'; comment on column CUX_WMS_PO_TRANSACTION_HEADER.return_mesg is '反馈信息'; -- Create/Recreate indexes create index WMS_PO_TRANSACTION_HEADER_N1 on CUX_WMS_PO_TRANSACTION_HEADER (CREATION_DATE, DECODE(RETURN_CODE,'S','S','E')) tablespace APPS_TS_TX_DATA pctfree 10 initrans 2 maxtrans 255 storage ( initial 64K next 128K minextents 1 maxextents unlimited pctincrease 0 ); create unique index WMS_PO_TRANSACTION_HEADER_U1 on CUX_WMS_PO_TRANSACTION_HEADER (ERP_MESSAGE_NO) tablespace APPS_TS_TX_DATA pctfree 10 initrans 2 maxtrans 255 storage ( initial 64K next 128K minextents 1 maxextents unlimited pctincrease 0 );
06-04

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值