As to the purpose of a Code of Canon Law - try reading the introduction, as with the intro of any document it usually gives an explanation as to its purpose.
I’ve given some excerpts from the Intro to the current Code of Canon Law. The bits I’ve highlighted should, I think, be sufficient to answer your questions. Read them in good health.
"APOSTOLIC CONSTITUTION SACRAE DISCIPLINAE LEGES
…
John Paul, bishop, servant of the servants of God as a perpetual record.
***During the course of the centuries the Catholic Church has been accustomed to reform and renew the laws of canonical discipline so that in constant fidelity to its divine founder, they may be better adapted to the saving mission entrusted to it. ***Prompted by this same purpose and fulfilling at last the expectations of the whole Catholic world, I order today, January 25, 1983, the promulgation of the revised Code of Canon Law. In so doing, my thoughts go back to the same day of the year 1959 when my predecessor of happy memory, John XXIII, announced for the first time his decision to reform the existing corpus of canonical legislation which had been promulgated on the feast of Pentecost in the year 1917.
…
***A second question arises concerning the very nature of the Code of Canon Law.
To reply adequately to this question one must mentally recall the distant patrimony of law contained in the books of the Old and New Testament from which is derived the whole juridical-legislative tradition of the Church, as from its first source.***
***Christ the Lord, indeed, did not in the least wish to destroy the very rich heritage of the law and the prophets which was gradually formed from the history and experience of the people of God in the Old Testament, but he brought it to completion ***(cf. Mt. 5:17) such that in a new and higher way it became part of the heritage of the New Testament. Therefore, although in expounding the paschal mystery St. Paul teaches that justification is not obtained by the works of the law but by means of faith (cf. Rom. 3:28; Gal. 2:16), he does not thereby exclude the binding force of the Decalogue (cf. Rom. 13:28; Gal. 5:13-25, 6:2), nor does he deny the importance of discipline in the Church of God (cf. I Cor. 5 and 6).
Thus the writings of the New Testament enable us to understand even better the importance of discipline and make us see better how it is more closely connected with the saving character of the evangelical message itself.
This being so, it appears sufficiently clear that the Code is in no way intended as a substitute for faith, grace, charisms, and especially charity in the life of the Church and of the faithful. On the contrary, its purpose is rather to create such an order in the ecclesial society that, while assigning the primacy to love, grace and charisms, it at the same time renders their organic development easier in the life of both the ecclesial society and the individual persons who belong to it.
As the Church’s principal legislative document founded on the juridicallegislative heritage of revelation and tradition, the Code is to be regarded as an indispensable instrument to ensure order both in individual and social life, and also in the Church’s own activity. Therefore, besides containing the fundamental elements of the hierarchical and organic structure of the Church as willed by her divine founder or as based upon apostolic, or in any case most ancient, tradition, and besides the fundamental principles which govern the exercise of the threefold of-fice entrusted to the Church itself, the Code must also lay down certain rules and norms of behavior.
…
As a matter of fact, the Code of Canon Law is extremely necessary for the Church. Since the Church is organized as a social and visible structure, it must also have norms: in order that its hierarchical and organic structure be visible; in order that the exercise of the functions divinely entrusted to it, especially that of sacred power and of the administration of the sacraments, may be adequately organized; in order that the mutual relations of the faithful may be regulated according to justice based upon charity, with the rights of individuals guaranteed and well-defined; in order, finally, that common initiatives undertaken to live a Christian life ever more perfectly may be sustained, strengthened and fostered by canonical norms.
…
Trusting therefore in the help of divine grace, sustained by the authority of the blessed apostles Peter and Paul, with certain knowledge, in response to the wishes of the bishops of the whole world who have collaborated with me in a collegial spirit, and with the supreme authority with which I am vested, by means of this Constitution, to be valid forever in the future, I promulgate the present Code as it has been set in order and revised. I command that for the future it is to have the force of law for the whole Latin Church, and I entrust it to the watchful care of all those concerned in order that it may be observed. So that all may more easily be informed and have a thorough knowledge of these norms before they have juridical binding force, I declare and order that they will have the force of law beginning from the first day of Advent of this year 1983, and this notwithstanding any contrary ordinances, constitutions, privileges (even worthy of special or individual mention), or customs.
I therefore exhort all the faithful to observe the proposed legislation with a sincere spirit and good will in the hope, that there may flower again in the Church a renewed discipline and that consequently the salvation of souls may be rendered ever more easy under the protection of the Blessed Virgin Mary, Mother of the Church.
Given at Rome, January 25, 1983, from the Vatican Palace, the fifth year of my pontificate.
John Paul II "