Line 25-29: You already have a bool function. Why not simply
return false;
? It's somewhat inconsistent to return false for one condition and throw an exception for the other. Actually this second check is unnecessary. See below.
BTW, your compiler should give you a warning on s.size() <= 0. Size() returns an unsigned type. It's pointless to compare an unsigned type < 0. Unsigned types can not be negative.
line 4-5 throwing an exception would satisfy that issue? |
Yes. That assumes you have appropriate try/catch blocks. IMO, it would be simpler to make set_username to be a
bool
function and have the caller check the result.
line 28 I am assuming you are suggesting that I create a range for an acceptable size length for a username? |
I misread your intent here. As I read it, I took it that 8 was the maximum length and that the return value of the function would be incorrect. Since you meant the name must be 8 or longer, then this statement is correct. This is why comments and meaningful names are so important. Had line 27 been "minimum_size" instead of "acceptable_size", there would have been no confusion. That would also have eliminated my comment about checking for 0 length. Checking >= 8 makes checking for > 0 unnecessary.
Lines 16-21: Again, the intent of these lines were unclear. Keep in mind that many of the posts here are from beginners and contain gross mistakes. Without comments indicating the intent of those lines, I had no way of telling what you actually intended. You may know what your code is supposed to do, but that doesn't mean that it's clear to someone else reading it for the first time.
Is that a bad way to express that notion? |
No. Your loop is fine, now that I understand the intent. Personally, I would have preferred a
for
loop for iterating over a string of known length, but a while loop works.