0

yesterday I was disucsing with more experienced programmer. He saw that I am using

for (var i =0; i< 6; i++) 

So 6 is the static number. I was iterating throug array. He said to use lengh, just in case array lemngh is not 6. I say - its always 6. Its not planned to be not 6.

He says still - who knows maybe code will be changed.

Ok, there might be some reason. But then I say - for me it looks more readable.

When I see 6 - I know its 6 and nothing else. WHen I see .lenght - then it automatically comes to my mind - this is not constant. So then I should make a commend for the code reader that its constant to not make him wasted time figuring out why can it be sometimes other than 6.

And even if its some time not 6, then it means ther is a bug in some other place which generates this array, so that bug needs to be fixed.

WHat do you think - what is better - the way I do or the way he says?

Edited by McLaren

4
Contributors
9
Replies
33
Views
4 Years
Discussion Span
Last Post by McLaren
0

It doesn't matter - the best way to accomplish your task, is the way that makes you happy.

Today, you are satisfied with for (var i =0; i< 6; i++); tomorrow, you may not!
But at any time in your life, - if you change your mind, you can come/go back and change it to say
for (var i =0; i< 8; i++); and latter on, -again revert to how it was. Or perhaps even decide to make it more generic and throw arrays that may be shorter or lengthier than 6 by doing: for (var i =0; i < ar.length; i++) and still -it wont matter, because its a scripting language, and that's the beauty of it -your lengthy program doesn't have to be recompiled simply because you decided to replace some "," with a ";" somewhere in its flow.

-It's your call...

Stay Happy!

0

I don't mind the fact that you're using 6 instead of length, but since it's a magic number (that could be used elsewhere in your code), make it a constant with a clear name.

0

yeah, making it as constant looks decent also. The interesting thing is that - how can he tell that the way it looks like its a must to do like he said if you want to write the quality code. Don't get it :) and probably I will not be able to change his mind.

0

It's just a different mind set. He wants to write compatible code. You (and I) want it to break should something change, so you can rethink the solution. By using length, accepting a different length array can lead to unexpected hard-to-find bugs. Aside: in a lot of languages length is a function, so calling it inside a loop condition can be a performance hit (it's re-evaluated every time).

Edited by pritaeas

0

It's just a different mind set. He wants to write compatible code.

I think there are many places like this. In same PHP. WHen I know there has to be array retured of 6x items, I dont check if count(array) = 6. I assume its always 6. if not - its a bug somewhere.
So if going to check everything, then it add a lot of work and longer code.

0

It's a judgement call. I was once called out for not having the company's name in a variable. Who knew they would eventually change their name? However it was fairly easy to use a script to change all occurrences of "Old Company Name" to "New Company Name".

So, what does 6 represent?

0

I was iterating through array.

If there is an actual array involved, then I would use array.length

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.