Reader small image

You're reading from  Hands-On Software Engineering with Golang

Product typeBook
Published inJan 2020
Reading LevelIntermediate
PublisherPackt
ISBN-139781838554491
Edition1st Edition
Languages
Right arrow
Author (1)
Achilleas Anagnostopoulos
Achilleas Anagnostopoulos
author image
Achilleas Anagnostopoulos

Achilleas Anagnostopoulos has been writing code in a multitude of programming languages since the mid 90s. His main interest lies in building scalable, microservice-based distributed systems where components are interconnected via gRPC or message queues. Achilleas has over 4 years of experience building production-grade systems using Go and occasionally enjoys pushing the language to its limits through his experimental gopher-os project: a 64-bit kernel written entirely in Go. He is currently a member of the Juju team at Canonical, contributing to one of the largest open source Go code bases in existence.
Read more about Achilleas Anagnostopoulos

Right arrow

Best Practices for Writing Clean and Maintainable Go Code

"Any fool can write code that a computer can understand. Good programmers write code that humans can understand."
- Martin Fowler [8]

Writing clean code that is easy to test and maintain is much harder than it seems at first glance. Fortunately, Go, as a programming language, is quite opinionated and comes with its own set of best practices.

If you take a look at some of the available material for learning Go (for example, Effective Go [6]) or watch some talks by prominent members of the core Go team such as Rob Pike, it becomes evident that software engineers are gently nudged toward applying those principles when working on their own Go projects. From my perspective and experience, these best practices tend to have a measurable positive effect on the code quality metrics associated with a code base and at the...

The SOLID principles of object-oriented design

The SOLID principles are essentially a set of rules for helping you write clean and maintainable object-oriented code. Let's go over what the initials stand for:

  • Single responsibility
  • Open/closed
  • Liskov substitution
  • Interface segregation
  • Dependency inversion

But hold on a minute! Is Go an object-oriented language or is it a functional programming language with some syntactic sugar tacked on top?

Contrary to other, traditional object-oriented programming languages, such as C++ or Java, Go has no built-in support for classes. However, it does support the concepts of interfaces and structs. Structs allow you to define objects as a collection of fields and associated methods. Even though objects and interfaces can be composed together, there is, by design, no support for classic object-oriented inheritance.

With these observations...

Organizing code into packages

As we saw in the previous section, the application of the SOLID principles works as a guide for splitting our code base into smaller packages where each package implements a specific bit of functionality and its interfaces serve as the glue for wiring packages together when building larger systems.

In this section, we will be examining the idiomatic Go way for naming packages, as well as some common potential pitfalls you may encounter while authoring code that relies on a complex package dependency graph.

Naming conventions for Go packages

Let's say you come across a package named server. Is that a good name, as per the preceding suggestion? Well, obviously, we can guess that it's...

Tips and tools for writing lean and easy-to-maintain Go code

In the upcoming sections, we will be covering some techniques, tools, and best practices that can assist you in writing more concise and clean code that is easier to test and at the same time help you get some praise from your colleagues and code reviewers.

Most of the topics that we will be discussing are specific to Go, but some of the principles can be generalized and applied to other programming languages and software engineering in general.

Optimizing function implementations for readability

During my early university days, my CS professors would be adamant about keeping function blocks short and concise. Their advice went along the lines of the following:

...

Summary

In the first section of this chapter, The SOLID principles of object-oriented design, we performed a deep dive into each of the SOLID principles and how they can be applied toward writing clean Go code:

  • SRP: Group structs and functions based on their purpose and organize them into packages with clear logical boundaries.
  • Open/Closed principle: Use composition and embedding of simple types to construct more complex types that still retain the same implicit interface as the types they consist of.
  • LSP: Avoid unnecessary coupling by using interfaces rather than concrete types to define the contract between packages.
  • ISP: Make sure your function or method signatures only depend on the behaviors they need and nothing more; use the smallest possible interface to describe function/method arguments and avoid coupling to the implementation details of concrete types.
  • DIP: Use the...

Further reading

  1. aligncheck: Identify inefficiently packed structs. URL: https://gitlab.com/opennota/check.
  2. bodyclose: A static analysis tool that checks whether res.Body is correctly closed. URL: https://github.com/timakin/bodyclose.
  3. copyfighter: Statically analyze Go code and report functions while passing large structs by value. URL: https://github.com/jmhodges/copyfighter.
  4. deadcode: Report unused blocks of code. URL: https://github.com/tsenart/deadcode.
  5. dupl: Report potentially duplicated blocks of code. URL: https://github.com/mibk/dupl.
  6. Effective Go: Tips for writing clear, idiomatic Go code.
  7. errcheck: Ensure that returned errors are checked. URL: https://github.com/kisielk/errcheck.
  8. Fowler, Martin: Refactoring: Improving the Design of Existing Code. Boston, MA, USA: Addison-Wesley, 1999 — ISBN 0-201-48567-2 (https://www.worldcat.org/title/refactoring-improving-the...
lock icon
The rest of the chapter is locked
You have been reading a chapter from
Hands-On Software Engineering with Golang
Published in: Jan 2020Publisher: PacktISBN-13: 9781838554491
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime

Author (1)

author image
Achilleas Anagnostopoulos

Achilleas Anagnostopoulos has been writing code in a multitude of programming languages since the mid 90s. His main interest lies in building scalable, microservice-based distributed systems where components are interconnected via gRPC or message queues. Achilleas has over 4 years of experience building production-grade systems using Go and occasionally enjoys pushing the language to its limits through his experimental gopher-os project: a 64-bit kernel written entirely in Go. He is currently a member of the Juju team at Canonical, contributing to one of the largest open source Go code bases in existence.
Read more about Achilleas Anagnostopoulos