# Shorter code

**URL:** <https://forum.kirupa.com/t/shorter-code/28427>\
**Category:** Uncategorized\
**Created:** [August 13, 2003, 6:14pm UTC](https://forum.kirupa.com/t/shorter-code/28427 "2003-08-13T18:14:46Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![paisley](https://avatars.discourse-cdn.com/v4/letter/p/a88e57/32.png) [@paisley](https://forum.kirupa.com/u/paisley)\
**Post date:** [August 13, 2003, 6:14pm UTC](https://forum.kirupa.com/t/shorter-code/28427/1 "2003-08-13T18:14:46Z")

</div>

```auto
function myfunction(object1,para2,para3) {
   if (!object1.initialized) {
      object1.myProperty = para3;
      object1.anotherProperty = para2;
      para2 = 1;
      object1.ratio = 30;
      object1.initialized = true;
   }
   else {
      object1.ratio++;
      ...more code here...
   }
}

```

Is there a shorter code for this? It may look short but the function I actually made is much more complicated and longer that the code above.

Is there a way to type ‘object1’ once?

[edit]I edited this to show the code more clearly \* Jubba :thumb:

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [August 13, 2003, 6:22pm UTC](https://forum.kirupa.com/t/shorter-code/28427/2 "2003-08-13T18:22:52Z")

</div>

you cannot shorten that code significantly.

You can, however, look up \<b\>with\</b\> in your ActionScript Dictionary =).

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [August 13, 2003, 6:23pm UTC](https://forum.kirupa.com/t/shorter-code/28427/3 "2003-08-13T18:23:18Z")

</div>

if object already has those values for it defined before they are being assigned there, you can say

with(object1){  
myProperty = para3;  
anotherProperty = para2;  
…  
}

otherwise… no

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [August 13, 2003, 6:24pm UTC](https://forum.kirupa.com/t/shorter-code/28427/4 "2003-08-13T18:24:51Z")

</div>

try

```auto
function myfunction(object1,para2,para3) {
with(object1) {
        if (!initialized) {
                myProperty = para3;
                anotherProperty = para2;
                para2 = 1;
                ratio = 30;
                initialized = true;
        }
        else {
                ratio++;
                ...more code here...
        }
}
}

```

im not sure if that’ll work, try it anyways…

[edit]

beat me to it…

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [August 13, 2003, 6:25pm UTC](https://forum.kirupa.com/t/shorter-code/28427/5 "2003-08-13T18:25:40Z")

</div>

This is the actual code:

function calculateColor(targetObject,targetObjectColor,targetColor) {  
if (!targetObject.notInitialized) {  
targetObject.targetColor = targetColor;  
targetObject.ratio = 30;  
targetObject.targetColor = targetObject.targetColor.toString(16);  
hexToRgb(targetObject,targetObject.targetColor);  
targetObject.targetR = targetObject.r;  
targetObject.targetG = targetObject.g;  
targetObject.targetB = targetObject.b;  
targetObject.oldColor = targetObjectColor.getRGB().toString(16);  
hexToRgb(targetObject,targetObject.oldColor);  
targetObject.oldR = targetObject.r;  
targetObject.oldG = targetObject.g;  
targetObject.oldB = targetObject.b;  
targetObject.xyzR = Math.max(targetObject.targetR,targetObject.oldR) - Math.min(targetObject.targetR,targetObject.oldR);  
targetObject.xyzG = Math.max(targetObject.targetG,targetObject.oldG) - Math.min(targetObject.targetG,targetObject.oldG);  
targetObject.xyzB = Math.max(targetObject.targetB,targetObject.oldB) - Math.min(targetObject.targetB,targetObject.oldB);  
targetObject.asdfR = Math.floor(Math.abs(targetObject.xyzR/targetObject.ratio));  
targetObject.asdfG = Math.floor(Math.abs(targetObject.xyzG/targetObject.ratio));  
targetObject.asdfB = Math.floor(Math.abs(targetObject.xyzB/targetObject.ratio));  
targetObject.notInitialized = true;  
}  
else {  
if (targetObject.ratio \> 0) {  
targetObject.ratio–;  
if (Math.max(targetObject.targetR,targetObject.oldR) == targetObject.targetR) {  
targetObject.oldR += targetObject.asdfR;  
}  
else {  
targetObject.oldR -= targetObject.asdfR;  
}  
if (Math.max(targetObject.targetG,targetObject.oldG) == targetObject.targetG) {  
targetObject.oldG += targetObject.asdfG;  
}  
else {  
targetObject.oldG -= targetObject.asdfG;  
}  
if (Math.max(targetObject.targetB,targetObject.oldB) == targetObject.targetB) {  
targetObject.oldB += targetObject.asdfB;  
}  
else {  
targetObject.oldB -= targetObject.asdfB;  
}

```
	}
	else {
		targetObject.oldG = targetObject.targetG;
		targetObject.oldB = targetObject.targetB;
		targetObject.oldR = targetObject.targetR;
		targetObject.onEnterFrame = null;
	}
	changeColor(targetObject,targetObjectColor);
}

```

}

Could I still change this?

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [August 13, 2003, 6:39pm UTC](https://forum.kirupa.com/t/shorter-code/28427/6 "2003-08-13T18:39:24Z")

</div>

> \*Originally posted by senocular \*  
> \*\*if object already has those values for it defined before they are being assigned there…  
> \*\*
