Last time, we learned how functions can call other functions to work together. But how do we decide which parts of our code should become separate functions?
Engagement Message
Have you ever wondered when to create a new function versus keeping code together?
A good rule of thumb: if you find yourself writing the same code twice, it's time for a function! But functions are also useful for breaking big problems into smaller, manageable pieces.
Engagement Message
Think of it like organizing your closet - what gets its own section?
Each function should have one main job. If you struggle to explain what your function does in one simple sentence, it might be trying to do too much.
Engagement Message
Can you describe this function's job in one sentence?
Good function names describe exactly what they do. Use verbs for actions (calculate_tax
, send_email
) and be specific enough that someone else could guess the function's purpose.
Engagement Message
Which name tells you more about what happens inside?
When solving complex problems, start by identifying the main steps. Each major step often becomes its own function. This makes your code like a recipe with clear ingredients and instructions.
