He claims that the 0 should be a constant, and actually docked me marks because of that.
I'd agree. The use of zero here is "magic". Imagine that you are reading this code for the first time. You do not know why zero is special, and the literal tells you nothing about why zero is special. If instead you said if(comboVendor.SelectedIndex == CreateNewVendorIndex)
then it becomes extremely clear to the first-time reader what the code means.
He claims I shouldn't use literals in my code at all.
That's an extreme position; a realistic position would be to say that use of literals is a red flag that indicates that the code might not be as clear as it could be. Sometimes it is appropriate.
The thing is, I don't understand why I would want to make that code in that situation a constant. That index will never change
That it will never change is an excellent reason to make it a constant. That's why constants are called constants; because they never change.
nor is it something that you would need to tweak.
Really? You can't see any situation in which someone might want to change the order of things in a combo box?
The fact that you can see a reason why this might change in the future is a good reason to not make it a constant. Rather it should be a non-constant readonly static integer field. A constant should be a quantity that is guaranteed to stay the same for all time. Pi and the atomic number of gold are good constants. Version numbers are not; they change every version. The price of gold is obviously a terrible constant; it changes every second. Only make constant things that never, ever change.
It seems like a waste of memory to keep a single 0 in memory that's used for a very specific situation and never changes.
Now we come to the crux of the matter.
This is perhaps the most important line in your question because it indicates that you have some deeply flawed understanding of (1) memory, and (2) optimization. You're in school to learn, and now would be a great time to get a correct understanding of the fundamentals. Can you explain in detail why you believe that "it is a waste of memory to keep a single zero in memory"? First off, why do you believe that optimizing the usage of four bytes of memory in a process with at least two billion bytes of user-addressible storage is relevant? Second, precisely what resource do you imagine is being consumed here? What do you mean by "memory" being consumed?
I am interested in the answers to these questions first because they're an opportunity for you to learn how your understanding of optimization and memory management is incorrect, and second because I always want to know why beginners believe bizarre things, so that I can design better tools to lead them to have correct beliefs.
-1
instr.indexOf(substr) != -1
for "str
containssubstr
" is prefectly justified. But here, the meaning of the 0 is neither obvious (what't the relation to creating a new vendor?) nor truly constant (what if the way to create a new vendor changes?).int.Zero
instead to make him happy :)