TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

Tips for stable and portable software

84 点作者 begriffs超过 4 年前

12 条评论

CJefferson超过 4 年前
I&#x27;ve currently involved with a system, written in C, which has been going for 30 years: GAP - <a href="https:&#x2F;&#x2F;www.gap-system.org" rel="nofollow">https:&#x2F;&#x2F;www.gap-system.org</a><p>While I write a lot of C, I immediately disagree with the idea that C has a &quot;simple (yet expressive) abstract machine model&quot;. Every so often we find a bug which has been present for over a decade, because some new compiler has added a (perfectly legal by the standard) optimisation which breaks some old code.<p>Picking one example: in the &quot;old days&quot;, it was very common (and important for efficiency) to freely cast memory between char, int, double, etc. For many years this was fine, then all compilers started keeping better track of aliasing and lots of old code broke.<p>Also, while POSIX is a nice base, it stops you using Windows, and also almost every non-trivial program ends up with a bunch of autoconf (which has to be updated every so often) to handle differences between linux&#x2F;BSD&#x2F;Mac.<p>Also, definatly don&#x27;t distribute code where you use flags like &#x27;-pedantic&#x27;, as it can lead to your code breaking on future compilers which tighten up the rules.
评论 #24352021 未加载
评论 #24351908 未加载
评论 #24353872 未加载
评论 #24351511 未加载
ut6Ootho超过 4 年前
This article certainly rings a bell, as I started rewriting my personal projects to C in the last year, precisely because I wanted to make them decades-proof. I still use the same vimscripts I wrote in early 2000&#x27;, I want the same thing for all my tooling and apps.<p>I&#x27;m not sure it makes sense professionally, though, as most codebase won&#x27;t survive a decade : after three years, the dev team will turn over, and the new team will want to rewrite everything from scratch. Or start rewriting parts of the exisiting system in a new language, until it ultimately eat it up. It may be related to the kind of companies I work with, though (very early stage startups).<p>Regarding interfaces, I think the author could have gone a step further. There is actually a standard and portable interface system: html&#x2F;js&#x2F;css. If you write a dependency free web app using things like webcomponents and other standard techs, you know it will stand time, and it actually matches all the reason why the author want to use C : standard and multiple implementations.
评论 #24351497 未加载
评论 #24351501 未加载
ludocode超过 4 年前
This is mostly good advice. I don&#x27;t love configure scripts, I don&#x27;t agree with the heavy reliance on POSIX if you intend to be compatible with Windows, and I don&#x27;t love the fact that the author recommends third party data structure libraries that they haven&#x27;t actually used. For container libraries in C, you really have to use them to get a feel for their usability (this sounds like a tautology but it&#x27;s not.)<p>I disagree strongly with one recommendation. This is just an example, but it holds for larger API design in general:<p>&gt; we could add a fallback to reading &#x2F;dev&#x2F;random. [...] However, in this case, the increased portability would require a change in interface. Since fopen() or fread() on &#x2F;dev&#x2F;random could fail, our function would need to return bool.<p>No, definitely not. It is dangerous to expect the application to sanely handle the case of randomness being unavailable when it is never going to occur in practice. On all POSIX platforms, &#x2F;dev&#x2F;random exists and will block until sufficient entropy is available. Something would have to go seriously wrong for this to fail. This is so rare that any error handling code for it will never be tested. The most likely outcome of forcing the caller to handle it is that the return value is ignored or improperly handled and the buffer is used uninitialized, leading to a security vulnerability.<p>My recommendation instead would be to error check your fopen() and fread() calls within get_random_bytes(), and print an error and abort() if they fail. This way if someone&#x27;s system is improperly configured and &#x2F;dev&#x2F;random doesn&#x27;t work the program will just crash. Same goes for macOS&#x27;s SecRandomCopyBytes() and Windows&#x27; half a dozen calls to use an HCRYPTPROV. This way you still return void and there is no danger of callers improperly handling errors.<p>In general, unless you&#x27;re writing safety-critical software, it&#x27;s fine for your code (or even library code) to abort() in these sorts of exceptional situations when there is no reasonable or safe way to handle the error. If someone truly wants to handle the error, they can just not use your API and do it manually.
kasperni超过 4 年前
&gt; Tips for stable and portable software<p>I think a more accurate title would be &quot;Tips for stable and portable C programs&quot;
评论 #24351641 未加载
评论 #24363875 未加载
chrisco255超过 4 年前
From the title, I was hoping to hear about software systems that have powered infrastructure for decades, but unfortunately it was more of a programming language analysis strategy.
jankotek超过 4 年前
Hm, decades is not that much, most enterprise code fits into that. But how about 200 years?<p>It is about people. Documentation, paper trail why some decisions were made, archiving build tools, VMs, dependency source code..<p>Also C, POSIX and Motif are terrible choice for their fragmentation. Java is very booring, but compiling and debugging 20 years old code is very common.
jart超过 4 年前
That forceinline definition is just tip of the iceberg. It&#x27;s so hard to define in a way that works with different versions of GCC, -Werror, instrumentation, MSVC, and profiling. If you care about portability, consider just not caring and using static. Too much special casing code can actually make it harder for people in weird environments to use your code, since something is going to break it, and reading past the ifdef soup becomes the biggest obstacle.
Cthulhu_超过 4 年前
I&#x27;m currently &quot;betting&quot; on Go for making a back-end (just a REST API + sqlite database) that will last a decade; I&#x27;m betting on the tooling to stay backwards compatible or with minimal changes in the codebase; I&#x27;m betting on the readability of my own code for the next decade, and I&#x27;m betting on the language + tools to continue to be developed whilst sticking to their original goals.<p>Generics is going to be fun.
评论 #24352489 未加载
评论 #24352140 未加载
MaxBarraclough超过 4 年前
Seems like good advice. I&#x27;d add another one that seems completely obvious, but some sloppy developers ignore it: avoid undefined behaviour. If you&#x27;re going to work with C, you need to know about undefined behaviour and take it seriously.
评论 #24352423 未加载
iso8859-1超过 4 年前
Really weird that he recommends Motif. Motif is not comparable to Web&#x2F;Gtk&#x2F;Qt since it has only the most primitive widgets, and no 3D support.<p>I would propose doing a web-app if you really care so much about compatibility. Web also allows for more custom widgets.
评论 #24352455 未加载
评论 #24353290 未加载
评论 #24352117 未加载
评论 #24352220 未加载
fjfaase超过 4 年前
One day, maybe when I am retired, I am going to develop a programming language-agnostic algorithm specifying language with which you can generate code for programming languages ;-). A kind om Mathematica, but than for software.
评论 #24352427 未加载
评论 #24354412 未加载
评论 #24351577 未加载
评论 #24351634 未加载
divan超过 4 年前
Obligatory mention of Ten Years Reproducibility Challenge <a href="https:&#x2F;&#x2F;github.com&#x2F;ReScience&#x2F;ten-years" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;ReScience&#x2F;ten-years</a>