Things to Avoid
There are few practices that experience has shown will cause you endless grief in the form of programs that don't work and bugs that are very hard to find and sometimes not repeatable ("Heisenbugs").
- Since you are likely to keep various information in files (such as commits),
you might be tempted to use apparently convenient file-system operations
(such as listing a directory) to sequence through all of them. Be
careful. Methods such as
File.list
andFile.listFiles
produce file names in an undefined order. If you use them to implement thelog
command, in particular, you can get random results. - Windows users especially should beware that the file separator character is
/
on Unix (or MacOS) and\
on Windows. So if you form file names in your program by concatenating some directory names and a file name together with explicit/
s or\
s, you can be sure that it won't work on one system or the other. Java provides a system-dependent file separator characterFile.separator
, as in".gitlet" + File.separator + "something"
, or the multi-argument constructors toFile
, as in \new File(".gitlet", "something")
, which you can use in place of".gitlet/something"
). - Be careful using a
HashMap
when serializing! The order of things within theHashMap
is non-deterministic. The solution is to use aTreeMap
which will always have the same order. More details here. Specifically, iterating through aHashMap
can lead to non-deterministic behavior, which is avoided with a TreeMap.