mirror of
https://github.com/Pungyeon/clean-go-article.git
synced 2024-11-23 14:14:05 +00:00
Edit README up until Naming Conventions
This commit is contained in:
parent
bbf5a24a56
commit
672aecd78d
34
README.md
34
README.md
|
@ -1,22 +1,22 @@
|
||||||
# Clean Go Code
|
# Clean Go Code
|
||||||
|
|
||||||
## Preface
|
## Preface: Why Write Clean Code?
|
||||||
|
|
||||||
The motivation behind writing this document, is to create a resource (and eventually a reference) for the Go community, which will help developers write cleaner code. This benefits every one of us. Whether we are writing code by ourselves, or writing code in larger teams. Establishing good paradigms for writing clean code and ensuring that this is available for everyone, will help prevent many meaningless hours on trying to understand and parse others (and our own) code.
|
This document is a reference for the Go community that aims to help developers write cleaner code. Whether you're working on a personal project or as part of a larger team, writing clean code is an important skill to have. Establishing good paradigms and consistent, accessible standards for writing clean code can help prevent developers from wasting many meaningless hours on trying to understand their own (or others') work.
|
||||||
|
|
||||||
<p align=center style="font-style: italic">We don’t read code, we <b>decode</b> it - Peter Seibel </p>
|
<blockquote><em>We don’t read code, we <b>decode</b> it – Peter Seibel</em></blockquote>
|
||||||
|
|
||||||
The matter of the fact is, as Peter Seibel put it. We decode code and we honestly can't help encoding it, in some way, shape or form. This document, will be a precursor for us, to make sure that our encoding method is effective. We want our code to be usable, readable and maintainable.
|
As developers, we're sometimes tempted to write code in a way that's convenient for the time being without regard for best practices; this makes code reviews and testing more difficult. In a sense, we're <em>encoding</em>—and, in doing so, making it more difficult for others to decode our work. But we want our code to be usable, readable, and maintainable. And that requires coding the <em>right</em> way, not the easy way.
|
||||||
|
|
||||||
This document will start with a simple and short introduction to the fundamentals behind writing clean code and will thereafter transition into concrete refactoring examples, more specific to Go. The aim of the document is to deliver the message of how easy it is to write clean code and how easy is it to write code, when it's clean.
|
This document begins with a simple and short introduction to the fundamentals of writing clean code. Later, we'll discuss concrete refactoring examples specific to Go.
|
||||||
|
|
||||||
##### A short word on `gofmt`
|
##### A short word on `gofmt`
|
||||||
I would like to take a few sentences to make my stance on `gofmt` very clear. There are extremely many things that I disagree with, when it comes to `gofmt`. I prefer snake case over camel case, I quite like my constant variables to be upper case and I also have many opinions on bracket placement. *That being said*: `gofmt` is what enables us to have a common standard for writing Go code. All Go code, will look somewhat similar and it ensures that no Go code becomes too exoteric. I think that this is overall extremely positive. I appreciate immensely, that all Go programmers are somewhat restricted to write similar code, despite being very unhappy with some of the formatting rules. In my opinion, I value homogenous code over complete expressive freedom.
|
I'd like to take a few sentences to clarify my stance on `gofmt` because there are plenty of things I disagree with when it comes to this tool. I prefer snake case over camel case, and I quite like my constant variables to be uppercase. And, naturally, I also have many opinions on bracket placement. *That being said*, `gofmt` does allow us to have a common standard for writing Go code, and that's a great thing. As a developer myself, I can certainly appreciate that Go programmers may feel somewhat restricted by `gofmt`, especially if they disagree with some of its rules. But in my opinion, homogenous code is more important than having complete expressive freedom.
|
||||||
|
|
||||||
## Context
|
## Table of Contents
|
||||||
* [Introduction to Clean Code](#Introduction-to-Clean-Code)
|
* [Introduction to Clean Code](#Introduction-to-Clean-Code)
|
||||||
* [Test Driven Development](#Test-Driven-Development)
|
* [Test-Driven Development](#Test-Driven-Development)
|
||||||
* [Naming](#Naming)
|
* [Naming Conventions](#Naming)
|
||||||
* * [Comments](#Comments)
|
* * [Comments](#Comments)
|
||||||
* [Function Naming](#Function-Naming)
|
* [Function Naming](#Function-Naming)
|
||||||
* [Variable Naming](#Variable-Naming)
|
* [Variable Naming](#Variable-Naming)
|
||||||
|
@ -38,22 +38,22 @@ I would like to take a few sentences to make my stance on `gofmt` very clear. Th
|
||||||
|
|
||||||
## Introduction to Clean Code
|
## Introduction to Clean Code
|
||||||
|
|
||||||
Clean Code, is the pragmatic concept of ensuring readable and maintainable code. Clean Code establishes trust in the codebase and will steer developers away from introducing bugs. Clean Code will also establish much more stability in development speed, which typically will take a nose dive in the later stages of projects, due to higher risk of increasing bugs when introducing changes, as the codebase expands.
|
Clean code is the pragmatic concept of promoting readable and maintainable software. Clean code establishes trust in the codebase and helps minimize the chances of careless bugs being introduced. It also helps developers maintain their agility, which typically plummets as the codebase expands due to the increased risk of introducing bugs.
|
||||||
|
|
||||||
### Test Driven Development
|
### Test-Driven Development
|
||||||
|
|
||||||
The core of creating clean code stems from creating good tests. Writing good tests helps create clean code, as it invites developers to think about the outcomes and test coverage of functions / functionality. It's easier to test a function that is only 4 lines, rather than a function, which is 40. In the same manner, a function which is 4 lines, is typically easier to understand than a function of 40 lines. Therefore, when using test driven development, the resulting code is much more likely to be of a cleaner nature.
|
Test-driven development is the practice of testing your code frequently throughout short development cycles or sprints. It ultimately contributes to code cleanliness by inviting developers to question the functionality and purpose of their code. To make testing easier, developers are encouraged to write short functions that only do one thing. It's certainly easier to test (and understand) a function that's only 4 lines long than one that's 40, for example.
|
||||||
|
|
||||||
The next important part of test driven development, which is very closely related to clean code, is the TDD cycle:
|
Test-driven development consists of the following cycle:
|
||||||
|
|
||||||
1. Write a test which fails
|
1. Write a test that fails
|
||||||
2. Make the test pass
|
2. Make the test pass
|
||||||
3. Refactor code
|
3. Refactor your code
|
||||||
4. Repeat
|
4. Repeat
|
||||||
|
|
||||||
Step three of the cycle, ensures that we can refactor our code as we are writing it. The tests ensure that our refactor doesn't change the outcome of our functions and we can therefore, essentially, go crazy refactoring our code to be as clean as possible. As we go along, and our codebase expands, we will still have our tests, to make sure that our refactoring will not affect the outcome of our functions.
|
Testing and refactoring are intertwined in this process. As you refactor your code to make it more understandable or maintainable, you need to test your changes thoroughly to ensure that you haven't altered the behavior of your functions. As the codebase expands, you will be able to rely on these tests to verify that your refactoring hasn't inadvertently changed the functionality of the software.
|
||||||
|
|
||||||
### Naming
|
### Naming Conventions
|
||||||
|
|
||||||
#### Comments
|
#### Comments
|
||||||
First things first: I want to address the topic of comments. Unnecessary comments are the biggest indicator of code smell. Comments are usually added into a code base, because something is so unclear, that it's deemed necessary to explain with a comment. Of course, this is not always the case. In Go, all according to `gofmt` all public variables and functions, should be annotated. I think this is absolutely fine, as this makes documentation easy. However, I therefore always want to distinguish between comments which enable auto-generated documentation and all other comments. Annotation comments, for documentation, should be written like documentation. They should be high level and concern the logical implementation as little as possible, other than on the highest abstraction level.
|
First things first: I want to address the topic of comments. Unnecessary comments are the biggest indicator of code smell. Comments are usually added into a code base, because something is so unclear, that it's deemed necessary to explain with a comment. Of course, this is not always the case. In Go, all according to `gofmt` all public variables and functions, should be annotated. I think this is absolutely fine, as this makes documentation easy. However, I therefore always want to distinguish between comments which enable auto-generated documentation and all other comments. Annotation comments, for documentation, should be written like documentation. They should be high level and concern the logical implementation as little as possible, other than on the highest abstraction level.
|
||||||
|
|
Loading…
Reference in a new issue