The course DEV275x Writing Professional Code is a very short introduction to best practices when it comes to writing code. As usual, this is only my notes I took during the course, you defiantly should check out the course for yourself at https://courses.edx.org/courses/course-v1:Microsoft+DEV275x+2T2017/course/
Module 1: Elements of Professional Code
Source Control with Git
Source Control is one of the most important aspects of programming.
- Backup of your Source Code
- Ability to compare with changes done in the past
- Restore previous versions if something goes wrong with the new version
- Easy collaboration with other people
There are many different Software packages that enable Source Control. Currently, the two most popular systems are Git (70% of Programmers) followed by SVN (10% of Programmers) (Survey of 30k Developers)
The core difference between the two is, that for SVN you need to set up a dedicated Source Control Server, and all changes are tracked there. With Git it is distributed, so you can use it locally and if you choose in combination with a server.
Especially Code Editors like Visual Studio Code have Git directly integrated making it really easy to set up and use Git.
Markdown is really great because you can learn it really fast, and even if you do not convert Markdown into a HTML site or PDF the text is still formatted quite neatly and readable.
- Markdown - the original created by Jon Gruber in 2004
- CommonMark - a standardization effort for the Markdown language
- GitHub Flavored Markdown - a derivative of CommonMark used on the GitHub site.
Like Git you find support for Markdown files in common editors like Visual Studio Code / Atom. And of course, in blogging software like Wordpress have plugins that enable Markdown for the Posts.
Module 2: Communicate with Code
Now, this chapter was rather interesting, it focused on how those smaller things like code conventions actually help to improve the codebase. While the presenter did not use automated tools to improve the code readability it was nice to see that it is a very important aspect of coding to get the really basic elements correct.
Consistency and Naming
The code should be formatted always in the same matter. It improves the readability and removes all personal style from the code enabling all developers to immediately take ownership of the code instead of saying well that is the style of developer A, he should fix it.
Naming is important and greatly improves the readability of the code it does not help to say
var c = 0, it is much better to say
var beanCounter= 0.
You do not write code for the computer but actually for other human beings. The compiler will then convert it into machine code, but you will probably not have to debug that.
Refactor Duplicate Code
A great problem is when the code base has a lot of duplicate code. As soon as that happens and some minor change changes the way how you do things, then you would have to go back and change all the different places where that piece of code is used.
Refactoring early reduces the risk that the next developer says, well, I will just do that with copy and paste.
This one is rather difficult, but by keeping the code and the structures simple and readable has a much higher benefit in the maintainability of the code than some complex structure that executes a micron second faster. Of course, that depends on the program you are writing.
As a rule of thumb, functions should be rather short, not hundreds of lines long. (Too short is also bad.) If you needed to add complexity then you also should document why you are adding it and what is the best approach to understand that complex structure.
Module 3: Code Confidently With Unit Tests
Well writing Unit Tests and overall having Tests for your code, allows you to a) know the use cases of your code and b) allows you to see when you change something what else may have been broken while you were developing a new feature.