Software Engineering For
Data Science (SEDS)
Class: 2 Year 2nd Cycle
Branch: AIDS
Dr. Belkacem KHALDI| ESI-SBA
Lecture 03:
Advanced Concepts for Python
Software Engineering: Modularity,
Readability, and Refactoring
Dr. Belkacem KHALDI
e-mail: [Link]@[Link] 1
Advanced Concepts for Python Software
Engineering: Modularity, Readability, and
Refactoring
1. Modularity
2. Readability
3. Refactoring
Dr. Belkacem KHALDI
e-mail: [Link]@[Link] 2
Modularity
Dr. Belkacem KHALDI
e-mail: [Link]@[Link] 3
Modularity
Why Adopting Modularity? Modularity in Python
● 3 ways that you can write modular code with
● Non-modular code can take the form of long, Python: classes, modules, and packages
complicated, hard to read scripts and functions.
● Modular Code ⇒ Code divided into shorter
functional units.
● Modular code ⇒ Code becomes more readable
and easier to fix when something breaks.
● Modular code ⇒ Provides code portability
(save time by avoiding re-solving problems you've
already solved in a previous project).
Dr. Belkacem KHALDI
e-mail: [Link]@[Link] 4
Modularity: Object-Oriented Programming
Procedural Programming Object-Oriented Programming
● Code as a sequence of steps. ● Code as interactions of objects.
● A program is divided into small functions ● A program is divided into objects.
● Importance is not given to data but to procedure. ● Importance is given to data rather than functions
● Why PP?
○ A good choice for general-purpose programming. ● Why OOP?
○ Offers a simple, intuitive, and straightforward ○ Organize your code better.
way of writing sequential code.
○ Offre a way of Securing Data (encapsulation).
○ Easier for compilers and interpreters.
○ Make the code more reusable and maintainable.
○ Easier to customize functionality from libraries.
Dr. Belkacem KHALDI
e-mail: [Link]@[Link] 5
Modularity: Object-Oriented Programming
Motivational Example: Pytorch
● Probably you will work a lot with Pytorch Deep
Learning framework.
● TinyModel ⇒ A new class is defined as a subclass
of [Link]
● __init__ () ⇒ A method to initialize an instance
object constructed from this class.
○ Two linear layers are declared and activation
functions.
● forward() ⇒ A function describing how the
network is connected is declared in function.
● tinymodel ⇒ An object is instantiated from the
TinyModel class.
Dr. Belkacem KHALDI
e-mail: [Link]@[Link] 6
Modularity: Object-Oriented Programming
Fundamental Concepts
● The fundamental concepts of OOP are classes
and objects.
● Classes ⇒ blueprint for objects describing
possible states and behaviors bundled together.
● Object ⇒ a just a specific realization
(instantiation) of a class with particular state
values.
We think of actions and data as one unit representing
a class ⇒ Encapsulation
Dr. Belkacem KHALDI
e-mail: [Link]@[Link] 7
Modularity: Object-Oriented Programming
Objects in Python
● Everything in Python is an object ⇒ Numbers,
Strings, DataFrames, even functions
● Every Object has a Class Object Class
● Use type() to find the class
5.2 float
“Welcome” str
[Link]() DataFrame
[Link] function
[1,2,3] list
… …
Dr. Belkacem KHALDI
e-mail: [Link]@[Link] 8
Modularity: Object-Oriented Programming
Objects in Python: State & Behavior
● Object = State (attributes) + Behavior State ⇐⇒ Attributes
(methods)
○ State ⇐⇒ variable ⇐⇒ obj.my_attribute
○ Behavior ⇐⇒ function() ⇐⇒ obj.my_method()
All attributes and methods of an object
Behavior ⇐⇒ Methods
Dr. Belkacem KHALDI
e-mail: [Link]@[Link] 9
Modularity: Object-Oriented Programming
Objects in Python: Basic Class
● class <name>: starts a class definition
● code inside class is indented
● Attributes are defined by assignment inside the
constructor
● method definition ⇒ function definition within
class
● use self to represent the instance of the class
○ used to access class’ attributes and methods
○ should be the 1st argument of any class method
○ Automatically handled by python when calling
methods
● Attributes are created once the object is created.
● Attributes can be created in methods, but will
be accessible only one the method is called (not
recommended)
Dr. Belkacem KHALDI
e-mail: [Link]@[Link] 10
Modularity: Object-Oriented Programming
Objects in Python: Core
Principles
● Instance-level data
○ name , salary are instance attributes
● Class-level data:
○ Useful for Global Constants related to the
class
○ MIN_SALARY is shared among all instances
○ Don't use self to define class attributes
● Printing just the value of an Employee object
(emp1) ⇒ will print the reference to @mem
allocated to the object.
○ We need a better representative meaning
when printing an object
Dr. Belkacem KHALDI
e-mail: [Link]@[Link] 11
Modularity: Object-Oriented Programming
Objects in Python: Core Principles Objects in Python: Core Principles
● Printing an object ● Printing an object
○ __str__() ○ __repr__()
■ print(obj) , str(obj) ■ repr(obj)
○ Informal, for end user ○ Formal, for developer
○ String representation ○ Reproducible representation
Dr. Belkacem KHALDI
e-mail: [Link]@[Link] 12
Modularity: Object-Oriented Programming
Objects in Python: Core Principles
● Implementing __str__()
• Calling print(object) ⇒ Will
implicitly call __str__()
Dr. Belkacem KHALDI
e-mail: [Link]@[Link] 13
Modularity: Object-Oriented Programming
Objects in Python: Core Principles
● Implementing __repr__()
• Calling just the object ⇒ Will
implicitly call __repr__()
• Surround string arguments
with quotation marks in the
__repr__() to represent
better the output
Dr. Belkacem KHALDI
e-mail: [Link]@[Link] 14
Modularity: Object-Oriented Programming
Objects in Python: Core Principles
● Solution: Overloading __eq__()
● Object Equality:
○ Implement __eq__() in your class ⇒ Called when
○ emp1 and emp2 are not equal even they contain the 2 objects of a class are compared using ==.
same content
○ Accepts 2 arguments, self and an other object to
○ Why? ⇒ Here, equality is asserted based on @mem compare.
Dr. Belkacem KHALDI
e-mail: [Link]@[Link] 15
Modularity: Object-Oriented Programming
Objects in Python: Core Principles
● Comparison Operators
Operator Method
== __eq__()
!= __ne__()
>= __ge__()
<= __le__()
> __gt__()
< __lt__()
Dr. Belkacem KHALDI
e-mail: [Link]@[Link] 16
Modularity: Object-Oriented Programming
Objects in Python: Core Principles
● Exception ● Exception Handling
○ Exceptions are classes
○ Prevent the program from terminating when raised
○ Many built-in Exceptions in Python:
Dr. Belkacem KHALDI
e-mail: [Link]@[Link] 17
Modularity: Object-Oriented Programming
Objects in Python: Core Principles
● Exception Handling Example ● Raising Exception
○ Exceptions are classes
○ Prevent the program from terminating when raised
○ Many built-in Exceptions in Python:
Dr. Belkacem KHALDI
e-mail: [Link]@[Link] 18
Modularity: Object-Oriented Programming
Objects in Python: Core Principles
● Class Inheritance & Polymorphism
○ Code reuse:
■ OOP is fundamentally about code reuse
■ Millions of people out there writing code to solve
parts of our problems
■ Modules are great for fixed functionally (code reuse)
○ What if that code doesn’t match your needs exactly?
■ OOP is great for customizing functionality ⇒
Inheritance
New class functionality = Old class functionality + extra
Dr. Belkacem KHALDI
e-mail: [Link]@[Link] 19
Modularity: Object-Oriented Programming
Objects in Python: Core Principles
● Class Inheritance & Polymorphism
○ Example
■ A basic BankAccount class that has a
balance attribute and a withdraw method.
■ By inheritance ⇒ several types of
accounts can be created via code reuse:
SavingAccount and CheckingAccount:
● Add new attributes or methods
● Modify version of existed methods
Dr. Belkacem KHALDI
e-mail: [Link]@[Link] 20
Modularity: Object-Oriented Programming
Objects in Python: Core Principles
● Inheritance: "is-a" relationship
● Class Inheritance & Polymorphism
○ A SavingsAccount is a BankAccount (Possibly
○ Implementing Class Inheritance: Basics
with extra functionalities)
Dr. Belkacem KHALDI
e-mail: [Link]@[Link] 21
Modularity: Object-Oriented Programming
Objects in Python: Core Principles
● Class Inheritance & Polymorphism
○ Customizing Constructors:
■ Can run constructor of the parent class
first by Parent __init__(self, args...)
■ Add more functionality as usual
■ Can use the data from both the parent
and the child class
Dr. Belkacem KHALDI
e-mail: [Link]@[Link] 22
Modularity: Object-Oriented Programming
Objects in Python: Core Principles
● Class Inheritance & Polymorphism
○ Customizing Constructors:
■ Can run constructor of the parent class
first by Parent __init__(self, args...)
■ Add more functionality as usual
■ Can use the data from both the parent
and the child class
■ Can override existing methods ⇒
Polymorphism
Dr. Belkacem KHALDI
e-mail: [Link]@[Link] 23
Modularity: Object-Oriented Programming
Objects in Python: Core Principles
● Protected Access: @property
○ Use "protected" attribute with leading _ to store
data.
○ Use @property on a method whose name is
exactly the name of the restricted attribute;
return the internal attribute.
○ Use @[Link] on a method attr() that will be
called on [Link] = value
● The value to assign passed as argument
Dr. Belkacem KHALDI
e-mail: [Link]@[Link] 24
Modularity – Modules
What is a Python Module?
● A module ⇒ A logical organization of a Python
Code so that similar piece of codes are grouped
into a single file.
● A module ⇒ A single namespace, with a collection
of functions, constants, class definitions and
variables grouped in a single file.
● How?
○ Collect classes and functions in library modules
○ just put classes and functions in a file
module_name.py
○ Put module_name.py in one of the directories
where Python can find it.
Dr. Belkacem KHALDI
e-mail: [Link]@[Link] 25
Modularity – Modules
Importing Python Modules
● “import” statement ⇒ most common way to use
modules in Python Code.
● To import entire module
○ import <module name>
○ Example: import math
● To import specific function/object from module:
○ from <module_name> import <function_name>
○ Example: from math import sqrt
● We may use import aliasing for abbreviation
purposes using the word as.
Note: Python comes with hundreds of built-in modules.
math is one among them. You may refer to
[Link] for the
complete python list modules.
Dr. Belkacem KHALDI
e-mail: [Link]@[Link] 26
Modularity – Modules
More on Modules
● The Module Search Path:
○ When a module is imported:
■ The interpreter first searches for a built-in
module with that name.
■ If not found, it then searches in a list of
directories given by the variable [Link]. sys.
Note: A __pycache__ directory will be created in the container
directory, if one does not already exist, to speed up loading modules.
Dr. Belkacem KHALDI
e-mail: [Link]@[Link] 27
Modularity – Modules
More on Modules
● Ensuring your Module is Found:
○ Using Current Directory:
■ Put <module_name.py> in the directory where the input script is located (the current directory)
○ Using PYTHONPATH:
■ Modify the PYTHONPATH environment variable to contain the directory where
<module_name.py> is located before starting the interpreter.
■ Or put <module_name.py> in one of the directories already contained in the PYTHONPATH
variable.
○ Using the installation-dependent directories:
■ Put <module_name.py>in one of the installation-dependent directories, which you may or may
not have write-access to, depending on the OS.
○ Using any Directory:
■ Put <module_name.py> in any directory of your choice and then modify [Link] at run-time
Dr. Belkacem KHALDI
e-mail: [Link]@[Link] 28
Modularity – Packages
What is a Python package?
● A “package” ⇒ a module, except it can have
other modules (and indeed other packages)
inside it.
● A package ⇒ a directory with a __init__.py
file and any number of python files or other
package directories:
● The __init__.py can be:
○ Totally empty – or
○ Have arbitrary python code in it. ● import science
■ The code will be run when the package is ○ will run the code in science/__init__.py.
imported.
● Any names defined in the __init__.py will be
● Idem while importing a nested
package. available in: science.a_nameBut,
● science.module_a
● Not: Modules or sub-packages inside ○ will not exist. To get submodules, you need to
packages are not automatically imported. explicitly import them
○ import science.module_a
Dr. Belkacem KHALDI
e-mail: [Link]@[Link] 29
Modularity – Packages
Building Your Own Basic Package Structure:
Package package_name/ The main package – this is where
the code goes
● Use a well structured, standard
layout for your package to help tests/ your unit tests (Recommended).
you build, install and distribute
it. docs/ documentation (Recommended).
● Have to be standardized for
publishing in the Python [Link] text of the license you choose (do
Package Index (PyPI) choose one!) (Optional).
repository.
[Link] description of what non-code files
● Create the structure to include (Optional).
○ Manually or
[Link] description of the package –
○ Use specific python packages (.txt, .md) should be written in ReST or
■ Cookiecutter Markdown (for PyPi)
(Recommended).
[Link] the script for building/installing
package (Mandatory).
Dr. Belkacem KHALDI
e-mail: [Link]@[Link] 30
Packages Example: Copy and Past the MIT License Content available
in [Link] to create a
Building Your Own Package: The [Link].
[Link] File
● [Link] file ⇒ A text file licensing your package
for anybody to:
Use Modify Share
For any purpose
Subject to conditions preserving the provenance and openness of the
package
● Most well-known open source licences:
○ GNU AGPLv3
○ MIT
Note: More details on open source Licenses can be found in
[Link]
Dr. Belkacem KHALDI
e-mail: [Link]@[Link] 31
Modularity – Packages
Building Your Own Package: The
[Link] File
● [Link] file ⇒ A declarative Python file
describing your package in terms of:
○ Version & package metadata
○ List of packages to include
○ List of other files to include
○ List of dependencies
○ List of extensions to be compiled
● It tells setuptools how to package, build and
install it.
○ Setuptools ⇒ A package development process
library designed for creating and distributing
Python packages.
Dr. Belkacem KHALDI
e-mail: [Link]@[Link] 32
Modularity – Packages
Building Your Own Package: The
[Link] File
● [Link] file ⇒ A descriptive manual file for ● A good README file should include:
your Python package project, alternatively named ○ A descriptive project title
[Link] or [Link].
○ Motivation (why the project exists)
● To be displayed properly on PyPI, choose a
markup language supported by PyPI: ○ How to setup
○ plain text ○ Copy-pastable quick start code example
○ reStructuredText ○ Recommended citation
○ Markdown
Dr. Belkacem KHALDI
e-mail: [Link]@[Link] 33
Modularity – Packages
Building Your Own Package: The [Link] File (Optional)
● A built source distribution (sdist) ⇒ by Some [Link] Commandes
default contains only a minimal set of files Command Description
(All Python source files, …).
● We want to include extra files in the include pat1 pat2 ... Add all files matching any of the listed patterns
source distribution, such as non-code files
exclude pat1 pat2 ... Remove all files matching any of the listed patterns
⇒ Solution use the [Link] file.
● [Link] file ⇒ A descriptive file global-include pat1 pat2 ... Add all files anywhere in the source tree matching any
for adding & removing files to & from the of the listed patterns
source distribution.
global-exclude pat1 pat2 ... Remove all files anywhere in the source tree matching
● A [Link] file ⇒ A set of
commands, executed one per line, any of the listed patterns
instructing setuptools to add or remove
some set of files from the sdist. graft dir-pattern Add all files under directories matching dir-pattern
prune dir-pattern Remove all files under directories matching dir-pattern
Example:
1. Add the contents of the directory tree tests to the sdist
2. Remove all files in the sdist with a .py extension
Dr. Belkacem KHALDI
e-mail: [Link]@[Link] 34
Modularity – Packages
Building Your Own Package: The [Link] File Example
Markdown Input Rendered Output
Dr. Belkacem KHALDI
e-mail: [Link]@[Link] 35
Modularity – Packages
Building Your Own Package: Builds
a Distribution
● With a [Link] script defined, setuptools can
do a lot:
python [Link] sdist
○ Build a source distribution (sdist) ⇒ a tar archive
of all the files needed to build and install the Offline pip install dist/<fi[Link]>
package.
○ Builds wheels (bdist_wheel) ⇒ a binary python [Link] bdist_wheel
distribution .whl file directly installable through
the pip install command. Offline pip install dist/<fi[Link]>
■ The same file to be uploaded to [Link]
■ to upload your packgage to [Link] you have twine upload dist/*
to first register an account:
[Link] Online pip install <package-name>
Twine package (have to be installed first) ⇒ provides a
Note: The generated sdist or .whl files will be under secure, authenticated, and verified connection between
subdirectory dist/ of your package. your system and PyPi over HTTPS.
Dr. Belkacem KHALDI
e-mail: [Link]@[Link] 36
Readability
Dr. Belkacem KHALDI
e-mail: [Link]@[Link] 37
Readability
Introducing PEP 8
● Pythonistas software engineering community has
conventions of coding in Python ⇒ Protocol 8
(PEP 8)
● PEP 8 is the defacto Style Guide for Python Code
○ Guide you how to format your code to be as
readable as possible.
● Example of Violating PEP 8
○ The module import isn't at the top of the file
○ The spacing and indentation is inconsistent
○ The lack of line breaks makes it difficult to tell
when one idea finishes and the next begins
Dr. Belkacem KHALDI
e-mail: [Link]@[Link] 38
Readability
Introducing PEP 8
● Example of Following PEP 8
○ The same chunk of code looks much better after
rewritten to conform to PEP 8 conventions.
■ ⇒ Following the agreed-upon rules in PEP 8
such as using spacing, indentation, break
lines, and others appropriately.
○ The code became much more readable despite
accomplishing the same exact task.
Dr. Belkacem KHALDI
e-mail: [Link]@[Link] 39
Readability
Introducing PEP 8
● PEP 8 Tools
○ Many rules defined in PEP 8 ⇒ We need tools that
can check our code.
○ Smart IDEs can flag violations as soon as you write
a bad line of code,
○ Other options ⇒ Use the pycodestyle package.
■ Pycodestyle
● Check code in multiple files at once
● Output descriptions of the violations
along with the required information to fix
the issue.
Dr. Belkacem KHALDI
e-mail: [Link]@[Link] 40
Readability
Introducing PEP 8 File Column # Error Description
● Using pycodestyle [Link]:10: E225 missing whitespace around operator
○ Installation:
Line #
pip install pycodestyle
Error Code
conda install pycodestyle
Example of using the code in slide 36 when saved in a
○ Using pycodestyle:
[Link] file
pycodestyle <your_python_fi[Link]>
Dr. Belkacem KHALDI
e-mail: [Link]@[Link] 41
Refactoring
Dr. Belkacem KHALDI
e-mail: [Link]@[Link] 42
Refactoring
Refactoring Code
● Refactoring ⇒ Restructuring your code to
improve its internal structure, without changing
its external functionality.
○ The more you refactor your code ⇒ the best
becomes cleaner and modular.
■ Clean ⇒ Readable, Simple, and Concise.
● Why Refactoring?
● When Refactoring?
○ Provide a better built, well-structured, more
readable code ○ Duplicated code
○ Long Method
○ Speed up your development time in the long run
○ Large Classes
○ Easier to maintain code ○ Long Parameter List
○ Reuse more of your code ○ Divergent Change
○ Become a much better programmer ○ …..
Dr. Belkacem KHALDI
e-mail: [Link]@[Link] 43
Refactoring
Basic Tips for Writing
Clean Code
● Use Meaningful Names
○ Be descriptive and consistent
○ Avoid abbreviations and
especially single letters
● Use Pre-built python
functions packages
○ Be efficient in coding
Dr. Belkacem KHALDI
e-mail: [Link]@[Link] 44
Refactoring
Useful Refactoring Techniques
● Streamline methods, remove code
Composing Methods duplication, and pave the way for future
improvements.
Simplifying Method ● Make method calls simpler and easier to
Calls understand.
● Conditionals tend to get more and more
Simplifying Conditional complicated in their logic over time, and
Expressions there are yet more techniques to combat
this as well.
Dr. Belkacem KHALDI
e-mail: [Link]@[Link] 45
Refactoring
Refactoring Techniques –
Composing Methods: Examples
● Extract Method
○ Problem – You have a code fragment that can be
grouped together.
○ Solution – Move this code to a separate new
method (or function) and replace the old code with
a call to the method.
○ Why?
■ The more lines found in a method, the harder
it’s to figure out what the method does.
Dr. Belkacem KHALDI
e-mail: [Link]@[Link] 46
Refactoring
Refactoring Techniques –
Composing Methods:
Examples
● Inline Method
○ Problem – When a method body is more
obvious than the method itself, use this
technique.
○ Solution – Replace calls to the method
with the method’s content and delete the
method itself.
○ Why?
■ A method simply delegates to
another method. In itself, this
delegation is no problem. But it may
become a confusing in some cases.
Dr. Belkacem KHALDI
e-mail: [Link]@[Link] 47
Refactoring
Refactoring Techniques –
Composing Methods:
Examples
● Extract Variable
○ Problem – You have an expression
that’s hard to understand.
○ Solution – Place the result of the
expression or its parts in separate
variables that are self-explanatory.
○ Why?
■ Make a complex expression more
understandable, by dividing it into
its intermediate parts.
Dr. Belkacem KHALDI
e-mail: [Link]@[Link] 48
Refactoring
Refactoring Techniques –
Composing Methods:
Examples
● Inline Temp
○ Problem – You have a temporary
variable that’s assigned the result of a
simple expression and nothing more.
○ Solution – Replace the references to the
variable with the expression itself.
○ Why?
■ Marginally improve the readability
of the code by getting rid of the
unnecessary variable.
Dr. Belkacem KHALDI
e-mail: [Link]@[Link] 49
Refactoring
Refactoring Techniques –
Composing Methods:
Examples
● Replace Temp with Query
○ Problem – You place the result of an
expression in a local variable for later use
in your code.
○ Solution – Move the entire expression to
a separate method and return the result
from it. Query the method instead of
using a variable. Incorporate the new
method in other methods, if necessary.
○ Why?
■ The same expression may sometimes
be found in other methods as well,
which is one reason to consider
creating a common method.
Dr. Belkacem KHALDI
e-mail: [Link]@[Link] 50
Refactoring
Refactoring Techniques –
Composing Methods:
Examples
● Substitute Algorithm
○ Problem – So you want to replace an
existing algorithm with a new one?.
○ Solution – Replace the body of the
method that implements the algorithm
with a new algorithm.
○ Why?
■ To make sure that you have
simplified the existing algorithm as
much as possible.
Dr. Belkacem KHALDI
e-mail: [Link]@[Link] 51
Refactoring
Refactoring Techniques –
Simplifying Conditional
Expressions: Examples
● Consolidate Duplicate Conditional
Fragments
○ Problem – Identical code can be found
in all branches of a conditional.
○ Solution – Move the code outside of the
conditional.
○ Why?
■ Code deduplication.
Dr. Belkacem KHALDI
e-mail: [Link]@[Link] 52
Refactoring
Refactoring Techniques –
Simplifying Conditional
Expressions: Examples
● Consolidate Conditional Expression
○ Problem – You have multiple
conditionals that lead to the same result
or action.
○ Solution – Consolidate all these
conditionals in a single expression.
○ Why?
■ To eliminate duplicate control flow
code and hence for greater clarity.
Dr. Belkacem KHALDI
e-mail: [Link]@[Link] 53
Refactoring
Refactoring Techniques –
Simplifying Conditional
Expressions: Examples
● Replace Nested Conditional with
Guard Clauses
○ Problem – You have a group of nested
conditionals and it’s hard to determine
the normal flow of code execution.
○ Solution – Isolate all special checks and
edge cases into separate clauses and place
them before the main checks.
○ Why?
■ To make it easy to figure out what
each conditional does.
Dr. Belkacem KHALDI
e-mail: [Link]@[Link] 54
Refactoring
Refactoring Techniques –
Simplifying Method Calls:
Examples
● Replace Parameter with Method
Call
○ Problem – Calling a query method and
passing its results as the parameters of
another method, while that method could
call the query directly.
○ Solution – Instead of passing the value
through a parameter, try placing a query
call inside the method body.
○ Why?
■ To get rid of unneeded parameters
and simplify method calls.
Dr. Belkacem KHALDI
e-mail: [Link]@[Link] 55
Refactoring
Refactoring Techniques –
Simplifying Method Calls:
Examples
● Replace Parameter with Explicit
Methods
○ Problem – A method is split into parts,
each of which is run depending on the
value of a parameter.
○ Solution – Extract the individual parts
of the method into their own methods and
call them instead of the original method.
○ Why?
■ To Improve code readability and
much easier to understand the
purpose of the method.
Dr. Belkacem KHALDI
e-mail: [Link]@[Link] 56
Refactoring
Refactoring Techniques –
Simplifying Method Calls:
Examples
● Replace Error Code with Exception
○ Problem – A method returns a special
value that indicates an error?
○ Solution – Throw an exception instead.
○ Why?
■ Returning error codes is an obsolete
holdover from procedural
programming.
■ To frees cod from a large number of
conditionals for checking various
error codes.
Dr. Belkacem KHALDI
e-mail: [Link]@[Link] 57
Thanks for your Listening
Dr. Belkacem KHALDI
e-mail: [Link]@[Link] 58
Refactoring
Tips for Writing Clean
Code
● Use Meaningful Names
○ Be descriptive and imply
type
○ Be consistent but clearly
differentiate
○ Avoid abbreviations and
especially single letters
● Use Pre-built python
functions packages
○ Be efficient in coding
Dr. Belkacem KHALDI
e-mail: [Link]@[Link] 59
Refactoring
Tips for Writing Clean
Code
● Use Modular Code
○ Avoide code repetition
○ Reuse your code
Dr. Belkacem KHALDI
e-mail: [Link]@[Link] 60
Refactoring
Tips for Writing Clean Code
● Use Modular Code
○ Don't Repeat Yourself ○ Functions should do one thing
Dr. Belkacem KHALDI
e-mail: [Link]@[Link] 61