提供: Japanese Scratch-Wiki


デバッグとは、スクリプトのデバッグ(バグを取り除くこと)に関するヘルプを表示するダイアログである。2024年12月16日のScratch 3.0のメジャーアップデートの一部として追加された。
ユーザーインターフェースのナビゲーションバーにある「デバッグ」のボタンをクリックすると、ユーザーがプロジェクト内の問題を理解するのに役立つさまざまなヒントを含む13の項目からなるダイアログが開く。このダイアログでは、ユーザーがコード内でミスを犯した可能性のあるさまざまな方法を説明し、タイミング、サウンドチェックポイント、コードへのコメントの追加など、スクリプトを修正するためのさまざまな方法がかかれている。
一覧
声に出して読む
コードを声に出して読むときは、コンピューターの気持ちになって考えてください。
- 存在しないステップを含めていませんか?
- その指示は正しいですか?
- プログラムが実行されるたびに何かをリセットする必要がある場合、その手順は含まれていますか?
分解する
ブロックを小さな塊(またはシーケンス)に分割し、それらをクリックして各シーケンスの動作を確認してください。
小さなシーケンスが期待どおりに動いたら、それらをメインプログラムに戻してください。
この過程は分解と呼ばれます。
ゆっくりにする
コンピューターは高速にプログラムを実行するため、それを目で追うことは難しいです。
一時的に「待つ」や「まで待つ」ブロックを追加して、シーケンスを遅らせましょう。こうすることで、その部分がうまく動いたかどうかを自分で判断する時間が生まれます。
これらの「待つ」ブロックは、コードが動いたら消しましょう。
音の目印をつける
「ゆっくりにする」と同じように、「終わるまで音を鳴らす」ブロックを目印となる場所に追加して、シーケンスを確認してみましょう。
もし音が鳴らなければ、バグはこのブロックの前にあると思われます。音が鳴ったら、バグはこのブロックの後にあるはずです。
コードが動いたら、音は削除しましょう。
ブロックの順序をいじる
ブロックの順序やシーケンスを調整してみてください。
- まず何が起こる必要がありますか?
- 次は何が起きるでしょうか?
- 次のコードの部分が実行される前に、変数またはスプライトを初期化する必要がありませんか?
繰り返しや条件文の外側ではなく、ループや条件文の内側でブロックを使用してみてください。
繰り返すか繰り返さないか
制御ブロックに「ずっと」や「繰り返す」などを使用している場合は、ループ内の全てのブロックがあるべき場所にあるか確認したり、タイミングの調整や動作のリセットを担うブロック(「待つ」など)が欠けていないか確認しましょう。繰り返しをずっと実行するか、それとも決まった回数だけ実行したいですか? 何かが繰り返しを止める必要はありませんか?
繰り返しを使うべき時に使っていないのではないでしょうか?例えば、「もし〜ならば」のような条件文ブロックを使っている場合、プログラムはその真偽を1回調べればいいだけですか?それとも継続的に調べる必要がある場合がありますか?後者の場合、条件文を無限ループの中に置く必要はありますか?
タイミングと並列性について考える
複数のイベントを同時に動かそうとしていますか?2つのシーケンスが同時に動くようなプログラムの場合、予期のつかない動作をするかもしれません。
短い待機、メッセージ、ユーザーの操作 (クリックやキーを押すなど) を入れてみて、結果が変わるか確認してみましょう。
ブロックの選択肢について考える
似ているけれど異なるブロックは使えるでしょうか?
「にする」と「ずつ変える」、「終わるまで鳴らす」と「鳴らす」などのブロックは、似たような見た目ですが働きは全く異なります。
今使っているブロックに似たものを代わりに使ってみて、結果に影響するか確認しましょう。
変数を調べる
変数や演算ブロックを使っている場合は、コードシーケンスが動いているときの値を確認しましょう。
- 変数を制御するべきなのは全てのスプライトでしょうか、それとも1つだけでしょうか?
- 値が初期化されるのはどこですか?どこで変わりますか?
コードの実行順序を確認する
コードが正しいスプライト、または背景についているか、確認してください。
コードを別のスプライトに移動する必要がある場合は、コードをクリックして正しいスプライトの上にカーソルが来るまでドラッグします。スプライトが小さく揺れたら、そこでマウスを離してください。
バックパック(画面の下)を使用して、コードやスプライトを保存、移動することもできます。
コードにコメントをつけましょう
コードにコメントを追加することで、ほかの人が見たときにコードの内容を理解しやすくなります。また、あとでコードを見返したときに、コードがどう動くか思い出す手助けにもなります。
スクリプトエリアを右クリックして「コメントを追加」を選びます。ブロックや、少しまとまったブロックが何をしているのかを書きましょう。
休憩したり、少し離れてみましょう。
問題に集中しすぎると、逆効果になってしまい、イライラすることがあります。
しっかり休憩を取って、画面から離れて、頭をすっきりさせましょう。 少し休んだり、何か他のことに集中したり、または水を飲むだけでも、新鮮な目で問題に取り組むことができます。
ヘルプを求めてみましょう
それでもうまくいかないときは、ほかの人に助けを求めてみましょう。デバッグや、助け合いをするスタジオを探して、プロジェクトを共有し、コメントやプロジェクトのメモで助けを求めてください。
1〜3人くらいの人にコードを試してもらいましょう。人によって、見方や解決方法が違うことがあります!
英語原文
| “ |
Read AloudAs you read your code aloud, think from the computer’s perspective.
Break It DownSeparate the blocks into smaller chunks (or sequences), and click to see what each sequence does. Once the smaller sequences work as you expect, add them back into the main program. The process is called decomposition.
Slow It DownThe computer runs your program so quickly it can be hard to follow with your eyes. Add temporary “wait” or “wait until” blocks to slow down the sequence. This gives you time to process if a piece worked or not. Remove these wait blocks once your code works.
Add Sound CheckpointsSimilar to the Slow It Down strategy, you can add different sounds with the “play until done” block at key points to test your sequence. If a sound doesn’t play, your bug may be before this block. If the sound plays, the bug is probably after this block. Remove the sounds once your code works.
Tinker with Block OrderTry adjusting the order/sequence of the blocks. What needs to happen first? What happens second? Do values or sprites need to reset before the next piece of code runs? Try using blocks inside a loop or conditional statement, versus outside a loop or conditional statement.
To Loop or Not to LoopIf using Control blocks like "forever" and "repeat", check that all blocks inside a loop should be there, or if a block (like “wait”) is missing to reset the action or adjust the timing. Do you want your loop to run forever or for a certain number of times? Should something stop the looping? Perhaps you aren't using a loop when you should be? For instance, if you are using a conditional statement block like "if then," does the program only need to check if it is true or false once? Or does it need to check continuously, in which case, you would want to place your conditional statement inside a forever loop?
Think About Timing & ParallelismDo you have multiple events trying to run at the same time? If two sequences are programmed to start at the same time, you can get unpredictable behavior. Add small waits, broadcasts, or user interaction (like clicking or pressing a key) to see if this affects the result.
Think About Block OptionsIs there a similar but different block you can use? Some blocks look similar but can behave differently, such as “set” vs “change” or “play until done” vs “start.” Try using a similar block in place of what you have, and see if this affects the result.
Check the ValuesIf you are using variables or reporter blocks, check the value at the moment the code sequence is run.
Check Code SequenceCheck that your code sequence is attached to the correct sprite or the backdrop, if appropriate. If you need to move your code to another sprite, click and drag it until you are hovering over the correct sprite. Release it once the sprite wiggles. You can also use your Backpack (bottom of screen) to store and move your code or assets.
Comment Your CodeAdding comments to your code can help others looking at your code to understand it. It can also help you remember how your code works when you come back to it later. Right click on script area to “Add Comment.” Use everyday language to explain what a block, or small sequence of blocks, does.
Take a Break, Step AwaySometimes, spending too much time focused on an issue can be counterproductive and frustrating. Take a break and step away from the screen to clear your mind. After some rest, focusing on something else, or getting some water, you can approach the problem with fresh eyes.
Ask for HelpIf you are still stuck, you can ask for help from a peer. Try finding a debugging/help studio and share your project, asking for help in a comment or the project notes. Ask one to three people to try your code, as different people may have different perspectives or solutions! |
” |