A place to cache linked articles (think custom and personal wayback machine)
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

4 anos atrás
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. title: Our Development Philosophy (2): Collaboration & Testing
  2. url: https://www.git-tower.com/blog/dev-philosophy-2
  3. hash_url: 03f7d88efe4db61d7007bcf736aa789f
  4. <p>We learned in countless situations within our own team how important these skills really are: very often when things went wrong, it had been due to a lack of testing and/or collaboration. Here are some things we've learned while growing <a href="https://www.git-tower.com/" rel="noopener noreferrer" target="_blank">Tower</a> to serve almost 100,000 users.</p>
  5. <p><br/></p>
  6. <h2>All Code Belongs to the Team</h2>
  7. <figure class="cartoon"><img src="https://www.git-tower.com/blog/content/posts/103-dev-philosophy-2/cartoon-code-belongs-to-the-team.svg" alt=""/></figure>
  8. <p><strong>Code is teamwork, not the work of any individual developer.</strong> This also means that none of the code you've written belongs to you – it belongs to the team! Looking at code in this way is helpful and liberating for a couple of reasons:</p>
  9. <ul>
  10. <li>
  11. <p><strong>It doesn't have to go your way.</strong> After understanding that code always belongs to the team, it becomes obvious that we need to comply with coding standards and guidelines. Since it's not your code you can't just write it any way you fancy. It's a common code base, so common rules must be adhered.<br/>
  12. This should also make you more tolerant when looking at your colleagues' code. You might instinctively think of all the ways you would have done it differently. But again: it doesn't have to go your way, but your team's way. This can help a lot in making you more open-minded towards other solutions and approaches.</p>
  13. </li>
  14. <li>
  15. <p><strong>Everything is your problem.</strong> When you spot a potential problem in the code, it doesn't really matter if it was your fault for not. Since all of the project's code belongs to the team – and you are part of this team – weak code from any source becomes your problem.<br/>
  16. Contrast this to an atmosphere where people are detached from their teams and their shared code: "this is not my problem" is an attitude that will never produce great products.</p>
  17. </li>
  18. <li><strong>You. You. You.</strong> I learned this one from <a href="https://opbeat.com/" rel="noopener noreferrer" target="_blank">opbeat's</a> <a href="https://twitter.com/roncohen" rel="noopener noreferrer" target="_blank">Ron Cohen</a> when I heard him speak at a conference. It's a simple reminder of our responsibilities as software developers: <strong>You</strong> are responsible for writing code. Then, <strong>you</strong> are also responsible for making it shippable or production ready. And of course, <strong>you</strong> are responsible for fixing any problems with it.<br/>
  19. In our own team, where we don't have a dedicated quality assurance team, this reminded us that developers are not only responsible for writing down lines of code – but also for the quality of that code. This responsibility is nothing that you can duck out of or hand over to someone else.</li>
  20. </ul>
  21. <div class="canned-tweet"> <p class="tweet-button"/> <blockquote> <span>"Code is teamwork, not the work of any individual developer." </span> </blockquote> </div>
  22. <h2>Sharing Knowledge with Code Reviews</h2>
  23. <p>Code reviews have a huge potential to lift your development team to new heights.</p>
  24. <p>Most obviously, discussing code in a review session can lead to a better solution than the original developer had had on her own. Two minds working together on the same problem will always have the chance to produce higher quality code.</p>
  25. <p>But other potential benefits of code reviews might be even more interesting. Shared ownership, for example, is crucial to reduce knowledge silos: whenever you find that only a single developer in your team knows a certain piece of code, you are at risk. Whenever this developer goes on holidays, gets sick, or maybe even leaves the company you'll be in trouble.<br/>
  26. But even if none of these situations occur: the fact that your team doesn't seem to have a truly shared code base is already telling. Have a look at the previous section and you'll understand why.</p>
  27. <p>Additionally, code reviews can solve the hairy problem of knowledge exchange. If, like most teams, you have a mixture of junior and senior developers, you will certainly want those junior developers to benefit from your experts' experience and knowledge. Code reviews are an excellent way to pass on knowledge in situations like these.</p>
  28. <p>And to make the process worthwhile for both sides (juniors and seniors), make sure your senior developers understand that both parties – the student and the teacher – will learn something on the way!</p>
  29. <div class="canned-tweet"> <p class="tweet-button"/> <blockquote> <span>"Senior developers should understand that they, too, might learn something when mentoring a junior colleague!" </span> </blockquote> </div>
  30. <h2>Not Everything Needs Improvement</h2>
  31. <figure class="cartoon"><img src="https://www.git-tower.com/blog/content/posts/103-dev-philosophy-2/cartoon-fast-car.svg" alt=""/></figure>
  32. <p>Since there is no such thing as a perfect solution, you could argue that everything can be improved. And you're probably right. But the interesting question is not if something can be improved – but rather if it's worthwhile to improve a certain part in your application.</p>
  33. <p>We know where our own product, <a href="https://www.git-tower.com">Tower</a>, has its weak spots. And if time was infinite, we would probably polish and improve every single bit. But with time being a scarce resource for any team you will have to set priorities. If you don't, your product will not take the direction you want it to take - or it might very well take no direction at all.</p>
  34. <p>Being able to responsibly ponder if or if not to improve something really is an art. It takes years of experience and, in the best case, even conscious training. Saying no to a lot of things is a prerequisite for achieving anything of worth.</p>
  35. <div class="canned-tweet"> <p class="tweet-button"/> <blockquote> <span>"Saying no to a lot of things is a prerequisite for achieving anything of worth." </span> </blockquote> </div>
  36. <p>True in life, and true in software development.</p>
  37. <h2>Ask Questions</h2>
  38. <p>When reading other people's code it's all too easy to assume that you would have done it better. On first encounter, this or that line might seem unnecessarily complex or even superfluous. And you might of course be right. But it's very hard to say because of one simple fact: you didn't actually <em>write</em> the code.</p>
  39. <p>From our own experience, we know that even the simplest problem can really contain a surprising and infinite amount of complexity. Only when actually working on the problem – when picking it apart, thinking about possible solutions, side effects, etc. – will one be able to understand its scope.</p>
  40. <p>Therefore, when coming across a piece of code that makes you suspicious, it's a great ground rule to <strong>assume a good amount of hidden complexity</strong>. From this point of view you can either dive deep into the code and try to understand it and the underlying problem – or you simply get into a conversation with your teammate(s) and ask some humble questions.</p>
  41. <h2>Software is Only Complete with Tests</h2>
  42. <figure class="cartoon"><img src="https://www.git-tower.com/blog/content/posts/103-dev-philosophy-2/cartoon-developer-tasks.svg" alt=""/></figure>
  43. <p>Unit tests are sometimes regarded as the icing on the cake. More appropriately, however, they would be better described as the cake's <em>base</em>.</p>
  44. <p>In smaller projects, you might get away without writing unit tests. In any project with just a slight amount of complexity, however, you will not. And, just as a side note, it's remarkable to see how many projects started simple and very soon became complex. So please don't act surprised.</p>
  45. <p>There are indeed countless reasons why unit testing is essential for us:</p>
  46. <ul>
  47. <li><strong>Making big changes, safely and quickly.</strong> In every larger project, you will have to perform refactorings at some point. But refactoring code without a decent amount of unit tests in place is a very frustrating experience: While fixing something you will often and inevitably break something else. Unit tests let you know when this happens.</li>
  48. <li><strong>Releasing to the public, confidently.</strong> If your application is shipped to paying customers, not having a solid unit test coverage should make you shiver every time you make a release.</li>
  49. <li><strong>Better code through tests.</strong> Writing good tests is one of the best preconditions to have good code in the end. This is because tests help you understand the problem you're trying to solve and create a simple, modular architecture.</li>
  50. <li><strong>Up-to-date documentation.</strong> In an ideal world, every important part of an application would be nicely documented. We would have wonderful, clean, and beautifully formatted documents that are a joy to read. And most importantly, we would update the documentation every time something in the code changes. Well... this is of course a fairytale.<br/>
  51. But having up to date documentation in general does <em>not</em> have to be a fairytale: unit tests can serve this purpose. Although probably not as <em>pleasant</em> to read as dedicated documentation, they are (almost by definition) always up to date. This makes unit tests a very valuable and reliable source of documentation.</li>
  52. </ul>
  53. <div class="canned-tweet"> <p class="tweet-button"/> <blockquote> <span>"If you have paying customers, not having unit tests for your app should make you shiver every time you ship it." </span> </blockquote> </div>
  54. <p>Good test <em>quality</em> and good test <em>coverage</em> of important code parts are mandatory for larger projects. It made us much more confident that we would always be able to deliver <a href="https://www.git-tower.com">Tower</a> with a high quality and that we would always be able to make changes (big and small) safely and effectively.</p>
  55. <h2>Tests Make Development Faster</h2>
  56. <p>At first glance, using a test-driven approach would seem to be time-consuming and slow down your progress. And indeed, writing good unit tests takes time.</p>
  57. <p>But over time, you will find that unit testing will in fact make you faster! For one thing, after thinking about your code design while writing a test, you will need less time to write the actual implementation code. You will have done a lot of the heavy lifting already by writing the tests.</p>
  58. <p>The other benefit that will make you a lot more effective in the long run is stability: the likelihood of breaking existing code when making changes is significantly reduced through tests. You will save countless hours of bug hunting that you can now invest in something more useful.</p>
  59. <h2>Fixing by Testing</h2>
  60. <figure class="cartoon"><img src="https://www.git-tower.com/blog/content/posts/103-dev-philosophy-2/cartoon-bugs-computer.svg" alt=""/></figure>
  61. <p>Bug fixing should be inseparably connected with writing tests. Of course you could just track that bloody animal down and simply fix the code. However, I guess that a lot of you are painfully familiar with the term "regression": the annoying re-emergence of a bug that you were so confident to have fixed.</p>
  62. <p>If you want to make sure (and you should!) that a bug is fixed for good, there is only on proven way - by writing a test to safeguard this piece of code.</p>
  63. <h2>Test Low-Level Components</h2>
  64. <p>While producing a <a href="https://www.git-tower.com">software product that is in daily use by almost 100,000 people</a>, we learned a lot about errors. For one thing, we learned that they are an inevitable part of the business; especially with the diverse and complex system setups that Tower has to deal with, you cannot rule out and safeguard everything. But for another thing, we also learned <em>where</em> errors mostly occured.</p>
  65. <p>In Tower (and most likely in a lot of other larger applications), most errors tend to occur in low level components. The good thing about this is that such components lend themselves very well to unit testing. In this area, a test-driven approach offers a huge chance to improve your application's overall stability and quality with a reasonable investment – by thoroughly testing low-level components.</p>
  66. <h2>Efficient Testing</h2>
  67. <figure class="cartoon"><img src="https://www.git-tower.com/blog/content/posts/103-dev-philosophy-2/cartoon-assert-test.svg" alt=""/></figure>
  68. <p>In general, having a large amount of your code covered by unit tests is a good thing. However, if you ever hear a software developer brag about his "100% test coverage", you should be wary. Testing every single line of your application code might sound like something to strive for - but it really isn't!</p>
  69. <p>On the one hand, this would of course afford an infinite amount of time. Much more importantly, however, there <em>is</em> in fact something like writing too many tests! At the latest, you will learn this when making a bigger refectoring: not only your code, but also your tests have to be maintained. When your code changes, your tests need to change, too.</p>
  70. <p>The crucial criteria is <em>value</em>. Does the test bring you enough value to justify writing it? Typically, tests are of <em>low</em> value if they test only very simple logic. In these cases, the burden of having to maintain the test outweighs the little value that it brings.</p>
  71. <p>Writing tests <em>efficiently</em>, then, means preferring fewer high-value tests over many low value tests.</p>
  72. <h2>Test, Don't Run</h2>
  73. <p>Manually testing a certain scenario in your application can really take a huge amount of time: you have to start the app, navigate your way to a certain view, and perform various additional actions to reproduce a certain scenario.</p>
  74. <p>Writing a unit test for a scenario will indeed afford some time initially. But very quickly, probably already after having to test this only a couple of times, running the test will be much faster than starting the whole application and testing your way through it manually.</p>
  75. <p>Besides being faster, however, the test will also provide better results in terms of quality and stability. In most cases, the initial effort is worth the long time benefits.</p>