Go to content
The Codest
  • About Us
  • Services
  • Our Team
  • Case studies
    • Blog
    • Meetups
    • Webinars
    • Resources
Careers Get in touch
  • About Us
  • Services
  • Our Team
  • Case studies
    • Blog
    • Meetups
    • Webinars
    • Resources
Careers Get in touch
2022-01-05
Software Development

Ruby on Rails modularization with Packwerk Episode I

Nicolas Nisoria

 Ruby on Rails modularization with Packwerk Episode I  - Image

Humans find it difficult to see the big picture of a problem without devoting a lot of time and effort. This happens especially while working with large and complex applications. What are the side effects of my changes? Why is this line here affecting the tests of a remote part of the codebase? A perfect or complete solution does not exist, but Shopify came out with a tool that will probably help you and your team.

Introduction

In order to talk about Packwerk, we need to introduce a few concepts first.
  • Cohesion: refers to the measure of how much elements in a module or class belong together.
  • Coupling: refers to the level of dependency between modules or classes.
  • Boundaries: refers to barriers between code. In this case, a code boundary refers to different domains of concern within the same codebase.
  • Modularization: the process of dividing a software system into multiple separate modules where each module works independently.

Problems

As we know, Ruby doesn't provide a good solution to enforce code boundaries. We can specify the visibility but all the dependencies will be loaded into the global namespace. In large or monolith applications, this lack of boundaries produces the following problems.

  • Low cohesion,
  • High coupling,
  • Spaghetti code.

In an attempt to modularize Shopify's monolith and enforce boundaries, they tried different solutions without achieving the expected results:

  • Setting private constants,
  • Establishing boundaries through gems,
  • Using tests to prevent cross-component associations,
  • Using Ruby‘s Modulation gem,
  • Creating microservices.

With all the knowledge from previous attempts, they decided to create their own tool: Packwerk.

Packwerk

What is Packwerk?

Packwerk is a static analysis tool used to enforce boundaries between groups of Ruby files called packages.

What is a package?

A package is a folder containing autoloaded code. Shopify's team encourages to use the best design practices while creating packages.

  • We should pack together things that have high functional cohesion,

  • Packages should be relatively loosely coupled with each other.

Types of boundary checks

We can enforce privacy and dependency boundaries, check violations of the boundaries and cyclic dependencies.

Packwerk into practice

There is not a single specific way to structure or re-structure your application while creating packages. In this article, we are going to follow the approach suggested by Stephan Hagemann in Gradual Modularization for Ruby on Rails.

Choose the project

You can create a new project or choose one of your projects. I decided to use an open-source project called CodeTriage. It is important to mention that we need a Rails 6 application since Packwerk uses Zeitwerk.

Initialize Packwerk

First, we need to add the gem to our Gemfile like gem 'packwerk' and then run bundlein the console. Then we are ready to initialize the gem running packwerk init.

After that, we notice that Packwerk generated three files for us:

  • packwerk.yml

  • package.yml

  • inflections.yml

packwerk.yml is the configuration file of Packwerk where we will define included and excluded files, list the load paths, define the inflections file, among other things;

package.yml is the configuration file of a package. In this file, we will add the configuration for the boundaries of our package. Any folder with package.yml will be recognized as a package by Packwerk. That's it, Packwerk created our first package and we call it the root package.

inflections.yml is where we will place our custom inflections and acronyms in case we are using them.

You can find more about the files and their configuration in Packwerk.

Packwerk properties

For modularization to work we need three basic properties: a named container, its content, and explicit dependencies on other containers. So let's define those properties in Packwerk:

  • Name: The name of a package is its relative path from the root of the

application.

  • Content: When we place a package.yml into a folder, all the files in the folder are now the content of the package.

  • Dependencies: We can define dependencies on other packages adding dependencies key to the package.yml.

Another file that's not included by default but is recommended is README. It is important to provide information about the usage of the package.

The End of Episode I

Digital product development consulting

Read More

GraphQL Ruby. What about performance?

Rails and Other Means of Transport

Rails Development with TMUX, Vim, Fzf + Ripgrep

Related articles

Software Development

3 Useful HTML Tags You Might Not Know Even Existed

Nowadays, accessibility (A11y) is crucial on all stages of building custom software products. Starting from the UX/UI design part, it trespasses into advanced levels of building features in code. It provides tons of benefits for...

Jacek Ludzik
Software Development

5 examples of Ruby’s best usage

Have you ever wondered what we can do with Ruby? Well, the sky is probably the limit, but we are happy to talk about some more or less known cases where we can use this powerful language. Let me give you some examples.

Pawel Muszynski
Software Development

Maintaining a Project in PHP: 5 Mistakes to Avoid

More than one article has been written about the mistakes made during the process of running a project, but rarely does one look at the project requirements and manage the risks given the technology chosen.

Sebastian Luczak
Software Development

5 reasons why you will find qualified Ruby developers in Poland

Real Ruby professionals are rare birds on the market. Ruby is not the most popular technology, so companies often struggle with the problem of finding developers who have both high-level skills and deep experience; oh, and by the...

Jakub
Software Development

9 Mistakes to Avoid While Programming in Java

What mistakes should be avoided while programming in Java? In the following piece we answers this question.

Rafal Sawicki
Software Development

A Deeper Look at the Most Popular React Hooks

In the course of many interviews, I noticed that even experienced programmers have a problem with distinguishing Hooks, not to mention their more advanced capabilities. So, I will try to explain in this article how Hooks should...

Pawel Rybczynski

Subscribe to our knowledge base and stay up to date on the expertise from industry.

About us

Tech company specializing in scaling tech teams for clients and partners thanks to top-class development engineers.

    United Kingdom - Headquarters

  • Office 303B, 182-184 High Street North E6 2JA London, England

    Poland - Local Tech Hubs

  • Business Link High5ive, Pawia 9, 31-154 Kraków, Poland
  • Brain Embassy, Konstruktorska 11, 02-673 Warsaw, Poland
  • Aleja Grunwaldzka 472B, 80-309 Gdańsk, Poland

    The Codest

  • Home
  • About us
  • Services
  • Case studies
  • Know how
  • Careers

    Services

  • PHP development
  • Java development
  • Python development
  • Ruby on Rails development
  • React Developers
  • Vue Developers
  • TypeScript Developers
  • DevOps
  • QA Engineers

    Resources

  • What are top CTOs and CIOs Challenges? [2022 updated]
  • Facts and Myths about Cooperating with External Software Development Partner
  • From the USA to Europe: Why do American startups decide to relocate to Europe
  • Privacy policy
  • Website terms of use

Copyright © 2022 by The Codest. All rights reserved.

We use cookies on the site for marketing, analytical and statistical purposes. By continuing to use, without changing your privacy settings, our site, you consent to the storage of cookies in your browser. You can always change the cookie settings in your browser. You can find more information in our Privacy Policy.