# Best practices for debugging Motoko?

**URL:** <https://forum.dfinity.org/t/best-practices-for-debugging-motoko/9738>\
**Category:** Developers\
**Tags:** Discussing\
**Created:** [December 16, 2021, 9:56pm UTC](https://forum.dfinity.org/t/best-practices-for-debugging-motoko/9738 "2021-12-16T21:56:45Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![heldrida](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/heldrida/32/2302_2.png) [@heldrida](https://forum.dfinity.org/u/heldrida)\
**Post date:** [December 16, 2021, 9:56pm UTC](https://forum.dfinity.org/t/best-practices-for-debugging-motoko/9738/1 "2021-12-16T21:56:45Z")

</div>

Hi,

In the rare occasions I have to read or write Motoko, I find it tricky, mainly because of having to deploy, making development iterations slow - probably there are better ways around this, as I found out about Haskell (it’s not a language I have knowledge) to write unit-tests, or the Motoko Playground.

What could help is to find how to use the `mo:base/Debug` to output any custom types, for example?

Any tips are appreciated,

Thanks!

---

<div class="post-metadata">

**Author:** ![PaulLiu](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/paulliu/32/4701_2.png) [@PaulLiu](https://forum.dfinity.org/u/PaulLiu)\
**Post date:** [December 16, 2021, 10:39pm UTC](https://forum.dfinity.org/t/best-practices-for-debugging-motoko/9738/2 "2021-12-16T22:39:05Z")

</div>

> [@heldrida](#):
>
> What could help is to find how to use the `mo:base/Debug` to output any custom types, for example?

You can use `debug_show(...)` which turns any value into `Text`. Then you can use `Debug.print(..)` to print the text to console. But you need to deploy your canister locally and call the method in order to run it.

For faster iteration, you can invoke the motoko compiler `moc` directly. For example, `$(dfx cache show)/moc` should give you the command.

---

<div class="post-metadata">

**Author:** ![claudio](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/claudio/32/322_2.png) [@claudio](https://forum.dfinity.org/u/claudio)\
**Post date:** [December 16, 2021, 11:02pm UTC](https://forum.dfinity.org/t/best-practices-for-debugging-motoko/9738/3 "2021-12-16T23:02:45Z")

</div>

If using the playground, you can also log messages by appending them to a mutable variable of type Text and providing a query to read that variable.

Not great, but not terrible either.

---

<div class="post-metadata">

**Author:** ![claudio](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/claudio/32/322_2.png) [@claudio](https://forum.dfinity.org/u/claudio)\
**Post date:** [December 16, 2021, 11:20pm UTC](https://forum.dfinity.org/t/best-practices-for-debugging-motoko/9738/4 "2021-12-16T23:20:54Z")

</div>

[GitHub - kritzcreek/motoko-matchers](https://github.com/kritzcreek/motoko-matchers) is pretty good for writing unit-tests of mainly libraries but also canisters (from dfx), but may have bit-rotted by now.

Some of the motoko-base libraries have their tests written with motoko-matchers.

---

<div class="post-metadata">

**Author:** ![heldrida](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/heldrida/32/2302_2.png) [@heldrida](https://forum.dfinity.org/u/heldrida)\
**Post date:** [December 17, 2021, 11:57am UTC](https://forum.dfinity.org/t/best-practices-for-debugging-motoko/9738/5 "2021-12-17T11:57:19Z")

</div>

Thanks! The `moc` is extremely useful, using it now from now on 😉

---

<div class="post-metadata">

**Author:** ![heldrida](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/heldrida/32/2302_2.png) [@heldrida](https://forum.dfinity.org/u/heldrida)\
**Post date:** [December 17, 2021, 11:58am UTC](https://forum.dfinity.org/t/best-practices-for-debugging-motoko/9738/6 "2021-12-17T11:58:00Z")

</div>

That’s a great idea, thank you so much for that!

---

<div class="post-metadata">

**Author:** ![AndreLeifert](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/andreleifert/32/9053_2.png) [@AndreLeifert](https://forum.dfinity.org/u/AndreLeifert)\
**Post date:** [November 19, 2023, 7:19am UTC](https://forum.dfinity.org/t/best-practices-for-debugging-motoko/9738/7 "2023-11-19T07:19:52Z")

</div>

Yes, PaulLiu, the docs suggest some degree of usefulness for Debug.print + debug\_show.  
And so in my youthful credulity I expected to enjoy the poetic value of the output of:

```auto
  public query func get1(): async Nat {
    Debug.print("I AM GOING TO RETURN 1 now");
    1;
  };

```

by running

```auto
dfx canister call bmos get1

```

How very disappointing was the output of (1 : nat) only!

Not very surprisingly by now, I fail to see how reading

```auto
BMOS.mo:1.1-1.34: import error [M0010], package "base" not defined

```

as a result of

```auto
$(dfx cache show)/moc BMOS.mo

```

can provide for faster iteration.

Would you please point out where my misunderstanding lies?

Thanks a lot in advance,

André

---

<div class="post-metadata">

**Author:** ![PaulLiu](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/paulliu/32/4701_2.png) [@PaulLiu](https://forum.dfinity.org/u/PaulLiu)\
**Post date:** [November 19, 2023, 7:53am UTC](https://forum.dfinity.org/t/best-practices-for-debugging-motoko/9738/8 "2023-11-19T07:53:55Z")

</div>

> [@AndreLeifert](#):
>
> How very disappointing was the output of (1 : nat) only!

The debug output only shows up with the replica logs, i.e. in the terminal where you did `dfx start` or `dfx start --background`. I know, this is very counter-intuitive. Definitely something to improve.

> [@AndreLeifert](#):
>
> `$(dfx cache show)/moc BMOS.mo`

Yes, you can run it like that after supplying the base library path to moc. For example:

```auto
moc --package base $(dfx cache show)/base -r BMOS.mo

```

The `-r` option would run the program instead of compiling it. Or you can use `-i` which will launch into an interactive shell where you can evaluate motoko expressions.

---

<div class="post-metadata">

**Author:** ![AndreLeifert](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/andreleifert/32/9053_2.png) [@AndreLeifert](https://forum.dfinity.org/u/AndreLeifert)\
**Post date:** [November 19, 2023, 8:13am UTC](https://forum.dfinity.org/t/best-practices-for-debugging-motoko/9738/9 "2023-11-19T08:13:50Z")

</div>

Thanks for your quick reply, PaulLiu!

Well, guess I should have realized it myself: WHICH stdout are we talking about, right?  
Certainly, I’ll start the replica in the foreground.

But may I make some more use of your helpfulness?  
I started adding text to a var \_log:Text = “”; and then “looking” at it by means of a query getter.  
I know text gets added as expected, but my getter always shows an empty string;  
Are variables inside the actor re-initialized / re-evaluated with every invocation?!

---

<div class="post-metadata">

**Author:** ![claudio](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/claudio/32/322_2.png) [@claudio](https://forum.dfinity.org/u/claudio)\
**Post date:** [November 21, 2023, 1:01am UTC](https://forum.dfinity.org/t/best-practices-for-debugging-motoko/9738/10 "2023-11-21T01:01:41Z")

</div>

I think this isn’t working for you because your function is itself a query so it’s state changes are discarded before returning, including the updates to your Text variable.
