tags:

views:

87

answers:

4

I wonder if syntax as follows would be helpful in your opinion as a code readability improvent and self-commenting of code:

std::map<std::string name, std::vector<int> scores> myMap;  

In this example it clearly says and no other comment is needed, what for we are using myMap variable.

Looking forward to your opinions.

+3  A: 

In my opinion more easier, nicer and practical is to implement some typedefs, such as:

typedef std::string NAME;
typedef std::vector<int> SCORES;

std::map<NAME, SCORES> myMap;
fritzone
typedef std::vector<int> vint;typedef vint SCORES;
Svisstack
@fritzone Well you can't rally say that it's easier, you have to do lots of typing. And why would it be more practical? You populating entire unit with those typedef's.
There is nothing we can do
@fritzone And about being nicer that's subjective, but in your solution when looking at myMap decl. you do not know what is the type of NAME and SCORES.
There is nothing we can do
+1  A: 

Personally I readily write using namespace std; As a general idea for namespaces it isn't recommended, but std is so ubiquitous that IMHO it's perfectly fine, and saves a lot of typing.

map<string, vector<int> > myMap;  

To any C++ programmer with at least some experience the above is as readable (and probably more, because of much less superfluous std:: syntax). Notice that I removed the names: I don't think they add much in real code. Where it's really important, just throw a short comment:

// maps names to an array of scores
map<string, vector<int> > myMap;  

A common argument against comments is that they won't get maintained because they have no real semantic value for the code. The same can be said for the names in your proposed syntax, so nothing is gained by adding more syntax.

Eli Bendersky
By my proposed syntax what is added is as I mentioned this before self-documenting of code.
There is nothing we can do
A: 

A more descriptive name for myMap probably helps more, e.g scores_by_name.

And what about:

std::map<std::string /*name*/, std::vector<int> /*scores*/> myMap;  

or

std::map<std::string, std::vector<int> > myMap; //name - scores

The new syntax wouldn't really do anything more than a comment. (Self-commenting code has the advantage, that the language would enforce that the comments are true, but in this case the identifiers would have zero syntactical meaning.)

Perhaps the proposition might be good for helping intellisense, but then wouldn't you also have to allow:

std::string name myString;

etc.

visitor
Why would I have to allow std::string name myString;? What I'm doing is typical <type name> var syntax, what you're suggesting is a declaration that would allow give an alias to a var (just one of possible deductions).
There is nothing we can do
A typename is a typename. Why do you think templates need special clarification, if you can't tell the purpose of `int myInt;`?
visitor
+1  A: 

The C++ standard committee will argue over it for 10 years, then reject it. Pick a good variable name, myMap is pretty useless:

std::map<...> NameToScoreMap;

And a good editor so you don't have to type that out completely every time you use the map. Functional hungarian is essentially the same idea.

Hans Passant