Asm2 Pro
Asm2 Pro
Student declaration
I certify that the assignment submission is entirely my own work and I fully understand the consequences of plagiarism. I understand that
making a false declaration is a form of malpractice.
Grading grid
P3 P4 P5 P6 M2 M3 M4 D2 D3 D4
1
Summative Feedback: Resubmission Feedback:
3
Contents
I. Introduction.............................................................................................................................................. 7
II. What is procedural, object-orientated and event-driven?.......................................................................7
1. Procedural Programming....................................................................................................................... 7
a) Definition.......................................................................................................................................... 7
b) Features........................................................................................................................................... 7
c) Code structure..................................................................................................................................8
d) Flow of code.....................................................................................................................................8
e) Readability and Maintainability....................................................................................................... 8
f) Limitations........................................................................................................................................ 8
g) Advantages and Disadvantages........................................................................................................9
2. Object-Oriented Programing (OOP)..................................................................................................... 10
a) Definition........................................................................................................................................ 10
b) Features......................................................................................................................................... 10
c) Code structure................................................................................................................................10
d) Flow of code...................................................................................................................................10
e) Readability and Maintainability..................................................................................................... 11
f) Limitations...................................................................................................................................... 11
g) Advantages and Disadvantages......................................................................................................11
3. Event-Driven Programing (EDP)............................................................................................................12
a) Definition........................................................................................................................................ 12
b) Features......................................................................................................................................... 12
c) Code structure................................................................................................................................13
d) Flow of code...................................................................................................................................13
e) Readability and Maintainability..................................................................................................... 13
f) Limitations...................................................................................................................................... 14
4
g) Advantages and Disadvantages.......................................................................................................14
III. Relationship between procedural, object-oriented, event driven.........................................................15
IV. Program flowchart.................................................................................................................................16
1. Login.................................................................................................................................................... 16
2. Add Customer to database..................................................................................................................17
3. Remove Customer from database.......................................................................................................18
4. Update Customer information............................................................................................................ 19
XI. PP, OOP and EDP examples through a simple code...............................................................................20
XII. Comparing PP, OOP, EDP...................................................................................................................... 20
XIII. Debugging and how it can be used to help develop more secure, robust applications......................21
XIV. References...........................................................................................................................................22
Figure 1: Function:
Login ..............................................................................................................................16
Figure 2: Function: Add customer ................................................................................................................
17 Figure 3: Function: Remove
customer ......................................................................................................... 18 Figure 4: Function: Update
customer data .................................................................................................. 19 Figure 5: Example:
Relationship between programming types ................................................................... 20
5
Table 1: PP, OOP and
EDP ............................................................................................................................21
6
I. Introduction
- C#, pronounced "C-Sharp", is a modern, object-oriented programming language designed by
Microsoft for the .NET Framework. C# offers a balance between ease of use and power, making it
popular for building a wide range of applications. Its object-oriented approach allows developers
to create well-structured, reusable code, while its type safety ensures program stability. With a
large community and extensive libraries, C# is a versatile tool for programmers of all levels.
- This report explores various programming paradigms and their suitability for this application. By
examining the advantages and disadvantages of different language approaches, we aim to identify
the optimal paradigm that fosters both security and efficiency for our water billing system,
replaces the less secure and difficult to use of console version.
1. Procedural Programming
a) Definition
o In procedural programming, you solve problems by breaking them down into step-by-step
instructions grouped into procedures (functions). This promotes code reuse and
organization. Think of it as a recipe with clear steps – instructions are followed
sequentially to achieve a result.
b) Features
o Sequential execution: Instructions follow a clear order. o Modularity: Functions group
related code for better organization. o Data structures: Built-in structures manage data
efficiently. o Abstraction: Procedures hide complex details, focusing on functionality. o
Control flow: Control statements guide program execution with decisions and loops.
7
c) Code structure
o Main Function: Program's starting point, calling other functions.
o Functions: Reusable code blocks for specific tasks. o Control Flow:
Statements (if/else, loops) guide execution order.
d) Flow of code
o Procedural code flows step-by-step. The program starts in the main function, which calls
smaller, reusable functions to complete tasks. These functions execute instructions
sequentially, with control flow statements (if/else, loops) guiding the order based on
conditions. Functions can receive data (parameters) and return results, allowing them to
work together and build upon each other's work.
entire program, making it challenging to track changes and potential side effects.
8
f) Limitations
o Reusability: Procedural programs tend to have repetitive code. If you need the same
functionality in multiple places, you often have to rewrite it, making the code harder to
maintain and update.
o Data focus: Procedural programming prioritizes the steps (procedures) to take rather than
the data itself. This can make it difficult to model complex systems where data is
important.
o Real-world modeling: Procedural code doesn't map well to real-world entities. Imagine a
program representing a car. In procedural programming, you might have separate
functions for accelerating, braking, and turning. Object-oriented programming, on the
other hand, would create a "Car" object that encapsulates these behaviors.
o Complexity: Procedural code can become cumbersome and hard to manage in large
projects. As the program grows, keeping track of all the functions and their interactions
becomes a challenge.
9
▪ Data Organization: Procedural programming prioritizes the procedures themselves
over the data they operate on. This can make it difficult to model complex systems
where data plays a central role.
▪ Real-world Modeling: Representing real-world entities with procedural code can
be cumbersome. For instance, a car simulation might have separate functions for
acceleration, braking, and steering. Object-oriented programming offers a more
natural way to model such scenarios.
2. Object-Oriented Programing (OOP)
a) Definition
o In Object-oriented programming (OOP), you build programs with "objects" that act like real-
world things. These objects bundle data and the functions that use that data. OOP keeps
your code organized and secure by focusing on modularity (reusable pieces) and
encapsulation (hiding an object's inner workings).
b) Features
o Objects (based on Classes): Imagine a car blueprint (class) defining features (data) and
actions (methods) for all cars (objects).
o Inheritance: Lets new classes inherit traits from existing ones. Race cars can inherit from
cars and add a boost function.
o Encapsulation: Bundles an object's data and methods, protecting it from unauthorized
changes.
o Polymorphism: Allows objects to respond differently to the same message. A car and
bicycle handle "drive" differently.
c) Code structure
o Classes: Blueprints defining data (properties) and actions (methods) for objects.
o Objects: Instances of classes holding specific data and performing actions defined in the
class.
o Inheritance: Reusing code by creating new classes based on existing ones. o
Encapsulation: Controlling access to an object's data for better security.
10
d) Flow of code
- OOP doesn't have a single, rigid flow of code like a traditional top-down script. Instead, it focuses
on interactions between objects. Here's how OOP code typically flows:
o Object Creation: You define classes with properties and methods. Then, you create objects
(instances) of those classes.
o Method Calls: Objects interact by sending messages (method calls) to each other. These
methods access and manipulate the object's data (properties). o Conditional Logic: Within
methods, you can use conditional statements (if/else) and loops to control the flow of
execution based on specific conditions.
o Inheritance: If classes inherit from others, the child class's methods might call inherited
methods from the parent class. This creates a flow where specific behaviors are reused
and potentially overridden.
f) Limitations
o Complexity: OOP can lead to more complex code, especially in large projects. The
emphasis on classes, inheritance, and relationships between objects can make it harder to
understand the overall program flow.
o Overhead: OOP code can be larger than procedural code due to the extra structure of
classes and objects. This can sometimes impact performance, especially for very
resourceconstrained systems.
o Over-engineering: Sometimes, problems can be solved more simply without all the
structure of OOP. OOP might not be the best choice for every situation.
o Debugging Challenges: Complex dependencies between objects can make debugging
trickier. Changes in one object might ripple through others, making it harder to pinpoint
the source of an issue.
11
g) Advantages and Disadvantages
o Advantages:
▪ Reusability: OOP allows you to create reusable code components (objects and
classes). This saves time and effort in future projects.
▪ Modularity: Code is broken down into manageable objects, making it easier to
understand, maintain, and modify.
▪ Encapsulation: Objects bundle data and methods, protecting data integrity and
hiding internal complexity.
▪ Inheritance: New classes can inherit properties and methods from existing ones,
promoting code reuse and extensibility.
▪ Real-world mapping: OOP lets you model code with objects that resemble
realworld things, making it more intuitive.
o Disadvantage:
▪ Difficult for beginners: OOP concepts like classes, inheritance, and polymorphism
can be challenging for beginners to grasp.
▪ Complexity: Large OOP projects can become complex due to intricate object
relationships and potential over-engineering.
▪ Performance overhead: OOP code can be larger and slower than procedural code
due to the extra structure. (Not always a significant concern)
▪ Debugging challenges: Dependencies between objects can make debugging
intricate. Issues in one object might affect others.
▪ Not ideal for all problems: Sometimes, simpler programming paradigms might be
better suited for specific tasks.
a) Definition
o In event-driven programming, the program waits for events (user clicks, system changes)
and reacts with specific code. This flexible approach is ideal for GUIs and network
applications
12
b) Features
o Event-driven architecture: Programs are structured around handling asynchronous events
(user interactions, system messages) that trigger specific handler functions. This modular
approach promotes loose coupling and maintainability.
o Event loop: A central mechanism continuously monitors for incoming events and
efficiently dispatches them to the appropriate event handlers. This ensures timely
processing and responsiveness.
o Event handlers: Dedicated functions encapsulate the logic for handling specific events.
This promotes code reusability and simplifies maintenance.
o Asynchronous and non-blocking execution: Event-driven programs avoid blocking the
main thread while waiting for tasks to complete. This allows for a more responsive user
experience and efficient handling of concurrent operations.
c) Code structure
o Define Events: Specify what can happen (clicks, messages, timers).
o Register Listeners: Components say what events they care about and how to respond
(handler functions).
o Event Loop: The program waits for events to be triggered.
o Dispatch & Handle: Events are matched to listeners, and their handler functions are
executed.
d) Flow of code
o Initialization: The program sets up the event loop, which continuously monitors for
incoming events.
o Component Registration: Different parts of the program (buttons, windows, network
connections) register themselves with the event loop, specifying the events they're
interested in (clicks, messages, etc.) and the corresponding handler functions to be called.
o Event Loop: The program enters a waiting state, constantly checking for events.
o Event Trigger: When an event occurs (user clicks a button, network data arrives), it's sent
to the event loop.
o Event Dispatch: The event loop matches the triggered event with the previously registered
components.
13
o Handler Execution: The event loop triggers the corresponding handler function associated
with the specific event and component. This handler function contains the code that
actually responds to the event (processing user input, updating the UI, etc.).
o Return to Loop: After the handler function finishes, control returns to the event loop, and
the cycle continues, waiting for the next event.
f) Limitations
o Complexity: Reasoning about program flow can be trickier due to asynchronous events
and potential interactions.
o Debugging: Traditional techniques are less effective due to unpredictable event timing.
o Event Ordering: Ensuring proper event sequence might require additional design and
techniques.
o Error Handling: Error propagation through the system needs careful consideration. o
Over-engineering Risk: EDP's flexibility can lead to overly complex code for simple tasks.
14
o Responsiveness: EDP excels at creating highly responsive applications. Programs don't
block waiting for tasks, allowing them to react quickly to user interactions and system
events. This is ideal for user interfaces (UIs) and network applications.
o Scalability: The loosely coupled nature of EDP makes it easy to scale applications.
Components can be added or removed independently without affecting others, simplifying
horizontal and vertical scaling to meet changing demands.
o Maintainability: EDP promotes well-organized code. Breaking down functionality into
focused event handlers improves readability and simplifies maintenance by minimizing
dependencies between parts of the program.
o Efficiency: EDP avoids unnecessary resource consumption by eliminating continuous
polling for updates. Events trigger actions only when necessary, making applications more
efficient.
- Disadvantages:
o Complexity: Reasoning about program flow in EDP can be more challenging due to the
asynchronous nature of events and potential interactions between components. This
requires careful design to avoid unintended behavior.
o Debugging: Debugging EDP can be trickier than traditional linear code. The unpredictable
timing of events makes it harder to pinpoint the root cause of issues. Debuggers and
logging become even more crucial.
o Over-engineering: The flexibility of EDP can be a double-edged sword. It's easy to
overengineer simple tasks by introducing unnecessary event-driven architecture where a
simpler approach would suffice.
15
In essence, OOP provides a way to organize code, while EDP dictates a program's flow in response to
events. OOP often complements EDP because objects can neatly encapsulate event handling logic.
They aren't mutually exclusive concepts; developers can leverage all three approaches to build
wellstructured and responsive software.
1. Login
16
2. Add Customer to database
17
3. Remove Customer from database
18
4. Update Customer information
19
XI. PP, OOP and EDP examples through a simple code
- Through the simple code above, we can understand the basic operations of PP, OOP and EDP and
their relationship in programming.
Code Modular but not More organized and Highly responsive and
wellorganized reusable asynchronous
20
State Difficult to manage Easier to manage with More complex due to
Management encapsulation asynchronous
Inheritance Not directly supported Supported for code reuse Not directly applicable
Table 1: PP, OOP and EDP
XIII. Debugging and how it can be used to help develop more secure,
robust applications.
- Identifying and Fixing Vulnerabilities:
o Logic Errors: Bugs in code logic can create security holes. Debugging helps uncover these
errors, like unintended data access or incorrect validation.
o Memory Leaks: Improper memory management can lead to vulnerabilities like buffer
overflows, where attackers overwrite memory to gain control. Debugging tools can detect
memory leaks and help prevent such vulnerabilities.
o Injection Attacks: Errors in user input handling can allow attackers to inject malicious code
(SQL injection, XSS). Debugging helps verify that input is properly sanitized and validated.
- Enhancing Robustness:
o Crash Prevention: Unhandled exceptions can crash applications. Debugging helps identify
potential causes of crashes and implement error handling mechanisms to make
applications more resilient.
o Resource Leaks: Leaving resources like files or database connections open can cause
performance issues and instability. Debugging can help identify resource leaks and ensure
proper cleanup.
o Unexpected Behavior: Debugging helps uncover unexpected behavior that might not be
immediately apparent. This allows developers to fix these issues and create more
predictable applications.
- Benefit of effective debugging:
o Reduces Security Risks: Eliminates vulnerabilities that could be exploited by attackers. o
Improves Stability: Makes applications less prone to crashes and unforeseen issues. o
Enhances Performance: Helps identify and fix performance bottlenecks.
o Increases Maintainability: Well-debugged code is easier to understand and modify in the
future.
21
XIV. References
My source code on Github:
22
V. Tools used for application development
• Programming language: C#
• IDE: Visual Studio 2022
VI. Application creating
1. Create project
• I use Windows Forms App template to be able to design forms
2. Set project name
• I named the project Assignment2
3. Set project framework
• I use .NET 8.0 for my project
VII. Application classes
1. Program.cs
• This class is entry point (main class) of my application.
• The program will first call the Configure() method, which will add two Account objects to ActiveAccounts in the
ProgramEnvironment class.
• The program will then call ShowBaseForm(), which will open the LoginForm.
•
2. DLLIntermediate.cs
• This class mediates my program with the Windows DLLs (Dynamic Link Library). Here are kernel32.dll and
user32.dll.
• This class imports GetConsoleWindow() method from
kernel32.dll and ShowWindow(IntPtr, int) from
user32.dll.
• In addition, this also add a method similar to
ShowWindow(IntPtr, int) but change the IntPtr type
parameter to Form so that we can use the
ShowWindow method directly from the provided Form.
3. utils/Log.cs (part
1)
• Provides methods for logging, helpful in testing and debugging
3. utils/Log.cs (part
2)
• Provides methods for logging, helpful in testing and
debugging
7. data/Database.cs
(part 2)
• Database for data, data class must be inherited from IData<T>
and have an IMinimalData<T> inherited class of itself
• The class provides Add and Remove() methods used to add
and delete data from the Database, it also provides private
Save() and Load() methods, called when data changes to store
data to file
8. data/Account.cs
• Account object use to login in LoginForm
9. data/Customer.cs (part 1)
• Customer object. Store data of customer, contains
CustomerType, calculation methods
9. data/Customer.cs (part 3)
• Customer object. Store data of customer, contains
CustomerType, calculation methods
9. data/Customer.cs (part 4)
• Customer object. Store data of customer, contains
CustomerType, calculation methods
9. data/Customer.cs (part 5)
• Customer object. Store data of customer, contains CustomerType, calculation methods
10. gui/BaseForm.cs
• Frames of other forms in the application, initialized
basic properties
• The code in InitializeComponent() is minified because
it’s too wordy and unnecessary to show, these codes
simply set the properties of Controls
11. gui/About.cs
• Information about application
12. gui/LoginForm.cs
• The window appears when starting the application. Used to verify
users.
• Like BaseForm, InitializeComponent() is also minified.
• Once the entered information is valid, go to MainForm
13. gui/MainForm.cs (part 1)
• Actions Add, Delete, Update, View detailed user
information here
13. gui/MainForm.cs (part 2)
• Actions Add, Delete, Update, View detailed user
information here
13. gui/MainForm.cs (part 3)
• Actions Add, Delete, Update, View detailed user information here
13. gui/MainForm.cs (part 4)
• Actions Add, Delete, Update, View detailed user
information here
13. gui/MainForm.cs (part 5)
• Actions Add, Delete, Update, View detailed user
information here
13. gui/MainForm.cs (part 6)
• Actions Add, Delete, Update, View detailed user information
here
14. gui/UserDetailsForm.cs (part 1)
• Used to display all information about the
Customer and the print action
14. gui/UserDetailsForm.cs (part 2)
• Used to display all information about the
Customer and the print action
VIII.Testing
At Login Form
1.
• This is the interface of LoginForm
• I will use some weird usernames and passwords
• And as expected, the application prevented the greedy hacker
from breaking into the application and editing his used water
down.
2. At Login Form
• So how to log into the application? Well, just log in with the
username admin and the password also admin. Or you can
use huymaster as your login username and don't need to
enter an annoying password
• Just in time, my wallpaper just changed to another image
because I set the wallpaper as a slideshow, we can see the
background of the program has changed color.
• This is the interface of MainForm:
2. At Login Form
• It's so empty, try adding something.
• What if I don't enter anything, will the program add air to the
table?
• Why can't I add? I can't I help John Wick not have to worry
about his monthly bills.
• Well, I had to enter the true amount of water he used.
2. At Login Form
• And this time it was successful. But, I entered the wrong
value, I need to correct it before someone sees it.
2. At Login Form
• Okay, now I will adjust the value at this month to the correct
number which is 58.
3. Customer details
• Luckily, I fixed it before anyone found out. Now I will check the details for him
and print the invoice. I just need to doubleclicked on his row.
3. Customer details
• Everything is fine, now I will print the invoice for him.
• Things are going wrong. My mom broke my printer because she
thought it was my router when she saw me playing games for 5
minutes after studying for 10 hours.
• Well, since the printer is gone, I will print at another time.
• I will remove his name from the list because I asked the masked guy I
just met on the street to print it for me.
4. Delete customer data
• I will remove his name from the list because I asked the masked
guy I just met on the street to print it for me.
• Done, my work for today is over. There's so much on my plate
today, I'll have to take a break
IX. IDE's function in enhancing algorithms
1.
Refactoring
• Improve code organization (rename variables,
extract methods, add comments). IDE can help us
refactor and update them throughout the entire
project and saves more time than when you
program with Notepad (Although I don't know
anyone who tries hard to program with
Notepad).
Before refactoring
3.
V
e
r
4.
s
i
o
n
C
o
5.
n
t
r
o
l
(
6.
V
C
S
)
Unit Testing
• Write small tests to ensure code correctness.
7.
U
n
i
t
8.
T
e
s
t
i
n
g
u
s
i
n
g
N
U
n
i
t
i
n
9.
I
D
E
Profiling (for
efficiency)
• Identify performance bottlenecks to
optimize algorithms.
• It can
be
10.
relied on to check code that consumes a lot
of resources to fix,
Keep: Change:
• Problem-solving steps and overall logic remain • Syntax, data structures, and efficiency tweaks the same regardless
of the code's syntax (C#, adapt to the chosen language's features.
Python, etc.).
Naming,Indent,Formatting