Knowledgebase

Functions and Code Organisation Print

  • phplaravel, troubleshooting, guide, howto, solution, zillionkinghost, hosting, support
  • 0

Structuring logic.

WHAT A FUNCTION IS

A named block of code, taking inputs and returning a result.

WHAT MAKES A GOOD ONE

It does one thing Its name says what It is short It does not depend on hidden state

WHAT TO DECLARE

The types it accepts and returns.

WHY

It documents intent and catches mistakes early.

WHAT TO AVOID

Functions with many parameters Functions modifying things outside themselves Global variables

WHY GLOBALS CAUSE PROBLEMS

Anything can change them, so nothing can be reasoned about.

WHAT TO USE INSTEAD

Parameters and return values Objects holding their own state

WHAT CLASSES PROVIDE

Grouping data with the operations on it.

WHEN TO USE ONE

When data and behaviour belong together When you need several instances of something

WHAT NAMESPACES DO

Prevent name collisions between your code and libraries.

WHAT TO ADOPT

A conventional structure where file paths match class names, so loading is automatic.


Was this answer helpful?
Back

Are you happy with your experience? Leave us a review on Trustpilot.


Trustpilot