What is Semantic Software Versioning?
Tags: softwaredevelopment, webdev, coding, programming
Published: 2023-10-15 04:08:18


*Versioning* is one of the essential elements to consider when publishing/using production-ready software. It helps us understand and maintain different varieties of software produced. It is also useful when you want to roll back to the previous software version in case of any problems. 

Semantic versioning has a particular syntax. Let's look at the syntax first.

`[major].[minor].[patch]`

Here,

1. The *Major* increments when significant changes in your application break the backward compatibility.
2. The *Minor* increments when new features are introduced, supporting backward compatibility.
3. And the *Patch* adds up if there are any backward-compatible bug fixes.

While developing any software, we might have specific dependencies. For example, we might use `react-bootstrap`, `Axios`, `react-scripts`, etc. Keeping track of stable versions/new features of individual software can be challenging, especially when the number of dependencies is growing.  On this occasion, we can take advantage of the software version **scopes** so that if new software is released, you can use it only if it's available in the scope. This way, you can ensure that your software doesn't break due to dependencies. 

Let's look at how to use these ranges.

---

## Comparators
A version range is a set of *comparators* that specify versions that satisfy the degree. A *comparator* consists of an operator and version. The collection of primitive operators is as follows:

- `<` Less than
- `<=` Less than or equal to
- `>` Greater than
- `>=` Greater than or equal to
- `=` Equal. This operator is the default, so it is optional, but We can include it.

Examples:

1. The comparator `>=1.2.7` matches the versions `1.2.7`, `1.2.8`, and `1.3.9`, but not the versions `1.2.5` or `1.1.1.`

2. The range `>=1.2.7 <1.3.0` would match the versions `1.2.7` and `1.2.99`, but not` 1.2.6`, `1.3.0`, or `1.1.1`.

We can also join by the `||` (`or`) operator.

1. The range `1.2.7 || >=1.2.9 <2.0.0` would match `1.2.7`, `1.2.9`, and `1.4.6`, but not the versions `1.2.8` or `2.0.0`.


## Exact Version 

You can specify an exact version number, such as 1.2.3. This means your project will only use version 1.2.3 of the dependency.

## Hyphen Ranges

*Hyphen ranges* specify the inclusive set. For example, If we define the range `3.4.5 - 5.6.7`, it will be equivalent to `>=3.4.5 <=5.6.7.`

For the partial versions, `0` is considered for the missing place.

For example, `2.3 - 5` means `>=2.3.0 <=5.0.0`

## X-Ranges

*X-Ranges* are similar to the partial versions, but we use `X` or `x` or `*` instead of blank spaces.

For example, 
1. `1.x` will be `>=1.0.0 <2.0.0` 
2. `1.2.x` will be `>=1.2.0 <1.3.0` 
3. Similarly, `*` will be >=0.0.0

## Tilde ranges
*Tilde ranges* allow the patch level changes if minor and major are present. For example, if you provide `~1.2.3` will allow this range `>=1.2.3 <1.3.0`.

If the minor is missing, then minor level changes are allowed. For example, if you specify `~1`, it will enable releases ranging `>=1.0.0 <2.0.0.`

## Caret Ranges

*Caret* will update the package to all later minor/patch versions without incrementing the major version. For example `^2.3.4` will use releases ranging `>=2.3.4 <3.0.0`

You can also combine x-ranges in this. For example, `^0.x` will allow `>=0.0.0 <1.0.0`

These version range operators allow developers to express their requirements for dependencies more flexibly and ensure that their projects can adapt to updates in the dependencies while still adhering to the principles of Semantic Versioning. This flexibility is essential because it helps maintain compatibility while allowing for updates and bug fixes in the dependencies.