|So, I've done mainly VBc and C/C++ for embedded device, and a tiny bit of VC++. When .Net came along, after a while, it was often repeated that .Net allowed people to write bad code, but will still work. I was a stickler for following good programming form.
On one occasion, someone had ask a question about what the .Net compiler will do with [whatever code it was], and someone else chimed in saying something else like 'the compiler is smart and will optimize...'.
But, you know, I'm still a stickler for proper form, allocating vars and objects, properly destroying them, stopping any timers before program exit, etc. I guess, proper "cleanup" you could say.
So I'm working with some code the company hired a supposedly experienced programmer to write a program for the company that monitors devices it makes. Some of this is so appalling, that it looks like an absolute beginner would write. So the below chunk of code is one function he had written. He has this running in it's own thread, and it's a UPD listening socket. This is used to prevent multiple instances of it running, and to return from the system tray after it's been minimized to the tray. So this function is always running in it's own thread...
Private Sub udpThreadsub()
udpcl = New UdpClient(UDPPORT)
Dim RemoteIpEndPoint As New IPEndPoint(IPAddress.Any, 0)
Dim rec = udpcl.Receive(RemoteIpEndPoint)
If rec.Length = 11 Then
Dim match = True
For i = 0 To 10
If rec(i) <> UDP_MSG(i) Then
match = False
If match Then
Catch e As Exception
Catch e As Exception
To me, this is some of the ugliest code I've ever seen. I mean, the compiler isn't THAT smart is it?
To me, this looks like the dim'd object will be created and sent to GC every iteration through the loop! But then again, I don't have an intimate knowledge of what the compiler will do with this. Will it just realize that it should just reusing these objects and not instantiate them and send GC as it appears they would?
I would have done the DIM's above the loops, and then reuse them in the code.
So is this some of the worse code ever seen (by me anyway) or is the compiler just 'that smart'?
Thanks in advance.