# Code Review: nanoid

**URL:** <https://forum.modular.com/t/code-review-nanoid/1472>\
**Category:** Mojo\
**Created:** [May 19, 2025, 5:30am UTC](https://forum.modular.com/t/code-review-nanoid/1472 "2025-05-19T05:30:07Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![ucirello](https://avatars.discourse-cdn.com/v4/letter/u/ecd19e/32.png) [@ucirello](https://forum.modular.com/u/ucirello)\
**Post date:** [May 19, 2025, 5:30am UTC](https://forum.modular.com/t/code-review-nanoid/1472/1 "2025-05-19T05:30:07Z")

</div>

Hi,

This is my first public work done with Mojo:  
[GitHub - cirello-py/nanoid](https://github.com/cirello-py/nanoid)

I would appreciate if I could get some code review. I must say I struggled a bit with the lack of examples in the documentation, and lack of access to the source code to validate some of my concerns (especially, around the `random` package, as I wanted to know what source it uses).

I look forward for feedback.

---

<div class="post-metadata">

**Author:** ![lesoup-mxd](https://sea1.discourse-cdn.com/flex001/user_avatar/forum.modular.com/lesoup-mxd/32/585_2.png) [@lesoup-mxd](https://forum.modular.com/u/lesoup-mxd)\
**Post date:** [May 19, 2025, 9:59am UTC](https://forum.modular.com/t/code-review-nanoid/1472/2 "2025-05-19T09:59:07Z")

</div>

Consider using String Literal for the alphabet as it is not a dynamic value.  
Initialising a string without a String constructor assumes StringLiteral

Also, did you try compiling to .mojopkg?  
I usually stub out **init**.mojo within the subdirs, but does it still work without it? Cause I see you have a global init

---

<div class="post-metadata">

**Author:** ![lesoup-mxd](https://sea1.discourse-cdn.com/flex001/user_avatar/forum.modular.com/lesoup-mxd/32/585_2.png) [@lesoup-mxd](https://forum.modular.com/u/lesoup-mxd)\
**Post date:** [May 19, 2025, 10:00am UTC](https://forum.modular.com/t/code-review-nanoid/1472/3 "2025-05-19T10:00:59Z")

</div>

Also as far as I understand you initialise an array buffer to 0 but then do a randint fill pass, rendering 0-setting redundant?  
Or was the compiler playing nicer?  
Consider compiling to mlir representation to observe the internal flow

---

<div class="post-metadata">

**Author:** ![lesoup-mxd](https://sea1.discourse-cdn.com/flex001/user_avatar/forum.modular.com/lesoup-mxd/32/585_2.png) [@lesoup-mxd](https://forum.modular.com/u/lesoup-mxd)\
**Post date:** [May 19, 2025, 10:01am UTC](https://forum.modular.com/t/code-review-nanoid/1472/4 "2025-05-19T10:01:32Z")

</div>

\*llvm IR, sorry

---

<div class="post-metadata">

**Author:** ![ucirello](https://avatars.discourse-cdn.com/v4/letter/u/ecd19e/32.png) [@ucirello](https://forum.modular.com/u/ucirello)\
**Post date:** [May 20, 2025, 11:16pm UTC](https://forum.modular.com/t/code-review-nanoid/1472/5 "2025-05-20T23:16:20Z")

</div>

> Also, did you try compiling to .mojopkg?  
> I usually stub out **init**.mojo within the subdirs, but does it still work without it? Cause I see you have a global init

I think I got this one wrong! I’m going to fix it.

---

<div class="post-metadata">

**Author:** ![bgreni](https://avatars.discourse-cdn.com/v4/letter/b/e495f1/32.png) [@bgreni](https://forum.modular.com/u/bgreni)\
**Post date:** [May 21, 2025, 8:34pm UTC](https://forum.modular.com/t/code-review-nanoid/1472/6 "2025-05-21T20:34:39Z")

</div>

> [@lesoup-mxd](#):
>
> Consider using String Literal for the alphabet as it is not a dynamic value.  
> Initialising a string without a String constructor assumes StringLiteral

`StringLiteral` is a dependent type now so I would avoid using it in this way now. With the recent work on SSO for `String` there’s also little reason to do so as `String` will intelligently build a reference to the static data where the default `alphabet` lives.

See this ctor for `String` [modular/mojo/stdlib/stdlib/collections/string/string.mojo at main · modular/modular · GitHub](https://github.com/modular/modular/blob/main/mojo/stdlib/stdlib/collections/string/string.mojo#L395-L407)

---

<div class="post-metadata">

**Author:** ![bgreni](https://avatars.discourse-cdn.com/v4/letter/b/e495f1/32.png) [@bgreni](https://forum.modular.com/u/bgreni)\
**Post date:** [May 21, 2025, 9:31pm UTC](https://forum.modular.com/t/code-review-nanoid/1472/7 "2025-05-21T21:31:04Z")

</div>

`randint` just repeatedly calls to `random_ui64/random_si64`. So you can avoid that allocation entirely and just call to `random_ui64` directly within you loop

---

<div class="post-metadata">

**Author:** ![ucirello](https://avatars.discourse-cdn.com/v4/letter/u/ecd19e/32.png) [@ucirello](https://forum.modular.com/u/ucirello)\
**Post date:** [May 24, 2025, 4:10pm UTC](https://forum.modular.com/t/code-review-nanoid/1472/8 "2025-05-24T16:10:18Z")

</div>

hi @bgreni – I iterated according to your suggestion. Thank you!

> **[Release v0.2.0 · cirello-py/nanoid](https://github.com/cirello-py/nanoid/releases/tag/v0.2.0)**
>
> Contribute to cirello-py/nanoid development by creating an account on GitHub.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex001/uploads/modular/original/1X/2751e0fbdc595a99718b216730957e9db4448cfd.jpeg) [@system](https://forum.modular.com/u/system)\
**Post date:** [May 31, 2025, 4:10pm UTC](https://forum.modular.com/t/code-review-nanoid/1472/9 "2025-05-31T16:10:58Z")

</div>

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.
