There is really no best time to visit Barcelona, nor a worst. Thanks to its location on the Mediterranean, it enjoys a mild, pleasant climate year-round. December-February is the chilliest period, although the sun shines most of the time. Even in winter, locals sunbathe on the beaches (in "suntraps" by the seawall) and eat and drink on terraces. The hardier ones even occasionally take a dip; you may not want to follow their example, however, as the water is surprisingly cold.
In January, the average temperature rarely falls below 50 F/10 C and is often considerably higher. Really cold spells are typically few and far between, although recent years have seen an increasing number of cold spells and even some snow.
Summers aren't necessarily hot—the average high in July and August is 78 F/25 C—but the high humidity can make them somewhat uncomfortable. School vacation runs late June to mid-September, and those who are able to do so spend the entire summer away from the city. Most shops and businesses take their annual vacations in August, leaving the city relatively empty.
Rain is infrequent, but can be heavy when it occurs, and thunderstorms can be violent.