Commenting the method
At the minimum, public API methods should always be commented.
The posted code is an implementation.
You should have the method declaration,
serving as the interface definition,
somewhere else.
It's best when the actual implementation is not even visible to users of your API.
That's good information hiding.
So, where this method is declared,
you should add comments explaining what it does and what it returns.
It could also use a better name,
as recvln
is overly and pointlessly succinct instead of receive_line
.
Commenting inside the method
Probably like you,
I also don't like commenting.
I like when the code is readable by itself.
When I feel the need to add comments,
that's a sign that perhaps there are better ways to write the code.
Looking at this code,
although some of it is straightforward and easy to understand,
there are elements that are not clear at all.
Let's refactor it and see where that leads us.
Why are these declarations at the top?
int size, count;
char* result;
This was mandatory in C, but not in C++.
It's not ideal to declare things up front,
it's much more readable when variables are declared closer to where they are used,
and ideally initialized at the same time.
By moving the declarations further down and combining with the initialization,
the code becomes shorter,
and now more information becomes apparent and easily visible,
the purpose of these variables:
int size = recvBuffer.size();
char* result = new char[size+1];
In this code:
for (count = 0; count < size; count++)
if (recvBuffer.at(count) == '\n')
break;
else
result[count] = recvBuffer.at(count);
Why does recvBuffer.at(count)
appear twice?
In fact, it seems in most iterations it will be evaluated twice,
which is pointless.
So let's improve that:
for (count = 0; count < size; count++) {
char c = recvBuffer.at(count);
if (c == '\n') break;
result[count] = c;
}
I also removed the else
line:
it's just not needed due to the break
as the result of the if
.
Notice how the code is getting shorter and shorter.
What is this about?
if (count >= size) // Should never >= size if '\n' found
count = 0;
Logically,
count
will be <= size
always.
And that comment is not accurate enough,
which makes it confusing instead of helping.
It would be better like this:
// if '\n' was not found, reset count
if (count == size) count = 0;
Actually, this comment could be avoided by using a boolean variable:
bool foundNewLine = false;
for (count = 0; count < size; count++) {
char c = recvBuffer.at(count);
if (c == '\n') {
foundNewLine = true;
break;
}
result[count] = c;
}
if (!foundNewLine) count = 0;
Now a comment would be pointless, the code speaks for itself.
Putting it together the method becomes:
OSFactory::lockMutex(recvLock);
int size = recvBuffer.size();
char* result = new char[size+1];
int count;
bool foundNewLine = false;
for (count = 0; count < size; count++) {
char c = recvBuffer.at(count);
if (c == '\n') {
foundNewLine = true;
break;
}
result[count] = c;
}
if (!foundNewLine) count = 0;
result[count] = '\0';
recvBuffer.erase(recvBuffer.begin(), recvBuffer.begin() + count);
OSFactory::unlockMutex(recvLock);
std::string _result = result;
delete[] result;
return _result;
This code is as clean as I can make it,
and now it seems clear enough that I don't feel the need to add comments.
About vertical spacing, I added blank lines between blocks that are not tightly related.
Conclusion
Maybe only poorly written code needs comments.
If you feel the need to add comments,
in other words you find the code is not readable by itself,
then it's poorly written.
And if it's poorly written,
then it's not comments it needs,
but refactoring.
So, keep refactoring until it doesn't need comments.
This is a great article about coding without comments.
int i = 1; // set i to 1
. \$\endgroup\$recvBuffer
. 2) Copy fromrecvBuffer
intoresult
. 3) Copy fromresult
into_result
. 4) Copy from_result
out of function. 5) Shift recvBuffer down n bytes. That's a very inefficient implementation of reading from a socket. Especially since a socket probably already has a buffer at the OS level (so that's another copy). \$\endgroup\$