# When static types make your code shorter

**URL:** <https://forum.crystal-lang.org/t/when-static-types-make-your-code-shorter/6202>\
**Category:** News\
**Created:** [December 5, 2023, 8:24pm UTC](https://forum.crystal-lang.org/t/when-static-types-make-your-code-shorter/6202 "2023-12-05T20:24:41Z")\
**Posts on this page:** 3\
**Page:** 1

<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:** [December 5, 2023, 8:24pm UTC](https://forum.crystal-lang.org/t/when-static-types-make-your-code-shorter/6202/1 "2023-12-05T20:24:41Z")

</div>

@EvanHahn was porting some Ruby code to Crystal and found that due to static typing, the Crystal code cam out 15% shorter.

> **[When static types make your code shorter](https://evanhahn.com/when-static-types-make-your-code-shorter/)**
>
> The type system can let you remove runtime validations because invalid states are impossible.

> My main takeaway was this: static typing gets a bad reputation for being verbose. That’s sometimes true! But sometimes, a static type system wins because it can prevent a hydra of impossible states.

There is quite an active comment thread on lobste.rs:

> **[When static types make your code shorter | Lobsters](https://lobste.rs/s/oxywix/when_static_types_make_your_code_shorter)**
>
> 86 comments

---

<div class="post-metadata">

**Author:** ![EvanHahn](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/evanhahn/32/1510_2.png) [@EvanHahn](https://forum.crystal-lang.org/u/EvanHahn)\
**Post date:** [December 5, 2023, 8:42pm UTC](https://forum.crystal-lang.org/t/when-static-types-make-your-code-shorter/6202/2 "2023-12-05T20:42:44Z")

</div>

Indeed, there is a very active comment thread…twice as many comments as upvotes! 😅

For a little more on my porting efforts, I wrote [a short blog post introducing the project](https://evanhahn.com/introducing-sqids-crystal/) which might be interesting to Crystal programmers.

---

<div class="post-metadata">

**Author:** ![kojix2](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/kojix2/32/1583_2.png) [@kojix2](https://forum.crystal-lang.org/u/kojix2)\
**Post date:** [December 6, 2023, 3:49am UTC](https://forum.crystal-lang.org/t/when-static-types-make-your-code-shorter/6202/3 "2023-12-06T03:49:20Z")

</div>

Ruby intentionally tries to avoid putting types in its code. The ruby core team’s answer is [rbs](https://github.com/ruby/rbs) and [typeprof](https://github.com/ruby/typeprof). The basic idea is to separate the type definition from the code, leaving the option of not writing types. Although `rbs` is not widely used, it is used by some companies. (Larger companies tend to prefer [sorbet](https://sorbet.org/).) I mention these tools because some of you may not be aware of recent attempts in Ruby.
