如何编写可撤销的vba代码_编写可理解且可维护的VBA代码指南

如何编写可撤销的vba代码

As a person who answers a lot of questions here at Experts-Exchange I often come across code that is hard to follow and/or hard to understand. Even if you don’t look at other people’s code you may well have had the experience of going back to your old code and finding that you had trouble understanding it. If you want to avoid that then the techniques described in this article will help you.

作为在Experts-Exchange上回答很多问题的人,我经常遇到难以理解和/或难以理解的代码。 即使您不看别人的代码,您也很有可能会回到原来的代码,发现自己在理解它时遇到了麻烦。 如果您想避免这种情况,那么本文中介绍的技术将为您提供帮助。

The techniques I’m going to describe are:

我要描述的技术是:

  • Documenting your code

    记录您的代码
  • Coding Standards

    编码标准
  • Layout

    布局

记录您的代码 (Documenting your code)

One of the most important things you can do to improve your code is to document it. Good documentation will make good code better and even poorly written code easier to understand. 

您可以做的最重要的事情之一就是对代码进行记录。 好的文档可以使好的代码变得更好,甚至编写得不好的代码也更容易理解。

How does it make it better? Well for one thing it helps you think. Occasionally I’ve written some code that I thought was fine but the process of documenting it brought more clarity to my thoughts and I was able to see that the code could be improved or was just plain wrong. This saved me the time I would have needed to debug the code. 

如何使其变得更好? 一方面,它可以帮助您思考。 有时我写了一些我认为不错的代码,但是记录下来的过程使我的想法更加清晰,我能够看到代码可以改进或完全是错误的。 这为我节省了调试代码所需的时间。

The problem with undocumented code is that what’s crystal clear now may be Greek six months from now when you want to change it. Also consider the unfortunate programmer who has to maintain it if he takes over for you. Since he probably has never seen the code before he may have to spend hours figuring out what it does. So do him and yourself a favor and document your code. Here are some guidelines for doing so.

未记录代码的问题在于,当您想要更改它时,现在很清楚,现在可能是六个月后的希腊语。 还要考虑不幸的程序员,如果他接管了您,他必须维护它。 由于他可能从来没有看过代码,所以他可能不得不花几个小时弄清楚它的作用。 因此,请他和您自己帮个忙并记录您的代码。 这是一些这样做的准则。

()

Do it as you code 

在编码时完成  

You should document the code as you write it because it will fresh in your mind and you won’t have forgotten why you coded it the way you did. Another reason for doing it then is that it’s not as much fun as writing code and if you put it off you may find excuses for not doing it at all.

您应该在编写代码时记录下来,因为它会让您耳目一新,并且不会忘记为什么要以这种方式编写代码。 这样做的另一个原因是,它不如编写代码那么有趣,如果您推迟使用它,您可能会发现完全不这样做的借口。

()

Every non-trivial procedure should have a header

每个非平凡的过程都应有一个标头

By a non-trivial procedure I mean one which is longer than two or three lines, and by a header I mean a short description of what the procedure does. Here’s an example.

通过非平凡的过程,我指的是一个比两或三行长的过程,而对于标头,则是对该过程的作用的简短描述。 这是一个例子。

Public Function CountOccur(sField As String, sFind As String, nMethod As Integer) As Integer
'***************************************************************************
'Purpose: Count the occurrences of sub-strings in a text or memo field
'***************************************************************************

    Dim nPos As Integer
    
    nPos = 1
    Do While nPos > 0
        nPos = InStr(nPos, sField, sFind, nMethod)
        If nPos > 0 Then
            CountOccur = CountOccur + 1
            nPos = nPos + Len(sFind)
        End If
    Loop

End Function

At a minimum that’s what it should look like but you can expand the header to something like this if you like.

至少应该是它的外观,但是您可以根据需要将标头扩展为类似的形式。

'***************************************************************************
'Purpose: Count the occurrences of sub-strings in a text or memo field
'Inputs:  sField - The text or memo field to be searched
'         sFind - The sub-string to be searched for
'         nMethod - The type of search to perform. If = vbTextCompare then
'            
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值