MV When it works, can it still be wrong?

So far there is one person who had actual insight into the plugin I create for my football manager, and even though everything works, he told me I did a couple things wrong.

Here is an example of what I did:
JavaScript:
var array = [,1,2,3,'A','B','C']
for (let pId = 1 ; pId < array.length; pId++) {
    var x = array[pId];
    var y = $gameVariables.value(pId);
    var z = $dataEnemies[pId].dropItems[0].dataId;
    var stuff = 'Insert formula using X, Y, Z and many other things depending on pId';
    $gameVariables.setValue(pId, stuff)
};

Okay, I made this piece of code up on the spot, but it is anything but random, this is a simplified version of the much larger code that was commented on. I just picked the stuff that explain my question.

So, what I do here, is I loop a set of code for each en entry in the array. For those who pay attention, you might see my array starts with , and my loop starts at 1, not at zero.

Inside this loop in the example, I gave a few reasons why I start my loop with 1, instead of 0. There is no variable 0, no actor 0, no enemy 0, no event 0...

Now, the person who took a look at my code, he's my nephew btw, he told me that it is wrong to start the loop with 1. He thinks the code should look like this:

JavaScript:
var array = [1,2,3,'A','B','C']
for (let pId = 0 ; pId < array.length; pId++) {
    var x = array[pId];
    var y = $gameVariables.value(pId+1);
    var z = $dataEnemies[pId+1].dropItems[0].dataId;
    var stuff = 'Insert formula using X, Y, Z and many other things depending on pId';

    $gameVariables.setValue(pId+1, stuff)
};

Note that array.length is still the right moment to end the loop, since the array now starts with 1 instead of ,

I asked him why, and he told me because that is what he was taught in school. But it works when I do it my way. In my mind "because that is what I was taught" isn't a reason not to do it. I remember math class all to well. My teacher (and in my mind 99% of the math teachers) overcomplicated all the math problems from day 1. So many times I had to take over from the teacher, to show that this complicated stuff was actually real easy. And the teacher had no idea, their mind was blown every time, because they just follow the book. True story. And I assume my nephews teachers are like that too.

With a little piece of code like this, I would be happy to do it his way, but I use it in much, much larger loops, where pretty much every single command would include pId. In my card game for example, slot 1 is linked to actor 1, and event 1, and variable 1, and switch 1. The same goes for slot 2, 3, 4 and all the way up to slot 40. So you might see why it is much easier for me, if I could just start my loop with 1.

Another issue I have with this, is that I don't want my arrays to start with index 0. I use this for things like coordinates for example. So index 1, should contain the coordinates of slot 1, not that of slot 2. If not, I get the same issue the other way around, where I need to use array[pId - 1] while my way I can just use array[pId] for example. I have much more arrays like this, which all depend in pId.

So... My question is... If I use a code I changed to meet my needs, and it works exactly as I wanted it, than is it better to still do it by the book? And if so, can anyone tell me why it is important to start my loop with 0?

(Edit: I know, I know... I start with saying my nephew seeing my football manager plugin, and then start talking about my card game. Well... Those are more alike than you may think. Those loops and Id's are used in both, but pId actually comes from the football game, and stands for Player Id.)
Context of the environment you are developing for is very important. Since the developers of the engine decided on a 1-index system, there is nothing wrong with sticking to that for consistency.

At the end of the day if it works and doesn't have any performance issues, you can code however you want!
if reading an array, it should start with 0 as array index = 0 and not 1.

as most index or number is 0, you start there.
even in a struct its 0 but with plugin command, struct is numbered 1.

so you do "i + 1" to always use 1 to start with.

also an array start with [,1,2,3] the error will be throw out of the comma or readed as "null"
so [null, 1, 2,3,"a","b"] works than with i = 1.

you can also do array.length - 1.
Looks OK to me too.

That said, particularly if you plan to release the code for others to use, I'd suggest explicitly putting a value in for the zeroth entry rather than leaving it undefined, e.g.
JavaScript:
var array = [null, 1, 2, 3, "A", "B", "C"];
This makes it clear that entry 0 is not intended to be used, and that the 1st comma is not a typo.
Well, if I would start doing that at my job, my colleagues would punch me as soon as they notice what I'm doing 😅

But when you do it in your own project, then it's all good. I think, every programmer has some individual handwriting and slightly deviates from the common coding conventions.
Context of the environment you are developing for is very important. Since the developers of the engine decided on a 1-index system, there is nothing wrong with sticking to that for consistency.

At the end of the day if it works and doesn't have any performance issues, you can code however you want!
Yeah, that is kind of what I was thinking too. 😛

if reading an array, it should start with 0 as array index = 0 and not 1.

as most index or number is 0, you start there.
even in a struct its 0 but with plugin command, struct is numbered 1.

so you do "i + 1" to always use 1 to start with.

also an array start with [,1,2,3] the error will be throw out of the comma or readed as "null"
so [null, 1, 2,3,"a","b"] works than with i = 1.

And array starting with [,1,2,3] does not cause any errors for me, since I never ask for id 0. Ever. I often fill index 0 with 0 or null or ' ', depending or what it is for, just to avoid crashes when testing, but during gameplay index 0 isn't ever called upon. Simply because there is no slot 0, no actor 0, no variable 0, etc.

But even with null or 0, the game still crashes when I try to ask for $gameVariables.value(array[pId]), and pId = 0.

you can also do array.length - 1.
Oh? I never actually used that, but I thought it would just be 1 less than the length of an array. 😛

Looks OK to me too.

That said, particularly if you plan to release the code for others to use, I'd suggest explicitly putting a value in for the zeroth entry rather than leaving it undefined, e.g.
JavaScript:
var array = [null, 1, 2, 3, "A", "B", "C"];
This makes it clear that entry 0 is not intended to be used, and that the 1st comma is not a typo.
Thanks. That is actually a good reason to use the null. But those codes are written so specifically for my own needs, I don't think anyone else could use it, unless they create the exact same games I create. I'll remember that for any future plugins for public use, though.

Well, if I would start doing that at my job, my colleagues would punch me as soon as they notice what I'm doing 😅
But why? If it is more efficient for the goal and it works flawlessly, then why would your colleagues still want to hurt you for doing this?

But when you do it in your own project, then it's all good. I think, every programmer has some individual handwriting and slightly deviates from the common coding conventions.
I think my handwriting is like a big scratch book. 😛 I never learned how to code properly. I just learned a script call, and then another script call, and that triggered me to experiment with my own script calls, which became larger and more complex, until I reached the point where I was like: "Wait, if I need to do 'var x is this and var y is that' over and over again, with the same values, can't I make var X and var Y permanent?" And before I knew it, I was converting my clock and calendar workshop into a plugin.

So pretty much nothing I do is by the book, I guess. 😛
But why? If it is more efficient for the goal and it works flawlessly, then why would your colleagues still want to hurt you for doing this?
Because it takes a while to understand what I was thinking, and when someones needs to change or extend my code because I'm not in office, it's more risky to do mistakes.
also try to use "let" or "const" instead of "var" so most variables in a block
that could change, use "let", use "const" for values that needs to remain unchanged.

but var x, y, z is not really proper names, try to target with names what they do 🙂
also try to use "let" or "const" instead of "var" so most variables in a block
that could change, use "let", use "const" for values that needs to remain unchanged.

but var x, y, z is not really proper names, try to target with names what they do 🙂
Those vars are just an example to show why I start the loop with 1. I don't actually use var x, y and z like that.

Original thread: https://forums.rpgmakerweb.com/threads/when-it-works-can-it-still-be-wrong.171837/