Create simpler triggers.

Keep your logic outside of the trigger.

Triggers are blocks of code that are run when a particular event happens, such as the insertion of a database record. In some applications, an event might have several triggers. It’s possible that various developers added them to perform different jobs when the event occurred.

Each trigger may perform some complex data manipulation or calculation. Unfortunately, the order that the triggers execute may be uncertain or random, leading to unexpected behavior. To avoid this problem, there should be only one trigger for any particular event.

Combining actions into one trigger makes for a messy trigger, so you should refactor that logic out. This change reduces the trigger to one job: keeping the order of actions to be performed.

For example, if creating a shipment has two triggers, one of which reserves space on a truck and one that updates adds promotional items to the box, then you should combine those two jobs into one trigger. Move the logic for each of the two jobs to separate methods. The trigger will merely call the two jobs. The order of the jobs is essential because adding promotional items to the box may affect how much weight should be reserved on the truck.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: