Commit 962ad651 authored by Laros's avatar Laros
Browse files

Added programming lecture.

parent 4a2cee14
../../submodules/presentation/Makefile
\ No newline at end of file
../../submodules/presentation/beamerthemelumc.sty
\ No newline at end of file
../../submodules/presentation/gen2phen_logo.eps
\ No newline at end of file
../../submodules/presentation/lgtc_logo.eps
\ No newline at end of file
../../submodules/presentation/lumc_logo.eps
\ No newline at end of file
../../submodules/presentation/lumc_logo_small.eps
\ No newline at end of file
../../submodules/presentation/nbic_logo.eps
\ No newline at end of file
../../submodules/presentation/ngi_logo.eps
\ No newline at end of file
../../submodules/presentation/nwo_logo_en.eps
\ No newline at end of file
../../submodules/presentation/nwo_logo_nl.eps
\ No newline at end of file
% LUMC presentation template by J. F. J. Laros.
% Last alteration on 20-02-2011.
%
% The packages texlive-latex-recommended, texlive-latex-base and
% texlive-latex-extra should be installed.
%
% Alter these four lines for a new presentation.
\providecommand{\me}{Jeroen F. J. Laros}
\providecommand{\myTitle}{Good Practices for Programming}
\providecommand{\myConference}{Bioinformatics Work discussion}
\providecommand{\myDate}{Tuesday, 1 February 2011}
% Now go to %%% BEGIN PRESENTATION %%%
\documentclass[a4, portrait]{seminar}
\usepackage{semcolor} % For coloured text.
\usepackage{slidesec} % For section headings.
\usepackage{newcent} % This is a better font for presentations.
\usepackage{listings}
\input{seminar.bug}
\usepackage{graphicx} % For pictures.
\usepackage{fancybox} % For the background picture.
\usepackage[labelfont={color=white}, textfont={color=white}]{caption}
\definecolor{Blue}{rgb}{0.,0.11372,0.37647} % Custom LUMC color
\renewcommand{\labelitemi}{\textcolor{white}{$\bullet$}} % Make the bullets for
\renewcommand{\labelitemii}{\textcolor{white}{--}} % itemising white.
\renewcommand{\labelitemiii}{\textcolor{white}{$\ast$}}
\renewcommand{\labelitemiv}{\textcolor{white}{$\circ$}}
\renewcommand{\labelenumi}{\textcolor{white}{\arabic{enumi}.}}
\newslideframe{TITLE}{ % Template for the title.
\boxput{
\rput(0, 0){\includegraphics[angle=90, scale=.485]{bg}}
}{#1}
}
\newslideframe{PRES}{ % Template for the body.
\boxput{
\rput(0, 0){\includegraphics[angle=90, scale=.485]{bg2}}
}{
\textcolor{Blue}{
\rput[l]{90}(8.57, -1.5){\scriptsize{\myConference}}
\rput[c]{90}(8.57, 5.35){\scriptsize{\theslide/\pageref{LastPage}}}
\rput[r]{90}(8.57, 12.2){\scriptsize{\myDate}}
}
\white #1
}
}
\renewcommand{\makeslideheading}[1]{ % Put the slide headings on top.
\rput[l](0.2, .40){
\textbf{
\textcolor{Blue}{#1}
}
}
\newline
}
\pagestyle{empty}
\begin{document}
\slideframe{TITLE} % Use the title template.
\begin{slide}
\setcounter{slide}{0}
\vspace*{1.5cm}
\begin{center}
{\bf\Large{\myTitle}}\\
\vfill
\textcolor{Blue}{
{\bf
\small{\me}\\
\small{Leiden Genome Technology Center}\\
\small{Department of Human Genetics}\\
\small{Center for Human and Clinical Genetics}
}
}
\vspace{1.1cm}
\end{center}
\end{slide}
\slideframe{PRES} % Use the body template.
%%% BEGIN PRESENTATION %%%
\lstset{
language = Python,
basicstyle = \footnotesize,
lineskip = -0.5ex,
frame = shadowbox,
rulesepcolor = \color{black},
captionpos = b,
numbers = left,
numbersep = -1em,
numberstyle = \tiny
}
\begin{slide}
\slideheading{Introduction}
A very short and incomplete introduction to good practices for programming.
\bigskip
We are trying to avoid:
\begin{itemize}
\item Throw away code.
\item Reinventing the wheel.
\item Wasting time reading your own code.
\item Wasting time reading \emph{someone else's} code.
\item \ldots
\end{itemize}
We will try to show the use and advantages of some standard practices by
giving examples.
\vfill
\end{slide}
\begin{slide}
\slideheading{Introduction}
We shall \emph{not} cover software development as a whole:
\bigskip
\begin{itemize}
\item Requirements engineering.
\item Design.
\item Planning.
\item Releasing.
\item \ldots
\end{itemize}
\bigskip
We focus on basic programming.
\vfill
\end{slide}
\begin{slide}
\slideheading{Introduction}
We \emph{will} focus on:
\bigskip
\begin{itemize}
\item Indentation.
\item Documentation.
\begin{itemize}
\item Inline comment.
\item Choosing good names.
\end{itemize}
\item Functions.
\begin{itemize}
\item Parameters and return values.
\item Documentation conventions.
\end{itemize}
\item Unit tests.
\end{itemize}
\vfill
\end{slide}
\begin{slide}
\slideheading{Indentation}
Indentation is a way to reveal the control flow of a program.
\bigskip
\begin{lstlisting}[caption = {Indented code}]
for i in range(10) :
print i # Executed 10 times.
if i == 1 :
print "hey" # Executed if i equals 1.
#for
\end{lstlisting}
Line $2$, $3$ and $4$ are in the \emph{scope} of the first
\lstinline!for!-loop.
Line $4$ is in the scope of the \lstinline!if!-clause.
\vfill
\end{slide}
\begin{slide}
\slideheading{Indentation}
A piece of C code without indentation.
\bigskip
\begin{lstlisting}[language = C, caption = {Unindented code}, label = u1]
for (i = 0; i < 10; i++)
for (j = 0; j < 20; j++) {
if (i == j)
printf("Equal\n");
if (i > j)
printf("Larger\n");
if (i < j)
printf("Smaller\n");
}
\end{lstlisting}
C (unlike Python) does not \emph{require} indentation.
\vfill
\end{slide}
\begin{slide}
\slideheading{Indentation}
A piece of C code without indentation.
\bigskip
\begin{lstlisting}[language = C, caption = {Unindented code}, label = u2]
for (i = 0; i < 10; i++) {
for (j = 0; j < 20; j++)
if (i == j)
printf("Equal\n");
if (i > j)
printf("Larger\n");
if (i < j)
printf("Smaller\n");
}
\end{lstlisting}
Notice the small change: the \lstinline!{! from line $2$ is now on line $1$.
\vfill
\end{slide}
\begin{slide}
\slideheading{Indentation}
Use indentation to reveal the control flow of the code.
\bigskip
\begin{lstlisting}[language = C, caption = {Same code as in Listing~\ref{u1}}]
for (i = 0; i < 10; i++)
for (j = 0; j < 20; j++) {
if (i == j)
printf("Equal\n");
if (i > j)
printf("Larger\n");
if (i < j)
printf("Smaller\n");
}
\end{lstlisting}
All \lstinline!if! statements are in the scope of the inner
\lstinline!for!-loop.
\vfill
\end{slide}
\begin{slide}
\slideheading{Indentation}
Notice the same small change, it is easier to spot.
\bigskip
\begin{lstlisting}[language = C, caption = {Same code as in Listing~\ref{u2}}]
for (i = 0; i < 10; i++) {
for (j = 0; j < 20; j++)
if (i == j)
printf("Equal\n");
if (i > j)
printf("Larger\n");
if (i < j)
printf("Smaller\n");
}
\end{lstlisting}
Only the first \lstinline!if! statement is in the scope of the inner
\lstinline!for!-loop.
\vfill
\end{slide}
\begin{slide}
\slideheading{Functions}
Functions (procedures, methods, subroutines) are used for:
\bigskip
\begin{itemize}
\item Code reuse.
\item Structuring code.
\item Abstraction.
\item \ldots
\end{itemize}
In general, try to use functions as much as possible
\vfill
\end{slide}
\begin{slide}
\slideheading{Functions}
A piece of sample code (with an intentional flaw).
\bigskip
\begin{lstlisting}[caption = {Flat, repetitive code}]
c1 = a1 * a1
c2 = b1 * b1
d1 = c1 + c2 / 2
c1 = a2 * a2
c2 = b2 * b2
d2 = c1 + c2 / 2
c1 = a3 * a3
c2 = b3 * b3
d3 = c1 + c2 / 2
c1 = a4 * a4
c2 = b4 * b4
d4 = c1 + c2 / 2
\end{lstlisting}
\vfill
\end{slide}
\begin{slide}
\slideheading{Functions}
Writing comment often helps to identify code duplication.
\bigskip
\begin{lstlisting}[caption = {Inline documentation}, label = f2]
c1 = a1 * a1 # c1 is a1^2.
c2 = b1 * b1 # c2 is b1^2.
d1 = c1 + c2 / 2 # d1 is the average of c1 and c2.
c1 = a2 * a2 # Same as previous block,
c2 = b2 * b2 # but now fow a2 and b2.
d2 = c1 + c2 / 2
c1 = a3 * a3 # Same as previous block,
c2 = b3 * b3 # but now for a3 and b3.
d3 = c1 + c2 / 2
c1 = a4 * a4 # Same as previous block,
c2 = b4 * b4 # but now for a4 and b4.
d3 = c1 + c2 / 2
\end{lstlisting}
\vfill
\end{slide}
\begin{slide}
\slideheading{Functions}
Use functions to avoid code duplication.
\bigskip
\begin{lstlisting}[caption = {Same functionality as in Listing~\ref{f2}},
label = f3]
def f(a, b) :
c1 = a * a # c1 is a1^2
c2 = b * b # c2 is b1^2
return c1 + c2 / 2 # The average of c1 and c2.
#f
d1 = f(a1, b1)
d2 = f(a2, b2)
d3 = f(a3, b3)
d4 = f(a4, b4)
\end{lstlisting}
It is now trivial to add more calls to this function.
\vfill
\end{slide}
\begin{slide}
\slideheading{Functions}
Functions also makes altering code easier.
\bigskip
\begin{lstlisting}[caption = {Cubes instead of squares}]
def f(a, b) :
c1 = a * a * a # c1 is a1^3
c2 = b * b * b # c2 is b1^3
return c1 + c2 / 2 # The average of c1 and c2.
#f
d1 = f(a1, b1)
d2 = f(a2, b2)
d3 = f(a3, b3)
d4 = f(a4, b4)
\end{lstlisting}
Altering the same behaviour in the flat code would be time consuming and
prone to errors.
\vfill
\end{slide}
\begin{slide}
\slideheading{Functions}
Use meaningful names for functions and variables.
\bigskip
\begin{lstlisting}[caption = {Funny, but uninformative}, label = m1,
numbers = none]
beer = 2 * yoghurt + bicycle
\end{lstlisting}
\bigskip
\begin{lstlisting}[caption = {Same functionality as Listing~\ref{m1}},
numbers = none]
insertSize = 2 * readLength + internalSize
\end{lstlisting}
\bigskip
Names should reflect the function.
\vfill
\end{slide}
\begin{slide}
\slideheading{Functions}
Use meaningful names for functions and variables.
\bigskip
\begin{lstlisting}[caption = {Same functionality as Listing~\ref{f3}}]
def meanOfSquares(a, b) :
aSquare = a * a
bSquare = b * b
return aSquare + bSquare / 2
#meanOfSquares
d1 = meanOfSquares(a1, b1)
d2 = meanOfSquares(a2, b2)
d3 = meanOfSquares(a3, b3)
d4 = meanOfSquares(a4, b4)
\end{lstlisting}
Notice that inline comment is no longer needed (in this case).
\vfill
\end{slide}
\begin{slide}
\slideheading{Functions}
Document each function.
\bigskip
\begin{lstlisting}[caption = {Documented function}]
def meanOfSquares(a, b) :
"""
Calculate the mean of the squares of two numbers.
Arguments:
int a ; The first number.
int b ; The second number.
Returns:
int ; Mean of the squares.
"""
aSquare = a * a
bSquare = b * b
return aSquare + bSquare / 2
#meanOfSquares
\end{lstlisting}
\vfill
\end{slide}
\begin{slide}
\slideheading{Functions}
Alternative documentation.
\begin{lstlisting}[caption = {Documentation with a markup language},
label = f4]
def meanOfSquares(a, b) :
"""
Calculate the mean of the squares of two numbers.
@arg a: The first number.
@type a: integer
@arg b: The second number.
@type b: integer
@returns: Mean of the squares.
@rtype: integer
"""
aSquare = a * a
bSquare = b * b
return aSquare + bSquare / 2
#meanOfSquares
\end{lstlisting}
\vfill
\end{slide}
\begin{slide}
\slideheading{Unit tests}
Test the implementation of a function.
\bigskip
\begin{lstlisting}[caption = {Unit test of Listing~\ref{f4}}]
def testMeanOfSquares() :
"""
Test the meanOfSquares() function.
"""
assert meanOfSquares(1, 1) == 1
assert meanOfSquares(2, 4) == 10
#testMeanOfSquares
\end{lstlisting}
\bigskip
This function will report an error because of a flaw in the function.
\vfill
\end{slide}
\begin{slide}
\slideheading{Functions}
Because of the unit test, we identified the flaw.
\begin{lstlisting}[caption = {Final function}]
def meanOfSquares(a, b) :
"""
Calculate the mean of the squares of two numbers.
@arg a: The first number.
@type a: integer
@arg b: The second number.
@type b: integer
@returns: Mean of the squares.
@rtype: integer
"""
aSquare = a * a
bSquare = b * b
return (aSquare + bSquare) / 2
#meanOfSquares
\end{lstlisting}
\vfill
\end{slide}
\begin{slide}
\rput(5.4,0.7){\includegraphics[scale=0.07]{lgtc_logo}}
\rput(10.6,0.6){\includegraphics[scale=0.1]{nbic_logo}}
\slideheading{Questions?}
\label{LastPage}
\vfill
\end{slide}
\end{document}
../../submodules/presentation/ul_logo.eps
\ No newline at end of file
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment