Moderators: phlip, Moderators General, Prelates
BOOL AddPlayer(Player** players,
int* totalPlayers,
int* maxPlayers,
char* name,
COLORREF colour,
BOOL isPlaying,
int moves,
int boxes,
int score)
{ //Takes the total number of players and the maximum number of players as
//pointers so it can update them if needed. /**
* Initiliases all member variables with the supplied values. No error
* checking, but this is for all intents and purposes a data structure.
* @param direction The direction this wall is in relative to the cell
* @param x The x position of the cell the wall belongs to
* @param y The y position of the aforementioned cell
*/
public Wall(int direction, int x, int y)
/**
* Constructor creates a maze automatically.
* @param width The width in cells of the maze
* @param height The height in cells of the maze
*/
public Maze(int width, int height)double ror(int x, int y){
/*Find the radius of the circle based on the x and y coordinates of a point on the circle*/
...
...
...
}
int main(...){
...
Circle c1(45); //Make a new circle with a circumference of 45
double rad = ror(c1.x, c1.y); //Find its radius
...
}Berengal wrote:If you need to explain what you're doing, you need to choose better names and structure your code better. Those kinds of comments only take care of the symptoms, not their cause.
Berengal wrote:I have no fondness for comments that aren't parsed by some tool at some point. If you need to explain what you're doing, you need to choose better names and structure your code better. Those kinds of comments only take care of the symptoms, not their cause.
Rysto wrote:Berengal wrote:I have no fondness for comments that aren't parsed by some tool at some point. If you need to explain what you're doing, you need to choose better names and structure your code better. Those kinds of comments only take care of the symptoms, not their cause.
I couldn't disagree more. Code only says what's happening. It doesn't say that the NIC hardware incorrectly counts broadcast packets in the multicast bin, which is why you have to subtract the number of broadcast packets received out of the multicast bin to get the correct value.
Berengal wrote:I have no fondness for comments that aren't parsed by some tool at some point. If you need to explain what you're doing, you need to choose better names and structure your code better. Those kinds of comments only take care of the symptoms, not their cause.
stephentyrone wrote:Berengal wrote:If you need to explain what you're doing, you need to choose better names and structure your code better. Those kinds of comments only take care of the symptoms, not their cause.
Broadly, I agree with this, but it is often (in my work) unavoidable. If you're (say) writing assembly to implement a complicated floating-point algorithm using integer arithmetic, you are going to need to explain what you're doing all over the place, because the actual instruction stream has (almost) nothing superficially in common with what you are actually doing. A single "multiply" might be split over 20 instructions, with other pieces of the algorithm interspersed where there are unavoidable pipeline bubbles. When I'm writing this sort of thing, I usually have a couple paragraphs of explanation for every 5-10 instructions in the code.
Rysto wrote:Berengal wrote:I have no fondness for comments that aren't parsed by some tool at some point. If you need to explain what you're doing, you need to choose better names and structure your code better. Those kinds of comments only take care of the symptoms, not their cause.
I couldn't disagree more. Code only says what's happening. It doesn't say that the NIC hardware incorrectly counts broadcast packets in the multicast bin, which is why you have to subtract the number of broadcast packets received out of the multicast bin to get the correct value.
lvc wrote:Berengal wrote:I have no fondness for comments that aren't parsed by some tool at some point. If you need to explain what you're doing, you need to choose better names and structure your code better. Those kinds of comments only take care of the symptoms, not their cause.
While you do have a point, there are useful ways use this type of comment. Improving your code can make it more obvious exactly what the code is trying to do, but only comments can explain why, in particular, it is trying to do it. This can include explaining any interesting pieces of maths it relies on, and (relatedly) explaining the general algorithm at a higher level. These things aren't overly useful to be in, say, API documentation (which is usually what goes into nicely formatted HTML, follows values around, and pops up in IDEs) - it is only really useful to other people looking at your code, not for people who just want to call it.
Like others have said, good comments (other than the four you mention) tend to be characterised by explaining why the code is there, rather than what it achieves.
Berengal wrote:Rysto wrote:I couldn't disagree more. Code only says what's happening. It doesn't say that the NIC hardware incorrectly counts broadcast packets in the multicast bin, which is why you have to subtract the number of broadcast packets received out of the multicast bin to get the correct value.
This is what abstraction was made for. Make a new getMulticaseCount function, put som doc-comments on it even if you don't export it. Putting comments on the internal API is also useful, if it pops up in the IDE when you need it.
def getPackets():
"""Get the number of packets broadcast"""
return packets - getMulticastPackets()
def powerset(s):
"Calculate the power set - the set of all subsets - of s"
s = tuple(s) # Need indexing
pset = set()
# Each unique element of the powerset is defined by the
# presence or absence of each element of the original set.
# So, if we use '1' for 'element is there' and '0' for 'not',
# then we can generate each subset by counting from 0 to the
# cardinality of the set (inclusive) in binary.
for i in range(int("1" + "0"*len(s), 2)):
key = basen(i, 2)
# Pad it out with leading 0s to the right length
diff = len(s) - len(key)) > 0:
key = "0" * diff + key
pset.add(frozenset(s[j] for j, k in enumerate(key) if k == "1"))
assert len(pset) == 2 ** len(s)
return pset
lvc wrote:
- Code: Select all
Select all
def getPackets():
"""Get the number of packets broadcast"""
return packets - getMulticastPackets()
The API documentation for getMulticastPackets() can't really explain why it is needed here.
Select all
def getPackets():
"""Get the number of packets broadcast"""
return packetsWithMultiCast - getMulticastPackets()
length_of_pset = int("1" + "0"*len(s), 2)
for i in range(length_of_pset):
binary_representation = basen(i, 2)
repr_too_short = len(s) - len(binary_representation)
if repr_too_short > 0:
padded_binary_repr = "0" * repr_too_short + binary_representation
picks_from_s = (s[j] for j, k in enumerate(padded_binary_repr) if k == "1")
pset.add(frozenset(picks_from_s))
cathrl wrote:There's a reason we use x and y in algebra, and not the-variable-along-the-x-axis and the-variable-along-the-y-axis.
Zamfir wrote:I would say that this is already close to clear code, it is obvious from reading that the multicast packages are being removed, which in turn implies that the original variable included them.
//have to use strncpy here because src isn't guaranteed to be null terminated. Don't use strlcpy; it might segfault
strncpy(dest, src, dest_len-1);
dest[dest_len] = '\0';Grumpy Code Monkey wrote:We have a coding standard where I work that requires a block of legalese at the head of the file, then a short statement describing the overall purpose of the class or module (in high-level English), then a short block at the head of each method describing the method's purpose, inputs, outputs, and return value (again, all high-level descriptions). Inline comments are typically limited to referencing standards, identifying patches, or justifying a hack.
Berengal wrote:There are four kinds of documentation I like: The kind that pops up in an IDE whenever I need it (mainly when I'm coding Java), the kind that follows values around (python style), the kind that's nicely formatted and navigable html (Java again, and Haskell), and types.
I have no fondness for comments that aren't parsed by some tool at some point. If you need to explain what you're doing, you need to choose better names and structure your code better. Those kinds of comments only take care of the symptoms, not their cause.
There are two ways to read code. One is to figure out how to use it, the other is to figure out how to fix it. In the first case, good API documentation should be enough, and in the second case you need to read the code anyway, at which point comments just get in the way.
Berengal wrote:There are four kinds of documentation I like: The kind that pops up in an IDE whenever I need it (mainly when I'm coding Java), the kind that follows values around (python style), the kind that's nicely formatted and navigable html (Java again, and Haskell), and types.
I have no fondness for comments that aren't parsed by some tool at some point. If you need to explain what you're doing, you need to choose better names and structure your code better. Those kinds of comments only take care of the symptoms, not their cause.
There are two ways to read code. One is to figure out how to use it, the other is to figure out how to fix it. In the first case, good API documentation should be enough, and in the second case you need to read the code anyway, at which point comments just get in the way.
levicc00123 wrote:Berengal wrote:There are four kinds of documentation I like: The kind that pops up in an IDE whenever I need it (mainly when I'm coding Java), the kind that follows values around (python style), the kind that's nicely formatted and navigable html (Java again, and Haskell), and types.
I have no fondness for comments that aren't parsed by some tool at some point. If you need to explain what you're doing, you need to choose better names and structure your code better. Those kinds of comments only take care of the symptoms, not their cause.
There are two ways to read code. One is to figure out how to use it, the other is to figure out how to fix it. In the first case, good API documentation should be enough, and in the second case you need to read the code anyway, at which point comments just get in the way.
While I agree with you, I do think comments like TODO, FIXME, HACK, UNDONE, and other such comments are useful to simply act as a trail of breadcrumbs while I'm working so I know what I need to work on later.
Berengal wrote:I have no fondness for comments that aren't parsed by some tool at some point. If you need to explain what you're doing, you need to choose better names and structure your code better. Those kinds of comments only take care of the symptoms, not their cause.
roc314 wrote:America is a police state that communicates in txt speak...
"i hav teh dissentors brb""¡This cheese is burning me! u pwnd them bff""thx ur cool 2"
http://en.wikipedia.org/wiki/DSV_Alvin#Sinking wrote:Researchers found a cheese sandwich which exhibited no visible signs of decomposition, and was in fact eaten.
Rysto wrote:Zamfir wrote:I would say that this is already close to clear code, it is obvious from reading that the multicast packages are being removed, which in turn implies that the original variable included them.
Which is fine, until some programmer goes and looks at the hardware documentation which says that broadcast packets and multicast packets are counted separately, says "WTF is that code doing?" and "fixes" it.
Here's another situation:
- Code: Select all
//have to use strncpy here because src isn't guaranteed to be null terminated. Don't use strlcpy; it might segfault
strncpy(dest, src, dest_len-1);
dest[dest_len] = '\0';
I can guarantee you that without the comment, somebody will come along and "fix" this code by replacing strncpy with strlcpy
All Shadow priest spells that deal Fire damage now appear green.
Big freaky cereal boxes of death.
Users browsing this forum: Bing [Bot], SlefBalia and 9 guests