Module 7.3- Programming Organisation & Logic Flows Flashcards

(13 cards)

1
Q

Simple Program Flow

A
  • Sequential Execution- code runs step by step in the order it’s written e.g. if you write code to print two statements, the first one will print before the second
  • Input and Output- programs often take user input, process that input, and produce an output
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Looping

A
  • For Loops- a for loop repeats a block of code a specific number of times, usually by iterating over a sequence (like a list or a range of numbers)
  • While Loops- a while loop continues to execute as long as a specified condition is true
  • useful when you don’t know in advance how many times you need to repeat the code
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Functions

A
  • one of the most important concepts in programming; allows you to encapsulate a block of code and reuse it throughout your program
  • helps keep your code organised, efficient, and DRY (Don’t Repeat Yourself)
  • a function is a named section of code that performs a specific task; you can pass inputs (called parameters) to a function, and it can return an output
  • help break a program into smaller, manageable pieces, making it easier to write and maintain
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Why use Functions?

A
  • Reusability- can define a function once and reuse it multiple times, saving time and reducing code duplication
  • Modularity- functions make it easier to structure code in a logical way
  • each function performs a specific task, which makes your code more readable and easier to debug
  • e.g. you could use functions to handle tasks like calculating the total price of items in a shopping cart or formatting user input for a web form
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Conditions

A
  • Conditions guide decision-making in your code
  • enable different actions based on specific criteria
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

How do Conditions work?

A
  • conditions use if-else statements to check whether something is true or false
  • if a condition is true, the program executes a specific block of code
  • if it’s false, it may execute a different block
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Common types of Conditionals

A
  • If Statements- checks if a condition is true and executes a block of code if it is
  • Else Statements- provides an alternative action if the if condition is false
  • Elseif (Else-If)- allows you to check multiple conditions in sequence
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Why are Conditions important?

A
  • conditionals allow your program to make decisions, which is critical for responding to user input or processing data
  • without conditions, programs would always follow a single, linear path, but with conditionals, you can introduce logic that makes programs more dynamic and useful
  • e.g. when writing a login system, you might check whether a user’s input matches stored credentials, and display a message accordingly (whether the login is successful or failed)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Organising Code with Classes

A
  • one of the most important aspects of organising your code, especially in Object-Oriented Programming (OOP), is the use of classes
  • classes help you define the structure and behavior of objects, allowing you to encapsulate data and methods related to that data
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Best Practices for using Classes

A
  • Keep Classes Focused- each class should have a single responsibility or purpose
  • e.g. if you’re building a game, you might have a Player class to represent players and a GameBoard class to manage the game’s environment
  • Naming Conventions- use clear, descriptive names for your classes
  • UpperCamelCase is commonly used for class names, like UserProfile or TransactionHandler
  • Use Inheritance Carefully- while inheritance can be powerful, overusing it can lead to complex code that’s hard to maintain
  • only use inheritance when a clear “is-a” relationship exists e.g. a Dog class might inherit from an Animal class, because a dog is an animal
  • e.g. if you’re building an e-commerce application, you might have a Product class with properties like name, price, and stock quantity; then, you could create a subclass like DigitalProduct that inherits from Product but has additional properties, such as file size or download link
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Utilities & Helper Functions

A
  • while classes are essential for organising object-oriented code, you’ll often need smaller, independent blocks of code that don’t belong to a specific class- this is where utility functions (or helper functions) come in
  • Keep Them General- utility functions should perform tasks that are general enough to be reused across different parts of your project
  • e.g. a function to format dates, validate input, or convert data types might be used in multiple places
  • Organise by Functionality- group related utility functions in the same file or module e.g. you might have a string_utils.py file for all functions that deal with string manipulation, and a file_utils.py file for handling file operations
  • e.g. if your project frequently needs to convert data formats, you could create a utility function like convert_to_json(data) that takes any input data and returns it in JSON format; can then call this function from anywhere in your project
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Folder Structure & File Organisation

A
  • as your projects grow in size, organising your files and folders becomes critical for maintaining clarity and efficiency
  • a well-structured project makes it easier to find code, understand the program’s layout, and collaborate with others
  • Use a Clear Directory Structure- organise your files into meaningful directories
  • e.g. you might have separate folders for models, views, utilities, tests, and assets
  • Separate Logic by Responsibility- keep your business logic, user interface code, and utility functions in separate places
  • this keeps your project modular and easier to maintain
  • e.g. all the database-related logic could be in a models folder, while the presentation code could go into a views folder
  • Use Naming Conventions Consistently- name files and folders in a way that makes it obvious what they contain
  • common conventions include using snake_case for file names and UpperCamelCase for class names
  • e.g. a file for user authentication might be called auth_utils.py
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Documentation is important

A
  • even with a well-organised project, it’s essential to include documentation so that other developers (and your future self) can understand the purpose of your code
  • well-documented code helps ensure that anyone who works on the project can easily grasp what each part of the codebase does
  • Comment Your Code- leave helpful comments to explain why certain decisions were made; use comments sparingly but meaningfully
  • avoid redundant comments that simply describe what the code does, as the code itself should be clear enough for that
  • Write a README- every project should have a README.md file that explains what the project is, how to set it up, and how to use it
  • this is especially important for collaborative projects or open-source work
  • Use Docstrings- for functions and classes, use docstrings (multi-line comments) to describe what the function or class does, what parameters it takes, and what it returns
  • e.g. in Python, docstrings are enclosed in triple quotes
How well did you know this?
1
Not at all
2
3
4
5
Perfectly