What are you using FB for and what features do you use most?

General discussion for topics related to the FreeBASIC project or its community.
caseih
Posts: 1968
Joined: Feb 26, 2007 5:32

Re: What are you using FB for and what features do you use most?

Post by caseih »

And you can take advantage of the fall-through behavior to do interesting things with few lines of code that you can't do with any other language's implementation of case. One man's bug is another man's feature. As warts go, it's extremely minor. I'm sure it's source of some bugs, but a linter might alert you. I'm sure a modern language wouldn't do that but C has aged extremely well over the last 40 or 50 years.

I much prefer curly braces to the more awkward begin/end syntax in Pascal[1]. FB's syntax is marginally better than Pascal's as the "begin" is implicit. However the problem with FB's syntax is it's much more complicated for an editor to trivially match beginning and end of blocks. Pascal makes it easier, but C makes it super fast to match beginning and end, which makes reading other people's code much easier, especially if it goes off the screen.

[1] Programmers are born with a finite number of keystrokes, so less typing prolongs the life of the programmer!
jj2007
Posts: 2327
Joined: Oct 23, 2016 15:28
Location: Roma, Italia
Contact:

Re: What are you using FB for and what features do you use most?

Post by jj2007 »

As I wrote in my last post at the bottom of the previous page, C is quite ok, but fall through is a horrible design error. If you are able to find some major C sources somewhere, check how often the fall through "feature" has been used, and compare that to the number of stupid break statements that the poor programmers had to use. There is absolutely no excuse of fall through.

Plus, even in Assembly you can use Case 16, 18, 20 which is, technically speaking, fall through, but with a much clearer syntax.
caseih
Posts: 1968
Joined: Feb 26, 2007 5:32

Re: What are you using FB for and what features do you use most?

Post by caseih »

jj2007 wrote:Plus, even in Assembly you can use Case 16, 18, 20 which is, technically speaking, fall through, but with a much clearer syntax.
Well sure you can do that in C also by combining the case statements which is just as readable as case 16,18,20. But that's not really what "fall through" describes, and is not what the gotcha is all about.

Code: Select all

switch(foo) {
case 16:
case 18:
case 20:
	/* This code is executed for only 16,18, and 20 */
	/* without the break we'll fall through */
case 22:
case 24:
case 26:
	/* This code is executed for 16,18,20, 22, 24, and 26. */
	/* without break we'll fall through again. */
case 28:
	/* and finally this is executed for 16,18, 20, 22, 24, 28 and 28. */
} 
Like I say, one man's flaw is another man's feature. Pretty sure this behavior is used quite a bit in the Linux kernel. It's quite useful when you want do do things in a sequential way. Would I want to do this in any other language? Probably not.
Munair
Posts: 1274
Joined: Oct 19, 2017 15:00
Location: Netherlands
Contact:

Re: What are you using FB for and what features do you use most?

Post by Munair »

jj2007 wrote:

Code: Select all

  mov eax, 17                ; try any combination you like,..
  mov edx, 77
  mov ecx, 99
  mov ebx, 17

  Switch eax
  Case 1
          print "eax is one", 13, 10
  Case edx .. ecx
          print "eax is between edx and ecx", 13, 10
  Case 10 .. 15
          print "eax is between 10 and 15", 13, 10
  Case ebx
          print "eax is the same as ebx", 13, 10
  Case 16, 18, 20
          print "eax is sixteen or eighteen or twenty", 13, 10
  Default
          print str$(eax), " is nowhere in the ranges above", 13, 10
  Endsw
Note that Case 16, 18, 20 is, technically speaking, fall through, but with a much clearer syntax.
No, Case 16, 18, 20 is not technically fall-through. It is a set comprising one condition (similar to an if-statement using or), technically jumping from one test to the next within a single branch.

Furthermore, the example of using multiple registers is a concept of higher level languages; it never works that way in assembler. When a higher level language like C is translated to assembler, a variable's value or address is typically loaded into a register after which the register is being tested by means of a jump table.

The problem with translating a switch statement to assembler is that the compiler generates labels (jump addresses) that are never used. For example the following FB code:

Code: Select all

dim a as integer = 10

select case a
case 0
  print "0"
case 1 to 3
  print "1 to 3"
case 4, 5, 6
  print "4, 5, 6"
case else
  print "7 to 10"
end select
is translated to assembler containing the following part:

Code: Select all

// ...
.L12:
	mov	esi, 7
	lea	rax, .LC3[rip]
	mov	rdi, rax
	call	fb_StrAllocTempDescZEx@PLT
	mov	QWORD PTR -24[rbp], rax
	mov	rax, QWORD PTR -24[rbp]
	mov	edx, 1
	mov	rsi, rax
	mov	edi, 0
	call	fb_PrintString@PLT
.L13:
.L5:
.L14:
	mov	edi, 0
// ...
The labels .L13 and .L5 are not used and never jumped to. I suspect that the original C developers decided to choose fall-through by default to minimize the generation of jumps and unused labels and to only generate a label when the programmer explicitly tells the compiler to do so with a break statement.

From an assembler point of view a compiler generated switch statement is a very ugly construct. From a compiler point of view a switch statement is a beast to translate to assembler.
Munair
Posts: 1274
Joined: Oct 19, 2017 15:00
Location: Netherlands
Contact:

Re: What are you using FB for and what features do you use most?

Post by Munair »

caseih wrote:And you can take advantage of the fall-through behavior to do interesting things with few lines of code that you can't do with any other language's implementation of case.
It should be noted that fall-through is used much less often because there are not so many cases that require or could use fall-through. Languages that do it the other way around (explicit fall-through) provide the same capability with just as much code and much less bug prone.
I much prefer curly braces to the more awkward begin/end syntax in Pascal[1]. FB's syntax is marginally better than Pascal's as the "begin" is implicit. However the problem with FB's syntax is it's much more complicated for an editor to trivially match beginning and end of blocks. Pascal makes it easier, but C makes it super fast to match beginning and end, which makes reading other people's code much easier
I very much disagree with the readability of curly braces. Moreover, on most keyboards it requires an extra keystroke (shift) making a two-character keyword easier when doing a lot of coding. That's why I decided for do..end blocks in SharpBASIC as a replacement for C's curly braces, which can then be used for other purposes, such as collections, dictionaries or sets. Also speaking against the readability of curly braces as code block delimiters is that with loop statements the placement of a condition such as while or until becomes a problem. So C decided to put that statement outside of the respective code block. I consider that bad language design.

Yes, Pascal's begin..end is not the best solution, especially because it is sometimes mandatory but not always. It also adds to verbosity. On the other hand, Pascal and ALGOL were developed before C and Pascal continued the tradition of ALGOL, partially as an educational language.
Munair
Posts: 1274
Joined: Oct 19, 2017 15:00
Location: Netherlands
Contact:

Re: What are you using FB for and what features do you use most?

Post by Munair »

jj2007 wrote:Plus, even in Assembly you can use Case 16, 18, 20
That very much depends on the assembly language used. MASM is not the only one out there. ;-).
jj2007
Posts: 2327
Joined: Oct 23, 2016 15:28
Location: Roma, Italia
Contact:

Re: What are you using FB for and what features do you use most?

Post by jj2007 »

Munair wrote:No, Case 16, 18, 20 is not technically fall-through. It is a set comprising one condition (similar to an if-statement using or), technically jumping from one test to the next within a single branch.
See Switch in C and MASM
marcov
Posts: 3331
Joined: Jun 16, 2005 9:45
Location: Netherlands
Contact:

Re: What are you using FB for and what features do you use most?

Post by marcov »

caseih wrote:And you can take advantage of the fall-through behavior to do interesting things with few lines of code that you can't do with any other language's implementation of case. One man's bug is another man's feature. As warts go, it's extremely minor. I'm sure it's source of some bugs, but a linter might alert you. I'm sure a modern language wouldn't do that
but C has aged extremely well over the last 40 or 50 years.
Is it? Many GNU applications migrated to C++, a move that happened in the business world long ago. C remains a preferred system language, simply because that field doesn't have much room for anything else. (Rust tries with a security angle, but I have my doubts)

Anyway the feature richness of the fall through could be debated if it was the other way around (fall through with keyword, default without). How it is currently is simply hopeless design. (and or maybe saving memory in the initial compilers for cases that have multiple switch selector values? )
I much prefer curly braces to the more awkward begin/end syntax in Pascal[1].
Curly braces are a fail on so many levels in the current situation. They are characters that need combined keys to make (which any type instructor can say should be avoided), it is quite easily to gloss over them, and training over time (I've been doing embedded C for a significant part of my time for 15 years no) only fixes that when well indented. Misplaced braces are too easily missed.
[1] Programmers are born with a finite number of keystrokes, so less typing prolongs the life of the programmer!
True. But not all characters have the same typing weight. Combo keystrokes and keys at the edge of the keyboard weigh heavier than alpha characters in the middle. Keyboards are designed that way.
caseih
Posts: 1968
Joined: Feb 26, 2007 5:32

Re: What are you using FB for and what features do you use most?

Post by caseih »

Well no matter how you cut it any language could remain in the top 5 most common languages for 50 years is impressive. It speaks to the at least some design staying power, despite now security weaknesses and deficiencies that are mattering more and more. Of course Pascal is also impressive for having a good basic design for even more years, and is still used today, although not nearly on the same scale as C. Obviously BASIC has a heritage that stretches decades also, but over the last few decades has remained extremely niche, and BASIC dialects are all wildly different and incompatible with each other. There is such thing as ANSI standard BASIC, but we should all be glad FB is not an implementation of that.

Most of you seem to misunderstand my point about curly braces. I was not talking about anyone's personal opinion on the readability of various methods of defining blocks. I was speaking from the point of view of the text editor, and a programmer using the text editor to navigate the code. Braces make matching extremely trivial. In fact even the oldest text editors had brace matching functionality to easily pop between braces. It requires very little understanding of the language's syntax to do; it can mostly work with all curly-braced languages, even ones the text editor, or even the programmer, has never seen before. All the text editor has to do is match a single character (which is very fast), and track the depth. Text editors have done this for decades, long before fancy syntax highlighting became common. It also makes it trivial to reformat the code to any particular style, and C reformatting tools have been around for almost as long as C. Of course we can do all this block matching with pascal code also, but now we have to search for a string instead of a single character match.

As for BASIC, well, that's impossible for a text editor to do without a thorough implementation of the language's grammar, and the grammar of a language like FB is very complicated, full of ambiguities, lookaheads, and probably backtracking. It also means that an editor that knows about one dialect of BASIC will fall down on another dialect when it comes to block detection, and code reformatting. And obviously an editor has the same issue with a language like Python, but Python's grammar is a lot simpler than any BASIC dialect. And any language that merges a statement with the block syntax would have this issue with text editors.
Munair wrote:So C decided to put that [while] statement outside of the respective code block. I consider that bad language design.
Obviously there is more about this that is subjective than any kind of technical argument. I can think of some things that are generally considered to be bad language design, such as PHP having no less than three different equality operators, or it's feature of automatically converting strings to numbers in expressions. But this is the first time I've heard the claim that one of the most consistent features of C's grammar is a design flaw! The fact that a <statement> production in the C grammar can be either a single statement or a block of statements was really neat thinking on K&R's part. It simplified the grammar a lot and reduces ambiguities. That's what I consider good design. K&R's decisions were also based on the realities of computers at the time. Grammars like FB has today would have made the compiler impossibly slow and memory-intensive back then. They struck an amazing balance between the needs of the programmer and the limited power of the compiler that still serve us well today. Obviously today we have more freedom to design things differently when starting over. So whatever works for Sharp BASIC, good on you.
caseih
Posts: 1968
Joined: Feb 26, 2007 5:32

Re: What are you using FB for and what features do you use most?

Post by caseih »

marcov wrote:Curly braces are a fail on so many levels in the current situation. They are characters that need combined keys to make (which any type instructor can say should be avoided), it is quite easily to gloss over them, and training over time (I've been doing embedded C for a significant part of my time for 15 years no) only fixes that when well indented. Misplaced braces are too easily missed.
Shrug. Mathematical expressions are heavily used in programming and they similarly require combination keys. And BASIC used type suffix symbols for years.

If you get off on your indentations, one could similarly lose track of begins and ends as much as curly braces.

Which brings me to a question I've always had (and acknowledge I'm well and truly in the weeds here), but just where should I put begin and end in Pascal? For example, should it be following the IF/THEN on the same line, on its own line but at the parent indent level, on its own line but indented slightly, or on its own line indented fully with the body indented further? No matter where I put it, it looks awkward to me, especially if then/else. Braces have similar problems, but at least it doesn't look half bad to the programmer to put them on their own lines, or K&R style on the same line. But I despise GNU style of half indenting them on their on line!
marcov
Posts: 3331
Joined: Jun 16, 2005 9:45
Location: Netherlands
Contact:

Re: What are you using FB for and what features do you use most?

Post by marcov »

caseih wrote:Well no matter how you cut it any language could remain in the top 5 most common languages for 50 years is impressive. It speaks to the at least some design staying power, despite now security weaknesses and deficiencies that are mattering more and more.
More than design, it simply was entrenched in some (unix) business. Or by that same metric Cobol and Fortran must be the best designed languages ever.
Of course Pascal is also impressive for having a good basic design for even more years, and is still used today, although not nearly on the same scale as C. Obviously BASIC has a heritage that stretches decades also, but over the last few decades has remained extremely niche, and BASIC dialects are all wildly different and incompatible with each other. There is such thing as ANSI standard BASIC, but we should all be glad FB is not an implementation of that.
My side of this discussion is not about C vs other languages. It is against looking at known design issues with C _nowadays_ with rose-tinted glasses and brand it features just because of some "100000 lemmings can't be wrong" argument.

I want to stress "nowadays" again, because if you look into reasons, you often find small but necessary reasons in the very early days with the limited memories of the day. But it is probably not an full screen editor, simply because there were few.
Braces make matching extremely trivial.
#define begin {
#define end }

Perfectly valid C, now run your editor again.
jj2007
Posts: 2327
Joined: Oct 23, 2016 15:28
Location: Roma, Italia
Contact:

Re: What are you using FB for and what features do you use most?

Post by jj2007 »

Code: Select all

	switch (svar)
	{
	case 12:
		printf("That was case 12\n");
		break;
	case 24:
		printf("That was case 24\n");
		break;
	case 16:
		printf("That was case 16\n");
		break;
	default:
		printf("That was default\n");
		break;
	}

Code: Select all

	switch svar
	case 12
		printf("That was case 12\n")
	case 24
		printf("That was case 24\n")
	case 16
		printf("That was case 16\n")
	default
		printf("That was default\n")
	endsw
See the difference? The lower one is Assembly: no ":", no ";", no "{", no break. In short, no unnecessary crap.
caseih
Posts: 1968
Joined: Feb 26, 2007 5:32

Re: What are you using FB for and what features do you use most?

Post by caseih »

marcov wrote:More than design, it simply was entrenched in some (unix) business. Or by that same metric Cobol and Fortran must be the best designed languages ever.
Some (unix) business? Too funny. While Cobol and Fortran are still in use all these decades later, that is not what I said nor what I meant. I was talking about a language that is not only still used, but *widely* used. As in one of the top five most widely-used language A big difference there! Furthermore dozens of languages have come and gone in the meantime. Reminds me of the famous quote by Stroustrup (about c++ but it applies to C), "There are only two kinds of languages: the ones people complain about and the ones nobody uses."
#define begin {
#define end }

Perfectly valid C, now run your editor again.
Oh sure you can deliberately break any editor with macros, including syntax highlighting, but it hardly speaks to the downsides of C's syntax.
Last edited by caseih on Jan 16, 2022 18:44, edited 5 times in total.
caseih
Posts: 1968
Joined: Feb 26, 2007 5:32

Re: What are you using FB for and what features do you use most?

Post by caseih »

jj2007 wrote:See the difference? The lower one is Assembly: no ":", no ";", no "{", no break. In short, no unnecessary crap.
I know people tire of my c/python/whatever posts and Marcov's pascal posts. But your MASMBASIC macro posts are particularly tiring. That is *not* assembly language, jj, sorry. That is what using MASM macros can look like, but it's not assembly code. I cannot run that in nasm, for example. If you wanted to show what assembly really looks like when implementing these structures, show the raw assembly output from the macro assembler. Macros are powerful concepts, no doubt, letting you create a higher level of abstraction. But you're just muddying the waters here, really. Although I admit I muddied the waters also in general on this thread.
Last edited by caseih on Jan 16, 2022 18:40, edited 1 time in total.
Munair
Posts: 1274
Joined: Oct 19, 2017 15:00
Location: Netherlands
Contact:

Re: What are you using FB for and what features do you use most?

Post by Munair »

jj2007 wrote:See the difference? The lower one is Assembly: no ":", no ";", no "{", no break. In short, no unnecessary crap.
I dare to question that it's Assembly. It is higher level language construct inserted into "assembly"; afterwards it will still be translated by the compiler to real assembly code over which the programmer has no control. MS at its best.

EDIT: I very much agree with caseih on this one.
Post Reply