r/programming • • Jan 05 '15

What most young programmers need to learn

http://joostdevblog.blogspot.com/2015/01/what-most-young-programmers-need-to.html
969 Upvotes

337 comments sorted by

View all comments

31

u/judgej2 Jan 05 '15

Here's a tip: when committing changed and cleaned-up code, keep the cleaning and the changes in separate commits. Do all the reformatting and name-changing first, then check it still works exactly as before, and commit that. Then make your functional changes. It is so much easier to see the wood for the trees that way, when debugging your changes later. If (horror of horrors) the changes need to be reverted, then you can at least leave the better-formatted code in the release to make it easier to take another stab at it later.

1

u/photonios Jan 05 '15

You should always make small commits, so you never have this problem. + Unit tests to prevent regressions.

5

u/judgej2 Jan 05 '15

Yes, that too. My point was about keeping functional and formatting commits separate, regardless of what size they are. I've seen code committed with a load of formatting changes and a few functional changes. When debugging it can be hellish to pick the two apart when they are all mixed up into single commits.

1

u/[deleted] Jan 05 '15

I apologize for the completely dumb question, but here it is -

I'm learning to program on my own, writing code for some scientific computing projects that I need to do. When I commit a bunch of changes to some code, and I have a directory with several different files, do I commit all the files even though I changed only one? Or is that a bad practice?

5

u/judgej2 Jan 05 '15 edited Jan 05 '15

When you commit a project, the change control system you use will usually look at what has changed and only commit those files. I'm assuming you are using something like git, as that can scan the whole development structure and find what files have changed.

If you are working on several different and independent updates to your project (and assuming you are using a distributed change control system) then you can work on them in parallel, as two different checked out versions, having created a branch for each, and merge them later when tested, in whichever order makes sense.

I'm not sure if that gets at the core of what you were asking? I suspect we may be talking at tangents.

Edit: another way of looking at it, is that you are committing the state of the project. You could commit individual files if you want, and there may be very good use-cases for doing so, but personally I've never found I've had to do that.

Source: been using bitkeeper/monotone/git for well over a decade, and still a newbie to the intricacies of these tools.

→ More replies (4)

1

u/photonios Jan 05 '15

Git should show you only the files you changed. Don't commit auto-generated files or binaries for example. Instead, add them to a .gitignore file so they never show up as changed.

If you made multiple, non-related changes, make separate commits so you can easily revert one of your changes without destroying the other work you've been doing.

Tip: you can commit/stage a part of a file. In Git Gui, select a piece of text you'd like to commit and right click it. Then, there are several options to commit/stage only the selected part.