Memory Management – Entry Level Engineer Questions

code-qualitygarbage-collectionprogramming practices

It has been a few months since I started my position as an entry level software developer. Now that I am past some learning curves (e.g. the language, jargon, syntax of VB and C#) I'm starting to focus on more esoteric topics, as to write better software.

A simple question I presented to a fellow coworker was responded with "I'm focusing on the wrong things." While I respect this coworker I do disagree that this is a "wrong thing" to focus upon.

Here was the code (in VB) and followed by the question.

Note: The Function GenerateAlert() returns an integer.

Dim alertID as Integer = GenerateAlert()
_errorDictionary.Add(argErrorID, NewErrorInfo(Now(), alertID))    

vs…

 _errorDictionary.Add(argErrorID, New ErrorInfo(Now(), GenerateAlert()))

I originally wrote the latter and rewrote it with the "Dim alertID" so that someone else might find it easier to read. But here was my concern and question:

Should one write this with the Dim AlertID, it would in fact take up more memory; finite but more, and should this method be called many times could it lead to an issue? How will .NET handle this object AlertID. Outside of .NET should one manually dispose of the object after use (near the end of the sub).

I want to ensure I become a knowledgeable programmer that does not just rely upon garbage collection. Am I over thinking this? Am I focusing on the wrong things?

Best Answer

"Premature optimization is the root of all evil (or at least most of it) in programming." - Donald Knuth

When it comes to your first pass, just write your code so that it's correct and clean. If it is later determined that your code is performance-critical (there are tools to determine this called profilers), it can be re-written. If your code is not determined to be performance-critical, readability is far more important.

Is it worth digging into these topics of performance and optimization? Absolutely, but not on your company's dollar if it's unnecessary.

Related Topic