Skip to main content
GameDev.net gamedev.net
🔒 Locked

Other peoples code

Started by Willm May 22, 2003 at 9:52 AM 18 replies 1.1k views
Original Post
Willm
Willm
Dont you just hate it?
Eskhan
Eskhan
Especially people that comment every other line, ugh...
--------------------Though this program be madness, yet there is a method in't
CpMan
CpMan
Commenting is good because it explains what''s going on. When you''re looking at their code, doesn''t it make sense that it will make more sense if they had the sense to put sensible comments in it?



Gamedev for learning.
libGDN for putting it all together.
An opensource, cross platform, cross API game development library.
VSEDebug Visual Studio.NET Add-In. Enhances debugging in ways never thought possible.
987th Yak
987th Yak
quote:
Original post by CpMan
Commenting is good because it explains what''s going on. When you''re looking at their code, doesn''t it make sense that it will make more sense if they had the sense to put sensible comments in it?



Gamedev for learning.
libGDN for putting it all together.
An opensource, cross platform, cross API game development library.


Yes, but it is very annoying when their code looks like this:

  

int SomeFunction(int parameter) //This is a function that

//returns a integer

{
int Counter; //A counter variable

int Result; //stores the Result of the function in a variable


for(Counter = 1; Counter > parameter; Counter++) // loop that //runs the number of times specified in the parameter variable

{
Result = Result + Counter; //Adds the Counter to the result

}

return Result; //returns the data in the result variable

}

-----------------------------------------------------------PLEASE don't comment on the pointlessness of this post!!
sjelkjd
sjelkjd
quote:
Original post by 987th Yak
Yes, but it is very annoying when their code looks like this:
snip


Yes, especially when it has bugs like that =)
987th Yak
987th Yak
quote:
Yes, especially when it has bugs like that =)


EXACTLY!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
-----------------------------------------------------------PLEASE don't comment on the pointlessness of this post!!
Necro-File
Necro-File
My code never has comments. When I code, it''s formyself so I find commenting to be pointless. I am smart enough to figure out what i did if I look at code I made long ago, and if someone steals my code I hope they aren''t smart enough to do the same (most of the tim they aren''t).

//comment this! ~punches you~
Beer Hunter
Beer Hunter
quote:
Original post by CpMan
...that it will make more sense if they had the sense to put sensible comments in it?
No complaint was never raised against "sensible comments".
OrangyTang
OrangyTang
Commenting is an art form, where leaning to either extream is just plain stupid.

Equally, the cautious maintenance programmer learns to never trust the comments blindly. And the thorough programmer remembers to maintain the comments as the source evolves. Concise and well written comments are robust to small change, and never misleading even when not updated with large change.
987th Yak
987th Yak
Really though, in my computer science class we are all
working on a final project as a class (a card game thingy).
I was in charge of melding all the functions that people
wrote to check for different card....type...things...(like Fullhouse, Flush, straight, ect.) into one function that would do them all. This was an easy task until I realized that.........
.....THEIR CODE DIDN''T WORK!!!!!!!

did they take the time to test it?

NOOO!!!

did it seem that they even carefully read over it?

NOOO!!!!

but they sure as hell took a long time putting in worthless
comments!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"Are your functions done?" "Yeah, but we''re documenting
them, give us a minute" Half a period later: "Are your functions done?" "Yeah, but we''re documenting them, give us a minute" The next day: "Here you go." I go to put them in.....ERROR!! fix the error(in THEIR code, the ones in mine were no problem) and.....
....WRONG RESULT!!! ect. ect. ect.

well, CLEARLY even with the extra time you spent so that I could know that counters count, YOUR FUNCTION DOESN''T WORK!!!!!






it annoyed me.






-----------------------------------------------------------
PLEASE don''t comment on the pointlessness of this post!!
-----------------------------------------------------------PLEASE don't comment on the pointlessness of this post!!
CpMan
CpMan
OK, so let's take an example, because what I'm currently writing is for public display and I want to make sure my Anal-Ness has not gone TOO far. Please note that while the comments above the functions are fairly unessary at times, I need them so doxygen will pick them up. Also don't yell at me if there are syntax errors, I haven't attempted a compile of this yet.


  
/***************************************************
* Particle Management. This is a proxy from the ParticleSet class
*/

//!Returns a vector of all of the particles
std::vector<Particle>& GetParticles() { return PSet; }
//Returns a particle at the given index. If the index is out of range, std::out_of_range is thrown.
Particle& operator[](std::size_t index) {
if(index < 0 || index >= Particles.size()) {
throw std::out_of_range("Particle with the given index does not exist");
}
return Particles[index];
}
//!Retrieves a pointer to an available particle and updates any counters.
/*!
If no particles are available, the system attempts a resize of the vector. If the ResizeFactor is 0, it throws std::out_of_range()
*/
Particle& AddParticle() {
if(ParticlesAvailable > 0) {
ParticlesAvailable--;
ParticlesInUse++;
return Particles[AvailableParticles.top()];
}
//gotta resize, but first make sure resize factor is correct
if(ResizeFactor == 0) {
throw std::out_of_range("Particle could not be created, out of available particles and resize factor is zero");
}
//now, unfortunately, InUseIsMaxParticles is not true anymore
InUseIsMaxIndex = false;
for(std::size_t i = 0; i < ResizeFactor; i++) {
Particles.push_back(Particle(ParticleProperties));
ParticlesAvailable.push(MaxParticles+i);
}
ParticlesAvailable+=ResizeFactor-1;
MaxParticles+=ResizeFactor-1;
//now grab one
return Particles[AvailableParticles.top()];
}
//!Adds a Particle to the set and returns a pointer to it, setting it's value to particle. If the particle cannot be created, std::out_of_range is thrown
Particle& AddParticle(Particle& particle) {
//attempt to get a new particle
Particle newparticle;
try {
newparticle = NewParticle();
}
catch(std::out_of_range outofrange) {
throw std::out_of_range("Could not add particle, no particles available");
}
newparticle = particle;
return newparticle;
}
//!Adds multiple particles. If not all of the particles could be created, returns false;
bool AddParticles(std::size_t numnewparticles) {
//attempt to add all of the particles
for(std::size_t i = 0; i < numnewparticles; i++) {
try {
AddParticle();
}
catch(std::out_of_range outofrange) {
//could not add this one, return, since no further particle creation will succeed;
return false;
}
}
return true;
}
//!Kills a single particle
void KillParticle(std::size_t index) {
if(index < 0 || index > Particles.size())
return;
if(Particles[index].IsAlive()) {
Particles[index].Kill();
TotalParticlesAvailable++;
ParticlesInUse--;
AvailableParticles.push(index);
}
}
//!Kills a range of particles, inclusive. If lastindex is (std::size_t)-1
void KillParticleRange(std::size_t firstindex, std::size_t lastindex = (std::size_t)-1) {
//simply kill() each of the particles in the range
std::size_t maxindex = (lastindex != (std::size_t)-1) ? lastindex : Particles.size();
for(std::size_t i = firstindex; i < maxindex; i++) {
KillParticle(i);
}
}
//!Kills all particles
void KillAll() {
//simply loop through the entire vector and Kill each particle
for(std::size_t i = 0; i < Particles.size(); i++) {
KillParticle(i);
}
}
//!Now since we have the possibility with the resizing, that we're going to lose the ability to predict the max index, we need a function to restore this abilitiy
/*!
Please note that this is not an inexpensive operation. It involves removal of all of the spaces in the Partilces array, emptying
of the AvailableParticles stack, and readdition of all the avialable particles.
*/
void RestoreOrder() {
//recompress the vertices and rebuild the stack.
Compress();
RebuildStack();
//reset boolean
InUseIsMaxIndex = true;
}

private:

//!Compresses the vertices into contiguous sections, with no dead particles in between.
/*!
Please note this is not an inexpensive operation, but is useful because it restores the ability to do the
InUseIsMAxIndex optimization. However, please note that while Compress will restore this condition, doing this
will invalidate the AvailableParticles stack. In order for everything to be correct, the stack must also be rebuilt.
*/
void Compress() {
//The first step is recompression of the particle array into contigious live particles
int i = 0;
int count = 0;
while(i < Particles.size()) {
//check if the current one is alive
if(Particles[i].IsAlive()) {
i++;
continue;
}
else {
count = 0;
while(i < Particles.size()) {
i++;
count++;
if(Particles[i].IsAlive()) {
Particles[i-count] = Particles[i];
Particles[i].Kill();
break;
}
}
//this means we reached the end of the vector and everything is in order, break
break;
}
}
//the compression reenabled the optimization, so reset the boolean var.
InUseIsMaxIndex = true;
}
//!Rebuilds the AvailableParticles Stack
void RebuildStack() {
std::stack<std::size_t> a;
TotalAvailableParticles = 0;
//now we need to push all available particles onto the stack
for(std::size_t i = Particles.size(); i >= ParticlesInUse; i--) {
a.push(i);
AvailableParticles++;
}
AvailableParticles = a;
}

public:

//!Returns the maximum number of particles available
std::size_t GetMaximumParticles() { return Particles.size(); }
//!Sets the maximum number of particles.
/*!
If this group has already had an initialize call (i.e. is alive), you must recall Initialize()
in order for the changes to take effect.
*/
void SetMaximumParticles(std::size_t numparticles) { MaxParticles = numparticles; }
//!Returns the number of available particles
std::size_t GetParticlesAvailable() { return TotalParticlesAvailable; }
//!Returns the number of particles currently in use
std::size_t GetInUseParticles() { return ParticlesInUse; }
//!Returns whether InUseParticles() also implies the maximum index
bool InUseEqualsMaxIndex() { return InUseIsMaxIndex; }
//!Returns the resize factor
std::size_t GetResizeFactor() { return ResizeFactor; }
//!Sets the resize factor
void SetResizeFactor(std::size_t newfactor) { ResizeFactor = newfactor; }

/*
****************************************************/





Gamedev for learning.
libGDN for putting it all together.
An opensource, cross platform, cross API game development library.

[edited by - CpMan on May 22, 2003 7:08:07 PM]
VSEDebug Visual Studio.NET Add-In. Enhances debugging in ways never thought possible.
CpMan
CpMan
Damn formatting got screwed up, oh well.



Gamedev for learning.
libGDN for putting it all together.
An opensource, cross platform, cross API game development library.
VSEDebug Visual Studio.NET Add-In. Enhances debugging in ways never thought possible.
KalvinB
KalvinB
Commenting C++ makes as much sense as commenting English.

I came back to my Cradle Quest 1 code several years after I wrote it and I still understand it even though there''s not a comment to be found in it. Sure it''s horrificly overly complicated and I could do it better but I still understand what I was doing.

The only time I really comment anything is when I''m using numbers which don''t make any sense on their own or for debugging.

Ben


IcarusIndie.com


KalvinB - (to Jessika) do you accept Jesus as your lord and savior

Jessika - Sure I can accept all forms of payment.
CpMan
CpMan
I disagree.

You may understand your own code, but it''s important to at least comment out the basic ideas and workings behind a piece of code, if someone else will read it.



Gamedev for learning.
libGDN for putting it all together.
An opensource, cross platform, cross API game development library.
VSEDebug Visual Studio.NET Add-In. Enhances debugging in ways never thought possible.
kordova
kordova
If n ot hing else, it co uldn''t hur t. (Don'' t you dare contradict me with th e argument I just formulated fo r t h is myself.)

DAMMIT SPACIN G .
billybob
billybob
i comment almost every line of class definitions, but barely any of the source

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.