How many comments?

Pages: 123
@Duoas
I never rely on comments to tell me what a name should.

This is somewhat related to one thing I love about C++; that being how strict it it towards typing. I've dabbled around in a few languages where you can just declare a varaible and use it as an int, float, etc., likewise with function return or argument types, and there's just something about the ambiguity and reliance on comments to tell you what argument types you should be passing I really don't like that much.

Also, regarding the example you gave to demonstrate your commenting; I'm curious if you intended the string IntegerToWords() function to have a dividing line both above and below the function name..? If it's intentional, then I'm curious to hear your reasonings for it is as the other functions only have 1 instance of said dividing line.

This is somewhat related to one thing I love about C++; that being how strict it it towards typing.


Huh? Well, it is better than PHP or even C in this regard, but calling it "strict" is probably an exaggeration. E.g. templates are not strict at all - they are like a macro processor which substitutes whatever you like for your type variables and it can break several levels below. And there, you really *need* comments - you can't encode that type infromation in the source code.

1
2
template <typename T>
int compare(const T& a, const T& b);


Without a comment, how do you know, what types of arguments you can pass? You can't.


Last edited on
Without a comment, how do you know, what types of arguments you can pass? You can't.

Are you thinking what I'm thinking? I'm thinking horrible. Horrible, horrible concepts.
http://en.wikipedia.org/wiki/Concepts_%28C%2B%2B%29
@rapidcoder

I know what a template is. And the way I see it if you're writing template functions / classes where you need to comment in specific types which are valid to use with said template you should probably not be using a template.

I know what a template is. And the way I see it if you're writing template functions / classes where you need to comment in specific types which are valid to use with said template you should probably not be using a template


Ouch, are you saying guys from STL are doing it wrong? Many STL templates do not work with any types you want.

There is nothing wrong with restricting template parameters to a supertype / subtype / a type with a given interface. Java programmers are doing it for years and it is a brighter side of generics - it makes not only code more readable, but also error messages are short and nice.

@Catfish2, well, yeah, concepts. I wonder why a similar thing in Java/Scala can be very simple and clean, while C++ proposition is... like it is.
Last edited on
you can't encode that type infromation in the source code.
template <typename T>

"T" is a great example of the poorly-chosen name.

There is nothing wrong with restricting template parameters to a supertype / subtype / a type with a given interface.

That's been done in C++ with enable_if, it just isn't usually needed.
Last edited on
Let's all just use an entirely reflective language where all you care about is if a method with the name, parameters, and return type you need exists.
@ rapidcoder: There is something to be said about planning ahead, Template Specialization exists for a reason. On that note a specialized template is the perfect example of when comments are wanted if that's what you are getting at.

As for me, the header files that I reuse and any accompanying .cpp files they may have might have a few comments, but despite me knowing better this is rarely the case. I mostly use comments to save URL's to the documentation of confusing or complicated API functions. I will almost always add a footnote or two right before I copy and paste it to this site but that's done on the fly. Other then that the code for the applications I have written to use in my job contain almost no comments except ideas about things to add. I'm a Sys Admin and not a developer so I guess most of you would have more reason to use comments but since I'm the only one who will ever see %95 of what I write, and for certain I am the only one who will ever edit it I just don't do it.
Last edited on
@rapidcoder

I'm not aware of any STL function where type is specifically limited. The only instances I can seem to find which have limitations are those regarding what an argument should represent. There's a noteable difference between limiting what an argument represents (e.g. it should be an iterator) vs limiting what type that argument can be (e.g. it should be an iterator for a vector).
Last edited on
Oh, Computergeek01 is a SysAdmin. This explains a lot. (Aaarrrgh!)
http://www.yuksrus.com/computers_sysadmin.html
http://www.gnu.org/fun/jokes/know.your.sysadmin.html

LOL. :->



@Ikaron
I tend to use full-line comments to divide up the code. The way I did it was just a fluke of how I threw that code together.
@Duoas
I also use comments to divide up code, I just havn't been able to settle on a method I particularly like. :/ Same goes with positioning headers.
@Ikaron: for one thing, std::map requires the key to have overloaded operator<
@L B
I'm not sure I understand what point you're getting at? One can have a non-template function require an argument with an overloaded operator as well. For example, this function would require that ClassA have an overloaded operator << which takes a right hand operand of type ClassB:

1
2
3
4
5
6
void SomeFunction(ClassA a)
{
    ClassB b;

    a << b;
}
I'm responding to this:
Ikaron wrote:
I'm not aware of any STL function where type is specifically limited. The only instances I can seem to find which have limitations are those regarding what an argument should represent. There's a noteable difference between limiting what an argument represents (e.g. it should be an iterator) vs limiting what type that argument can be (e.g. it should be an iterator for a vector).
Another example would be std::sort
http://www.cplusplus.com/reference/algorithm/sort/
Only works with iterators or pointers to arrays - things that support the correct operators.
Last edited on
I never write comments, though I think I should have after the fact, but don't care enough to go back and add them.
@L B
I'm not sure that's a differing type though. Take the sort function you pointed out; the variable must represent an iterator but what is that iterator's type? Is it a list iterator? A vector iterator? Or for the array pointer; what type of array pointer is it?

Perhaps it's just me, but the with the way I tend to see it: An iterator is a "thing", and a vector iterator is what type of "thing" it is (or could be). Likewise the number 40 is a "thing", and an unsigned int is what type of "thing" it is (or could be).

This is the distinction I'm making when taking templates into consideration. Regardless of what the argument should represent (an iterator, an array pointer, etc) the expected types of those representations is effectively unknown. If the expected types are known, however, I don't feel it's appropriate to use a template unless you're dealing with a rather large number of differing types and overloading would be unreasonable.

Perhaps that'll clear things up some. xD
Last edited on
An iterator is a "thing"
The term you're looking for is "abstract data type". For example, vectors and lists are two types of contaners.

Likewise the number 40 is a "thing", and an unsigned int is what type of "thing" it is (or could be).
This analogy is broken. It'd be better to say that int, double, and bigint are all kinds of number types; that is, types whose instances can be added or multiplied among themselves.

I agree with Ikaron. It'd be unreasonable to expect that a template function should operate on any conceivable type just because it's a template function. What does it mean to, say, sort a graph?
@helios
The term you're looking for is "abstract data type". For example, vectors and lists are two types of contaners.

Thank you, I'm far from as fluent in terminology as I really should be.

This analogy is broken. It'd be better to say that int, double, and bigint are all kinds of number types; that is, types whose instances can be added or multiplied among themselves.

Well I was sort of trying to generalize that an iterator is to a number like an iterator type (e.g. a vector iterator) is to a number type (e.g. an unsigned int). Or is that still incorrect?

Well I was sort of trying to generalize that an iterator is to a number like an iterator type (e.g. a vector iterator) is to a number type (e.g. an unsigned int).
Yeah, I know, but you were saying that if int is like a vector iterator, then 40 is like an iterator, which doesn't make any sense. "40" is less abstract than "int", while "iterator" is more abstract than "vector iterator".
Right, I see. Perhaps I should have said something along the lines of "The concept of an argument taking an iterator is alike the concept of an argument taking a number" (and the type is what resolves the concept into something concrete) ..?

Sorry, this has gone so off topic, I'm just trying to get what I'm saying to match what I seem to be thinking.
Pages: 123