473,788 Members | 2,800 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
18 1309
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 #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(By Val 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******** ******@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 #14
Commenting is important,
I know Dutch, and here is MY function:

Public Function GetWordCount(By Val 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******** ******@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 #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*********@REM OVETHISTEXTmetr omilwaukee.com
URL http://www.metromilwaukee.com/clintongallagher/
"Brian Henry" <br**********@n ewsgroups.nospa m> wrote in message
news:uN******** ******@TK2MSFTN GP10.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******** ******@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 #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.Reg ularExpressions .Regex("\w+")
Return regex.Matches(i nput).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>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
...


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(By Val 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******** ******@TK2MSFTN GP12.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.Reg ularExpressions .Regex("\w+")
Return regex.Matches(i nput).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>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
...


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
12259
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
2496
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
1866
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
1086
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
4762
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
10364
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
10172
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
10110
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,...
0
9967
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8993
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7517
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
6750
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
4069
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3670
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.