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

科技回声

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

GitHubTwitter

首页

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

资源链接

HackerNews API原版 HackerNewsNext.js

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

Ask HN: Running a command, are you saying “take this action” or “make this so”?

6 点作者 comice超过 3 年前
When you use a CLI tool, do you feel you&#x27;re saying &quot;take this action&quot; or &quot;make this so&quot;?<p>For example, if you ran a command like this:<p>$ resize 500G file<p>and file was already 500G, should the command return an error code? Or a success code?

8 条评论

selfhoster11超过 3 年前
If the file being already 500G is not an error condition, it shouldn&#x27;t generate an error. Just like a package management operation (refresh software index, remove or add a package) is idempotent, and will just do nothing if the conditions are already met.<p>To address the broader question: it really depends. A command is basically a function, but systemwide: you get to pick whether it&#x27;s a pure function or whether it has side effects, or whether it&#x27;s idempotent. Don&#x27;t emit excessive errors, but don&#x27;t hide them either.
评论 #29271789 未加载
eevilspock超过 3 年前
First off, the name &quot;resize&quot; has the semantics of &quot;take this action&quot;.<p>The &quot;make this so&quot; version would be:<p><pre><code> size 500G file </code></pre> I think you&#x27;re effectively asking, <i>&quot;Should an individual CLI command be imperative or declarative?&quot;</i>[1] I think when possible, declarative is superior.<p>Whether an error should be returned is a separate question. In the declarative &quot;make this so&quot; version, you would not return an error if the file were already 500G (ideally it would not mutate the file).<p>But in the imperative &quot;take this action&quot; version, it depends on the intended semantics of the imperative command. Compare `mkdir` (see tacostakohashi&#x27;s comment) and `chmod`. In some cases you just need to know that you have what you wanted (chmod), and in others you might need to know if an actual occurred (mkdir).<p>---<p>[1]: You might think the question to be <i>imperative vs functional</i>. I&#x27;m not an expert, but I&#x27;m pretty sure purely functional programming is a subset of declarative programming that involves first class and higher order functions. I don&#x27;t know of any CLI shell environments that support that. At the very least, though, a functional version of `size 500G file` would not change the state of the file system, and would instead &quot;return&quot; a 500G version via stdout.
Jtsummers超过 3 年前
The phrasing matters, but in general on a command line I take it as imperative commands or queries. Statements of what <i>should</i> happen (not what <i>state</i> should become true) or queries about the state of the system. &quot;resize&quot; suggests that the size should be changed, the error would be if 500GB can&#x27;t be allocated for the file. If it were already 500GB it amounts to a no-op (though you could signal that this is the case, it shouldn&#x27;t be an error because the file is the appropriate size already).<p>A declarative form might be <i>ensure-size 500G file</i>. With that it&#x27;s clearer that you expect a no-op in the case the file is already the desired size. It could be the exact same command under the hood, but the name suggests a different perspective. Specifically that no-ops are in the set of expected and desirable actions that might come from the command.
rthomas6超过 3 年前
Depends on if I&#x27;m thinking in an imperative or declarative programming style. ;)<p>I typically prefer declarative (functional). I&#x27;ll often have 0-length arrays that I iterate over, and expect the program to do nothing and not generate an error.
tacostakohashi超过 3 年前
It depends, could be either, often depends on flags.<p>For example, mkdir dir (error if it exists) &#x2F; mkdir -p dir (no error if it exists).<p>The latter style allows for creating idempotent scripts, which can be nice.
jiehong超过 3 年前
I’d favour idempotence, because it allows retrying with the same result.<p>So more of a “make it so” approach, but it doesn’t matter if it’s imperative or declarative.<p>That being said, it depends on the action. “Send missile” should be imperative because it destroys something. I wouldn’t even turn it into “ensure 1 missile hit that target”, because it could lead to many missiles could be launch and that’s probably not the amount of control I’d like.
warrenm超过 3 年前
You&#x27;re <i>telling</i> the system to &quot;take this action&quot;<p>The system needs to respond with whether or not it <i>has been made so</i><p>In your example, the proper response from `resize 500G file` is `file is 500G`, or `cannot resize file`
jdlyga超过 3 年前
Think of it this way. Your partner asks you to empty the dishwasher. But the dishwasher is already empty. Is that an error, dishwasher is already empty? Or a guess what it&#x27;s already done, success!