4 ms·
Sometimes the code isn't clear because the reason for the "if" has nothing to do with the code and everything to do with externalities. For example, I wrote a
by brian_cunnie 4y ago
Sometimes the code isn't clear because the reason for the "if" has nothing to do with the code and everything to do with externalities.
For example, I wrote a DNS server that had to take into account that on many Linux systemd distributions a daemon `systemd-resolve` binds to 127.0.0.54:53, preventing my DNS server from binding to INADDR_ANY port 53.
So my DNS server has a contorted piece of code that, when binding to INADDR_ANY fails, iterates through all the interfaces and all the IP addresses and binds to each one individually.
Anybody looking at the code would rightfully ask, "What the heck?", but if they read the commit messages, the reason becomes clear.
- thom 4y agoWhy not just have a comment? Or structure the code such that the initial failure calling your bind_to_in_addr_any function sets a flag called inaddr_any_already_bound or something and checking that variable calls another function called bind_to_available_interfaces? There is nothing that can be explained in a commit message (which is just text) that can’t be explained in code (which is just text).
- Dunedan 4y agoProper naming of variables/functions is important, but would only tell a part of the story. In the example you'd loose the context why that port might be already in use. Using comments instead of commit messages is of course always possible, but if you'd note all relevant information you'd otherwise put into comment messages in comments, you might end up with more comments than actual code, which IMO hinders readability significantly. Take the Linux kernel for example. git commit messages there are very informative and often pretty long. Putting this information into comments would make the code pretty hard to read. Also sometimes there isn't a clear place where you'd put such a comment at, as it only makes sense when looking at multiple changes in different locations (= a git commit) at once. I believe having the explanation why certain aspects of the code are as they are in the metadata in form of the git commit messages with an occasional comment in the code for important information is a good compromise between readability/verbosity of the code and having all relevant context available.