Code Annotation

way2bord

Registered User.
Local time
Today, 07:15
Joined
Feb 8, 2013
Messages
177
Below is code to adjust file attributes.

Code:
    [COLOR=blue]Public[/COLOR] [COLOR=blue]Shared[/COLOR] [COLOR=blue]Sub[/COLOR] Main()
        [COLOR=blue]Dim[/COLOR] path [COLOR=blue]As[/COLOR] [COLOR=blue]String[/COLOR] = [COLOR=#a31515]"c:\temp\MyTest.txt"[/COLOR] 
        [COLOR=green]' Create the file if it exists. [/COLOR]
        [COLOR=blue]If[/COLOR] File.Exists(path) = [COLOR=blue]False[/COLOR] [COLOR=blue]Then[/COLOR]
            File.Create(path)
        [COLOR=blue]End[/COLOR] [COLOR=blue]If[/COLOR] 
 
        [COLOR=blue]Dim[/COLOR] attributes [COLOR=blue]As[/COLOR] FileAttributes
        attributes = File.GetAttributes(path)
 
       [U][COLOR=blue]If[/COLOR] (attributes [COLOR=blue]And[/COLOR] FileAttributes.Hidden) = FileAttributes.Hidden [COLOR=blue]Then[/COLOR][/U] [U]' FA.Hidden = FA.Hidden...? Is this necessary?[/U]
            [COLOR=green]' Show the file.[/COLOR]
            attributes = RemoveAttribute(attributes, FileAttributes.Hidden)
            File.SetAttributes(path, attributes)
            Console.WriteLine([COLOR=#a31515]"The {0} file is no longer hidden."[/COLOR], path)
        [COLOR=blue]Else[/COLOR] 
            [COLOR=green]' Hide the file.[/COLOR]
            File.SetAttributes(path, [U]File.GetAttributes(path) [COLOR=blue]Or[/COLOR] FileAttributes.Hidden[/U]) [U]'...how is this setting both existing attributes and a new hidden attribute?[/U]
            Console.WriteLine([COLOR=#a31515]"The {0} file is now hidden."[/COLOR], path)
        [COLOR=blue]End[/COLOR] [COLOR=blue]If[/COLOR] 
    [COLOR=blue]End[/COLOR] [COLOR=blue]Sub[/COLOR] 
 
    [COLOR=blue]Public[/COLOR] [COLOR=blue]Shared[/COLOR] [COLOR=blue]Function[/COLOR] RemoveAttribute([COLOR=blue]ByVal[/COLOR] attributes [COLOR=blue]As[/COLOR] FileAttributes, [COLOR=blue]ByVal[/COLOR] attributesToRemove [COLOR=blue]As[/COLOR] FileAttributes) [COLOR=blue]As[/COLOR] FileAttributes
        [COLOR=blue]Return[/COLOR] attributes [COLOR=blue]And[/COLOR] ([COLOR=blue]Not[/COLOR] attributesToRemove) ' [U]??? what is this returning?[/U]
    [COLOR=blue]End[/COLOR] [COLOR=blue]Function[/COLOR]

Can someone annotate this code :( I've read it over a few times and I still can't figure out why the underlined isn't redundant...
 
Search for "bitwise math" or "bit math" or "bitwise operators" for more info.

In bitwise math you can use integers as if they were binary numbers to store multiple values in a single number, which is how windows file attributes work. ReadOnly is 1, Hidden is 2, System file is 4, Directory is 16, Archive is 32, but notice how each of these is a power of 2, so in binary, those numbers are ReadOnly 1, Hidden 10, System 100, Directory 10000 and archive 100000. So a file can have many attributes defined in the single number.

To add binary numbers together we use OR, so a file that is a readonly hidden system file is . . .
Code:
   0001 --> read only
or 0010 --> hidden
or 0100 --> system
   ====
   0111 --> all three values stacked in a single integer.
. . . and as an integer that would be 1 + 2 + 4 = 7. Now, to see if a particular position is set we AND the numbers together with a known setting, and if the item is set the result will be the known setting, which makes more sense if you look at the math . . .
Code:
    0111 --> original number
and 0010 --> testing for
    ====
    0010 --> result same as testing for, then the item is set.
. . . as opposed to the case where the item is not set . . .
Code:
    0101 --> original number
and 0010 --> testing for
    ====
    0000 --> result not what we were testing for, item is not set.
Does that make sense? But bitwise math. There's tons more info on the internets.
 
Can someone annotate this code?
Certainly…”It is not VBA.”

A second annotation:
We need to be careful when asking questions.
>> I've read it over a few times and I still can't figure out why the underlined isn't redundant...<<
The point is that some of the underlined is redundant and some of it isn’t.
For example: the comments are redundant but the code they comment on is not.
By definition then, if the underlined was redundant then it should be removable.
If the underlined is removed then the remainder of the code would fail.
If the remainder of the code fails then the removed was not redundant.
Ergo: The underlined is not redundant.


Another point:
You spent some time on presenting the code, and thank you for that, but the logic of the presentation got lost in the comments. That is a common fault with comments. Some people go through a stage of over commenting code and it can become a nightmare to both read and maintain.

For what it is worth, I would suggest keeping copy/pasted code completely separate from our opinion of that code. In other words, copy and paste the code verbatim into a code box and comment on that code externally to that code box. The reason for that is to isolate our opinion of the code from the actual code.

Chris.
 
Can someone annotate this code?
Certainly…”It is not VBA.”

A second annotation:
We need to be careful when asking questions.
>> I've read it over a few times and I still can't figure out why the underlined isn't redundant...<<
The point is that some of the underlined is redundant and some of it isn’t.
For example: the comments are redundant but the code they comment on is not.
By definition then, if the underlined was redundant then it should be removable.
If the underlined is removed then the remainder of the code would fail.
If the remainder of the code fails then the removed was not redundant.
Ergo: The underlined is not redundant.


Another point:
You spent some time on presenting the code, and thank you for that, but the logic of the presentation got lost in the comments. That is a common fault with comments. Some people go through a stage of over commenting code and it can become a nightmare to both read and maintain.

For what it is worth, I would suggest keeping copy/pasted code completely separate from our opinion of that code. In other words, copy and paste the code verbatim into a code box and comment on that code externally to that code box. The reason for that is to isolate our opinion of the code from the actual code.

Chris.

1. vb.net/vb6/vba are frequently interchangeable with a slight modification of object declarations. If I can't find what I'm looking for in strict VBA, converting from one of the other languages is a simple solution. I've found the range of talents on this board to be impressive and did not think the question inappropriate or outside their expertise.

2. The english language can certainly be ambiguous and I can be more specific when explaining the purpose behind underlining sections of the code. Perhaps I should have said, "These lines include my commented out questions and the aspect of the code I am having trouble interpreting." I did not believe there would considerable confusion. Also, the first post was spot on.

3. I can see it's clearly a pet peeve of yours when people include questions directly in a code block, even if appropriately commented out; I will take this into consideration in the future.

For what it is worth, your entire post could be summed up in a single line:
I would suggest keeping copy/pasted code completely separate from our opinion of that code. In other words, copy and paste the code verbatim into a code box and comment on that code externally to that code box. The reason for that is to isolate our opinion of the code from the actual code.

Note: This suggestion, while possibly good, is not relevantly on topic with OP. Also, please use QUOTE blocks when quoting other posters. It will add clarity to your posts.
 

Users who are viewing this thread

Back
Top Bottom