IF (Thinking outside the Box?)

General FreeBASIC programming questions.
Post Reply
NorbyDroid
Posts: 70
Joined: May 21, 2016 22:55

IF (Thinking outside the Box?)

Post by NorbyDroid »

I thought for fun I would toss this in the forum.

How I like to use IF is like this:

Code: Select all

  If Condition then _
      Do This
This may have advantages and disavantages I cannot think of, but one advantage is the removal of havin to add "ENDIF" at the end of the IF statement.

I know in modern languages where IF is used there are no "ENDIF".

Well, any comments on what ya think about usin IF in this way or any other way and its advantages and disadvantages, please let me know.

I could be mistaken, but I find this interesting and one way to use IF that may be considered 'Thinking outside the box".
dodicat
Posts: 7983
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: IF (Thinking outside the Box?)

Post by dodicat »

It is OK the way you have it, if is fine without end if.

Code: Select all

var condition=5>4
#define this :print "OK":exit do:loop

  If condition then _
      Do This
      sleep
       
adeyblue
Posts: 300
Joined: Nov 07, 2019 20:08

Re: IF (Thinking outside the Box?)

Post by adeyblue »

NorbyDroid wrote: Apr 12, 2023 5:00 Well, any comments on what ya think about usin IF in this way or any other way and its advantages and disadvantages, please let me know.
The only to thing to keep in mind is that the indentation may lie to you. If you want to add another thing to your if in future, you might do this

Code: Select all

  If printTheThing then _
      Print "Printing the thing"
      Print "The boss wanted this bit printing too"
And then it looks like the second print will also only happen if printTheThing is true. But the second Print isn't part of the if and will always happen.

Coding styles are personal things though, there's no wrong or right way to do it and this isn't some fatal flaw where you must immediately go and add End Ifs everywhere. It's just something to remember.
caseih
Posts: 2157
Joined: Feb 26, 2007 5:32

Re: IF (Thinking outside the Box?)

Post by caseih »

Yup many a bug has resulted from the short form of IF (without block braces) in C and C++.
marcov
Posts: 3462
Joined: Jun 16, 2005 9:45
Location: Netherlands
Contact:

Re: IF (Thinking outside the Box?)

Post by marcov »

Many things like "END IF" were probably originally added to lessen the dependency of formatting (line-based, identation or otherwise) on syntax, and thus avoid bugs.

I mostly lean towards "no single line block" principle nowadays. It is safer, and because in that case a block always follows certain tokens, IDEs can easily add the "end <x>" for you when triggered to win back most, if not all, of the extra typing.
dodicat
Posts: 7983
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: IF (Thinking outside the Box?)

Post by dodicat »

marcov

for n:=0 to 10 do writeln(n);

Surely you wouldn't advise
for n:=0 to 10 do
begin
writeln(n);
end;

I don't use an ide which adds an end of block automatically, whether end; or end if or }, code completion? or whatever it is called.
Jut a thing I find annoying.
I am actually quite happy to use notepad sometimes to write code.
There is a strange satisfaction in immersion in blandness. Saying that, notepad is really quite a handy editor, it has been around for a long time.
NorbyDroid
Posts: 70
Joined: May 21, 2016 22:55

Re: IF (Thinking outside the Box?)

Post by NorbyDroid »

There is just somethin quite satisfying usin plain text editors. Yes the new ide programs are great and pack full of handy features but usin plain text like DOS editor, Pedit, Notepad, etc. is just as good.
caseih
Posts: 2157
Joined: Feb 26, 2007 5:32

Re: IF (Thinking outside the Box?)

Post by caseih »

I'm not sure if the Pascal gurus would balk at this formatting, but you can do:

Code: Select all

for n:=0 to 10 do begin
	writeln(n);
end;
Gets a little messy with the else class in IF statements though if you do that.
marcov
Posts: 3462
Joined: Jun 16, 2005 9:45
Location: Netherlands
Contact:

Re: IF (Thinking outside the Box?)

Post by marcov »

dodicat wrote: Apr 13, 2023 16:22 marcov

for n:=0 to 10 do writeln(n);

Surely you wouldn't advise
for n:=0 to 10 do
begin
writeln(n);
end;
The begin is to signal a multiline block. If it is _always_ a multiline block it can be omitted for non procedural blocks (and indeed Pascal's successor Modula-2 did just that). Compilable Basics like QB and VB might have adapted that from the get go.

One can discuss if the END must be decorated with a block type ("end if" or even M2's "end 'subname';"), but in general I'm not in favour of that. Preferably the syntax should be good enough to localize the error without this. (C sets a bad examples here, as mistakes in the blockstructure can swallow the start of a new procedure)
I don't use an ide which adds an end of block automatically, whether end; or end if or }, code completion? or whatever it is called.
No problem. The mandatory End is still only a 3 letter word without difficult and hard to type shifted letters. But you can always insert extra lines into the block (no adding begin.. end with a lot of extra cursor movements that require moving your hand)

The mandatory end-block marker also makes it easier to find mistakes.

Of course for Pascal this is all hypothetical, the base block structure has been fixated since the seventies (and dates to the sixties)
marcov
Posts: 3462
Joined: Jun 16, 2005 9:45
Location: Netherlands
Contact:

Re: IF (Thinking outside the Box?)

Post by marcov »

NorbyDroid wrote: Apr 13, 2023 16:29 There is just somethin quite satisfying usin plain text editors. Yes the new ide programs are great and pack full of handy features but usin plain text like DOS editor, Pedit, Notepad, etc. is just as good.
A text editor can do auto completion too.
Post Reply