Self Documenting Code是否重要?

代码注释与文档
本文探讨了代码注释的重要性,特别是在解释代码为何如此编写方面。好的注释应该说明代码背后的逻辑和假设,这对于维护和理解代码至关重要。同时,文章还讨论了如何保持注释与代码的一致性以及何时注释更为必要。

写Self Documenting Code很重要;这里有一个不错的帖子,它的讨论更好,有兴趣的朋友可以看看。

原帖:

  http://progfu.com/post/2668280164/your-code-is-not-self-documenting

 

讨论精华:

The people who have commented disagreeing with you are clearly inexperienced

developers who have never had to come back and maintain a project six months

later, and have never had to take over someone else's project after they have

left. Do these people think they never leave bugs in their code? Without comments

you cannot tell the real intention of code that has a bug in it. And without

comments you cannot understand how the code is meant to fit into the rest of the

project. A good comment explains the reasoning behind the code. Why is it doing

this? What are the assumptions and limitations? Professional software development

means good design, good documentation and good code.






In my opinion saying WHAT the code does is less important than saying WHY it does

it. This is a shorter way of saying what Paul said just now.

It is usually obvious what the code does (though finding where it does it can be

a pain) but harder to figure out why it is there.

Which is why one of my rules is a paragraph at the top of each class detailing

responsibilities and context in the wider codebase.
Normally this does not change as the code evolves.





Completely disagree with this. When push comes to shove, documentation and code

*alway* diverge. The kind of information you're talking about is either

inadequate to answer the questions someone is going to have or will be detailed

enough that it will need maintenance. And since there is no way to guarantee that

the documentation and code will match, people will have to read the source

anyhow.

Write better tests. Tests should have good names that contain the same

information that your comment should, should have a point that is made clearly so

that they can be understood at a glance, and should have assertions that back up

that point clearly. The advantage is that tests *always* match the code.

The cost is that you have to treat your tests as you would documentation and

optimize their content for readability over most other concerns.






Personally, I like writing the comments of a unit of code first (before writing

any code), just to check the logic of my intention. If it makes sense to me after

writing the comments, then my final implementation will usually work as intended.

This is a form of pseudocode / simulation testing prior to coding that works for

me. YMMV

One of the features of this method is that if you are subject to frequent

interruptions, the comments can help restore your train of thought much faster

than without them.

Last thought: Comments of Why are essential long-term project documentation,

Comments of How primarily assist testing and bug-finding, and are more prone to

maintenance errors.

If you comment (how) before you write the code, and update the comments before

you change the code, the problem of out-of-date comments should be greatly

reduced.




I agree that commenting your code is a must. But commenting the "WHY" and not the

"WHAT" is more appropriate. The code itself tells the what, but we cannot always

determine the intent of the author. Also, if you are building API docs, you've

got to have comments - what, why, and how.





Code can actually be self-documenting, but not self-commenting.

Also, your rant isn't about documentation, but commenting (documentation!

=comments).

Trust me, I've had cases where each line of code was proceeded with 2-5 lines of

comments, and they didn't help one bit explaining what the all code was actually

doing.

So not only "less is more", but "quality over frequency" as well.




of course the real question isn't how fast programmers type, it's how fast they

delete...

Self commenting code is nothing to do with external documentation. Documentation

is to describe the general outline/design of the system, the code is to describe

the system implementation. The more comments you put it, the more things you need

to maintain and to look at.

would your backup code look better like this:
void Backup()
{
//Backup database
... lots of code

//Backup repositories
... lots of code
}

Why have comments there, again, this has nothing to do with documentation, it's

to do with implementation.

Code without properly grained documentation is not maintainable, code with too

many comments is not maintainable, code with no tests is not maintainable.

Good clean code, with good clean naming conventions, and unit/integration tests

is well, you get my drift...

Again, it's not about how much you write, it's about how much you can remove...




Documenting code well is a learned discipline and, like all aspects of writing

software, requires judgement. Most often what needs to be documented is why

something is being done or why it is important. Just ask yourself what comments

would be helpful two years from now if you have to revisit the code. More than

anything, good documentation reduces the time it takes to understand code.


Obviously no programming language is perfect, and that's why self-documenting

code is impossible to always acheive, but the more the better!




I agree totally. "Self documenting code" is much faster and easier to read, but

it isn't the end of the story: it doesn't explain how to use a method/class, what

order a related sequence of calls must be made in, whether a parameter can be

null, what return code is given to mean 'not found', etc. And with modern IDEs

any additional documentation can be shown live as you type to confirm all the

details you need, so you don't have to find and read the source to understand how

to use it.



Completely agree with you especially when I come back to my own uncommented code

and have to figure out exactly what I was thinking...




I would never argue that documentation isn't important, but most of the time, you

aren't working in an ideal scenario (e.g. when you have enough time to do

everything right). How to resolve the need to get a job done, while minimizing

the risk of leaving behind badly documented code that will cost you (or someone

else) a lot of time in the future?




I've come to change my opinion after discovering two things: experienced

programmers _are_ able to write properly self-documenting code, and the statement

"debug only code. comments can lie"


Good naming can explain a lot of things, but it doesn't explain what happens when

you pass wrong parameter, eg. you pass already closed socket instead of open one,

or empty array ... or when the method throws exceptions.





Not all languages force you to define the types of parameters or return value. A

simple thing such as Array.index(0) — without documentation, what would could you

possibly expect it to return? null? false? raise exception? default value? the

array itself?

If you change code, you change the documentation. If you test your code, you

should test your documentation.



If there is any doubt about what will happen in a special case, it should be

documented, because otherwise you can only figure it out manually or digg into

the source code.

 

 

 

 

 

 

 

 

 

 

另外有一个关于better code的帖子,也一并给出。

 

http://progfu.com/post/384161195/11-tips-for-better-code#

 

 

I don't have any hard rule here, but I'd say most should not be more than 100 lines. Setting hard limits is for inexperienced people who have no idea what is going on and causes people to write code to match the rules rather than follow a logical flow. Extremely short methods leads to method spam and spaghetti-zed code. If you think having to scroll down a bit to read a method is bad, try jumping to a completely new class or another method hundreds of lines down. Trying to keep track of what is happening (state) through chained method calls is extremely frustrating. I'm not promoting the use of giant methods, but rather just like class design, do what makes logical sense.

 

If we adhere to the SRP, both at the class level and at the method level, then
* we "Keep methods short"
* we have a chance to choose method names that follow "Use self-descriptive variable and method names".
* we inevitably "Define variables as close as possible to the place of their first usage"
* we "Never ever ever reuse a variable for different purpose"

 

A summary principle extracted from this is: Code is about communication and all these points lead to letting another programmer understand your code better even in furture.

 

 

I agree with them all in principle
In particular number 1 "keep the entire method on screen". I reckon this is very important for readability. Though code complete points out that long methods don't necessarily lead to more bugs.

 

#1: I dont believe you can make number a hard and fast rule. The concept is right, tho.

#9: Definately disagree: you want refractoring (i.e good programming practices) to become second nature. Leaving everything up to unit tests invites lazy programming, especially in larger projects.

 

 

one more.

Get 5 lines of code to work before 50. Many a time I've been brought non-functioning code with the plea, "Help me." Usually I end up gutting the code down to the bare minimum to get it to work and then add on the bells and whistles and error handling.

 

they're more of a guidelines than rules that must be obeyed 110% of the time.

 

 

From my experience, it's almost always bad when you optimize by guessing, but optimizing based on hard evidence isn't a bad practice.

 

 

"Programmers waste enormous amounts of time thinking about, or worrying about, the speed of noncritical parts of their programs, and these attempts at efficiency actually have a strong negative impact when debugging and maintenance are considered. We should forget about small efficiencies, say about 97% of the time: premature optimization is the root of all evil. Yet we should not pass up our opportunities in that critical 3%."

基于数据驱动的 Koopman 算子的递归神经网络模型线性化,用于纳米定位系统的预测控制研究(Matlab代码实现)内容概要:本文围绕“基于数据驱动的 Koopman 算子的递归神经网络模型线性化,用于纳米定位系统的预测控制研究”展开,提出了一种结合数据驱动方法与Koopman算子理论的递归神经网络(RNN)模型线性化方法,旨在提升纳米定位系统的预测控制精度与动态响应能力。研究通过构建数据驱动的线性化模型,克服了传统非线性系统建模复杂、计算开销大的问题,并在Matlab平台上实现了完整的算法仿真与验证,展示了该方法在高精度定位控制中的有效性与实用性。; 适合人群:具备一定自动化、控制理论或机器学习背景的科研人员与工程技术人员,尤其是从事精密定位、智能控制、非线性系统建模与预测控制相关领域的研究生与研究人员。; 使用场景及目标:①应用于纳米级精密定位系统(如原子力显微镜、半导体制造设备)中的高性能预测控制;②为复杂非线性系统的数据驱动建模与线性化提供新思路;③结合深度学习与经典控制理论,推动智能控制算法的实际落地。; 阅读建议:建议读者结合Matlab代码实现部分,深入理解Koopman算子与RNN结合的建模范式,重点关注数据预处理、模型训练与控制系统集成等关键环节,并可通过替换实际系统数据进行迁移验证,以掌握该方法的核心思想与工程应用技巧。
基于粒子群算法优化Kmeans聚类的居民用电行为分析研究(Matlb代码实现)内容概要:本文围绕基于粒子群算法(PSO)优化Kmeans聚类的居民用电行为分析展开研究,提出了一种结合智能优化算法与传统聚类方法的技术路径。通过使用粒子群算法优化Kmeans聚类的初始聚类中心,有效克服了传统Kmeans算法易陷入局部最优、对初始值敏感的问题,提升了聚类的稳定性和准确性。研究利用Matlab实现了该算法,并应用于居民用电数据的行为模式识别与分类,有助于精细化电力需求管理、用户画像构建及个性化用电服务设计。文档还提及相关应用场景如负荷预测、电力系统优化等,并提供了配套代码资源。; 适合人群:具备一定Matlab编程基础,从事电力系统、智能优化算法、数据分析等相关领域的研究人员或工程技术人员,尤其适合研究生及科研人员。; 使用场景及目标:①用于居民用电行为的高效聚类分析,挖掘典型用电模式;②提升Kmeans聚类算法的性能,避免局部最优问题;③为电力公司开展需求响应、负荷预测和用户分群管理提供技术支持;④作为智能优化算法与机器学习结合应用的教学与科研案例。; 阅读建议:建议读者结合提供的Matlab代码进行实践操作,深入理解PSO优化Kmeans的核心机制,关注参数设置对聚类效果的影响,并尝试将其应用于其他相似的数据聚类问题中,以加深理解和拓展应用能力。
在大数据技术快速发展的背景下,网络爬虫已成为信息收集与数据分析的关键工具。Python凭借其语法简洁和功能丰富的优势,被广泛用于开发各类数据采集程序。本项研究“基于Python的企查查企业信息全面采集系统”即在此趋势下设计,旨在通过编写自动化脚本,实现对企查查平台所公示的企业信用数据的系统化抓取。 该系统的核心任务是构建一个高效、可靠且易于扩展的网络爬虫,能够模拟用户登录企查查网站,并依据预设规则定向获取企业信息。为实现此目标,需重点解决以下技术环节:首先,必须深入解析目标网站的数据组织与呈现方式,包括其URL生成规则、页面HTML架构以及可能采用的JavaScript动态渲染技术。准确掌握这些结构特征是制定有效采集策略、保障数据完整与准确的前提。 其次,针对网站可能设置的反爬虫机制,需部署相应的应对方案。例如,通过配置模拟真实浏览器的请求头部信息、采用多代理IP轮换策略、合理设置访问时间间隔等方式降低被拦截风险。同时,可能需要借助动态解析技术处理由JavaScript加载的数据内容。 在程序开发层面,将充分利用Python生态中的多种工具库:如使用requests库发送网络请求,借助BeautifulSoup或lxml解析网页文档,通过selenium模拟浏览器交互行为,并可基于Scrapy框架构建更复杂的爬虫系统。此外,json库用于处理JSON格式数据,pandas库则协助后续的数据整理与分析工作。 考虑到采集的数据规模可能较大,需设计合适的数据存储方案,例如选用MySQL或MongoDB等数据库进行持久化保存。同时,必须对数据进行清洗、去重与结构化处理,以确保其质量满足后续应用需求。 本系统还需包含运行监控与维护机制。爬虫执行过程中可能遭遇网站结构变更、数据格式调整等意外情况,需建立及时检测与自适应调整的能力。通过定期分析运行日志,评估程序的效率与稳定性,并持续优化其性能表现。 综上所述,本项目不仅涉及核心爬虫代码的编写,还需在反爬应对、数据存储及系统维护等方面进行周密设计。通过完整采集企查查的企业数据,该系统可为市场调研、信用评价等应用领域提供大量高价值的信息支持。 资源来源于网络分享,仅用于学习交流使用,请勿用于商业,如有侵权请联系我删除!
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值