Fighting (and loosing?) the `val, _ := errorReturningFunction` anti-pantern on StackOverflow answers

polaris · · 804 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<p>It would appear that some people at StackOverflow think that asking people leaving Go code in answers to at least use <code>v, err := …</code> instead of <code>v, _ := …</code> is too onerous. :(</p> <p>Edit: Opps, lost the link: <a href="http://meta.stackoverflow.com/q/290223/55504">http://meta.stackoverflow.com/q/290223/55504</a> and <a href="http://meta.stackoverflow.com/a/290224/55504">http://meta.stackoverflow.com/a/290224/55504</a></p> <hr/>**评论:**<br/><br/>elithrar_: <pre><p>Hah, I saw your comment discussion on SO with another user about this. I agree with you - ensuring your example code handles errors in an environment <em>guaranteed</em> to have newbies is really useful.</p> <p>I don&#39;t think anyone is expecting some vast error-handling logic that distracts from the problem, but either:</p> <pre><code>a, err := someFunc() // handle error </code></pre> <p>or...</p> <pre><code>b, err := someFunc() if err != nil { // handle error { </code></pre> <p>... is not inappropriate. </p></pre>dchapes: <pre><p>Indeed. I was surprised at the slight backlash (or perhaps I imagined this) to this suggestion. The answer on which I commented (not the first) about this was &#34;fixed&#34; just by changing <code>, err</code> from <code>, _</code> one place and adding <code>err =</code> to a <code>rows.Scan</code> call. Hardly too much to ask for IMO and yet making a big difference (again IMO).</p> <p>I wonder if perhaps some people thought I was implying that nothing should be simplified or some such.</p> <p>With respect to Go I also don&#39;t get those that said that &#34;it&#39;s distracting&#34;. Shouldn&#39;t all good Go code have the appropriate level of error checking? Does this mean that we&#39;re constantly being distracted when reviewing/looking-at Go code? (Some of course think so, some even think the solution is exceptions or some such). If we&#39;re used to typing it in, and if we&#39;re used to seeing it, then why shouldn&#39;t it be in all answers? Of course I use short-cuts like <code>log.Fatal(err)</code> much more on SO then in &#34;real code&#34;.</p> <p>Perhaps those that were downvoting me on SO aren&#39;t aware of how common it is to see &#34;newbies&#34; cut-n-paste code they don&#39;t understand. It&#39;s fine to say that&#39;s foolish and they deserve what they get but this doesn&#39;t just effect them; it effects everyone that tries to help them (here, on SO, on go-nuts, at the workplace, whatever) when they waste everyone&#39;s time asking why they get &#34;some random panic&#34; in their code and don&#39;t realize they have <code>f, _ := os.Open(&#34;filenam&#34;)</code> and made a typo on the filename.</p> <p>I wonder if some of these people even know what <code>_</code> means or if they think it&#39;s just some magic invocation they &#34;need to do&#34; because &#34;that guy on the blog I read did it!&#34;. One of my pet peeves is seeing anyone <em>ever</em> do something like <code>y, _ := x.(int)</code>; I can only think of one extremely rare case where using the two valued version of a type assertion but ignoring the bool wouldn&#39;t be out-right wrong. (But I&#39;ve seen exactly that repeatedly on SO!)</p> <p>&lt;/rant&gt; :)</p></pre>DavidDavidsonsGhost: <pre><p>Why is the second example not appropriate? Seems pretty clear what you need to do. Error handling is its own topic and something the devneeds to think about.</p></pre>the_starbase_kolob: <pre><p>He said it&#39;s not inappropriate </p></pre>Bromlife: <pre><p>Loost the link?</p></pre>dchapes: <pre><p>I screwed it up. I normally don&#39;t start reddit posts, I meant this to be a link post but I mistakenly switched it to a text post without realising that completely removed the link I had entered. At least I noticed right away and immediately edited the link into the text.</p></pre>Bromlife: <pre><p>Sorry, I was being a dick and making fun of the &#34;loosing&#34; typo in your title.</p> <p>It&#39;s one of my pet hates when people say loosing instead of losing.</p></pre>anoland: <pre><p>Coming from PHP land, where there are tons of bad examples, I think it ultimately doesn&#39;t matter. It isn&#39;t your job to make sure somebody else&#39;s code compiles or is even safe. The onus is on the programmer to make sure that their work is working and is safe in their environment. Excessively harping on them about handling errors, or down voting an otherwise helpful answer isn&#39;t conducive to someone learning something new.</p> <p>Edited to add: unless the question is specifically about how to handle some <del>error</del> boilerplate problem, ignoring boilerplate should be fine.</p></pre>dchapes: <pre><p>I somewhat agree but on the other hand I can&#39;t stand the outright incompetence I see all around the software industry. And it may not be my job but it is my (and everyone&#39;s) problem when we have to use software written by people that are barely competent.</p> <p>I&#39;m reminded of:</p> <blockquote> <p>Weinberg&#39;s Second Law: If builders built buildings the way programmers wrote programs, then the first woodpecker that came along would destroy civilization. -- Gerald Weinberg</p> </blockquote></pre>klaaax: <pre><p>Yes it does matter. You need to educate people or general go code quality will go down. If you behave a like an error handling nazi ,that will piss off people a bit, but they will behave like this themselves after sometime, because they know it really matters.</p></pre>: <pre><p>[deleted]</p></pre>dchapes: <pre><p>IMO if the example is incomplete than it&#39;s fine for it not to compile (e.g. just giving a handful of lines as example of how to use some package or something) and that clues in the would be copy-n-paster that it is incomplete. (i.e. even without the comment I prefer your first example).</p> <p>If the example is complete or is meant to compile (or be run on the playground) than it should probably at least have a <code>panic(err)</code> (I prefer <code>log.Fatal(err)</code> for examples) or <code>return (err)</code>. It&#39;s really not much to write (we&#39;re all used to it) and it&#39;s really not too distracting (after all, the code we look at all the time does this and it doesn&#39;t distract us).</p> <p>And if you&#39;re writing a complete example for the playground you tested that it runs correctly, right? That often means having checked the errors (unless, unlike me, you never make a mistake while assembling your example).</p></pre>gogolang: <pre><p>I could go either way on this. I&#39;m inclined to say errors should be ignored for the sake of brevity unless the error has to do with the question. </p> <p>If someone has to investigate a bug that has nothing to do with the error, it&#39;s easier to look through code that ignored the error checking.</p> <p>For example, for questions related to something like Read in <a href="http://golang.org/pkg/io/#Reader" rel="nofollow">io.Reader</a> where error value doesn&#39;t just take a nil/non-nil value, it makes sense to take the err value and do something with it.</p> <p>On the other hand, if someone on StackOverflow is just doing this:</p> <pre><code>if (err != nil) { fmt.Printf(&#34;err=%s\n, err) return } </code></pre> <p>That&#39;s just 4 useless boilerplate lines. </p> <p>EDIT: Dammit, I can&#39;t get reddit to show the line breaks in the code block</p></pre>mwholt: <pre><p>I have mixed feelings about this. It depends who your audience is, I think, and the audience varies <em>greatly</em> on Stack Overflow. Basically, if you know your audience is mainly experienced Go programmers, you can focus the example on relevant code by ignoring errors (for the sake of brevity in a code snippet).</p> <p>If the reader is a newbie, though, it&#39;s more likely that they&#39;ll forget to handle the error if the snippet doesn&#39;t. On the other hand, if you do handle the error in your code snippet, though, the reader may forget to change it to accommodate their program. In the latter case, you&#39;ve essentially assumed that you know better than the reader how to how to handle errors for the reader&#39;s situation which you actually know nothing about. It also makes the snippet more confusing, since who knows -- especially a newbie -- if the way you&#39;ve handled the error is essential for the snippet to work correctly! (For example, continuing program execution through <code>log.Println</code> vs. terminating it with <code>log.Fatal</code>.)</p> <p>I think a comment to <code>// handle err</code> is appropriate and, I daresay, the snippet ought not compile on its own until the reader (the &#34;copier+paster&#34;) modifies it so that <code>err</code> is not declared and unused any more.</p></pre>sigmonsays: <pre><p>Please spell losing..you&#39;re not loose..</p></pre>dchapes: <pre><p>D&#39;oh, sorry. Reddit doesn&#39;t let me edit title text. I&#39;ll be more careful next time I post.</p></pre>

入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889

804 次点击  
加入收藏 微博
0 回复
暂无回复
添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传