|
1 | | -\hsection{Interlude: Finding Errors in your Code with the IDE}% |
| 1 | +\hsection{Interlude: Finding Errors in your Code with the IDE and Exceptions}% |
2 | 2 | \label{sec:errorsInIde}% |
3 | 3 | % |
4 | 4 | \gitLoadAndExecPythonAndErrors{variables:assignment_wrong}{}{variables}{assignment_wrong.py}{}% |
|
101 | 101 | They help us to find errors in the code. |
102 | 102 | Errors are common. |
103 | 103 | They happen all the time. |
104 | | -Every programmer sometimes makes a typo, accidentally switches the order of parameters of a function, stores a \pythonil{float} in an \pythonil{int} variable, and so on. |
| 104 | +All programmers make mistakes. |
| 105 | +Sometimes it's a typo. |
| 106 | +Sometimes we accidentally switch the order of parameters of a function. |
| 107 | +Sometimes we mix up \pythonils{float} and \pythonils{int}. |
| 108 | +Sometimes we make logical errors. |
| 109 | +Sometimes we misunderstood what the function that we are calling does. |
| 110 | +Sometimes we misunderstood the algorithm that we try to implement. |
105 | 111 | Some errors are obvious and easy to fix. |
106 | | -Some require more serious debugging (see \cref{sec:dunder:debugging}). |
107 | | -In many cases, however, our \pgls{ide} can already show us what and where the mistake happened. |
108 | | - |
| 112 | +Some errors can be discovered based on the error output of the program. |
| 113 | +Some errors require more serious \glslink{debugger}{debugging} to even spot them~(see \cref{sec:dunder:debugging}). |
| 114 | +In some simple cases like typos, however, our \pgls{ide} can already point us to the mistake.% |
| 115 | +% |
| 116 | +% |
| 117 | +\hsection{Finding the Error by using the Program Output and Exceptions}% |
109 | 118 | In \cref{lst:variables:assignment_wrong}, we prepared program \textil{assignment_wrong.py}, a variant of \textil{assignment.py}~(\cref{lst:variables:assignment}) with an error. |
110 | | -For the sake of the example, let us assume that the programmer made a type in line~12 of the program: |
111 | | -They misspelled \pythonil{int_var} and \pythonil{intvar}. |
| 119 | +For the sake of the example, let us assume that the programmer made a typo in line~12 of the program: |
| 120 | +They misspelled \pythonil{int_var} as \pythonil{intvar}. |
112 | 121 | Executing the program with the error leads to the output given in \cref{exec:variables:assignment_wrong}. |
113 | 122 |
|
114 | | -The questions now are: |
115 | | -How can we see this same error in our \pycharm\ \pgls{ide}? |
116 | | -Could we have found this error even without executing the program? |
117 | | - |
118 | | -To answer these questions, we open the program \textil{assignment_wrong.py} given in \cref{lst:variables:assignment_wrong} in the \pycharm\ \pgls{ide}. |
| 123 | +We can also open the program \textil{assignment_wrong.py} given in the \pycharm\ \pgls{ide}. |
119 | 124 | We execute this program manually by clicking on the \pycharmRun\ button or by pressing~\keys{\shift+F10} in \cref{fig:errorsInIde01runProgram}. |
120 | 125 | As you can see, the output in the run window is the same as given in \cref{exec:variables:assignment_wrong}~(\cref{fig:errorsInIde02exception}). |
121 | | -Reading this output is the \emph{first} way to find out what went wrong. |
122 | | -The text that appeared tells us what went wrong and even suggests how to fix it. |
| 126 | + |
| 127 | +If a program crashes, then \emph{carefully} reading its output is always the \emph{first} way to find out what went wrong. |
| 128 | +Indeed, in our example, the text that appeared tells us what went wrong and even suggests how to fix it. |
123 | 129 | It says:~\emph{\inQuotes{\pythonilIdx{NameError}: name \inSQuotes{\pythonil{intvar}} is not defined. Did you mean: \inSQuotes{\pythonil{int_var}}?}} |
124 | 130 | This is already pretty clear. |
125 | | -We accessed some variable (name), \pythonil{intvar}, which has not been defined or assigned. |
| 131 | +We accessed some variable~(a name), \pythonil{intvar}, which has not been defined or assigned. |
126 | 132 | It simply does not exist. |
| 133 | + |
127 | 134 | The \python\ interpreter then checks whether some similar name exists. |
128 | 135 | It found that there is a variable named \pythonil{int_var}. |
129 | 136 | Even more, it also tells us the exact file and line where the error occurred, namely in line~12 of file \textil{assignment_wrong.py}! |
130 | 137 | With this information, we have a good chance of finding the mistake. |
131 | | -The so-called \pythonil{Exception} \pgls{stackTrace} that it prints thus not just tells us the error, a probable cause, and the most-likely location that caused the error. |
| 138 | + |
| 139 | +The so-called \pythonilIdx{Exception} \pgls{stackTrace} that it prints to \pgls{stderr} thus not just tells us that there was an error. |
| 140 | +It even tells us a probable cause and the most-likely line of code that caused the error. |
132 | 141 | We will discuss this topic in-depth later in \cref{sec:exceptions}, but even at this stage, the message here is pretty clear.% |
133 | 142 | % |
134 | 143 | \bestPractice{readErrorMessage}{Always carefully \emph{read} error messages. % |
135 | 144 | They often provide you very crucial information where to look for the mistake. % |
136 | 145 | Not reading error messages is wrong.% |
137 | 146 | }% |
138 | 147 | % |
139 | | -In the run console of \pycharm, we click on the linked file location. |
| 148 | +\endhsection% |
| 149 | +% |
| 150 | +\hsection{Finding the Error by using the IDE}% |
| 151 | +% |
| 152 | +We found the error by executing the program, seeing that it crashed, and then reading the output. |
| 153 | +In the run console of \pycharm\ that presents the program output, we can even click on the linked file location. |
140 | 154 | This takes us to where the error occurred in \cref{fig:errorsInIde03underlined}. |
141 | | -When looking at this line, we notice that the incorrectly typed word is (and always was) underline with red color. |
| 155 | + |
| 156 | +The question is: |
| 157 | +Could we have found this error even without executing the program? |
| 158 | +Actually, when looking at the line to which the click took us, we notice that the incorrectly typed word is~(and always was) underline with red color. |
142 | 159 | This should have told us already that something is fishy without the need to even run the program in the first place.% |
143 | 160 | % |
144 | 161 | \bestPractice{redUnderline}{% |
|
157 | 174 | We can also click on that note, and it takes us again to the dodgy line in \cref{fig:errorsInIde06errorsListToLine} |
158 | 175 |
|
159 | 176 | The fourth method in which the \pycharm\ \pgls{ide} can help us to discover errors are small red marks at the right-hand side of our editor window, shown in \cref{fig:errorsInIde07errorMark}. |
160 | | -Holding the mouse cursor over these lines will open a small view with the suggested error message. |
| 177 | +Holding the mouse cursor over these marks will open a small view with the suggested error message. |
161 | 178 |
|
162 | 179 | The fifth way to get a list of potential errors in \pycharm\ is to click on the \pycharmErrorsButton~button in the side menu on the left-hand side or to press~\keys{\Alt+6}, as illustrated in \cref{fig:errorsInIde08sidebar}. |
163 | 180 | This again takes us to the list of potential errors in \cref{fig:errorsInIde09sidebarToView}. |
|
167 | 184 | Read error messages. % |
168 | 185 | If your \pgls{ide} -- regardless whether it is \pycharm\ or something else -- annotates your code with some marks, then you should check every single one of them.% |
169 | 186 | }% |
| 187 | +\endhsection% |
| 188 | +% |
170 | 189 | % |
171 | | -These tools make it much much easier to find errors. |
172 | | -You can guess the importance of such features also by how many different ways \pycharm\ implements to get you to click and investigate its list of proposed errors and warnings. |
| 190 | +\hsection{Summary}% |
| 191 | +Programmers make errors. |
| 192 | +We make errors. |
| 193 | +You are going to make lots of errors. |
| 194 | +Later and now. |
| 195 | +Even now, in very simple programs, we are going to make errors. |
| 196 | +This cannot be prevented. |
| 197 | +The question can only be how we deal with that. |
| 198 | +How can we minimize the number of errors that we are going to make? |
| 199 | +How can we maximize that chance to discover and fix errors? |
| 200 | + |
| 201 | +The answer is simple:~\emph{By using all the tools at our disposal.} |
| 202 | +We can use the output of programs to find errors after running the programs. |
| 203 | +And we can use our \pgls{ide} to find errors based on the hints it provides to us even before executing the programs. |
| 204 | +Both tools make it much much easier to find errors. |
| 205 | +You can guess the importance of error-finding features also by the fact that \pycharm\ implements the error hints in several different ways, in the hope to get you to click and investigate its list of proposed errors and warnings. |
173 | 206 | As mentioned in \cref{bp:readErrorMessage,bp:redUnderline}, using the \pgls{ide} features for error discovery and detection is incredibly important. |
| 207 | + |
174 | 208 | Even if your program executes as expected, there still might be hidden errors in the code. |
175 | | -Sometimes, you cannot easily tell whether the output of a program is correct. |
176 | | -And the output you see might actually be wrong. |
| 209 | +Sometimes, you can easily tell whether the output of a program is correct. |
| 210 | +Sometimes, you cannot. |
| 211 | +In some cases, an output that looks OK might still be wrong. |
| 212 | + |
177 | 213 | Sometimes, there might be some incorrect instructions in your program that just weren't used in your last execution. |
178 | 214 | So even correct program output does not guarantee that the program itself is correct. |
179 | 215 | Therefore, always checking each and every piece of code that your \pgls{ide} marks as dodgy is very important. |
180 | 216 | Make sure that you full understand all error and warning messages. |
181 | 217 |
|
182 | 218 | Warnings can be important, too. |
183 | | -They can indicate possible errors, potential problems with variable types, or missing required packages (see, e.g., \cref{sec:gitClonePycharm}). |
184 | | -Always fix errors and warnings wherever possible (obviously). |
| 219 | +They can indicate possible errors, potential problems with variable types, or missing required packages~(see, e.g., \cref{sec:gitClonePycharm}). |
| 220 | +Always fix errors and warnings wherever possible~(obviously). |
185 | 221 | Even where you deem the warnings as false-positives, try to fix them anyway. |
186 | 222 | They could result from non-standard code formatting that still \inQuotes{works}, but may be confusing for readers of your code. |
187 | 223 | You should always try to produce warning-free code. |
188 | 224 | For every warning or error that you deem as not a problem, remember: |
189 | 225 | On one hand, \emph{you} might wrong. |
190 | 226 | On the other hand, having fewer warnings and false-positive suspected errors makes it easier to find the actual problem if an actual problem happens.% |
191 | 227 | % |
| 228 | +\endhsection% |
192 | 229 | \FloatBarrier% |
193 | 230 | \endhsection% |
194 | 231 | % |
0 commit comments