Author Topic: !() vs != (and $= " ")  (Read 891 times)

1) This has been bothering me for a while now.

Code: [Select]
if(%string != "test")
Code: [Select]
if(!(%string $= "test"))
Which of the two is the better option in terms of readability, syntax or function?


2) Occasionally, when I'm running through and checking values of objects, I will hit odd results on this:

Code: [Select]
new ScriptObject(TestObjectA)
{
   Class = "TestClass";
   Name = "Test Object A";
   ValueImLookingFor = true;
}

new ScriptObject(TestObjectB)
{
   Class = "TestClass";
   Name = "Test Object B";
   // Intentionally leaving out ValueImLookingFor
}

Function TestClass::valueimlookingfor(%this)
{
    Return x ? Y : something I forgot the format
}

//leaving out packaging for sake of space
Function TestClass::doSomething(%this)
{
   If(%this.valueimlookingfor())
    //continue code
}

Regardless of the code errors that I intentionally put in there, is it safer to do %this.valueimlookingfor $= " " to check for objects that are under the same class as TestOBjectA but are missing that specific assigned value, or run a separate function to return true or false based on the variable's value or absence.

!= "string" will always return true. This is because != is for comparing numbers and when interpreting strings as numbers it will always interpret it as 0. Use !$= instead.

!= "string" will always return true. This is because != is for comparing numbers and when interpreting strings as numbers it will always interpret it as 0. Use !$= instead.

Code: [Select]
%string = "not test";
Code: [Select]
if(%string !$= "test")
{
  //do stuff
}
break it down:
Code: [Select]
if(true)
{
  //do stuff
}

Code: [Select]
if(!(%string $= "test"))
{
  //do stuff
}
break it down
Code: [Select]
if(false != (%string $= "test"))
{
  //do stuff
}
if(false != true)
{
  //do stuff
}
basically the second method is a weird round about way of doing things


would you mind elaborating what you are trying to accomplish on the second case?
« Last Edit: March 02, 2016, 04:47:23 PM by Swollow »

would you mind elaborating what you are trying to accomplish on the second case?
He's wondering if it's better to do this:
Code: [Select]
function object::isUgly(%obj)
{
      return %obj.isUgly ? true : false;
}

function myFunction(%obj)
{
      if(%obj.isUgly())
      {
            //Do stuff
      }
}
Or this:
Code: [Select]
function myFunction(%obj)
{
      if(%obj.isUgly)
      {
            //Do stuff
      }
}

I'm assuming the second case would be ever so slightly faster.
« Last Edit: March 02, 2016, 05:20:34 PM by jes00 »

I'm assuming the second case would be ever so slightly faster.
it would be. the former would be better if you may have a case where you might want to change how isUgly behaves beyond just setting it to true/false directly (eg return a different value for isUgly if other things are true, etc)

He's wondering if it's better to do this:
Code: [Select]
function object::isUgly(%obj)
{
      return %obj.isUgly ? true : false;
}

function myFunction(%obj)
{
      if(%obj.isUgly())
      {
            //Do stuff
      }
}
Or this:
Code: [Select]
function myFunction(%obj)
{
      if(%obj.isUgly)
      {
            //Do stuff
      }
}

I'm assuming the second case would be ever so slightly faster.
However, i'm working with a class that includes multiple objects, and some of the objects have an .isUgly value while some don't. When I try to get %obj.getUgly but the value doesn't actually exist and is null, it causes some errors.

if(%obj.isUgly) won't work correctly sometimes unless its an actual value, so I sometimes do %obj.isUgly $= " " instead

However, i'm working with a class that includes multiple objects, and some of the objects have an .isUgly value while some don't. When I try to get %obj.getUgly but the value doesn't actually exist and is null, it causes some errors.

if(%obj.isUgly) won't work correctly sometimes unless its an actual value, so I sometimes do %obj.isUgly $= " " instead
Using a variable that doesn't exist on an object should affect anything because you don't need to initialize variables in TorqueScript.

Using a variable that doesn't exist on an object should affect anything because you don't need to initialize variables in TorqueScript.
so if .isUgly doesn't exist it will just resort to false

so if .isUgly doesn't exist it will just resort to false
Yes.