<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Yannick Rehberger</title>
    <description>The latest articles on DEV Community by Yannick Rehberger (@yannick_rest).</description>
    <link>https://dev.to/yannick_rest</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F12286%2F7878e76b-1ffd-47e8-b697-a8a3bdaca5c5.PNG</url>
      <title>DEV Community: Yannick Rehberger</title>
      <link>https://dev.to/yannick_rest</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/yannick_rest"/>
    <language>en</language>
    <item>
      <title>Simple setup for Static Code Analysis</title>
      <dc:creator>Yannick Rehberger</dc:creator>
      <pubDate>Wed, 09 Dec 2020 19:29:17 +0000</pubDate>
      <link>https://dev.to/yannick_rest/simple-setup-for-static-code-analysis-4f58</link>
      <guid>https://dev.to/yannick_rest/simple-setup-for-static-code-analysis-4f58</guid>
      <description>&lt;p&gt;When I'm working on small to medium-sized private development projects it happens from time to time that I look at my code I have developed over weeks and months and ask myself - is that a good thing?&lt;/p&gt;

&lt;p&gt;My impulsive answer is yes, because I put a lot of time and passion into this code. But sometimes I would like someone to confirm that or someone who gives me tips on what I can improve. You don't always have someone available to do a review.&lt;/p&gt;

&lt;p&gt;I don't necessarily want to know whether the capabilities are good, real users can tell me that later, but at this point I want to know whether the code is technically clean.&lt;/p&gt;

&lt;p&gt;One tool that has helped me with this problem is Static Code Analysis.&lt;/p&gt;

&lt;h1&gt;
  
  
  Static Code Analysis
&lt;/h1&gt;

&lt;blockquote&gt;
&lt;p&gt;Static program analysis is the analysis of computer software that is performed without actually executing programs, in contrast with dynamic analysis, which is analysis performed on programs while they are executing.[1] In most cases the analysis is performed on some version of the source code, and in the other cases, some form of the object code.&lt;/p&gt;

&lt;p&gt;The term is usually applied to the analysis performed by an automated tool, with human analysis being called program understanding, program comprehension, or code review. Software inspections and software walkthroughs are also used in the latter case.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Source: &lt;a href="https://en.wikipedia.org/wiki/Static_program_analysis"&gt;Wikipedia&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;So this static analysis of your code can't tell us how the runtime behaviour of our program exactly is, but it can tell us that we are using features in a way, that e.g. could degrade performance. It can't say something about the dynamic behaviour of the program in a specific environment, but it can say a lot about our static code. It's about the following:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Reliability - "You have some Bugs in your code."&lt;/li&gt;
&lt;li&gt;Security - "There is a vulnerability that could be exploited."&lt;/li&gt;
&lt;li&gt;Maintainability - "Please have a look at that code smells."&lt;/li&gt;
&lt;li&gt;Coverage - "Write some more unit tests, your test coverage is too low."&lt;/li&gt;
&lt;li&gt;Duplications - "Put that code duplication into a function."&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;You can also understand why an improvement is proposed and see some compliance and not compliant examples. If it's something you accept, you can configure the rules that are checked.&lt;/p&gt;

&lt;p&gt;There are actually many &lt;a href="https://en.wikipedia.org/wiki/List_of_tools_for_static_code_analysis"&gt;implementations of Static Code Analysis&lt;/a&gt;, but I would like to take &lt;a href="https://www.sonarsource.com/plans-and-pricing/community/"&gt;SonarQube&lt;/a&gt; as an example. Interest in it has grown steadily since 2013 and it supports many different languages.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--QAN9zymK--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/bb5v8v81x5mwf8qgw6xn.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--QAN9zymK--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/bb5v8v81x5mwf8qgw6xn.png" alt="SonarQube Stats"&gt;&lt;/a&gt;&lt;br&gt;
Source: &lt;a href="https://trends.google.com/trends/explore?date=all&amp;amp;q=SonarQube"&gt;Google Trends&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Annotation&lt;/strong&gt;: SonarQube is my example because it's well known in my perimeter, it's OpenSource and there is a Community Edition. I have made no comparisons between different tools. I would rather encourage you to present other implementations of Static Code Analysis as a comment here.&lt;/p&gt;
&lt;h1&gt;
  
  
  Setup SonarQube with Docker locally
&lt;/h1&gt;

&lt;p&gt;&lt;a href="https://www.docker.com"&gt;Docker&lt;/a&gt; makes it easy to setup SonarQube locally and you can use an &lt;a href="https://hub.docker.com/_/sonarqube/"&gt;image from Docker Hub&lt;/a&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;docker pull sonarqube
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Once you've pulled the image, you can run your SonarQube Container.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;docker run -d --name sonarqube -p 9000:9000 sonarqube
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now meet SonarQube at &lt;a href="http://localhost:9000"&gt;http://localhost:9000&lt;/a&gt;. You can login with &lt;em&gt;admin&lt;/em&gt; as username and password.&lt;/p&gt;

&lt;h2&gt;
  
  
  Java - Example
&lt;/h2&gt;

&lt;p&gt;You can analyse Java projects using &lt;a href="https://maven.apache.org"&gt;Maven&lt;/a&gt; or &lt;a href="https://gradle.org"&gt;Gradle&lt;/a&gt;. In my example I use Maven. So I create a project in SonarQube called &lt;em&gt;java-sample&lt;/em&gt; and generate a key for it. To analyse my Java project I simply have to run the Maven sonar goal.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;mvn sonar:sonar \
  -Dsonar.projectKey=java-sample \
  -Dsonar.host.url=http://localhost:9000 \
  -Dsonar.login=f84f7b8d932b3348ca1ea61d734e537a19d27cde
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can select the Bugs or the Code Smells to have a closer look to the affected lines of code. And if you want to understand why SonarQube gives you a hint for that specific case, you can click on &lt;em&gt;See Rule&lt;/em&gt; and you get the more specific explanation.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--RFUmHq1F--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/8wtruv388hj92ao8dpiv.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--RFUmHq1F--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/8wtruv388hj92ao8dpiv.png" alt="Rule Sample"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;There is also a code example for noncompliant code and one for compliant code.&lt;/p&gt;

&lt;p&gt;For most of the other languages you need sonar scanner. My example for this is a python project.&lt;/p&gt;

&lt;h2&gt;
  
  
  Python
&lt;/h2&gt;

&lt;p&gt;You can install sonar scanner on all operating systems. In my case it's MacOS.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;brew install sonar-scanner
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I create a new project in SonarQube, call it &lt;em&gt;python-sample&lt;/em&gt; and generate a new key for it. To analyse my Python project I simply have to run sonar scanner inside my python project root.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sonar-scanner \
  -Dsonar.projectKey=python-sample \
  -Dsonar.sources=. \
  -Dsonar.host.url=http://localhost:9000 \
  -Dsonar.login=d2734c766021037d05476283c1f7169dfc96a21d
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As you can see here, sonar scanner does not simply analyse the Python code but every code that it can understand. In this project we have for example 19k lines of JavaScript code, which were also analysed.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--fw1-F5kE--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/a521zsnqqpgcekhiybvj.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--fw1-F5kE--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/a521zsnqqpgcekhiybvj.png" alt="Different Languages Sample"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  Conclusion
&lt;/h1&gt;

&lt;p&gt;So you have seen how simple it is to setup a static code analysis tool, which analyses your Code and gives you helpful hints to make it better. I think it's a good way to get some sort of an external perspective. If you want to go one step further with a persistent database to have a historic analysis of your code you can do that too. But before it gets too complex I think you should consider to integrate this static code analysis inside a CI/CD pipeline like you would do in a larger context.&lt;/p&gt;

&lt;p&gt;SonarQube and other tools for static code analysis have much more features than we've seen in this short post. Maybe there is a more lightweight one for local quality checks. If you know one, leave a comment. There may also be many plugins for IDE's that gives you the same functionality without setting up a separate Docker Container for static code analysis. If you have recommendations, please leave a comment.&lt;/p&gt;

</description>
      <category>staticcodeanalysis</category>
      <category>codesmells</category>
      <category>sonarqube</category>
      <category>docker</category>
    </item>
    <item>
      <title>Can you recommend a book for practical learning of Go?</title>
      <dc:creator>Yannick Rehberger</dc:creator>
      <pubDate>Mon, 19 Oct 2020 07:59:48 +0000</pubDate>
      <link>https://dev.to/yannick_rest/can-you-recommend-a-book-for-practical-learning-of-go-1nga</link>
      <guid>https://dev.to/yannick_rest/can-you-recommend-a-book-for-practical-learning-of-go-1nga</guid>
      <description>&lt;p&gt;As the title suggests, I am looking for a book that teaches Go in a practical way. That does not mean that it should just be a demo application that the reader copies. In my view a good book that teaches a programming language or framework contains the following:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Basic setup of the development environment&lt;/li&gt;
&lt;li&gt;Chapters that teach the language/framework with practical examples and that references the original documentation for details&lt;/li&gt;
&lt;li&gt;Hints and best practices for common challanges&lt;/li&gt;
&lt;li&gt;Chapters about Build and Deployment&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In addition to that it could also contain:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Some historic information about the language/framework&lt;/li&gt;
&lt;li&gt;Chapters for advanced features of the language/framework&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Of course, soft factors like the author's writing style and the illustrations also play a role.&lt;/p&gt;

&lt;p&gt;For me a good example for a practical book that teaches a language or framework is "Two Scoops of Django" by Daniel and Audrey Roy Greenfeld. &lt;a href="https://www.djangoproject.com/"&gt;Django&lt;/a&gt; is a python web framework. I bought &lt;a href="https://www.feldroy.com/collections/two-scoops-press/products/two-scoops-of-django-1-8"&gt;the book for Django version 1.8&lt;/a&gt; in 2016 and it was exactly what i was looking for.&lt;/p&gt;

&lt;p&gt;Now I would really like to learn Go and it works best for me with the combination of a good book and a private side project.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;So can you recommend a good book to help me learn Go?&lt;/li&gt;
&lt;li&gt;You may also want to comment on the criteria for a good book about languages and frameworks.&lt;/li&gt;
&lt;/ol&gt;

</description>
      <category>discuss</category>
      <category>learn</category>
      <category>go</category>
      <category>firstpost</category>
    </item>
  </channel>
</rss>
