Colons?
Blitz3D Forums/Blitz3D Beginners Area/Colons?
| ||
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. |
| ||
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. |
| ||
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+1Looks 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. |
| ||
DONT USE THEM!!! They will increase the difficulty in reading your code. |
| ||
They can make for hard to read code, however, I find them quite handy for shortcutting with IFsIf a=5 Then a=a+1:b=b-1As opposed to If a = 5 Then a=a+1 b=b-1 EndIfOf course if you're doing anything more complex than that the second method is preferble |
| ||
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! |
| ||
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; |
| ||
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. |
| ||
![]() ? |
| ||
So has anyone ever had their Blitz programs' colons' cleansed? |
| ||
Hahaha.. |
| ||
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 |
| ||
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. |
| ||
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. |