473,834 Members | 1,873 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1314
Do you have a question?
"Marian F." <No****@NoSpam. Net> wrote in message
news:uf******** ******@tk2msftn gp13.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*********@REM OVETHISTEXTmetr omilwaukee.com
URL http://www.metromilwaukee.com/clintongallagher/

"Marian F." <No****@NoSpam. Net> wrote in message
news:uf******** ******@tk2msftn gp13.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******** ******@tk2msftn gp13.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******** ******@tk2msftn gp13.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******** ******@tk2msftn gp13.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*********@REM OVETHISTEXTmetr omilwaukee.com
URL http://www.metromilwaukee.com/clintongallagher/

"Marian F." <No****@NoSpam. Net> wrote in message
news:uf******** ******@tk2msftn gp13.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******** ******@tk2msftn gp13.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******** ******@tk2msftn gp13.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.Reg ularExpressions .Regex("\w+")
Return regex.Matches(i nput).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>Functi on to count words in a string</summary>
''' <param name="input">st ring 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******** ******@tk2msftn gp13.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

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

Similar topics

383
12291
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 poor man's DBMS, a broken email server and various other /application/ servers to try and crack the Internet and IS markets. In the case where they didn't spend their own money to get companies to
46
2505
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 working, you'll comment it.
8
1867
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
1089
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 info on whether documentation generation is supported with VB 2005 using xml comments. Thanks, Ron
100
4777
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 use comments. Thanks in advance! -- http://www.gotw.ca/resources/clcm.htm for info about ]
0
9799
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9646
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10793
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10510
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10548
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7758
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5627
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
3978
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3081
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.