In the early days of computing, this technique was more common; early computers often did not have features (such as index registers), which made it necessary to use self-modifying code to perform certain tasks (for index registers, processing an array). Since they were programmed in assembly language, it was relatively easy to write such code.
Since such code is generally harder to understand and debug, it is generally not used now. Also, modern compilers do not emit such code; there is no reason to. Exceptions remain (e.g. when a program must be as small as possible, e.g. some bootstraps).