I fix it!!!111
-
We have two functions
apiCall
andhandleChangeEvent
. The simplified version below.function apiCall() {
// smart stuff here
}function handleChangeEvent(newData,oldData) {
if(newData !== oldData)
{
apiCall();
}
}One of my colleagues reports they have a problem with
apiCall
not being called fromhandleChangeEvent
because of theif
condition. They claim that the event is sometimes handled and sometimes not. Suspicious claim, as theapiCall
code can be triggered from other locations, but it's an issue they should be able to handle so I didn't investigate further. Later the cheery colleague says they solved the issue and I decided to review their code. I take a look at their work from time to time to help improve their code and this is the solution they came up with:function handleChangeEvent(newData,oldData) {
newData = !oldData;
if(newData !== oldData)
{
apiCall();
}
}:wtf: :(( I told them that this is not the way to solve the issue, and took the time to show them that that
apiCall
was in fact called from another location. I also gave them a few solutions that could be made and left to take a break. I hope to help guide them, but I fear that there is no helping them :sigh: . I'm still puzzled and angry that someone would write such a solution!? -
We have two functions
apiCall
andhandleChangeEvent
. The simplified version below.function apiCall() {
// smart stuff here
}function handleChangeEvent(newData,oldData) {
if(newData !== oldData)
{
apiCall();
}
}One of my colleagues reports they have a problem with
apiCall
not being called fromhandleChangeEvent
because of theif
condition. They claim that the event is sometimes handled and sometimes not. Suspicious claim, as theapiCall
code can be triggered from other locations, but it's an issue they should be able to handle so I didn't investigate further. Later the cheery colleague says they solved the issue and I decided to review their code. I take a look at their work from time to time to help improve their code and this is the solution they came up with:function handleChangeEvent(newData,oldData) {
newData = !oldData;
if(newData !== oldData)
{
apiCall();
}
}:wtf: :(( I told them that this is not the way to solve the issue, and took the time to show them that that
apiCall
was in fact called from another location. I also gave them a few solutions that could be made and left to take a break. I hope to help guide them, but I fear that there is no helping them :sigh: . I'm still puzzled and angry that someone would write such a solution!?what about handling null values for new and old data parameters? this has bitten me in the ass before.
-
what about handling null values for new and old data parameters? this has bitten me in the ass before.
-
what about handling null values for new and old data parameters? this has bitten me in the ass before.
The code is sufficient in this case not to check for
null
:). The parameters passed are from an event, and the function is called when the field it is bound to changes. Additionally theapiCall
function doesn't change anything, but the overhead of making a request to the api should be avoided, hence it's wrapped in anif
statement. -
We have two functions
apiCall
andhandleChangeEvent
. The simplified version below.function apiCall() {
// smart stuff here
}function handleChangeEvent(newData,oldData) {
if(newData !== oldData)
{
apiCall();
}
}One of my colleagues reports they have a problem with
apiCall
not being called fromhandleChangeEvent
because of theif
condition. They claim that the event is sometimes handled and sometimes not. Suspicious claim, as theapiCall
code can be triggered from other locations, but it's an issue they should be able to handle so I didn't investigate further. Later the cheery colleague says they solved the issue and I decided to review their code. I take a look at their work from time to time to help improve their code and this is the solution they came up with:function handleChangeEvent(newData,oldData) {
newData = !oldData;
if(newData !== oldData)
{
apiCall();
}
}:wtf: :(( I told them that this is not the way to solve the issue, and took the time to show them that that
apiCall
was in fact called from another location. I also gave them a few solutions that could be made and left to take a break. I hope to help guide them, but I fear that there is no helping them :sigh: . I'm still puzzled and angry that someone would write such a solution!?It looks fine syntax wise however you should add what the solution is meant to do. If this is dealing with a database or with some form of data than parameters should be put there. Someone did say Null values and that can come with some downsides. With handling data some limitations should be added. However as I said I have no idea what data the function is trying to deal with. All I know is that it handles two objects. One newData and Two oldData. I know that if newData and oldData are the same than it will be false and in that instance the API would be called. Could you go in depth a little bit further or what type of data you are dealing with.
-
We have two functions
apiCall
andhandleChangeEvent
. The simplified version below.function apiCall() {
// smart stuff here
}function handleChangeEvent(newData,oldData) {
if(newData !== oldData)
{
apiCall();
}
}One of my colleagues reports they have a problem with
apiCall
not being called fromhandleChangeEvent
because of theif
condition. They claim that the event is sometimes handled and sometimes not. Suspicious claim, as theapiCall
code can be triggered from other locations, but it's an issue they should be able to handle so I didn't investigate further. Later the cheery colleague says they solved the issue and I decided to review their code. I take a look at their work from time to time to help improve their code and this is the solution they came up with:function handleChangeEvent(newData,oldData) {
newData = !oldData;
if(newData !== oldData)
{
apiCall();
}
}:wtf: :(( I told them that this is not the way to solve the issue, and took the time to show them that that
apiCall
was in fact called from another location. I also gave them a few solutions that could be made and left to take a break. I hope to help guide them, but I fear that there is no helping them :sigh: . I'm still puzzled and angry that someone would write such a solution!? -
As a matter of curiosity what language is that in? I've never seen that construct !== before
Probably javascript. 1 == "1" will be true in JavaScript because == casts objects to a common type. However 1 === "1" is false since one is int and the other is string. === does not cast objects. !== will be not equal to without casting of objects. If you're sure both sides of the operand are of the same type then using === or !== will be faster.
There are only 10 types of people in the world, those who understand binary and those who don't.
-
Probably javascript. 1 == "1" will be true in JavaScript because == casts objects to a common type. However 1 === "1" is false since one is int and the other is string. === does not cast objects. !== will be not equal to without casting of objects. If you're sure both sides of the operand are of the same type then using === or !== will be faster.
There are only 10 types of people in the world, those who understand binary and those who don't.
-
It looks fine syntax wise however you should add what the solution is meant to do. If this is dealing with a database or with some form of data than parameters should be put there. Someone did say Null values and that can come with some downsides. With handling data some limitations should be added. However as I said I have no idea what data the function is trying to deal with. All I know is that it handles two objects. One newData and Two oldData. I know that if newData and oldData are the same than it will be false and in that instance the API would be called. Could you go in depth a little bit further or what type of data you are dealing with.
The actual data passed is a timestamp when a change has happened. The
handleChangeEvent
function is an event handler bound to a field, like an input fieldonchange
event in the browser. The framework takes care of when to call this function, and then the control takes responsibility of what needs to happen. The point of my semi-rant being that the dev in question tried to fix a problem by doing the wrong thing in the wrong place :) -
Probably javascript. 1 == "1" will be true in JavaScript because == casts objects to a common type. However 1 === "1" is false since one is int and the other is string. === does not cast objects. !== will be not equal to without casting of objects. If you're sure both sides of the operand are of the same type then using === or !== will be faster.
There are only 10 types of people in the world, those who understand binary and those who don't.
I was thinking the same thing ,because you can automatically rule out Vb.net due to structure. You can rule out C# due to operator types involved. There are some serious violations with C#,C++,C,Objective C and Java. I felt confused by the extended/longer operator types. Good call on it being javascript!
-
As a matter of curiosity what language is that in? I've never seen that construct !== before
-
The actual data passed is a timestamp when a change has happened. The
handleChangeEvent
function is an event handler bound to a field, like an input fieldonchange
event in the browser. The framework takes care of when to call this function, and then the control takes responsibility of what needs to happen. The point of my semi-rant being that the dev in question tried to fix a problem by doing the wrong thing in the wrong place :) -
Why does the object even support a logical not operator and what is it supposed to do to the object?
The good thing about pessimism is, that you are always either right or pleasently surprised.
It doesn't support it :). The actual code created a new string with
Date.now().toString()
, which would make the subsequentif
check fail every time. -
Probably javascript. 1 == "1" will be true in JavaScript because == casts objects to a common type. However 1 === "1" is false since one is int and the other is string. === does not cast objects. !== will be not equal to without casting of objects. If you're sure both sides of the operand are of the same type then using === or !== will be faster.
There are only 10 types of people in the world, those who understand binary and those who don't.
Indeed it is javascript :)