There are a few "laws" or "rules" that govern how spells work in Harry Potter, but there are a few things things that have connections with stuff that determines how effective a spell is.
Emotion is one of them for certain spells.
For instance Nymphadora Tonks losing her powers.
A witch or wizard's emotional state can affect their inherent
abilities. For example, Nymphadora Tonks temporarily lost her power as
a Metamorphmagus after suffering severe emotional turmoil and sadness
over her grief for the death of Sirius Black, and when Remus Lupin
would not return her affections. In effect, the form of her Patronus
changed to reflect her love for him.
Another being Dumbledore's sister's magic being volatile from being emotionally scarred.
As related to Harry by Dumbledore during their private lessons, Merope
Gaunt only demonstrated any magical ability when removed from her
father's oppression, but then seemed to lose it again when her husband
abandoned her. Many other examples of emotion-influenced magic include
Ariana Dumbledore (Dumbledore's sister) being emotionally scarred at a
young age and then her magic turned volatile and uncontrolled.
And the best example is emotion based magic :
Several magical spells involve the use of emotion when casting them.
The Patronus Charm, for example, requires the caster to concentrate on
a happy memory. Force of will under extenuating circumstances also
helps in casting spells, and affects the force with which they are
cast. An example of this is when Harry is able to conjure a corporeal
Patronus when Sirius Black is in danger of being administered the
Another example of magic and emotions is when Ron Weasley caused it to
snow above him with his wand when he felt guilt over breaking up with
Lavender Brown without using any worded spells.
Magic is sometimes an exact science because there are some rules for it in general.
Such rules include :
- Rule of Conjuration : While it is possible to conjure things out of thin air, it is far more tricky to create something that fits an
exact specification rather than a general one; moreover, any objects
so conjured tend not to last.
- Rule Against Ressurection : It is also impossible to resurrect the dead. While corpses can be transformed into obedient Inferi on a
living wizard's command, they are little more than zombies with no
soul or will of their own. It is also possible via the rare Priori
Incantatem effect to converse with ghost-like "shadows" of magically
murdered people. The Resurrection Stone also allows one to talk to
the dead, but those brought back by the Stone are not corporeal, nor
do they wish to be disturbed from their peaceful rest. The result of
such a summoning is usually detrimental to the summoner.
- Rule Against Immortality : Likewise, it is impossible to make oneself immortal unless one makes use of a mystical object of great
power to sustain life (such as the Philosopher's Stone created by
Nicolas Flamel or a Horcrux, the latter having been used by Lord
Voldemort and Herpo the Foul). Becoming a ghost is also an option for
wizards and witches; however, it is said that this is "a pale
imitation of life". Whether or not ghosts are truly sentient beings
possessed of an independent existence is unclear; Snape states that a
ghost is merely "the imprint of a departed soul left upon the earth".
- Laws of Elemental Transfiguration : Food is one of these: witches or wizards can cook and prepare food using magic, but not create it
from nothing. Out of the five exceptions, only food is mentioned
explicitly, although speculation has proposed many other
possibilities. There is a strong possibility that money is another
exception, since if wizards could simply materialise money out of
thin air the economic system of the wizarding world would be
As mentioned in another answer there is the "Fundemental Laws of Magic" as well.
The most notable was the first law, as not many others were shown in the series.
As stated in the wikia :
The Fundamental Laws of Magic are, presumably, a number of statements
about the general nature of magic. They were formulated by the magical
theoretician Adalbert Waffling, the first of which is quoted above.
It's unknown how many Laws Waffling created.
The exact consequences mentioned in the first law are not known, only
vaguely stated to be "of the most extreme and dangerous kind".
However, during Albus Dumbledore's discussion of the first law in his
notes on the well known fairytale "The Warlock's Hairy Heart" (in The
Tales of Beedle the Bard), he mentions that "in seeking to become
super-human this foolhardy young man renders himself inhuman".
The wikia states that the first law somewhat mimics Newton's third law because violators of the first law always end up with some sort of karmic payback.
Likewise, Dumbledore also notes that the creation of a Horcrux evokes
the first law, tampering with the essence of self or rather "dividing
what was clearly not meant to be divided...body and soul". Voldemort
created the number of Horcruxes he did solely in order to fulfil his
perennial goal of evading death. Therefore, when he was finally killed
the result is almost as karmic as the aforementioned warlock's: in
seeking to fix himself immutably to one plane forever he renders
himself eternally removed from all of them, save "the between-space".
Therefore, it may be that the consequences of the first Fundamental
Law of Magic (when evoked) mimic Newton's Third Law of Motion: For
every action there is an equal and opposite reaction.