  Communities are fundamental entities for the characterization of the
structure of real networks. The standard approach to the identification of
communities in networks is based on the optimization of a quality function
known as "modularity". Although modularity has been at the center of an intense
research activity and many methods for its maximization have been proposed, not
much it is yet known about the necessary conditions that communities need to
satisfy in order to be detectable with modularity maximization methods. Here,
we develop a simple theory to establish these conditions, and we successfully
apply it to various classes of network models. Our main result is that
heterogeneity in the degree distribution helps modularity to correctly recover
the community structure of a network and that, in the realistic case of
scale-free networks with degree exponent $\gamma < 2.5$, modularity is always
able to detect the presence of communities.
