r/learnprogramming • • Mar 07 '23

Is it normal as a junior programmer..

To spend hours looking over the same block of code, trying to sort out ‘why the f*ck isn’t this working!?’, only to realize the error you made is so monumentally, and stupidly obvious that it leaves you feeling like a completely useless moron? Lol

1.6k Upvotes

297 comments sorted by

View all comments

118

u/dmazzoni Mar 07 '23

Yes, it's normal!

However, with experience you can reduce the time.

I think the biggest mistake I see a lot of junior programmers making is not checking their assumptions.

You added a new line of code, and you ran your program again. Now it doesn't work. You assume the problem is with that new line of code so you spend an hour studying that line of code.

Actually, it turns out the problem wasn't there. You accidentally typed an extra character at the top of the file while saving, and that was the problem all along.

How can you discover that faster?

By using version control, and testing your assumptions. Here's my exact thought process:

  • I believe the problem is with the line of code I just added, so if I comment it out the program should work again.

  • Wait, it's still broken! Now I know my assumption was wrong.

  • Thankfully I'm using Git, so I can do a "diff" to see what I've changed since the last time I did a "git commit".

  • Hmmm, I still don't see the issue, so let me stash my changes or save them in a branch called "broken" and go back to the last commit.

  • Hooray, the last commit worked!

  • Now my working assumption is that something that changed between my last commit, and the "broken" branch is the problem.

  • But, test that assumption. Go back to the broken branch. Is it still broken?

  • Yes, it's still broken. Now I'm confident the problem is in the changes on that branch.

  • Now I can approach this methodically. Look at every change, one line at a time. If I don't see the problem, I can still try reverting every individual change until I find the one that broke it.

It sounds tedious, but approaching it methodically like that will likely lead to finding the issue in a few minutes, rather than hours.

Oh, and notice that it's absolutely key that you use Git and commit frequently.

13

u/Total-Subject2763 Mar 07 '23

That’s helpful! Thank you!

13

u/mysticreddit Mar 07 '23

Also using a better text editor that lets you have a branching history / undo can help — that way you can go forward/backwards to narrow down the change(s) that caused the issue.

Even if you only have a linear undo history saving every N changes to unique files will let you do a visual difference to spot the bug.

  • Save foo.100

  • Undo 25 times, save foo.75

  • Undo 25 times, save foo.50

  • Undo 25 times, save foo.25

  • Undo 25 times, save foo.0

Now you can do diff foo.25 foo.50, compile a specific version, etc.

0

u/[deleted] Mar 08 '23

[deleted]

3

u/mysticreddit Mar 08 '23

That’s what git bisect is for.

You are using version control, right? :-)

Commit early, commit often.

7

u/waitplzdontgo Mar 08 '23

I think the biggest mistake I see a lot of junior programmers making is not checking their assumptions.

11 YOE and this is extremely correct

Juniors often spend hours agonizing over the wrong part of the code because of this issue. The first thing I do when a junior asks me to help out is start from no information and ask them to walk me through their work so far in a Socratic approach to see if there are any things they might have accidentally neglected to notice.

Btw I had to go through this process too. I spent 3 days agonizing in a college lab over why my code was executing improperly. Turns out the answer was I had written an if statement like

if (myvar = b) {

And I didn’t notice I was missing the second equals sign for comparison, which meant this was an assignment operation, and assignment operations always resolve to true.

6

u/chartley1988 Mar 08 '23

Yeah. Easily switching between commits in VS Code has been like god-mode for me! Good old isolating the problem.

3

u/Montags25 Mar 08 '23

How often should I commit? If I write a function let’s say should I commit after each one I write? I guess the more commits means less code to sift through when something goes wrong?

5

u/aaachris Mar 08 '23

Generally a good practice to commit often while it's working

1

u/dmazzoni Mar 08 '23

I try to commit every time I make something new work, so ideally most commits are not in a broken state. That should be multiple times per workday.

But, if I am going to take a break or if something is taking a while, I might commit with a comment saying it's a half finished change.

1

u/[deleted] Mar 08 '23

I need to get over my dislike of rebasing and commit more

2

u/dmazzoni Mar 08 '23

Do you mean squashing?

If it's a chain of your own commits, you can just squash them together once you decide you don't need the history. Or stick the history on some other branch and just keep the squashed highlights on the main branch.

1

u/[deleted] Mar 08 '23

that's the one! I still struggle with git terminology.

1

u/Conely Mar 08 '23

This is how everyone should explain any kind of problem solving to other people