Skip to content

Latest commit

 

History

History
41 lines (28 loc) · 2.5 KB

5.5.3-code-styling.md

File metadata and controls

41 lines (28 loc) · 2.5 KB
docs/5-software-development-practices/5.5.3-code-styling.md
category estReadingMinutes
Software Quality
30

Code Styling and Linting

“Formatting issues are the most contentious but the least consequential. People can adapt to different formatting styles but it's better if they don't have to, and less time is devoted to the topic if everyone adheres to the same style.”

- Effective Go

When writing or dealing with a lot of code, it can become quite cumbersome when reading and writing code of different styles. Some people might use snake_case, some people might use camelCase. Some people might capitalize their class names, and some people might not. To solve this, a particular style is adopted for the codebase. Some languages such as Golang are built around a specific code style provided by the gofmt tool. Other languages such as C++ have a myriad of different styles, all with the same general goals such as the Google C++ guide.

Oftentimes, the style guides are long and in-depth. To solve this, specific tools known as formatters and linters have been developed to help developers adhere to specific style guides.

Popular tools

This list is by no means exhaustive.

Read about the differences between formatters and linters.

Golang gofmt

Gofmt is a standard code formatting tool for the Go language. Using tools like these can create consistency and predictability for your source code. This means a codebase that has gone through the gofmt will be easier to read and easier to maintain. Running gofmt -d . will print the differences between the code in the current directory and what the gofmt standard would look like. Running gofmt -w . will write the changes proposed by gofmt into your code.

Gofmt example

gofmt -d . is an example that shows that gofmt prefers a different type of indent than the one currently used.

Deliverables

  • Discuss why code styling and formatting are important.
  • Do you know of any other formatting, styling, or linting tools?