How many times have you named a variable foo, bar, foobar, shit, shittyShit, or something similar? The answer is most likely an obscene amount. But you know what, at least these names are not wrong – yes they are confusing but not straight-up wrong.
The naming of the modules net and dgram from NodeJS’s standard library is however just straight-up wrong. What would you think this does?
const net = require('net')
const dgram = require('dgram')
const netSocket = net.connect({ host: '127.0.0.1', port: 1234 })
netSocket.on('connect', () => {
netSocket.write('hello using netSocket\n')
console.log('just sent a message using netSocket')
netSocket.destroy()
})
const dgramSocket = dgram.createSocket('udp4')
dgramSocket.connect(4321, '127.0.0.1', () => {
dgramSocket.send('hello using dgramSocket\n')
console.log('just sent a message using dgramSocket')
dgramSocket.close()
})
Both do something with sockets. The astute reader might even have noticed that the dgram library was used to create a socket with an argument of udp4, so dgram somehow relates to UDP and therefore it wouldn’t be a stretch that someone could deduce that net has something to do with TCP. TCP and UDP are after all the two best-known transport protocols1.
The thing is that the naming is just blatantly wrong. dgram is most likely short for datagram which is also found in what the acronym UDP stands for, i.e. User Datagram Protocol2. But wait what is a datagram? Well according to RFC 15943 the definition of a datagram is:
A self-contained, independent entity of data carrying sufficient information to be routed from the source to the destination computer without reliance on earlier exchanges between this source and destination computer and the transporting network.
So yeah sure UDP falls under that category, but so does a lot of other stuff, what about for instance ICMP? But does dgram support ICMP? No it doesn’t. Should it? Probably not, but it sure should be named something different. What about something obvious like udp?
This brings us to the naming of the net module and here we don’t have to go into a whole spiel about definitions etc. it is so obviously wrong that it isn’t needed. Or at the very least it’s confusing as hell.