473,320 Members | 1,902 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Commenting source code is for amateurs

The 12 years old genius function to count english words in a sentence:

' This is my function to count english words in your string
' s is the string with your words to be counted
' Returns an integer as the number of words found
Public Function iCW(ByVal s As String) As Integer
' We start declaring the variable to count words
Dim c As Integer
' This is the variable used in the for next loop
' to check every char in your string s
Dim i As Integer
' Now check all chars in your string s
' Starts with the 1st char, i = 1
' up to last char, s.Length
For i = 1 To s.Length
' check is the char is a space
If Mid(s, i, 1) = " " Then
' if it is a space, increment the word count WordCount
c += 1
' if it is not, then do nothing
End If
' continue to check words until no more
Next
' return the word count, c
' after adding 1 to compensate for the word before the first space
Return c + 1
End Function
The semi-pro solution to count english words in a sentence:

Public Function WordCount(ByVal Text As String) As Long
While InStr(Text, " ") ' remove double spaces
Replace(Text, " ", " ")
End While
If Replace(Text, " ", "") = "" Then Return 0 ' only spaces
Dim WordCount As Long
For i = 1 To Len(Text)
If Mid(s, i, 1) = " " Then WordCount += 1
Next
Return WordCount + 1
End Function

The pro solution is the same as the semi-pro but without comments

MF

Nov 21 '05 #1
18 1287
Do you have a question?
"Marian F." <No****@NoSpam.Net> wrote in message
news:uf**************@tk2msftngp13.phx.gbl...
The 12 years old genius function to count english words in a sentence:

' This is my function to count english words in your string
' s is the string with your words to be counted
' Returns an integer as the number of words found
Public Function iCW(ByVal s As String) As Integer
' We start declaring the variable to count words
Dim c As Integer
' This is the variable used in the for next loop
' to check every char in your string s
Dim i As Integer
' Now check all chars in your string s
' Starts with the 1st char, i = 1
' up to last char, s.Length
For i = 1 To s.Length
' check is the char is a space
If Mid(s, i, 1) = " " Then
' if it is a space, increment the word count WordCount
c += 1
' if it is not, then do nothing
End If
' continue to check words until no more
Next
' return the word count, c
' after adding 1 to compensate for the word before the first space
Return c + 1
End Function
The semi-pro solution to count english words in a sentence:

Public Function WordCount(ByVal Text As String) As Long
While InStr(Text, " ") ' remove double spaces
Replace(Text, " ", " ")
End While
If Replace(Text, " ", "") = "" Then Return 0 ' only spaces
Dim WordCount As Long
For i = 1 To Len(Text)
If Mid(s, i, 1) = " " Then WordCount += 1
Next
Return WordCount + 1
End Function

The pro solution is the same as the semi-pro but without comments

MF

Nov 21 '05 #2
I thank the pot for calling the kettle black as doing so makes
it perfectly clear that only 'professional' sissies use Visual Basic.

--
<%= Clinton Gallagher, "Twice the Results -- Half the Cost"
Architectural & e-Business Consulting -- Software Development
NET cs*********@REMOVETHISTEXTmetromilwaukee.com
URL http://www.metromilwaukee.com/clintongallagher/

"Marian F." <No****@NoSpam.Net> wrote in message
news:uf**************@tk2msftngp13.phx.gbl...
The 12 years old genius function to count english words in a sentence:

' This is my function to count english words in your string
' s is the string with your words to be counted
' Returns an integer as the number of words found
Public Function iCW(ByVal s As String) As Integer
' We start declaring the variable to count words
Dim c As Integer
' This is the variable used in the for next loop
' to check every char in your string s
Dim i As Integer
' Now check all chars in your string s
' Starts with the 1st char, i = 1
' up to last char, s.Length
For i = 1 To s.Length
' check is the char is a space
If Mid(s, i, 1) = " " Then
' if it is a space, increment the word count WordCount
c += 1
' if it is not, then do nothing
End If
' continue to check words until no more
Next
' return the word count, c
' after adding 1 to compensate for the word before the first space
Return c + 1
End Function
The semi-pro solution to count english words in a sentence:

Public Function WordCount(ByVal Text As String) As Long
While InStr(Text, " ") ' remove double spaces
Replace(Text, " ", " ")
End While
If Replace(Text, " ", "") = "" Then Return 0 ' only spaces
Dim WordCount As Long
For i = 1 To Len(Text)
If Mid(s, i, 1) = " " Then WordCount += 1
Next
Return WordCount + 1
End Function

The pro solution is the same as the semi-pro but without comments

MF


Nov 21 '05 #3
Bad news Marian - Pros comment their code.

Maybe you should have commented which version was which, then you wouldn't
have mixed up semi-pro and pro versions in your final statement.

"Marian F." <No****@NoSpam.Net> wrote in message
news:uf**************@tk2msftngp13.phx.gbl...
The 12 years old genius function to count english words in a sentence:

' This is my function to count english words in your string
' s is the string with your words to be counted
' Returns an integer as the number of words found
Public Function iCW(ByVal s As String) As Integer
' We start declaring the variable to count words
Dim c As Integer
' This is the variable used in the for next loop
' to check every char in your string s
Dim i As Integer
' Now check all chars in your string s
' Starts with the 1st char, i = 1
' up to last char, s.Length
For i = 1 To s.Length
' check is the char is a space
If Mid(s, i, 1) = " " Then
' if it is a space, increment the word count WordCount
c += 1
' if it is not, then do nothing
End If
' continue to check words until no more
Next
' return the word count, c
' after adding 1 to compensate for the word before the first space
Return c + 1
End Function
The semi-pro solution to count english words in a sentence:

Public Function WordCount(ByVal Text As String) As Long
While InStr(Text, " ") ' remove double spaces
Replace(Text, " ", " ")
End While
If Replace(Text, " ", "") = "" Then Return 0 ' only spaces
Dim WordCount As Long
For i = 1 To Len(Text)
If Mid(s, i, 1) = " " Then WordCount += 1
Next
Return WordCount + 1
End Function

The pro solution is the same as the semi-pro but without comments

MF

Nov 21 '05 #4
Comments and massive documentation are done in professional software
development... if you don't explain what a function does in comments and
documentation how will you remember what it does after writing another 400
functions? most likely you will forget what 80% of them do by that point and
will need some kind of reference.. and when someone else starts working on
your code also in a team they definatly need reference points to look at
such as comments, markers, and documentation to even begin to understand
what is going on in code especially on a massive scale like 100,000+ lines
of code... so saying comments are useless, pointless, or anything else
similar to that is just stupid. people may think they are "1337" just
because they can read code without comments, but if you had a 100 line
function and had no clue what it did uncommented would you want to step
through it line by line to determine exactly what it does, what it effects
and what it may return? I doubt you would want to waste that time.
"Marian F." <No****@NoSpam.Net> wrote in message
news:uf**************@tk2msftngp13.phx.gbl...
The 12 years old genius function to count english words in a sentence:

' This is my function to count english words in your string
' s is the string with your words to be counted
' Returns an integer as the number of words found
Public Function iCW(ByVal s As String) As Integer
' We start declaring the variable to count words
Dim c As Integer
' This is the variable used in the for next loop
' to check every char in your string s
Dim i As Integer
' Now check all chars in your string s
' Starts with the 1st char, i = 1
' up to last char, s.Length
For i = 1 To s.Length
' check is the char is a space
If Mid(s, i, 1) = " " Then
' if it is a space, increment the word count WordCount
c += 1
' if it is not, then do nothing
End If
' continue to check words until no more
Next
' return the word count, c
' after adding 1 to compensate for the word before the first space
Return c + 1
End Function
The semi-pro solution to count english words in a sentence:

Public Function WordCount(ByVal Text As String) As Long
While InStr(Text, " ") ' remove double spaces
Replace(Text, " ", " ")
End While
If Replace(Text, " ", "") = "" Then Return 0 ' only spaces
Dim WordCount As Long
For i = 1 To Len(Text)
If Mid(s, i, 1) = " " Then WordCount += 1
Next
Return WordCount + 1
End Function

The pro solution is the same as the semi-pro but without comments

MF

Nov 21 '05 #5
Do you have a question?
"Marian F." <No****@NoSpam.Net> wrote in message
news:uf**************@tk2msftngp13.phx.gbl...
The 12 years old genius function to count english words in a sentence:

' This is my function to count english words in your string
' s is the string with your words to be counted
' Returns an integer as the number of words found
Public Function iCW(ByVal s As String) As Integer
' We start declaring the variable to count words
Dim c As Integer
' This is the variable used in the for next loop
' to check every char in your string s
Dim i As Integer
' Now check all chars in your string s
' Starts with the 1st char, i = 1
' up to last char, s.Length
For i = 1 To s.Length
' check is the char is a space
If Mid(s, i, 1) = " " Then
' if it is a space, increment the word count WordCount
c += 1
' if it is not, then do nothing
End If
' continue to check words until no more
Next
' return the word count, c
' after adding 1 to compensate for the word before the first space
Return c + 1
End Function
The semi-pro solution to count english words in a sentence:

Public Function WordCount(ByVal Text As String) As Long
While InStr(Text, " ") ' remove double spaces
Replace(Text, " ", " ")
End While
If Replace(Text, " ", "") = "" Then Return 0 ' only spaces
Dim WordCount As Long
For i = 1 To Len(Text)
If Mid(s, i, 1) = " " Then WordCount += 1
Next
Return WordCount + 1
End Function

The pro solution is the same as the semi-pro but without comments

MF

Nov 21 '05 #6
I thank the pot for calling the kettle black as doing so makes
it perfectly clear that only 'professional' sissies use Visual Basic.

--
<%= Clinton Gallagher, "Twice the Results -- Half the Cost"
Architectural & e-Business Consulting -- Software Development
NET cs*********@REMOVETHISTEXTmetromilwaukee.com
URL http://www.metromilwaukee.com/clintongallagher/

"Marian F." <No****@NoSpam.Net> wrote in message
news:uf**************@tk2msftngp13.phx.gbl...
The 12 years old genius function to count english words in a sentence:

' This is my function to count english words in your string
' s is the string with your words to be counted
' Returns an integer as the number of words found
Public Function iCW(ByVal s As String) As Integer
' We start declaring the variable to count words
Dim c As Integer
' This is the variable used in the for next loop
' to check every char in your string s
Dim i As Integer
' Now check all chars in your string s
' Starts with the 1st char, i = 1
' up to last char, s.Length
For i = 1 To s.Length
' check is the char is a space
If Mid(s, i, 1) = " " Then
' if it is a space, increment the word count WordCount
c += 1
' if it is not, then do nothing
End If
' continue to check words until no more
Next
' return the word count, c
' after adding 1 to compensate for the word before the first space
Return c + 1
End Function
The semi-pro solution to count english words in a sentence:

Public Function WordCount(ByVal Text As String) As Long
While InStr(Text, " ") ' remove double spaces
Replace(Text, " ", " ")
End While
If Replace(Text, " ", "") = "" Then Return 0 ' only spaces
Dim WordCount As Long
For i = 1 To Len(Text)
If Mid(s, i, 1) = " " Then WordCount += 1
Next
Return WordCount + 1
End Function

The pro solution is the same as the semi-pro but without comments

MF


Nov 21 '05 #7
Bad news Marian - Pros comment their code.

Maybe you should have commented which version was which, then you wouldn't
have mixed up semi-pro and pro versions in your final statement.

"Marian F." <No****@NoSpam.Net> wrote in message
news:uf**************@tk2msftngp13.phx.gbl...
The 12 years old genius function to count english words in a sentence:

' This is my function to count english words in your string
' s is the string with your words to be counted
' Returns an integer as the number of words found
Public Function iCW(ByVal s As String) As Integer
' We start declaring the variable to count words
Dim c As Integer
' This is the variable used in the for next loop
' to check every char in your string s
Dim i As Integer
' Now check all chars in your string s
' Starts with the 1st char, i = 1
' up to last char, s.Length
For i = 1 To s.Length
' check is the char is a space
If Mid(s, i, 1) = " " Then
' if it is a space, increment the word count WordCount
c += 1
' if it is not, then do nothing
End If
' continue to check words until no more
Next
' return the word count, c
' after adding 1 to compensate for the word before the first space
Return c + 1
End Function
The semi-pro solution to count english words in a sentence:

Public Function WordCount(ByVal Text As String) As Long
While InStr(Text, " ") ' remove double spaces
Replace(Text, " ", " ")
End While
If Replace(Text, " ", "") = "" Then Return 0 ' only spaces
Dim WordCount As Long
For i = 1 To Len(Text)
If Mid(s, i, 1) = " " Then WordCount += 1
Next
Return WordCount + 1
End Function

The pro solution is the same as the semi-pro but without comments

MF

Nov 21 '05 #8
Comments and massive documentation are done in professional software
development... if you don't explain what a function does in comments and
documentation how will you remember what it does after writing another 400
functions? most likely you will forget what 80% of them do by that point and
will need some kind of reference.. and when someone else starts working on
your code also in a team they definatly need reference points to look at
such as comments, markers, and documentation to even begin to understand
what is going on in code especially on a massive scale like 100,000+ lines
of code... so saying comments are useless, pointless, or anything else
similar to that is just stupid. people may think they are "1337" just
because they can read code without comments, but if you had a 100 line
function and had no clue what it did uncommented would you want to step
through it line by line to determine exactly what it does, what it effects
and what it may return? I doubt you would want to waste that time.
"Marian F." <No****@NoSpam.Net> wrote in message
news:uf**************@tk2msftngp13.phx.gbl...
The 12 years old genius function to count english words in a sentence:

' This is my function to count english words in your string
' s is the string with your words to be counted
' Returns an integer as the number of words found
Public Function iCW(ByVal s As String) As Integer
' We start declaring the variable to count words
Dim c As Integer
' This is the variable used in the for next loop
' to check every char in your string s
Dim i As Integer
' Now check all chars in your string s
' Starts with the 1st char, i = 1
' up to last char, s.Length
For i = 1 To s.Length
' check is the char is a space
If Mid(s, i, 1) = " " Then
' if it is a space, increment the word count WordCount
c += 1
' if it is not, then do nothing
End If
' continue to check words until no more
Next
' return the word count, c
' after adding 1 to compensate for the word before the first space
Return c + 1
End Function
The semi-pro solution to count english words in a sentence:

Public Function WordCount(ByVal Text As String) As Long
While InStr(Text, " ") ' remove double spaces
Replace(Text, " ", " ")
End While
If Replace(Text, " ", "") = "" Then Return 0 ' only spaces
Dim WordCount As Long
For i = 1 To Len(Text)
If Mid(s, i, 1) = " " Then WordCount += 1
Next
Return WordCount + 1
End Function

The pro solution is the same as the semi-pro but without comments

MF

Nov 21 '05 #9
Marian,
The pro solution is the same as the semi-pro but without comments
What would this solution be?

'
' Function to count words in a string
'
' Parameters:
' input - string containing words to be counted
'
' Returns:
' The number of words in the string
'
Private Shared Function WordCount(ByVal input As String) As Integer
Static regex As New System.Text.RegularExpressions.Regex("\w+")
Return regex.Matches(input).Count
End Function
Alternatively I would consider using String.Split, Strings.Split, or
RegEx.Split to split the input into "words" then count the number of
elements returned, especially if there was other processing to do on the
words. Note in VS.NET 2005 (aka Whidbey, due out later in 2005) The comments
preceding the function would be in XML Documentation format so as to show up
in the Object Browser & intellisense.

Something like:

''' <summary>Function to count words in a string</summary>
''' <param name="input">string containing words to be counted</param>
''' <returns>The number of words in the string</returns>
''' <remarks>
''' Sample function to return the number of words in a string.
''' </remarks>
Private Shared Function WordCount(ByVal input As String) As Integer
...

Hope this helps
Jay
"Marian F." <No****@NoSpam.Net> wrote in message
news:uf**************@tk2msftngp13.phx.gbl... The 12 years old genius function to count english words in a sentence:

' This is my function to count english words in your string
' s is the string with your words to be counted
' Returns an integer as the number of words found
Public Function iCW(ByVal s As String) As Integer
' We start declaring the variable to count words
Dim c As Integer
' This is the variable used in the for next loop
' to check every char in your string s
Dim i As Integer
' Now check all chars in your string s
' Starts with the 1st char, i = 1
' up to last char, s.Length
For i = 1 To s.Length
' check is the char is a space
If Mid(s, i, 1) = " " Then
' if it is a space, increment the word count WordCount
c += 1
' if it is not, then do nothing
End If
' continue to check words until no more
Next
' return the word count, c
' after adding 1 to compensate for the word before the first space
Return c + 1
End Function
The semi-pro solution to count english words in a sentence:

Public Function WordCount(ByVal Text As String) As Long
While InStr(Text, " ") ' remove double spaces
Replace(Text, " ", " ")
End While
If Replace(Text, " ", "") = "" Then Return 0 ' only spaces
Dim WordCount As Long
For i = 1 To Len(Text)
If Mid(s, i, 1) = " " Then WordCount += 1
Next
Return WordCount + 1
End Function

The pro solution is the same as the semi-pro but without comments

MF

Nov 21 '05 #10
Marian,
The pro solution is the same as the semi-pro but without comments
What would this solution be?

'
' Function to count words in a string
'
' Parameters:
' input - string containing words to be counted
'
' Returns:
' The number of words in the string
'
Private Shared Function WordCount(ByVal input As String) As Integer
Static regex As New System.Text.RegularExpressions.Regex("\w+")
Return regex.Matches(input).Count
End Function
Alternatively I would consider using String.Split, Strings.Split, or
RegEx.Split to split the input into "words" then count the number of
elements returned, especially if there was other processing to do on the
words. Note in VS.NET 2005 (aka Whidbey, due out later in 2005) The comments
preceding the function would be in XML Documentation format so as to show up
in the Object Browser & intellisense.

Something like:

''' <summary>Function to count words in a string</summary>
''' <param name="input">string containing words to be counted</param>
''' <returns>The number of words in the string</returns>
''' <remarks>
''' Sample function to return the number of words in a string.
''' </remarks>
Private Shared Function WordCount(ByVal input As String) As Integer
...

Hope this helps
Jay
"Marian F." <No****@NoSpam.Net> wrote in message
news:uf**************@tk2msftngp13.phx.gbl... The 12 years old genius function to count english words in a sentence:

' This is my function to count english words in your string
' s is the string with your words to be counted
' Returns an integer as the number of words found
Public Function iCW(ByVal s As String) As Integer
' We start declaring the variable to count words
Dim c As Integer
' This is the variable used in the for next loop
' to check every char in your string s
Dim i As Integer
' Now check all chars in your string s
' Starts with the 1st char, i = 1
' up to last char, s.Length
For i = 1 To s.Length
' check is the char is a space
If Mid(s, i, 1) = " " Then
' if it is a space, increment the word count WordCount
c += 1
' if it is not, then do nothing
End If
' continue to check words until no more
Next
' return the word count, c
' after adding 1 to compensate for the word before the first space
Return c + 1
End Function
The semi-pro solution to count english words in a sentence:

Public Function WordCount(ByVal Text As String) As Long
While InStr(Text, " ") ' remove double spaces
Replace(Text, " ", " ")
End While
If Replace(Text, " ", "") = "" Then Return 0 ' only spaces
Dim WordCount As Long
For i = 1 To Len(Text)
If Mid(s, i, 1) = " " Then WordCount += 1
Next
Return WordCount + 1
End Function

The pro solution is the same as the semi-pro but without comments

MF

Nov 21 '05 #11
Marian,

A comment or a lesson has always to be on the knowledge of the one who reads
it or who get the lessons, learning writing Dutch to a child of 6 is
normally in my country no problem, that will be a different situation in
most other countries in the world.

Making the books will be the same with that, you can not make the books for
people who are learning Dutch on university the same as for kids from 6 in
Holland or Belgium, and you cannot even use those books in Suriname (South
America) for kids in that culture.

That is the same with commenting, there is a difference when you are
commenting for professionals or do that how to learn to write a program. For
the first ones is in my opinion the less commenting the better choice, than
they know there is something special happening when there is a comment

Just my thought,

Cor

"Marian F." <No****@NoSpam.Net>
The 12 years old genius function to count english words in a sentence:

' This is my function to count english words in your string
' s is the string with your words to be counted
' Returns an integer as the number of words found
Public Function iCW(ByVal s As String) As Integer
' We start declaring the variable to count words
Dim c As Integer
' This is the variable used in the for next loop
' to check every char in your string s
Dim i As Integer
' Now check all chars in your string s
' Starts with the 1st char, i = 1
' up to last char, s.Length
For i = 1 To s.Length
' check is the char is a space
If Mid(s, i, 1) = " " Then
' if it is a space, increment the word count WordCount
c += 1
' if it is not, then do nothing
End If
' continue to check words until no more
Next
' return the word count, c
' after adding 1 to compensate for the word before the first space
Return c + 1
End Function
The semi-pro solution to count english words in a sentence:

Public Function WordCount(ByVal Text As String) As Long
While InStr(Text, " ") ' remove double spaces
Replace(Text, " ", " ")
End While
If Replace(Text, " ", "") = "" Then Return 0 ' only spaces
Dim WordCount As Long
For i = 1 To Len(Text)
If Mid(s, i, 1) = " " Then WordCount += 1
Next
Return WordCount + 1
End Function

The pro solution is the same as the semi-pro but without comments

MF

Nov 21 '05 #12
Marian,

A comment or a lesson has always to be on the knowledge of the one who reads
it or who get the lessons, learning writing Dutch to a child of 6 is
normally in my country no problem, that will be a different situation in
most other countries in the world.

Making the books will be the same with that, you can not make the books for
people who are learning Dutch on university the same as for kids from 6 in
Holland or Belgium, and you cannot even use those books in Suriname (South
America) for kids in that culture.

That is the same with commenting, there is a difference when you are
commenting for professionals or do that how to learn to write a program. For
the first ones is in my opinion the less commenting the better choice, than
they know there is something special happening when there is a comment

Just my thought,

Cor

"Marian F." <No****@NoSpam.Net>
The 12 years old genius function to count english words in a sentence:

' This is my function to count english words in your string
' s is the string with your words to be counted
' Returns an integer as the number of words found
Public Function iCW(ByVal s As String) As Integer
' We start declaring the variable to count words
Dim c As Integer
' This is the variable used in the for next loop
' to check every char in your string s
Dim i As Integer
' Now check all chars in your string s
' Starts with the 1st char, i = 1
' up to last char, s.Length
For i = 1 To s.Length
' check is the char is a space
If Mid(s, i, 1) = " " Then
' if it is a space, increment the word count WordCount
c += 1
' if it is not, then do nothing
End If
' continue to check words until no more
Next
' return the word count, c
' after adding 1 to compensate for the word before the first space
Return c + 1
End Function
The semi-pro solution to count english words in a sentence:

Public Function WordCount(ByVal Text As String) As Long
While InStr(Text, " ") ' remove double spaces
Replace(Text, " ", " ")
End While
If Replace(Text, " ", "") = "" Then Return 0 ' only spaces
Dim WordCount As Long
For i = 1 To Len(Text)
If Mid(s, i, 1) = " " Then WordCount += 1
Next
Return WordCount + 1
End Function

The pro solution is the same as the semi-pro but without comments

MF

Nov 21 '05 #13
Commenting is important,
I know Dutch, and here is MY function:

Public Function GetWordCount(ByVal sText As String) As Long
sText = Trim(sText)
While InStr(sText, " ")
Replace(sText, " ", " ")
End While
Return Split(sText, " ").Length 'create array, count elements (words).
End Function

Note that the name of the function will make clear what it does. Less and
easier to understand code, and the complicated (short handed) part is
commented.
Leo Muller
"Marian F." <No****@NoSpam.Net> wrote in message
news:uf**************@tk2msftngp13.phx.gbl...
The 12 years old genius function to count english words in a sentence:

' This is my function to count english words in your string
' s is the string with your words to be counted
' Returns an integer as the number of words found
Public Function iCW(ByVal s As String) As Integer
' We start declaring the variable to count words
Dim c As Integer
' This is the variable used in the for next loop
' to check every char in your string s
Dim i As Integer
' Now check all chars in your string s
' Starts with the 1st char, i = 1
' up to last char, s.Length
For i = 1 To s.Length
' check is the char is a space
If Mid(s, i, 1) = " " Then
' if it is a space, increment the word count WordCount
c += 1
' if it is not, then do nothing
End If
' continue to check words until no more
Next
' return the word count, c
' after adding 1 to compensate for the word before the first space
Return c + 1
End Function
The semi-pro solution to count english words in a sentence:

Public Function WordCount(ByVal Text As String) As Long
While InStr(Text, " ") ' remove double spaces
Replace(Text, " ", " ")
End While
If Replace(Text, " ", "") = "" Then Return 0 ' only spaces
Dim WordCount As Long
For i = 1 To Len(Text)
If Mid(s, i, 1) = " " Then WordCount += 1
Next
Return WordCount + 1
End Function

The pro solution is the same as the semi-pro but without comments

MF

Nov 21 '05 #14
Commenting is important,
I know Dutch, and here is MY function:

Public Function GetWordCount(ByVal sText As String) As Long
sText = Trim(sText)
While InStr(sText, " ")
Replace(sText, " ", " ")
End While
Return Split(sText, " ").Length 'create array, count elements (words).
End Function

Note that the name of the function will make clear what it does. Less and
easier to understand code, and the complicated (short handed) part is
commented.
Leo Muller
"Marian F." <No****@NoSpam.Net> wrote in message
news:uf**************@tk2msftngp13.phx.gbl...
The 12 years old genius function to count english words in a sentence:

' This is my function to count english words in your string
' s is the string with your words to be counted
' Returns an integer as the number of words found
Public Function iCW(ByVal s As String) As Integer
' We start declaring the variable to count words
Dim c As Integer
' This is the variable used in the for next loop
' to check every char in your string s
Dim i As Integer
' Now check all chars in your string s
' Starts with the 1st char, i = 1
' up to last char, s.Length
For i = 1 To s.Length
' check is the char is a space
If Mid(s, i, 1) = " " Then
' if it is a space, increment the word count WordCount
c += 1
' if it is not, then do nothing
End If
' continue to check words until no more
Next
' return the word count, c
' after adding 1 to compensate for the word before the first space
Return c + 1
End Function
The semi-pro solution to count english words in a sentence:

Public Function WordCount(ByVal Text As String) As Long
While InStr(Text, " ") ' remove double spaces
Replace(Text, " ", " ")
End While
If Replace(Text, " ", "") = "" Then Return 0 ' only spaces
Dim WordCount As Long
For i = 1 To Len(Text)
If Mid(s, i, 1) = " " Then WordCount += 1
Next
Return WordCount + 1
End Function

The pro solution is the same as the semi-pro but without comments

MF

Nov 21 '05 #15
Professionals also use short paragraphs and white space when writing ;-)

--
<%= Clinton Gallagher, "Twice the Results -- Half the Cost"
Architectural & e-Business Consulting -- Software Development
NET cs*********@REMOVETHISTEXTmetromilwaukee.com
URL http://www.metromilwaukee.com/clintongallagher/
"Brian Henry" <br**********@newsgroups.nospam> wrote in message
news:uN**************@TK2MSFTNGP10.phx.gbl...
Comments and massive documentation are done in professional software
development... if you don't explain what a function does in comments and
documentation how will you remember what it does after writing another 400
functions? most likely you will forget what 80% of them do by that point and will need some kind of reference.. and when someone else starts working on
your code also in a team they definatly need reference points to look at
such as comments, markers, and documentation to even begin to understand
what is going on in code especially on a massive scale like 100,000+ lines
of code... so saying comments are useless, pointless, or anything else
similar to that is just stupid. people may think they are "1337" just
because they can read code without comments, but if you had a 100 line
function and had no clue what it did uncommented would you want to step
through it line by line to determine exactly what it does, what it effects
and what it may return? I doubt you would want to waste that time.
"Marian F." <No****@NoSpam.Net> wrote in message
news:uf**************@tk2msftngp13.phx.gbl...
The 12 years old genius function to count english words in a sentence:

' This is my function to count english words in your string
' s is the string with your words to be counted
' Returns an integer as the number of words found
Public Function iCW(ByVal s As String) As Integer
' We start declaring the variable to count words
Dim c As Integer
' This is the variable used in the for next loop
' to check every char in your string s
Dim i As Integer
' Now check all chars in your string s
' Starts with the 1st char, i = 1
' up to last char, s.Length
For i = 1 To s.Length
' check is the char is a space
If Mid(s, i, 1) = " " Then
' if it is a space, increment the word count WordCount
c += 1
' if it is not, then do nothing
End If
' continue to check words until no more
Next
' return the word count, c
' after adding 1 to compensate for the word before the first space
Return c + 1
End Function
The semi-pro solution to count english words in a sentence:

Public Function WordCount(ByVal Text As String) As Long
While InStr(Text, " ") ' remove double spaces
Replace(Text, " ", " ")
End While
If Replace(Text, " ", "") = "" Then Return 0 ' only spaces
Dim WordCount As Long
For i = 1 To Len(Text)
If Mid(s, i, 1) = " " Then WordCount += 1
Next
Return WordCount + 1
End Function

The pro solution is the same as the semi-pro but without comments

MF


Nov 21 '05 #16
Jay B. Harlow [MVP - Outlook] wrote:
What would this solution be?
A Phd solution from someone who is making a deep study of the framework.

The function name, parameter type and return type are enough to know
what does the function do.
'
' Function to count words in a string
'
' Parameters:
' input - string containing words to be counted
'
' Returns:
' The number of words in the string
'
Private Shared Function WordCount(ByVal input As String) As Integer
Static regex As New System.Text.RegularExpressions.Regex("\w+")
Return regex.Matches(input).Count
End Function
Note in VS.NET 2005 (aka Whidbey, due out later in 2005) The comments
preceding the function would be in XML Documentation format so as to show up
in the Object Browser & intellisense.

Something like:

''' <summary>Function to count words in a string</summary>
''' <param name="input">string containing words to be counted</param>
''' <returns>The number of words in the string</returns>
''' <remarks>
''' Sample function to return the number of words in a string.
''' </remarks>
Private Shared Function WordCount(ByVal input As String) As Integer
...


That's great :)

MF

Nov 21 '05 #17
Leo Muller wrote:
Commenting is important,
I know Dutch, and here is MY function:

Public Function GetWordCount(ByVal sText As String) As Long
sText = Trim(sText)
While InStr(sText, " ")
Replace(sText, " ", " ")
End While
Return Split(sText, " ").Length 'create array, count elements (words).
End Function

Note that the name of the function will make clear what it does. Less and
easier to understand code, and the complicated (short handed) part is
commented.

Agree

MF

Nov 21 '05 #18
Wow...

If you think that is a PHd solution I would hate to see what kind of CS
background you have...

See PERL.
"Marian F." <No****@NoSpam.Net> wrote in message
news:eo**************@TK2MSFTNGP12.phx.gbl...
Jay B. Harlow [MVP - Outlook] wrote:
What would this solution be?


A Phd solution from someone who is making a deep study of the framework.

The function name, parameter type and return type are enough to know
what does the function do.
'
' Function to count words in a string
'
' Parameters:
' input - string containing words to be counted
'
' Returns:
' The number of words in the string
'
Private Shared Function WordCount(ByVal input As String) As Integer
Static regex As New System.Text.RegularExpressions.Regex("\w+")
Return regex.Matches(input).Count
End Function
Note in VS.NET 2005 (aka Whidbey, due out later in 2005) The comments
preceding the function would be in XML Documentation format so as to show up in the Object Browser & intellisense.

Something like:

''' <summary>Function to count words in a string</summary>
''' <param name="input">string containing words to be counted</param> ''' <returns>The number of words in the string</returns>
''' <remarks>
''' Sample function to return the number of words in a string.
''' </remarks>
Private Shared Function WordCount(ByVal input As String) As Integer
...


That's great :)

MF

Nov 21 '05 #19

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

383
by: John Bailo | last post by:
The war of the OSes was won a long time ago. Unix has always been, and will continue to be, the Server OS in the form of Linux. Microsoft struggled mightily to win that battle -- creating a...
46
by: Profetas | last post by:
Hi, I know that this is off topic. but I didn't know where to post. Do you comment your source code while coding or after coding. for example: you write a procedure and after it is...
8
by: lallous | last post by:
Hello I've been programming for a number of years, however my commenting style is always different. Sometimes I use something like: /************************ * method .... * comments......
2
by: RYoung | last post by:
Can someone point me to a reference concerning source code commenting with VB 2005, ala C# commenting? I googled and found alot of links to add-ins and commercial products, but I can't find any...
100
by: Angel Tsankov | last post by:
Can someone recommend a good source of C/C++ coding style. Specifically, I am interested in commenting style and in particular how to indent comments and the commented code, rather than when to...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.