Add comments to End If and Loop statements

Add comments to End If and Loop statements

Here’s a little programming tip that will save you hours of headacheslater.

Most of us already indent our Ifs, Selects, Do…Loops, etc., and that is good. But suppose you have some fairly complex code with several levels of indentation. Example:

If A= 0 and B=1 then    ' ...(several lines of code)    If C < 0 and D > 0 then        ' ... (several more lines of code)        Do while E > 0            ' ... (several more lines of code)            If F <> G or H < I then                Do While J < 0                    ' ... (several more lines of code,                     '      with several more If -EndIf blocks)                Loop                ' ... (several more lines of code)            End If            ' ... (several more lines of code)        Loop        ' ... (several more lines of code)    End If    ' ... (several lines of code)End If

It does get a little confusing as to which End If goes with which If, etc, because the original statement has scrolled up off the screen. My suggestion is this: modify your programming style slightly so that you create your If – End If or Do – Loop statements together and comment the End or Loop statement with the conditional clause. Example:

If A= 0 And B=1 Then    ' ...End If ' A= 0 and B=1 

Now put your cursor in the blank line and continue programming. Do this for every pair and you will wind up with our example above looking like this:

If A= 0 and B=1 then    ' ... (several lines of code)    If C < 0 and D > 0 then        ' ... (several more lines of code)        Do While E > 0            ' ... (several more lines of code)            If F <> G or H < I then                Do While J < 0                    ' ... (several more lines of code,                     '      with several more If-EndIf blocks)                Loop 'While J < 0                ' ... (several more lines of code)            End If ' F <> G or H < I             ' ... (several more lines of code)        Loop ' while E > 0        ' ... (several more lines of code)    End If ' C < 0 and D > 0     ' ... (several lines of code)End If ' A= 0 and B=1 

Of course you can use the same approach with other VB code blocks, such as For-Next loops and Select Case clauses.

Our code is now much easier to maintain. You will be quite happy you did this when you re-visit the code in six months, and believe me, the person who takes your place when you leave will bless you in their prayers at night!

Share the Post:
Heading photo, Metadata.

What is Metadata?

What is metadata? Well, It’s an odd concept to wrap your head around. Metadata is essentially the secondary layer of data that tracks details about the “regular” data. The regular

XDR solutions

The Benefits of Using XDR Solutions

Cybercriminals constantly adapt their strategies, developing newer, more powerful, and intelligent ways to attack your network. Since security professionals must innovate as well, more conventional endpoint detection solutions have evolved

AI is revolutionizing fraud detection

How AI is Revolutionizing Fraud Detection

Artificial intelligence – commonly known as AI – means a form of technology with multiple uses. As a result, it has become extremely valuable to a number of businesses across

AI innovation

Companies Leading AI Innovation in 2023

Artificial intelligence (AI) has been transforming industries and revolutionizing business operations. AI’s potential to enhance efficiency and productivity has become crucial to many businesses. As we move into 2023, several

data fivetran pricing

Fivetran Pricing Explained

One of the biggest trends of the 21st century is the massive surge in analytics. Analytics is the process of utilizing data to drive future decision-making. With so much of

kubernetes logging

Kubernetes Logging: What You Need to Know

Kubernetes from Google is one of the most popular open-source and free container management solutions made to make managing and deploying applications easier. It has a solid architecture that makes