Colons?

Blitz3D Forums/Blitz3D Beginners Area/Colons?

Q(Posted 2005) [#1]
I saw in some blitz3d code somewhere that used colons out of nowhere. What do they do? it was set up something like this:

[code] : [code] : [code]

All in one line.


puki(Posted 2005) [#2]
Colons are a bit like a new line but on the same line. It's just a way of typing separate lines on one line.

I used to program a lot of stuff in one line - some people think it can be quicker to do it that way for certain code.


VP(Posted 2005) [#3]
Colons are just used to say "next command" and serve no other purpose than to enable you to put more than one blitz command on a line.

A colon can be used pretty much anywhere and there are no hardened rules to say where they should be used.

My personal preference is to use them to highlight that some commands should be considered as one operation.

Examples:
;--------------------------------------------------------------------------
; PNG signature.
WriteByte(fh%,137):WriteByte(fh%,80):WriteByte(fh%,78):WriteByte(fh%,71)
WriteByte(fh%,13):WriteByte(fh%,10):WriteByte(fh%,26):WriteByte(fh%,10)
;--------------------------------------------------------------------------
The only reason I split the above code into 2 lines was to prevent the line length exceeding 80 characters (for when other people are browsing my code).


Second example:
	sfx_array(SFX_BIGEXP)\sound	= LoadSound(folder$ + "\" + sfx_array(SFX_BIGEXP)\filename$)
	error$ = error$ + sfx_errorCheck$(i) : i=i+1

	sfx_array(SFX_LASER)\sound	= LoadSound(folder$ + "\" + sfx_array(SFX_LASER)\filename$)
	error$ = error$ + sfx_errorCheck$(i) : i=i+1

	sfx_array(SFX_SHOOT)\sound	= LoadSound(folder$ + "\" + sfx_array(SFX_SHOOT)\filename$)
	error$ = error$ + sfx_errorCheck$(i) : i=i+1

	sfx_array(SFX_ZAPPER)\sound	= LoadSound(folder$ + "\" + sfx_array(SFX_ZAPPER)\filename$)
	error$ = error$ + sfx_errorCheck$(i) : i=i+1
Looks complex if you do not know the context for the code. Colon use here just seems to be a neater method of formatting the code and infers that "sort the error string out and move to the next part" - is one operation.


Techlord(Posted 2005) [#4]
DONT USE THEM!!! They will increase the difficulty in reading your code.


Rob Farley(Posted 2005) [#5]
They can make for hard to read code, however, I find them quite handy for shortcutting with IFs
If a=5 Then a=a+1:b=b-1
As opposed to
If a = 5 Then
	a=a+1
	b=b-1
EndIf 
Of course if you're doing anything more complex than that the second method is preferble


Ross C(Posted 2005) [#6]
Dam, i just realised, i don't use them. I used to use them all the time, but a quick look over my code, and there's none :-O. I must use more!


octothorpe(Posted 2005) [#7]
DONT USE THEM!!! They will increase the difficulty in reading your code.


How ironic, every time I use them it's to improve readability. This is just the old dogma that you shouldn't use something which can be misused. Next you'll tell me not to use GOTO. ;)

Completely off topic, there's an even more fun structure in C known as the tertiary operator which provides inline conditionals:

if (x>6) { y=7; } else { y=8; }
// is equivilent to
y = (x>6) ? 7 : 8;



Shifty Geezer(Posted 2005) [#8]
Colons can be good to group related operations onto a quick to comment out line though.

x#=EntityX(object)
y#=EntityY(object)
z#=EntityZ(object)

becomes

x#=EntityX(object) : y#=EntityY(object) : z#=EntityZ(object)

Works best when just assigning values at the beginning of loops etc.

a=1
b=2
c=a+b
d=c-3

a=1 : b=2 : c=a+b : d=c-3

It's the same conceptually as multiple declarations on a line.

Global cat
Global dog
Global sheep
Global giraffe
Global lema
Global porpoise

Global cat, dog, sheep, giraffe, lema, porpoise

I'm sure anyone who says 'don't use colons, put every command ona seperate line,' doesn't put every variable declaration on a seperate line :p

Don't use colons myself. Don't use IF THEN's either. Always go with IF...code...ENDIF. It's a matter of personal preference.


jhocking(Posted 2005) [#9]


?


WolRon(Posted 2005) [#10]
So has anyone ever had their Blitz programs' colons' cleansed?


Q(Posted 2005) [#11]
Hahaha..


splinux(Posted 2005) [#12]
Colons don't do anything. They are null-operators:

Global a, b, c

a = 1 b = 2 c = 3

Print a
Print b
Print c


This is equal to:

Global a, b, c

a = 1 : b = 2 : c = 3

Print a
Print b
Print c



jhocking(Posted 2005) [#13]
Is that true? Regardless, as bad for readability as it is to use colons to put multiple statements on one line, not using them when you have multiple statements on one line is even worse.


big10p(Posted 2005) [#14]
a = 1 b = 2 c = 3
I didn't know you could do this in blitz. I also don't know why you'd ever want to! At least with using colons you can clearly delimit separate commands.