Just playing, code review?

xuanbao · · 530 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<p><a href="http://pastebin.com/6NRis0yP" rel="nofollow">http://pastebin.com/6NRis0yP</a></p> <p>Not a programmer but thought I would take a crack at Rock Paper Scissors in GO. Seemed to go pretty well, not sure if my process would be considered &#34;proper&#34; but was hoping to get some ideas on how it could be incrementally improved?</p> <p>Everything I pretty much did just from the documentation, the only exception is the random function which I was banging my head on.</p> <hr/>**评论:**<br/><br/>SaidinWoT: <pre><p>Annotated a bit here: <a href="http://play.golang.org/p/eTwkCArUvF" rel="nofollow">http://play.golang.org/p/eTwkCArUvF</a> Note that block comments like that aren&#39;t really &#34;the Go way&#34; - I merely wanted them to be visually distinct from your comments.</p> <p>I iterated on it a bit while attempting to stick with the overall flow that you had created. Below is the progression if such a thing would be useful in learning:</p> <p>With the suggested clean-ups: <a href="http://play.golang.org/p/6JtARfM2_5" rel="nofollow">http://play.golang.org/p/6JtARfM2_5</a></p> <p>A bit more idiomatic: <a href="http://play.golang.org/p/J2TWA11Z4n" rel="nofollow">http://play.golang.org/p/J2TWA11Z4n</a></p> <p>And finally, a decent bit more idiomatic (and annotated) while still sticking to the general form of the original: <a href="http://play.golang.org/p/pxYw4vlKsa" rel="nofollow">http://play.golang.org/p/pxYw4vlKsa</a></p> <p>Disclaimer: I&#39;m tired and only wrote these up in the playground, which is less than ideal for testing programs expecting input from a user. I hope they are educational but cannot guarantee that they are fully correct. All versions do at least compile.</p></pre>monkeywork: <pre><p>Thank you very much, I&#39;m glad to know that for my first time doing anything in go and first time coding anything in a long while that it wasn&#39;t too incredibly wrong - the cleanups do definitely make sense.</p> <p>The more idiomatic versions I will need review closely and make sure I understand. </p></pre>barsonme: <pre><p>It&#39;s interesting how we both wrote pretty much the same code...</p> <p><a href="https://play.golang.org/p/ZI4Vt6a2L5" rel="nofollow">https://play.golang.org/p/ZI4Vt6a2L5</a></p></pre>mvtmrs: <pre><p>Why are you using <code>float64</code> instead of <code>int</code> for storing the guesses?</p> <p>Also, you should probably just call <code>rand.Seed(time.Now().Unix())</code> once at the start of the program.</p></pre>monkeywork: <pre><p>When I was playing with the math.mod I ran into a case where it seemed to want me to use float and in a haste to make it work I converted that from int to float.</p> <p>The reason for the seed being in the function was I was intending to expand the program to loop the game several times with a score board tracking.</p></pre>shazow: <pre><p>It&#39;s a bad habit to seed rand more than once. It gives you no benefit, and it actually introduces a security vulnerability if you ever end up in a position where that matters.</p> <p>The vulnerability is that when you control when the random number generator is seeded (ie. current timestamp of the request), then you can easily guess what the next random value will be (given a known seed, the sequence of random numbers is deterministic). And of course for anything security-sensitive, crypto/rand should be used instead but that&#39;s even further out of scope here.</p> <p>Obviously not an issue with a toy game, but still a bad habit. :)</p> <p>On the other hand, if you&#39;re writing tests, then being able to seed the engine on demand is really valuable for deterministic results. Better yet, replace the random number generator with a deterministic one you mocked yourself.</p></pre>akiross: <pre><p>A little OT, but you forgot lizard and spock :D</p></pre>monkeywork: <pre><p>:)</p></pre>

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

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