デバッグの6つのステップ
次の文章をプログラマ視点で日本語でわかりやすく翻訳して
1welcome to this episode of Dev questions 2with Tim Corey join us as we tackle the 3questions you are asking about a career 4in software development understanding 5the industry and new technology if you 6are just starting out or you want to 7grow stronger as a developer this is the 8place to get your questions answered now 9here's your host expert developer and 10online educator Tim Corey what are the 11steps to quickly debugging an 12application this is a question is asked 13on a suggestion site and it's one I 14thought be a great question to answer in 15today's episode of Dev questions now 16every bug is a learning opportunity so 17let's start here don't waste it don't 18waste the opportunity to learn something 19from this bug so trying to escape early 20trying to just get somebody else to fix 21it for you these are ways to keep your 22growth stunted as a developer the way to 23grow as a developer is to learn how to 24debug by working through the tough stuff 25if you always try to escape the tough 26stuff you'll never be strong enough to 27stay on your own so you want to make 28sure that you work this list in order 29okay so don't just waste the debugging 30and stunt your growth instead I'm going 31to go over the six steps you should take 32to quickly debug an application if you 33do them in order 34you'll have a lot of success and you'll 35note that getting help doesn't come in 36till the end okay there's a lots of 37things that you can do regardless of 38your skill level there's lots of things 39you can do to learn how to debug earlier 40and more quickly okay so you're going to 41struggle through this this is going to 42be a struggle it almost always is a 43debugging debugging is just it's one of 44the things that developers have to do 45it's the thing that I struggle with 46because you're always going to have 47these weird bugs that you don't 48understand you don't know why they're 49working the way they are and it seems 50like you know you never get simple bugs 51it's G to happen and what's going to 52happen is you're going to figure out how 53to solve those and then that will become 54a simple bug after a few times and so 55you never have that again because you've 56learned how to avoid it in the first 57place you've become a better developer 58Okay so work these steps and don't skip 59steps okay the only time you ever skip a 60step is if you solve the problem okay if 61you get through step one you solve the 62problem cool no need to move on to the 63next step okay but otherwise go through 64this one by one this will also help you 65by the way when you get to the point of 66asking questions you will have the right 67information to give somebody to make 68them not upset at you to give them 69something that shows that you have put 70the work in yourself okay number one and 71here's where a lot of newer developers 72especially fall down they fail this step 73and try to go on a step two so don't 74miss this step number one clearly 75identify the issue don't just start 76trying to fix it identify what the 77problem is 78first okay that means that when your 79system pops up an error message 80don't just go Oh no I got an error 81message close it out and say now what 82you've you've missed part of the first 83step which is to read the error 84message the error messages today 85actually tell you in a lot of cases they 86tell you what the problem is maybe not 87the root problem but they tell you what 88their problem is things like uh we have 89a type mismatch or or we um have you 90know bad data invalid data or whatever 91the case may be they tell you some type 92of message that gives you a clue as to 93what is going on so clearly identify the 94issue read the error message write it 95down not not 96paraphrase get the actual message that 97way if you do end up Googling we'll get 98to that later but if you Google it well 99then you have the actual error message 100to put in there so you get an actual 101result not you know no results because 102that's not the actual error message it's 103just an approximation of the error 104message um reading log statements if you 105have logging going on and hopefully you 106do then read the logs see what happened 107watch what happened up to that point see 108what was going on identify the state of 109the 110application so for instance if you can 111if you know where the error is put a 112break point there and then run the 113application again and when it stops at 114that break point you hopefully know that 115the error is coming next before you get 116to that error look at what are the 117variables holding what information do 118they have what data is in memory what's 119it working with and working on to 120understand what was the state of the 121application when it 122broke and then identify which action tra 123here is the issue so is it certain input 124is it certain time Etc okay so try and 125figure out what's causing this what the 126actual issue is where the issue is you 127know anything else you can find about 128the system the state of it at the time 129now that's step number one and again you 130may find the issue at that point okay 131but if not then number two recreate the 132issue reliably 133so this is where you might say okay it 134looks like whenever I try to insert data 135into this table the application crashes 136so create a little test application that 137the only thing it does is inserts data 138into your your database wherever the the 139problem was and see if it crashes too 140and if it does you have a simple 141Recreation of your application not not 142all the bells and whistles not all the 143proprietary stuff not all of the the 144stuff that shouldn't matter just the 145actual thing that causes the problem try 146and recreate it reliably so set up a 147test environment verify the error occurs 148in that environment because if you know 149where the error is you know what's going 150on at the time of the error you should 151be able to recreate it in another 152environment in a clean project and if 153not well then you're missing some kind 154of context like is it that that 155machine is it some kind of configuration 156on that computer is there something I'm 157assuming that's not the case like for 158example maybe you're assuming you have a 159connection to the SQL server and it 160accidentally has connection to the MySQL 161server and that's different maybe that's 162it okay so try to recreate in a test 163environment and that's going to help you 164down the road when come to talking to 165people as well because now you have an 166example of the problem 167now once you haven't example the problem 168or if not once you're figure out how to 169recreate in the actual environment that 170crashes step through the code to observe 171the issue this is where you can use 172break points or Advanced breakpoints for 173example a breakpoint just pauses your 174code at a certain spot an advanced 175breakpoint might just count how many 176times the breakpoint gets hit so we can 177say hey I got this Loop how many times 178does it get hit I'm expecting 100 and 179then you put a break point after the 180loop and count the previous break point 181it says 10,000 and you're like Ah that's 182not right or it might say two and go Ah 183that's not right so now you use Advanced 184break points to give you more 185information use debugging log statements 186so you can put debugging statements in 187your code to say this method was hit 188this method was hit this method was hit 189and go on down a list or say hey here's 190a data at this spot here's a data at 191this spot and get more information about 192every step along the way where you may 193find hey wait we're missing a method 194call in here that method never got calls 195or this method got called but then it 196short circuited the process or whatever 197the case may be it helps you understand 198what's going on and then this is also 199helpful sometimes is to talk through 200what each line will do and what the 201inputs and outputs would be so out loud 202this may be where you bring the rubber 203duck in you talk through 204every line what you think the compiler 205is going to do this may be tedious maybe 206slow but what it helps you do is when 207you're talking out loud to something you 208know rubber duck that when you're 209talking out loud you kind of reveal some 210of your assumptions where you go and 211then the file gets loaded you're like 212wait wait wait what file where where did 213that file come from I don't have the 214file oh we're not loading the file 215there's the problem okay so by talking 216through you start talking through your 217assumptions okay number four break apart 218the system so if you have this long 219complicated process and it's breaking 220somewhere in there or it's it's breaking 221at one piece break apart and test each 222section independently so this is where 223unit test can be really helpful you 224validate multiple inputs and outputs for 225each little piece each little unit along 226the way and attempt to create the error 227in a specific method so maybe you've 228narrow it down to a section now break it 229down even further into a specific method 230so you can say okay when I pass in a two 231and a you know test is the word that 232breaks something let's find out why okay 233and so then you can create a unit test 234to test that and say yep that's when it 235breaks so and then replicate that 236problem in a new application okay again 237we're trying to pull this out into a 238test 239project re recreate the problem in a new 240application make it as simple as 241possible make the code generic and non 242proprietary this is where you find out 243oh some that proprietary code in that 244really tricky logic we created well that 245doesn't actually work and that's what's 246causing the problem because if you try 247to recreate with some generic code and 248you find out well that works then why 249doesn't ours work maybe you've been 250calling you know your your internal C 251you know company dll and it does some 252things for you like truncating strings 253and you know doing all uppercase in your 254string whatever it's doing right and 255you're calling that over and over again 256well take that out and put in your own 257code in that section and just see if it 258still has a problem because then you're 259eliminating some issues and you got 260something to recreate and this is where 261step six comes in talk it through with 262someone start again with that rubber 263duck so the overall the problem how you 264recreate it the whole thing talk it 265through with someone start the rubber 266duck because it's you know you can take 267as much time as you want with that 268rubber duck but what'll happen is you'll 269start talking through what's going on 270and it might trigger something then talk 271to a cooworker or a friend doesn't even 272have to be a developer just talk to 273someone and say hey this is what I'm 274doing just listen and maybe I might ask 275a stupid question that triggers 276something 277okay only after all those things do you 278then go talk to the internet and say hey 279internet I've got this problem because 280maybe there's a bug in the system maybe 281there's a bug 282inet that's causing the problem now most 283likely the answer is no the problem is 284in your code but by going through all 285those steps first you'll have all the 286information about how you recreate it 287what the error was what the log 288statements told you what the process was 289the the actual code you could give 290someone because it's generic and not 291proprietary and and it recreates the 292problem that you can show someone and 293they can test it and see the problem too 294because then they can say okay here's 295all little work you've done I can see 296that and it's still causing this problem 297okay I spot the problem and I I'm gonna 298feel you know fine about sharing you you 299know we all have blind spots we all 300don't no one knows everything and so 301sure you know I spot a problem I realize 302oh you didn't realize that in this case 303this happens okay not a problem you've 304now learned something new and you fixed 305the problem or they realize hey I can't 306get this solved either looks like it is 307a problem with the overall system we can 308escalate that up to let's say Microsoft 309support and they can look at it so by 310doing all those steps first you have 311really worked the problem you have 312tested things out you've gone as far as 313you can go and you have made sure that 314you have a full understanding of 315everything you can about what the 316problem is and how it 317operates knowing all of that will help 318you solve most of your problems but if 319you can't solve a problem knowing all of 320that will help you communicate clearly 321in a way that shows this is not a 322homework question this is not me just 323trying to avoid work this is I'm stuck 324I've done all the work I can and I just 325need someone to help me out and it's 326much more likely people will be willing 327to help you out if you do those steps I 328have seen people complain about things 329like stack Overflow I have never had a 330problem with stack Overflow I have asked 331multiple questions on stack Overflow I 332have asked questions to Microsoft 333employees before I've never had a 334problem I've never had people get angry 335at me or or downvote me and part of the 336reason why is because I don't come to 337them until I've gone through steps 1 338through five thoroughly I don't go a 339step two until I understand fully the 340answers to step one I go through to step 341three until I understand fully the 342answers to step two and I go through 343step by step by step so that when I'm 344done when I ask a question it's 345thoroughly documented I have this long 346list of information here's a system I'm 347on here's the the versions of everything 348I'm using here is the 349recreation spot where I've recreated the 350problem here's the code I'm using here's 351the the error message I'm getting here's 352the problem here is like I summarize 353everything then I have all the details 354so that people know this is what my 355problem is and that tends to I won't say 356it always will because there's always 357angry people on the internet but it will 358help with that process I also talk to 359others first before I just go ask a 360random stranger on the internet I try 361and ask my co-workers and my friends 362first for help before I go out and ask 363just random people okay so this process 364will take you time you will struggle 365through this process and when you start 366working the process you might not 367understand the whole thing you might 368take a lot of time getting through steps 369one through five that's okay take the 370time don't shortcut the process like I 371said this is how you become a much 372better developer this is how you become 373a senior developer okay this is part of 374the process is understanding how to 375debug a system and it's not just go ask 376somebody else okay learn to do it on 377your own learn to build this skill up 378you will always have problems in your 379code because you don't write perfect 380code in need as anybody else so you're 381always going to have these problems 382you're always going have to work through 383them but the more you can work through 384yourself the more you build up this 385skill the faster you will be at 386debugging problems the less you will 387write actual bad code because you have 388seen it and you'll have learned how to 389fix it ahead of time and the better you 390will be at moving through an application 391and making those fixes okay so those are 392my six steps I encourage you to follow 393them take the time to work through them 394and make sure you really understand the 395problem and how to recreate it before 396you go and get help from someone else 397thanks for listening as always I am Tim 398Corey
この長い文章は、プログラムのデバッグに関するアドバイスを含んでいます。以下はその日本語訳です。
「Dev questions」の今回のエピソードへようこそ。ティム・コリーと一緒に、ソフトウェア開発のキャリア、業界の理解、新しい技術について、あなたが質問している内容に取り組みます。初心者であれ、より優れた開発者になりたいと思っていれば、質問に答える場所はこちらです。今日の「Dev questions」のエピソードで答える素晴らしい質問は、「アプリケーションの迅速なデバッグ方法は何か?」です。
まず、バグは学習の機会です。この機会を無駄にしないでください。つまり、すぐに脱出しようとしたり、他人に修正させようとするのは、開発者としての成長を妨げる行為です。困難な問題を解決することで、デバッグ方法を学び、成長するのです。常に逃げ道を探すと、自分の力だけで立ち向かうのに十分な強さを持つことはありません。
それでは、アプリケーションを迅速にデバッグするための6つのステップについて説明します。順番に行えば、成功しやすくなります。
-
問題を明確に特定する 最初に問題を特定しましょう。エラーメッセージを読み、ログステートメントをチェックし、アプリケーションの状態を特定して、何が原因で問題が発生しているのかを理解します。
-
問題を確実に再現する 問題が発生する環境を再現し、それがどのように発生するかを理解します。
-
コードをステップスルーして問題を観察する ブレークポイントやデバッグログステートメントを使用して、問題がどのように発生するかを確認します。
-
システムを分解して検証する 複雑なプロセスを分解し、各セクションを個別にテストして、特定のメソッドでエラーを再現します。
-
誰かと話して問題を理解する 問題を他人と話し合い、新たな視点を得ることで、問題解決に役立つかもしれません。
-
インターネットで質問する 他の手段で問題を解決できなかった場合に限り、インターネットで質問します。
これらのステップを踏むことで、問題解決のスキルが向上し、より効率的なデバッグが可能になります。また、他人に助けを求める前に、自分で解決策を見つけようとすることが大切です。
以上、ティム・コリーによるアドバイスです。
ブレークポイントの使い方の具体的な内容の部分を翻訳して
ティム・コリーが説明しているブレークポイントの使い方について、以下に翻訳します。
ブレークポイントの使用法:
-
コードの特定の部分でプログラムを一時停止させる: ブレークポイントは、プログラムの実行を特定の行で停止させ、その時点での変数の状態やプログラムの流れを確認するために使用されます。
-
高度なブレークポイントを使用する: 高度なブレークポイントでは、ブレークポイントがヒットした回数をカウントするなど、追加の情報を提供することができます。例えば、ループが予想通りに実行されているかを確認するために、ループの前後にブレークポイントを設置し、それぞれのヒット回数を比較することができます。
-
デバッグログステートメントを使用する: デバッグログステートメントをコードに挿入することで、特定のメソッドが実行されたか、あるいは特定の条件下でどのようなデータが処理されているかを追跡することが可能になります。これにより、どの部分のコードが実行されているか、または実行されていないかを確認できます。
ブレークポイントを活用することで、コードの動作を細かく観察し、問題の原因を特定しやすくなります。また、問題のある部分のコードを詳細に検証することで、バグの原因をより正確に特定することができます。