Advertisement

Well written code

Started by September 26, 2000 12:39 AM
25 comments, last by gimp 24 years, 3 months ago
quote: Original post by JonStelly

multiple #includes on a single line = yick!


I think that was a problem with the [ source ] tags. I''ve had it happen to me also when posting sample code. I think you have to hit return twice to make it space correctly.




BeS
It's Da BOMB Baby!!!
BeSIt's Da BOMB Baby!!!. o O ~ A little nonsense now and then,is relished by the wisest men~ O o .-- Willy Wonka
quote: Original post by Neophyte

        cout << "Hello!!";	// Displays "Hello!!" to the screenreturn 0;		// Returns 0          

is complete overkill. If whoever reads the code doesn''t instantly realize what those lines of code does without reading the comment then they have *no business whatsoever* reading the source.


i realise that! I was just putting out an example. i really don''t overkill, i underkill (that is, i seldom comment, but when i do, that''s the style with what i do it).
Advertisement
Since the last post, I''ve changed my commenting style a little again based on the doco in Code Complete. Basically I now use older C style blocks as you can expand them without worrying about comment symbols for each line.

The format that I originally posted with was loosely based on the the style of ... ? Chris Hargrove a coder at 3D Realms I think. He used to run a column over at loonygames on programming.

For those that haven''t yet read Code Complete and have been commenting on when to comment ( sounds like another recent thread), the gist of what the discuss in Code Complete is to comment the code BEFORE you write the code. The comments should also be language independent, english(or whatever) representations of what your doing. In doing this you end up writing mush simpler and better structured code. Then once a routine is complete in comment(they call it PDL) you slip the actually code in to the routine.

To me the first time I heard this I thought "sounds too slow", "takes too much time", "can''t write comments as you don''t know how the code will turn out".... There were all VERY lame statements on my part. I decided to give this a try anyway... and... I LOVE IT. I found that freeing my mind from worrying about the minor details of the language allowed me to better plan the function to do what it was meant to. My code is now better structured , easier to read, and best of all written with FAR fewer bugs, esp. the larger bugs that you often don''t notice when your twiddling bits. But how long does it take? Well pre commenting takes me a little time to do, it is offset however by the fact that my code is now faster to write as I know ahead of time what I''m doing but, the big benefit''s come from not pissing about debugging the code as much.

The other two cool things I''ll mention that I''ve recently taken up after reading eh book that have had a large improvement on my coding productivity are no compiles until code is complete and running all code through a debugger line by line. Not compiling the code until a function is complete is something that you can only really do once you know the language moderately well but once you start doing this you''ll find yourself concentrating on quality rather than speed of your coding. I had also reduce my stress level ''s for some reason.

Debugging ALL code line by line to make sure that it actually does what it''s meant to (as opposed to just producing the result you expect) turned out to be a great idea. Not only did it catch bug that might not have arose for some time but if found that my defensive coding improved as I could see line by line where ''other'' things might go wrong.

So, if you don''t have this book yet GET IT. Sell you sister and get the book. Have a look at gamedev''s book reviews, nobody has given this book less than the highest score possible. That says something.
Chris Brodie
I will read this "Code Complete" my friends, so that it will change my life as well.
cmaker- I do not make clones.
I personally believe that there should never be more lines of comments than code. Code should speak for itself. I've found that over time my commenting has become leaner as I have become a better programmer. I personally do something like this:
    #include "blah.h"  //******************************************************// Static Defs//****************************************************** int MyClass::foo; //******************************************************// Local Functions//****************************************************** //// these are random notes about this function// comments may include what it's for, limitations, etc.//int DoStuff( int param ){    for( int i = 0; i < param; i++ )        std::cout << "hi" << std::endl;     // this is a particularly confusing block    // of code or uses some nasty hack, so    // I'm leaving an explanation for future    // reference    blah( param );}    


Edited by - Kensai on September 29, 2000 1:15:04 PM
I personally find comments generally distracting from the code.
It''s like I get my head into code mode, and then plain english comments throw my head out of code mode, and into english mode again, and then i''ve gotta get back it''s frustrating

I especially hate LaMothe''s style of coments/coding
it seems very sloppy, and it relies too much on globals, even where they aren''t needed, which makes things difficult when you don''t know what''s been initialized thus far, or where it was initialized to what, and so forth. grrr, that''s irritating



-arsenius
'after three days without programming, life becomes meaningless' -The Tao of Programming
Advertisement
quote: Original post by arsenius

I personally find comments generally distracting from the code.
It''s like I get my head into code mode, and then plain english comments throw my head out of code mode, and into english mode again, and then i''ve gotta get back it''s frustrating

I especially hate LaMothe''s style of coments/coding
it seems very sloppy, and it relies too much on globals, even where they aren''t needed, which makes things difficult when you don''t know what''s been initialized thus far, or where it was initialized to what, and so forth. grrr, that''s irritating



-arsenius
''after three days without programming, life becomes meaningless'' -The Tao of Programming



hey. shhh... watch what you say. The last time I talked like this, I found myself having to fight off a lynch mob of angry commenters.


btw
i just witnessed a win95 miracle. A scrollbar with 2 bars in my explorer window.

cmaker- I do not make clones.

This topic is closed to new replies.

Advertisement