Delete your code
anton-pirker.at
anton-pirker.at
We can (and should) safely delete commented code because we have branches which track the evolution of features and random experiments that might someday be useful. So long as the branches exist, we can always recover that code we just deleted.
But there's not value in deleting old branches. They represent the history of a project's development and should be respected as such.
But I think having a lot of dead experiment branches is like having a huge back log. It just some place where ideas go to die.
You stumple upon them once in a while and start thinking about why you never finished them and about how this was a good idea but management killed it and so on. I think it is better to delete them and set your mind free so you can focus on the tasks at hand, not the past.
The source repository is a workplace, not a museum.
Just create a `bla-rearchitecting-experiment.md` in your `doc/` and paste the relevant code and a paragraph or two about what it does, why it could have been better and why it was abandoned. Now your history is in the place it should be, and not cluttering up the workspace of your successors.
History belongs, well, in history.
Obviously, the answer to this question will depend on a whole bunch of different factors. If given the choice, I would personally choose code readability and simplicity over efficiency.
Depends what you mean by "correctness" and how you determine it. In a perfect world the correct way to do something is the same thing as doing it in the most readable and simple way possible.
If you are asking if I think you should give up readability and simplicity if the appropriate standard requires it, then yes. You should probably implement and follow the standard even if the standard is shit (looking at you DOM).
> Sometimes efficiency is a requirement.
Absolutely, which is why I prefaced my opinion with:
>"Obviously, the answer to this question will depend on a whole bunch of different factors."
If you are writing performance critical code, then simplicity and readability will sometimes (probably) have to take a back seat.
It really depends on the project, but sometimes efficiency is important and when it is having a guide to what the code is trying to achieve in a clear and simple form is really helpful.
The way I work is I do any changes on the 'simple' code and only once I have something that works correctly do I try to update the 'efficiency' code. I have found if I try to change the 'efficiency' code directly then after a couple of small changes (or even one) I introduce some nasty bug. The basic approach I take is simple and working first and then any optimization second.
Then, in the tests, have a set of sanity check calls to both the original functions and the optimized versions.
If ever they differ, the tests fail.
What do you think?
If a developer doesn't want to lose code they've deleted they can tag the revision in their repo so they can always get back to it quickly if needed.
How is an unfamiliar developer supposed to find out that the deleted code exists?
[1] Including if(0){} abominations
git bisect
That should identify the/your commit in (usually) < 10 tests. At which point the previous code is still intact.
Or
git blame
and look at the problematic lines, see who changed it and when. Now look at the git log (filtered just for this file if you want) and identify the last changes to this part of code that way.
More complex than just
// timestamp, initials: Changed foo, commenting the following 100 lines out
maybe, but makes the day to day code so much more readable.
No!
Your test suite is meant to be a last safety net, there to catch you if you make a mistake. It's never something to rely upon. Your tests will never be comprehensive enough. Adopting a 'if the tests pass, all is OK' attitude, as implied by the article, is a terrible mistake.
If you maintain the attitude that your test suite is a last safety net, then you will never be able to confidently refactor, which is what TDD in my opinion is all about.
Instead of using the tests for confirmation, you should be reasoning about the code that you are editing to be happy that your changes make sense. Having the test suites as well is just icing on the cake.
Has anyone ever faced this dilemma before? If yes how did you guys dealt with it?
That test suite is also code. How about we go delete some stuff there?
The only reason it could ever be a burden is that the tests take a lot of time to run. In which case you could see if there's anything you could optimize, perhaps only run parts of your suite at any time.
They're not independent. They depend on the code being tested.
This isn't a problem for well chosen & well made tests (i.e. testing precisely interfaces and behavior that are intended to remain the same from release to release).
But due to various religious practices, most of the tests I've seen aren't like that. Instead, they're tightly coupled to the implementation of an app, which makes refactoring harder, not easier.