4 ms·
> The advantages of having default arguments is not something that needs convincing. It's just very nice and convenient to have them. The Google Style Guide di
by haberman 15y ago
> The advantages of having default arguments is not something that needs convincing. It's just very nice and convenient to have them.
The Google Style Guide disallows default arguments, and I find their rationale convincing enough that I never use default arguments in my own code: http://google-styleguide.googlecode.com/svn/trunk/cppguide.xml#Default_Arguments http://google-styleguide.googlecode.com/svn/trunk/cppguide.x...
- gaius 15y agoDefault parameters are more difficult to maintain because copy-and-paste from previous code may not reveal all the parameters I am surprised this is an issue at Google.
- gcp 15y agoOne of the more typical ways to figure out how to use a complex API is usually "look at examples".
- Arelius 15y agoWhy? Google, like most companies, Google is made up mostly ofretty standard. Most programmers enjoy referencing existing code to help understand how an interface works. Just because they have more stringing hiring practices doesn't make their engineers some superhuman level of programmer.
- mistercow 15y agoReferencing existing code is one thing. Copy/pasting that code, or writing new code based on it, without actually looking at the documentation to know what you're doing, is another thing entirely.
- Arelius 15y agoThe issue is not what tends to be the correct way to properly code, but how in fact people end up coding. It seems that many engineering decisions are made at google, to minimize the amount of damage a particular programmer can do. This suggests that while Google does/may have some very good people running the place, they also have quite a few programmers whom are mediocre at best. These choices are about building an environment that forces a programmer to do things the right way, rather then expecting them to do such on their own.
- mistercow 15y agoI really don't find that convincing at all. Default arguments are one of the most effective ways of removing boilerplate and making code easier to read and write. Bad programmers will be bad programmers no matter what, and copy/pasted code that the coder doesn't understand will be fragile or broken no matter what. The problem with this reasoning is contained in this: > to force programmers to consider the API and the values they are passing for each argument rather than silently accepting defaults they may not be aware of. All you will force poor coders to do will be to fudge the arguments until the code "works". This is actually worse than the default arguments scenario, because the defaults are more likely to be acceptable than the first value the coder stumbles upon that makes the function go.
- haberman 15y agoFor me it has nothing to do with bad programmers. It has to do with not having "hidden" input to a function. For example, consider a parsing function that has a boolean parameter indicating whether the parsing is "strict" or not. If you make this parameter have a default, it would be easy to use this function (or read code that uses it) without even realizing that the function can operate in more than one "mode." There is no indication at the call site what mode it is operating in, and indeed if the default value is flipped in the header file the caller could change behavior with no code change at the call site! Taking a line from the Python philosophy: "explicit is better than implicit."
- ajross 15y agoC++ is filled with hidden input. The identity of the this pointer is hidden from the execution of the function. The identity of the function to be executed is hidden from the caller. If that's really your requirement, then you're using the wrong language, the ship sailed. I agree with mistercow: default arguments are, on the whole, a Good Thing. Think of them as a restricted form of overloading which requires (much!) less code to be written. Obviously they can be misused, but again, that's the nature of programming (and especially of C++). Sane default argument conventions make code easier to read and write.
- Symmetry 15y agoI don't know, I think I find the Google Python Style Guide just as persuasive in the opposite direction. http://google-styleguide.googlecode.com/svn/trunk/pyguide.html#Default_Argument_Values http://google-styleguide.googlecode.com/svn/trunk/pyguide.ht... The reason I would tend to use default arguments in Python and C but not in C++ would be that it can get confusing if you throw in C++'s function overloading. A function by its very nature can't help but have default behavior, and if someone realizes they need to override the defaults they'll probably end up in the documentation or function definition or some other place that will let them realize they can adjust the defaults.
- Xurinos 15y agoI think it is inconsistent with the idea of allowing the instantiation of classes with default constructors, classes whose member variables may be given default values by the constructors.