8 ms·
I want to do something very violent to that person who "hates const with a passion". No wonder he didn't provide his name.
by golergka 11y ago
I want to do something very violent to that person who "hates const with a passion". No wonder he didn't provide his name.
- krapp 11y agoCasey Muratori considers const a complete waste of time, so apparently that's a thing with some C/C++ programmers.
- cautious_int 11y agoThat is why you should never learn from one exclusive source, especially if they don't provide valid arguments, and imply "trust me". I saw a couple of his introductory videos and I think he is great to teach specifics about game programming, but should definitely not teach how to write C.
- lgieron 11y agoHis argument is that he reckons that over his career he made very few bugs that could be avoided if he had used const. He admits that it's specific to his coding style and may not apply to other people.
- hibikir 11y agoIf you want to go by authority, instead of by the fact that const just makes sense, there's this guy who claims to be a complete const nazi who you might have heard of. His name is John Carmack. http://www.phoronix.com/scan.php?page=news_item&px=MTI3NDQ http://www.phoronix.com/scan.php?page=news_item&px=MTI3NDQ
- Patient0 11y agowhere did it mention that in the article? I don't see it
- bluehawk 11y agoThe article is in four pages. I missed it at first too.
- bitwize 11y ago'const' in C and C++ is broken. Not only is it like a Chinese word that varies wildly in meaning depending on where you use it and how it is inflected, but in C-family languages, 'const' modifies type, making objects that benefit from the compiler hints 'const' implies type-incompatible with objects that do not. This results in the problem of some know-it-all deciding that a core API needs to be const-correct, and checks in changes making it so, only to find that the entire fucking code base is now broken. Note that I'm not saying "don't use 'const'" with the above (and I'm CERTAINLY not saying "using '#define const' in your code base is a good idea"); only that 'const' in C-family languages has significant drawbacks which may affect the engineering tradeoffs you make when you decide whether to use it. By contrast, languages such as Ada have explicit and implicit constantness restrictions ('in' parameters, for instance, cannot be modified), but these do not modify the type of the object, allowing objects with these restrictions to be type-compatible with objects without them.
- chimeracoder 11y ago> only that 'const' in C-family languages has significant drawbacks AFAIK this is pretty much limited to C and C++. Many (and I am willing to venture most, or at least the most-used) C-family languages that aren't C/C++ don't suffer this issue. Go and Javascript, for example, both use "const" in a way that doesn't modify the type of the underlying object.
- bitwize 11y ago"C-family languages" here means C, C++, Objective-C and D -- not any of the "look-like-C-but-have-completely-different-semantics" languages, or Go which is probably closer to proposed C replacement Alef than to C itself.
- golergka 11y agoI think that the scope of this discussion is limited to C/C++. If a programmer doesn't like immutability as a broad concept, he is clearly an incompetent imbecile (and I'm trying to write that as polite as possible, given the severity of the issue).
- 11y ago
- andresmanz 11y agoWhat people like him don't get is that const is not only about security. It also documents the intents behind the code and thus makes writing new code around it easier. Const really helped me thinking about better designs when I started coding in C++.
- daodedickinson 11y agoWhat about final for Java function parameters? A professor was requiring that and I'm not sure whether its something I should continue doing or not.
- golergka 11y agoMake everything you can as immutable as possible. The less possibilities exist for data to change, the more you know about it during runtime, the easier it is to reason about it. I love the trend that new languages, like Swift and Rust, make it really easy to make variables immutable and even disallow using "null" values unless a variable is explicitly nullable.
- gamegoblin 11y agoTo provide a counterpoint, and I say this as a big casual user of Haskell and Rust (both immutable by default), but a professional Java developer. I work on a large, mainly Java codebase. If we used `final` everywhere it is applicable, the code would have a ton of visual noise. We find that `final` is not as valuable in most cases. `final` doesn't buy you as much in Java as `const` does in C, or (the lack of) `mut` in Rust. In Rust, I can pass in an object reference without mut, and I know that the object will be the exact same after the function call. In Java, even if a function takes `final ArrayList<..> xs`, the function can still delete every element of your array, modify elements at will, etc. That means `final` is basically useless as far as using an API goes (I don't care if the implementation of a function reassigns to the same variable as the object I'm passing in, but I do care if they mutate it). `final` only helps you avoid dumb reassignment bugs that tests should probably catch anyway, and in that case, if you are already writing good tests, why pollute the code with the visual noise of `final`?
- steveklabnik 11y agoSmall nit: you can know it's the same, unless it's a type which offers interior mutability.
- 11y ago
- mahyarm 11y agoconst is an awkward poorly thought out keyword syntax that is too ambiguous. Const is a good idea, how it was implemented in C++ was not. One of my old managers used it as his C++ interview question to 'really see if they know C++ well'. See http://duramecho.com/ComputerInformation/WhyHowCppConst.html http://duramecho.com/ComputerInformation/WhyHowCppConst.html
- danielweber 11y agoAbsolutely nothing there was surprising. The worst thing is that if a library uses const, you have to use it too.
- CountSessine 11y agoThat library might have been coded assuming invariance and a mutable implementation might not have been possible with the same time/space behaviour. If I wrote a library and made something const, I did that for a reason. I didn't do that just to piss you off.
- cmma 11y agoExplain? The most common usage of const in library code is for function parameters: Iterator find(T const& value) const; The const actually _helps_ callers: find accepts both const and non-const references. A non-const argument will be implicitly cast to const by the compiler. Compare that to: Iterator find(T& value) const; This function signature accepts only non-const references (unless T is const). A caller must const_cast a const argument to use it (bad). Return values are occasionally references: T& front(); T const& front() const; But "auto" makes the code shorter when the return type is const or unknown: auto& v = c.front(); // overload is deduced by the compiler based on the constness of c.
- golergka 11y agoI don't think I saw anything strange or ambiguous in this article, and the only thing I didn't know before. And I'm a beginner C++ programmer.
- CountSessine 11y ago