Syntax proposition: readability of templated type declarations
I won开发者_StackOverflow中文版der 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.
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;
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.
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.
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.
精彩评论