kikito wrote:
... Actually, it breaks two (that I can think of).
- "You shall give proper names to your variables"
- "Don't repeat yourself" (a.k.a. don't copy-paste code)
I used to have a coworker that repeatedly stated that once you finished some code, you should be able to retype it from memory in a matter of minutes. Your variables might be different and the functions might be rearranged, but once you figure it out once it should be like riding a bike. It's still one of the best DRY arguments I've ever seen in action.
com_1, an approach you may want to take is something referred to as "code transparency" where I'm from. The idea is that all of your code should be self-apparent: when you define a variable, the name should tell anyone reading it what it's used for. All of your comments then become reasons rather than references: because the code is transparent (the reader can intuit what it does) the comments serve to illustrate why the code is that way.
I'm not a prime example of this approach, but here's a bit of code from my game that shows what I'm talking about:
Code: Select all
for i=1, multiplayer do
local headX = (screenWidth / (multiplayer + 1)) * i -- player heads are equidistant from each other and the screen edges
local headY = (screenHeight / 2) - 64
love.graphics.draw(headImg, headX, headY)
end
Note that the variables are pretty clear: headX is the x coordinate of the head image, which is stored in a variable called headImg outside the loop. The comment tells us why the code is there: we want the player heads at specific coordinates for a reason. This code isn't terribly complex, but it illustrates what I'm talking about: clarity for the sake of clarity.
And future sanity, I suppose.