The properties of interstellar grains, such as grain size distribution and grain porosity, are affected by interstellar processing, in particular, coagulation and shattering, which take place in the dense and diffuse interstellar medium (ISM), respectively. In this paper, we formulate and calculate the evolution of grain size distribution and grain porosity through shattering and coagulation. For coagulation, we treat the grain evolution depending on the collision energy. Shattering is treated as a mechanism of forming small compact fragments. The balance between these processes are determined by the dense-gas mass fraction $eta_mathrm{dense}$, which determines the time fraction of coagulation relative to shattering. We find that the interplay between shattering supplying small grains and coagulation forming porous grains from shattered grains is fundamentally important in creating and maintaining porosity. The porosity rises to 0.7--0.9 (or the filling factor 0.3--0.1) around grain radii $asim 0.1~mu$m. We also find that, in the case of $eta_mathrm{dense}=0.1$ (very efficient shattering with weak coagulation) porosity significantly enhances coagulation, creating fluffy submicron grains with filling factors lower than 0.1. The porosity enhances the extinction by 10--20 per cent at all wavelengths for amorphous carbon and at ultraviolet wavelengths for silicate. The extinction curve shape of silicate becomes steeper if we take porosity into account. We conclude that the interplay between shattering and coagulation is essential in creating porous grains in the interstellar medium and that the resulting porosity can impact the grain size distributions and extinction curves.