  Stolarsky's invariance principle quantifies the deviation of a subset of a
metric space from the uniform distribution. Classically derived for spherical
sets, it has been recently studied in a number of other situations, revealing a
general structure behind various forms of the main identity. In this work we
consider the case of finite metric spaces, relating the quadratic discrepancy
of a subset to a certain function of the distribution of distances in it. Our
main results are related to a concrete form of the invariance principle for the
Hamming space. We derive several equivalent versions of the expression for the
discrepancy of a code, including expansions of the discrepancy and associated
kernels in the Krawtchouk basis. Codes that have the smallest possible
quadratic discrepancy among all subsets of the same cardinality can be
naturally viewed as energy minimizing subsets in the space. Using linear
programming, we find several bounds on the minimal discrepancy and give
examples of minimizing configurations. In particular, we show that all binary
perfect codes have the smallest possible discrepancy.
