Yes this is generally a good idea. Intellectual property (IP) and copyright around code are only protected if you have a contract in place. Otherwise you are negotiating based on supposed IP contributions.
This is not for just code, but any IP generating activity. It will save you lots of headaches later to establish clearly what is being developed and what is not. This will also help the process as well, since it will establish some basic specifications that you can work with. In general I find that when it's just talking, nothing gets serious, but once something hits paper you have a commitment together and the work is enabled by the established terms.
I highly recommend you going to read Founder Dilemmas by Noah Wasserman if you want to see all the ways not having some basic contracts goes wrong.