Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I\'ll preface this with I don\'t know if anyone else who\'s been programming as

ID: 642893 • Letter: I

Question

I'll preface this with I don't know if anyone else who's been programming as long as I have actually has this problem, but at the very least, the answer might help someone with less xp.

I just stared at this code for 5 minutes, thinking I was losing my mind that it didn't work:

    var usedNames = new HashSet<string>();
    Func<string, string> l = (s) =>
        {
            for (int i = 0; ; i++)
            {
                var next = (s + i).TrimEnd('0');
                if (!usedNames.Contains(next))
                {
                    return next;
                }
            }
        };
Finally I noticed I forgot to add the used name to the hash set.

Similarly, I've spent minutes upon minutes over omitting context.SaveChanges().

I think I get so distracted by the details that I'm thinking about that some really small details become invisible to me - it's almost at the level of mental block.

Are there tactics to prevent this?

update: a side effect of asking this was fixing the error it would have for i > 9 (Thanks!)

    var usedNames = new HashSet<string>();
    Func<string, string> name = (s) =>
        {
            string result = s;
            if(usedNames.Contains(s))
                for (int i = 1; ; result = s + i++)
                    if (!usedNames.Contains(result))
                        break;
            usedNames.Add(result);
            return result;
        };

Explanation / Answer

A unit test likely would've / should've caught this omission.

It would also catch the likely bug you'll have when i = 10.

Test-driven development is proactive by its definition. In your example, you would immediately see that it wasn't working as designed and you would get feedback as to what it was returning.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote