Table Of ContentSoftware
Engineering for
Absolute Beginners
Your Guide to Creating Software
Products
—
Nico Loubser
Software Engineering
for Absolute Beginners
Your Guide to Creating
Software Products
Nico Loubser
Software Engineering for Absolute Beginners
Nico Loubser
London, UK
ISBN-13 (pbk): 978-1-4842-6621-2 ISBN-13 (electronic): 978-1-4842-6622-9
https://doi.org/10.1007/978-1-4842-6622-9
Copyright © 2021 by Nico Loubser
This work is subject to copyright. All rights are reserved by the Publisher, whether the whole or
part of the material is concerned, specifically the rights of translation, reprinting, reuse of
illustrations, recitation, broadcasting, reproduction on microfilms or in any other physical way,
and transmission or information storage and retrieval, electronic adaptation, computer software,
or by similar or dissimilar methodology now known or hereafter developed.
Trademarked names, logos, and images may appear in this book. Rather than use a trademark
symbol with every occurrence of a trademarked name, logo, or image we use the names, logos,
and images only in an editorial fashion and to the benefit of the trademark owner, with no
intention of infringement of the trademark.
The use in this publication of trade names, trademarks, service marks, and similar terms, even if
they are not identified as such, is not to be taken as an expression of opinion as to whether or not
they are subject to proprietary rights.
While the advice and information in this book are believed to be true and accurate at the date of
publication, neither the authors nor the editors nor the publisher can accept any legal
responsibility for any errors or omissions that may be made. The publisher makes no warranty,
express or implied, with respect to the material contained herein.
Managing Director, Apress Media LLC: Welmoed Spahr
Acquisitions Editor: Spandana Chatterjee
Development Editor: James Markham
Coordinating Editor: Divya Modi
Cover designed by eStudioCalamar
Cover image designed by Pixabay
Distributed to the book trade worldwide by Springer Science+Business Media New York,
1 New York Plaza, Suite 4600, New York, NY 10004-1562, USA. Phone 1-800-SPRINGER, fax (201)
348-4505, email orders-ny@springer-sbm.com, or visit www.springeronline.com. Apress Media,
LLC is a California LLC and the sole member (owner) is Springer Science + Business Media
Finance Inc (SSBM Finance Inc). SSBM Finance Inc is a Delaware corporation.
For information on translations, please e-mail booktranslations@springernature.com; for reprint,
paperback, or audio rights, please e-mail bookpermissions@springernature.com.
Apress titles may be purchased in bulk for academic, corporate, or promotional use. eBook
versions and licenses are also available for most titles. For more information, reference our Print
and eBook Bulk Sales web page at http://www.apress.com/bulk-sales.
Any source code or other supplementary material referenced by the author in this book is
available to readers on GitHub via the book’s product page, located at www.apress.com/
978- 1- 4842- 6621- 2. For more detailed information, please visit www.apress.com/source- code.
Printed on acid-free paper
This book is dedicated to Kim,
who keeps me going when I feel like stopping.
Table of Contents
About the Author ��������������������������������������������������������������������������������xv
About the Technical Reviewer ����������������������������������������������������������xvii
Acknowledgments �����������������������������������������������������������������������������xix
Introduction ���������������������������������������������������������������������������������������xxi
Chapter 1: Editors ���������������������������������������������������������������������������������1
The Different Families of Programming Editors ����������������������������������������������������2
Shell-Based Editors �����������������������������������������������������������������������������������������2
Text Editors ������������������������������������������������������������������������������������������������������3
IDEs �����������������������������������������������������������������������������������������������������������������4
The Benefits of an IDE or an Editor like VS Code ��������������������������������������������������5
Installing Visual Studio Code ���������������������������������������������������������������������������������6
Workspaces �����������������������������������������������������������������������������������������������������6
Built-In Features ����������������������������������������������������������������������������������������������7
Features to Install ��������������������������������������������������������������������������������������������9
Summary�������������������������������������������������������������������������������������������������������������10
Chapter 2: Containerizing Your Environment ��������������������������������������11
What Are Containers? �����������������������������������������������������������������������������������������11
The Main Components of Docker Explained �������������������������������������������������������14
The Dockerfile �����������������������������������������������������������������������������������������������14
The Docker Image �����������������������������������������������������������������������������������������15
Docker Containers �����������������������������������������������������������������������������������������16
v
Table of ConTenTs
Setup and Usage �������������������������������������������������������������������������������������������������16
Preparation ����������������������������������������������������������������������������������������������������16
How to Install Docker ������������������������������������������������������������������������������������17
Creating the Dockerized Environments for Your Software’s Infrastructure ������19
Preparations Before You Start Cooking ����������������������������������������������������������20
First Docker Image and Container �����������������������������������������������������������������20
Building the Image and Pushing It to the Repository ������������������������������������22
Pushing the Image to a Docker Repository ���������������������������������������������������26
Docker Orchestration with Docker Compose �������������������������������������������������27
Final Docker Experiment �������������������������������������������������������������������������������31
Docker Checklist and Cheat Sheet ����������������������������������������������������������������������34
Docker Commands ����������������������������������������������������������������������������������������34
Docker-compose Commands�������������������������������������������������������������������������35
Chapter 3: Repositories and Git ����������������������������������������������������������37
A Word About Windows Git Usage and Hidden Files �������������������������������������������37
What Is Source Control? ��������������������������������������������������������������������������������������38
Additional Functionality ���������������������������������������������������������������������������������41
Installing Git and Creating a GitLab Account �������������������������������������������������������41
Using GitLab ��������������������������������������������������������������������������������������������������������42
Commits ��������������������������������������������������������������������������������������������������������������44
Branches �������������������������������������������������������������������������������������������������������������45
A More Advanced Use Case ��������������������������������������������������������������������������������49
Merging Conflicts ������������������������������������������������������������������������������������������������51
Removing the Need to Type Your Password Every Time: SSH �����������������������������54
Gitignore ��������������������������������������������������������������������������������������������������������������56
git stash ��������������������������������������������������������������������������������������������������������������57
git reset and revert ���������������������������������������������������������������������������������������������58
Cheat Sheet ���������������������������������������������������������������������������������������������������������60
vi
Table of ConTenTs
Chapter 4: Programming in Python ����������������������������������������������������63
What Is Programming? ���������������������������������������������������������������������������������������64
Python �����������������������������������������������������������������������������������������������������������������65
Setup for This Chapter and How to Use It ������������������������������������������������������65
Basics ������������������������������������������������������������������������������������������������������������66
Commenting Your Code ���������������������������������������������������������������������������������67
Variables ��������������������������������������������������������������������������������������������������������67
Sequences and Maps ������������������������������������������������������������������������������������������75
Lists and Strings ��������������������������������������������������������������������������������������������76
Tuples ������������������������������������������������������������������������������������������������������������79
When to Use a List and When to Use a Tuple �������������������������������������������������79
Dictionaries ���������������������������������������������������������������������������������������������������80
Decision-Making Operators and Structures ��������������������������������������������������������81
Operators �������������������������������������������������������������������������������������������������������81
Scope and Structure of Python Code �������������������������������������������������������������90
Control Statements ����������������������������������������������������������������������������������������91
Functions �������������������������������������������������������������������������������������������������������99
Custom Functions ������������������������������������������������������������������������������������������99
Classes and Objects ������������������������������������������������������������������������������������������106
The Anatomy of a Class �������������������������������������������������������������������������������108
Instantiating the Class ���������������������������������������������������������������������������������109
Inheritance ��������������������������������������������������������������������������������������������������113
Polymorphism ����������������������������������������������������������������������������������������������115
Composition �������������������������������������������������������������������������������������������������116
Magic Methods ��������������������������������������������������������������������������������������������116
Exceptions ���������������������������������������������������������������������������������������������������������118
The Anatomy of an Exception ����������������������������������������������������������������������119
Raising an Exception �����������������������������������������������������������������������������������120
vii
Table of ConTenTs
Catching an Exception ���������������������������������������������������������������������������������121
Writing an Exception������������������������������������������������������������������������������������121
Imports ��������������������������������������������������������������������������������������������������������������122
Static Access to Classes �����������������������������������������������������������������������������������124
Cheat Sheet �������������������������������������������������������������������������������������������������������125
Scope �����������������������������������������������������������������������������������������������������������125
Variables ������������������������������������������������������������������������������������������������������126
Arrays ����������������������������������������������������������������������������������������������������������126
Control statements ��������������������������������������������������������������������������������������127
Functions �����������������������������������������������������������������������������������������������������127
Classes ��������������������������������������������������������������������������������������������������������128
Exceptions ���������������������������������������������������������������������������������������������������129
Import ����������������������������������������������������������������������������������������������������������129
Reference ����������������������������������������������������������������������������������������������������������129
Chapter 5: Object Calisthenics, Coding Styles, and Refactoring �������131
Object Calisthenics �������������������������������������������������������������������������������������������132
1� Do not exceed one level of indentation per method� (Or rather,
limit the levels of indentation as much as you can�) �������������������������������133
2� Do not use the else keyword �������������������������������������������������������������������134
3� Wrap all primitives and strings ����������������������������������������������������������������136
4� Use only one dot per line �������������������������������������������������������������������������137
5� Do not abbreviate ������������������������������������������������������������������������������������140
6� Keep entities small ����������������������������������������������������������������������������������140
7� Limit classes to use no more than two instance variables ����������������������140
8� Use first-class collections �����������������������������������������������������������������������141
Refactoring Code ����������������������������������������������������������������������������������������������143
viii
Table of ConTenTs
Coding Styles ����������������������������������������������������������������������������������������������������144
Linting ����������������������������������������������������������������������������������������������������������145
Commenting Your Code �������������������������������������������������������������������������������145
Maximum Line Length ���������������������������������������������������������������������������������149
Indentation ��������������������������������������������������������������������������������������������������149
Blank Lines ��������������������������������������������������������������������������������������������������151
Encoding ������������������������������������������������������������������������������������������������������151
Imports ��������������������������������������������������������������������������������������������������������152
Whitespace ��������������������������������������������������������������������������������������������������152
Naming Conventions������������������������������������������������������������������������������������153
Chapter Summary ���������������������������������������������������������������������������������������������156
References ��������������������������������������������������������������������������������������������������������157
Chapter 6: Databases and Database Design �������������������������������������159
Three Things You Can Do with Data ������������������������������������������������������������������160
Overview of Database System Components �����������������������������������������������������161
Setting Up Your DBMS ���������������������������������������������������������������������������������������162
Ports ������������������������������������������������������������������������������������������������������������163
Environment ������������������������������������������������������������������������������������������������163
Volumes �������������������������������������������������������������������������������������������������������164
The Final Docker File �����������������������������������������������������������������������������������164
Viewing Your Database Using Adminer ��������������������������������������������������������165
Cleaning Up and Pushing to the Remote �����������������������������������������������������168
Preparing Your Database �����������������������������������������������������������������������������������169
Primary Keys �����������������������������������������������������������������������������������������������������170
Indexes ��������������������������������������������������������������������������������������������������������170
Index Caveats ����������������������������������������������������������������������������������������������171
ix
Table of ConTenTs
Data Types ���������������������������������������������������������������������������������������������������������171
Creating a Database ������������������������������������������������������������������������������������������172
Creating the Table ����������������������������������������������������������������������������������������174
Filling the Database with Data ��������������������������������������������������������������������175
Your First SQL Queries ���������������������������������������������������������������������������������175
Normalizing the Current Classes Table �������������������������������������������������������������180
First Normal Form ���������������������������������������������������������������������������������������181
Second Normal Form �����������������������������������������������������������������������������������182
Third Normal Form ��������������������������������������������������������������������������������������188
Last Word on Joins ��������������������������������������������������������������������������������������190
Conclusion ��������������������������������������������������������������������������������������������������������191
Cheatsheet and Checklist ���������������������������������������������������������������������������������191
References ��������������������������������������������������������������������������������������������������������192
Chapter 7: Creating a RESTful API: Flask ������������������������������������������193
The Project ��������������������������������������������������������������������������������������������������������194
What Is REST? ���������������������������������������������������������������������������������������������������195
JSON ������������������������������������������������������������������������������������������������������������196
HTTP Verbs ��������������������������������������������������������������������������������������������������197
REST Query Routes ��������������������������������������������������������������������������������������199
HTTP Status Code ����������������������������������������������������������������������������������������199
HATEOAS ������������������������������������������������������������������������������������������������������200
The Technology You Will Use �����������������������������������������������������������������������������201
Setting Up the Environment ������������������������������������������������������������������������������202
Creating the GitLab Project ��������������������������������������������������������������������������202
Project Layout ����������������������������������������������������������������������������������������������203
Creating the docker-compose and Docker Files ������������������������������������������204
The Final Steps: Coding �������������������������������������������������������������������������������216
x