userdata for functions.
Posted: Fri Jun 05, 2015 9:20 pm
This is not a big deal really, just to keep code bit cleaner.
But just today there came a situation where I made this function, which is accessed from several different places of the code.
That function is pretty self reliant, that all you have to do is call that function, and it does what it is supposed to do without needing any additional info.
Except for one thing. It needed one variable. I didnt need to know the state of that variable except inside the function. So basically that variable wouldnt need to exist anywhere else but inside that function.
However, I had to set it Gloval variable so it doesnt disapear everytime that function disappears, as it keeps saving false/true state of something which the function uses.
In practice, my solution was to add one variable to the beginning of code. However, if someone else reads that code, I need to add comment to it to explain that it is solely used by that one function, and nowhere else.
What I would have preferred is to having some sort of "userdata" like buttons have for a function. That way I could have simply set that variable as that function userdata, and everytime function is called, I would simply call that functions userdata to see the state of that variable, and if necessary, change it into different one.
This way code had stayed bit cleaner as if someone would be reading that code, he would only need to read that function and know exactly what that function is doing, instead of having to go and check where that variable is being initialized the first time.
It is a very little thing, but just came to my mind and in case it is easy to implement, then why not.
But just today there came a situation where I made this function, which is accessed from several different places of the code.
That function is pretty self reliant, that all you have to do is call that function, and it does what it is supposed to do without needing any additional info.
Except for one thing. It needed one variable. I didnt need to know the state of that variable except inside the function. So basically that variable wouldnt need to exist anywhere else but inside that function.
However, I had to set it Gloval variable so it doesnt disapear everytime that function disappears, as it keeps saving false/true state of something which the function uses.
In practice, my solution was to add one variable to the beginning of code. However, if someone else reads that code, I need to add comment to it to explain that it is solely used by that one function, and nowhere else.
What I would have preferred is to having some sort of "userdata" like buttons have for a function. That way I could have simply set that variable as that function userdata, and everytime function is called, I would simply call that functions userdata to see the state of that variable, and if necessary, change it into different one.
This way code had stayed bit cleaner as if someone would be reading that code, he would only need to read that function and know exactly what that function is doing, instead of having to go and check where that variable is being initialized the first time.
It is a very little thing, but just came to my mind and in case it is easy to implement, then why not.