Let $M$ be a countable transitive model of enough ZFC, let $P \in M$, and let $\dot Q \in M$ be a $P$-name for a forcing poset. If $K \subseteq P*\dot Q$ is $(P*\dot Q)$-generic over $M$, define
\begin{align*}
G = \{p \in P : \exists \dot q\, ((p,\dot q) \in K)\}.
\end{align*}
Define also
\begin{align*}
H = \{\dot q_G : \exists p\, ((p,\dot q) \in K)\}.
\end{align*}
Then $G$ is $P$-generic over $M$, $H$ is $\dot Q_G$-generic over $M[G]$, and
\begin{align*}
M[K] = M[G][H].
\end{align*}
Conversely, if $G$ is $P$-generic over $M$ and $H$ is $\dot Q_G$-generic over $M[G]$, then
\begin{align*}
G*H = \{(p,\dot q) \in P*\dot Q : p \in G \text{ and } \dot q_G \in H\}
\end{align*}
is $(P*\dot Q)$-generic over $M$, and $M[G*H]=M[G][H]$.