Skip to content

Commit a34f554

Browse files
committed
some improvements to the errors finding section
1 parent 37c481b commit a34f554

File tree

3 files changed

+64
-26
lines changed

3 files changed

+64
-26
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ Die Slides zum Kurs in deutscher Sprache können unter <https://thomasweise.gith
3636
11. [Der Datentyp `str`](https://thomasweise.github.io/programmingWithPythonSlidesDE/11_str.pdf)
3737
12. [`None`](https://thomasweise.github.io/programmingWithPythonSlidesDE/12_none.pdf)
3838
13. [Variablen: Wertzuweisung](https://thomasweise.github.io/programmingWithPythonSlidesDE/13_variablen_wertzuweisung.pdf)
39+
14. [Fehler im Kode mit Exceptions und IDE finden](https://thomasweise.github.io/programmingWithPythonSlidesDE/14_fehler_im_kode_mit_exceptions_und_ide_finden.pdf)
3940

4041
### 2.3. The Slides in English
4142
The slides for the course are available at <https://thomasweise.github.io/programmingWithPythonSlides> and also listed below.

text/main/basics/variables/errorsInIde/errorsInIde.tex

Lines changed: 62 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
\hsection{Interlude: Finding Errors in your Code with the IDE}%
1+
\hsection{Interlude: Finding Errors in your Code with the IDE and Exceptions}%
22
\label{sec:errorsInIde}%
33
%
44
\gitLoadAndExecPythonAndErrors{variables:assignment_wrong}{}{variables}{assignment_wrong.py}{}%
@@ -101,44 +101,61 @@
101101
They help us to find errors in the code.
102102
Errors are common.
103103
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.
105111
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}%
109118
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}.
112121
Executing the program with the error leads to the output given in \cref{exec:variables:assignment_wrong}.
113122

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}.
119124
We execute this program manually by clicking on the \pycharmRun\ button or by pressing~\keys{\shift+F10} in \cref{fig:errorsInIde01runProgram}.
120125
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.
123129
It says:~\emph{\inQuotes{\pythonilIdx{NameError}: name \inSQuotes{\pythonil{intvar}} is not defined. Did you mean: \inSQuotes{\pythonil{int_var}}?}}
124130
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.
126132
It simply does not exist.
133+
127134
The \python\ interpreter then checks whether some similar name exists.
128135
It found that there is a variable named \pythonil{int_var}.
129136
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}!
130137
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.
132141
We will discuss this topic in-depth later in \cref{sec:exceptions}, but even at this stage, the message here is pretty clear.%
133142
%
134143
\bestPractice{readErrorMessage}{Always carefully \emph{read} error messages. %
135144
They often provide you very crucial information where to look for the mistake. %
136145
Not reading error messages is wrong.%
137146
}%
138147
%
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.
140154
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.
142159
This should have told us already that something is fishy without the need to even run the program in the first place.%
143160
%
144161
\bestPractice{redUnderline}{%
@@ -157,7 +174,7 @@
157174
We can also click on that note, and it takes us again to the dodgy line in \cref{fig:errorsInIde06errorsListToLine}
158175

159176
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.
161178

162179
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}.
163180
This again takes us to the list of potential errors in \cref{fig:errorsInIde09sidebarToView}.
@@ -167,28 +184,48 @@
167184
Read error messages. %
168185
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.%
169186
}%
187+
\endhsection%
188+
%
170189
%
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.
173206
As mentioned in \cref{bp:readErrorMessage,bp:redUnderline}, using the \pgls{ide} features for error discovery and detection is incredibly important.
207+
174208
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+
177213
Sometimes, there might be some incorrect instructions in your program that just weren't used in your last execution.
178214
So even correct program output does not guarantee that the program itself is correct.
179215
Therefore, always checking each and every piece of code that your \pgls{ide} marks as dodgy is very important.
180216
Make sure that you full understand all error and warning messages.
181217

182218
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).
185221
Even where you deem the warnings as false-positives, try to fix them anyway.
186222
They could result from non-standard code formatting that still \inQuotes{works}, but may be confusing for readers of your code.
187223
You should always try to produce warning-free code.
188224
For every warning or error that you deem as not a problem, remember:
189225
On one hand, \emph{you} might wrong.
190226
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.%
191227
%
228+
\endhsection%
192229
\FloatBarrier%
193230
\endhsection%
194231
%

0 commit comments

Comments
 (0)