# Should \`divisible\_by?\` be preferd over \`x % y == 0\`?

**URL:** <https://forum.crystal-lang.org/t/should-divisible-by-be-preferd-over-x-y-0/6161>\
**Category:** Help & Support\
**Created:** [November 12, 2023, 6:43pm UTC](https://forum.crystal-lang.org/t/should-divisible-by-be-preferd-over-x-y-0/6161 "2023-11-12T18:43:21Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![meatball133](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/meatball133/32/1869_2.png) [@meatball133](https://forum.crystal-lang.org/u/meatball133)\
**Post date:** [November 12, 2023, 6:43pm UTC](https://forum.crystal-lang.org/t/should-divisible-by-be-preferd-over-x-y-0/6161/1 "2023-11-12T18:43:21Z")

</div>

I see quite regularly the use of `x % y == 0` but some may argue that `divisible_by?` adds readability to code and shows the intent of what the operation is being done. Thereby I wonder if one is preferred over the other in certain situations or if it doesn’t matter from a language perspective which to use?

---

<div class="post-metadata">

**Author:** ![Devonte](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/devonte/32/1927_2.png) [@Devonte](https://forum.crystal-lang.org/u/Devonte)\
**Post date:** [November 12, 2023, 10:39pm UTC](https://forum.crystal-lang.org/t/should-divisible-by-be-preferd-over-x-y-0/6161/2 "2023-11-12T22:39:26Z")

</div>

From a language perspective it doesn’t matter as both are quite readable, but there are cases where you’d want to use the former over the latter (for example, using the modulo result in further calculations).

---

<div class="post-metadata">

**Author:** ![straight-shoota](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/straight-shoota/32/36_2.png) [@straight-shoota](https://forum.crystal-lang.org/u/straight-shoota)\
**Post date:** [November 13, 2023, 11:38am UTC](https://forum.crystal-lang.org/t/should-divisible-by-be-preferd-over-x-y-0/6161/3 "2023-11-13T11:38:14Z")

</div>

I’d prefer `divisible_by?` because it clearly describes the intent.

As a bonus it should also be a tiny bit more performant. This probably doesn’t matter much unless you do loads of operations in which case you might want more specialized code anyway.

> [@Devonte](#):
>
> for example, using the modulo result in further calculations

But that would not be a use case for `x % y == 0`. Only for `x % y` which isn’t asked here.

---

<div class="post-metadata">

**Author:** ![HertzDevil](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/hertzdevil/32/1023_2.png) [@HertzDevil](https://forum.crystal-lang.org/u/HertzDevil)\
**Post date:** [November 13, 2023, 11:57am UTC](https://forum.crystal-lang.org/t/should-divisible-by-be-preferd-over-x-y-0/6161/4 "2023-11-13T11:57:56Z")

</div>

`0.divisible_by?(0)` _should_ be true even when `0 % 0` is undefined, because there exists an integer `n` such that `0 * n == 0`. Currently this is how `BigInt` defines it

---

<div class="post-metadata">

**Author:** ![yxhuvud](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/yxhuvud/32/27_2.png) [@yxhuvud](https://forum.crystal-lang.org/u/yxhuvud)\
**Post date:** [November 13, 2023, 5:07pm UTC](https://forum.crystal-lang.org/t/should-divisible-by-be-preferd-over-x-y-0/6161/5 "2023-11-13T17:07:54Z")

</div>

> _should_ be true

That depends on what definition of divisibility that is used. Not allowing m to be 0 is pretty common. See discussion in the defintion part of [Divisor - Wikipedia](https://en.wikipedia.org/wiki/Divisor) .

The problem is that 0\*n == 0 for all n, so it is pretty pointless. Especially as `divisor` typically means a division can be done, which is not the case here. I’d be quite surprised if I found 0 to be the divisor of anything, as it cannot divide anything by definition.

---

<div class="post-metadata">

**Author:** ![Xen](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/xen/32/2288_2.png) [@Xen](https://forum.crystal-lang.org/u/Xen)\
**Post date:** [November 13, 2023, 10:23pm UTC](https://forum.crystal-lang.org/t/should-divisible-by-be-preferd-over-x-y-0/6161/6 "2023-11-13T22:23:14Z")

</div>

What straight-shoota said. Just as I prefer `array.empty?` over `array.size == 0`, for the same reason. Remember, you’re not writing for the compiler, but for the next developer (and often yourself). Make the intent clear rather than the implementation.
