TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

C++ Anti-Patterns

38 pointsby Foeover 4 years ago

10 comments

dbattagliaover 4 years ago
“I have asked why he did not just write T t; and be done with it. He said that he is used to the T *t = new T(); syntax. Then I pointed out that he has a memory leak. He replied that the runtime will take care of that. Too bad there isn&#x27;t a runtime in C++ ...”<p>Yikes! I don’t know if I’d call this a “not using RAII” anti-pattern, it’s just sheer ignorance &#x2F; incompetence of how the language works.
flohofwoeover 4 years ago
IMHO most of those are not general &quot;C++ anti-patterns&quot;, but instead define a personal C++ coding style (e.g. another C++ subset). It&#x27;s important for teams to have such a coding style &#x2F; C++ subset, but that doesn&#x27;t mean that others are better or worse.
评论 #25871827 未加载
cozzydover 4 years ago
strong disagree on printf... maybe there&#x27;s some modern typesafe C++ way to do the same thing, but format specifiers are way easier to remember than all the ways of modifying iostream.
评论 #25870673 未加载
评论 #25868949 未加载
评论 #25870016 未加载
评论 #25868909 未加载
评论 #25868899 未加载
hctawover 4 years ago
Most of these are just mistakes you see in code written by novices or non-professionals.<p>Although its symptomatic of a larger issue when working with production C++ codebases, which is that you need a lot of eyes on the code to prevent these mistakes and automated tools only go so far.<p>As always, -Wall, -Wpedantic, -Wextra, and -Werror are your friends. But you should understand the error before you fix warnings (which is what `double x = double(1)` looks like)
SloopJonover 4 years ago
The advice about the order of includes is interesting, as it&#x27;s the exact opposite of my usual habit. I see the point, and I&#x27;ll definitely consider adopting it.<p>One thing I&#x27;ve been bitten by in the past is that Clang &#x2F; libc++ on Mac sometimes doesn&#x27;t require a standard header that GCC &#x2F; libstdc++ does. I don&#x27;t think this pattern will fix that, though.
评论 #25872900 未加载
pvitzover 4 years ago
I am not sure about his example of large classes. If &quot;energy()&quot; is an integral part of the model, why shouldn&#x27;t it be a method of it? Maybe the example is not well chosen, but it looks more like extreme minimalism to me. If there are different kind of models (like Ising and XY), they could all implement the same interface with an energy method. Any views on this?
评论 #25873151 未加载
sudeepjover 4 years ago
&gt; #pragma once<p>Is there every a scenario where this is not needed in a header file?
评论 #25870473 未加载
评论 #25868750 未加载
MaxBarracloughover 4 years ago
Should this have [2016]?
bfrogover 4 years ago
C++ is an anti-pattern
评论 #25873402 未加载
_pmf_over 4 years ago
Modern C++ is the anti-pattern.