Irwell
Well-Known Member
So, given a scenario where you have a particularly complex function performing a number of calculations within a series of loops would you:Damocles said:That's silly, this is the exact reason why comments and decent code exists. Naming variables in a clunky and overly long manner doesn't solve anything. Any programmer worth his salt should fully understand what is going on using i and j just by looking at the Wall.RemoveBrick call. i will represent the column and j represents the row. It's perfectly intuitive because that's how 2D arrays work.
a) create a lot of extra functions to subdivide to functionality to make it easier to read
b) reuse your i,j,k variables and leave it to the reader to work out where one loop has ended and the next has started
c) give a small amount of detail in the variable name to allow the reader to work out what the variable contains without having to work out where in the code it appears first
The common method is c), for obvious reasons. Nobody wants a load of extra functions or to have to spend time working out which version of i they are dealing with.
Agreed, x,y parameters probably weren't the best example. Maybe if it was intAge and intGrumpiness instead? If I was trying to create grumpy cats of all possible age and grumpiness combinations would the code GrumpyCats.CreateNew(i,j) be easy to read or would GrumpyCats.CreateNew(intAge,intGrumpiness) be easier? Would GrumpyCats.CreateNew(j,i) be incorrect? I don't know, I'd have to scroll somewhere else or look in some documentation to see what i and j were.Damocles said:Anybody building the RemoveBrick method with the prototype of (i, j) will automatically use it in the same way. Why would anybody in their right mind reverse the perfectly understood and well established precedent?
Autocomplete fixes the typing issue. Regardless of how good you are at subdividing your code, sometimes you end up with functions that are hundreds of lines with multiple loops. The last thing I want to be doing when I'm debugging some code at line 219 is scrolling up to line 27 to see what variable i was. It should be obvious when reading line 219 what line 219 is supposed to do.Damocles said:More to the point, for the time it took to write intCurrentWallRow and intCurrentWallCol, I could have written
Code:// i=col, j=row
for the exact same benefit.
Agreed, and I wouldn't expect it to the nth degree, for example intNewCatGrumpinessAgeValue, just a simple description that fits within the context of the surrounding code would do, for example intAge.Damocles said:Readable code is not created by naming every single variable as explicitly as possible, there has to be some common sense at some point. Otherwise you end up with CarFactoryFactoryFactory and the like of.
We have scripts that document the code automatically. So do most places these days. If naming conventions aren't followed then that bit of code needs to be manually documented, so not following the convention has to be the exception rather than the rule. Also, developers have a habit of nesting things and doing things in a way that makes sense to them rather than you, I accept that because I do it myself. The problem is, if you don't describe your classes, objects, functions, variables, etc. effectively it gets quite messy trying to see where someone else's logic has gone wrong. It's one of the pitfalls of collaborative development.Damocles said:If my project manager asked me to do so, unless the loops were particularly long or complicated, I'd laugh. It's the height of "too much time on my hands"