Navigation

    Fuze Arena Logo
    • Register
    • Login
    • Search
    • Categories
    • Recent
    • Popular
    • Users
    • Groups
    • Help
    • Discord

    Is it me, or are if statements messy?

    Comments & Feedback
    13
    17
    802
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • _
      _JKDOS F last edited by

      if statements look a bit messy to me. In a more familiar programming language style, multiple if conditions would look like this.

      if (abc == 123) {
          // Do these
          // statements
      } else if (abc == 321) {
          // Do these
          // statements instead 
      } 
      else {
          // Forget it, just
          // do these statements
      }
      

      In Fuze, it looks more like this

      if abc == 123 then
          // Do these
          // statements
      else if abc == 321
          // Do these
          // statements instead 
      else
          // Forget it, just 
          // do these statements
      endif
      endif
      

      Looking at these side-by-side, the main problem is that the endif encloses the else.

      I feel the else should be allowed to appear on the outside immediately following an endif. Making it look more like this

      if abc == 123 then
          // Do these
          // statements
      endif
      else if abc == 321
          // Do these
          // statements instead 
      endif
      else
          // Forget it, just 
          // do these statements
      endif
      
      1 Reply Last reply Reply Quote 0
      • Wanderer1412
        Wanderer1412 Donator last edited by Wanderer1412

        Is it a case of using an indenting style that makes the nesting clear?....

        if abc == 123 then
               // Do these
               // statements
        else
               if abc == 321 then
                       // Do these
                       // statements instead 
               else
                      // Forget it, just 
                      // do these statements
               endif
        endif
        
        1 Reply Last reply Reply Quote 0
        • Martin
          Martin Fuze Team last edited by Martin

          Well, ignoring that one is syntactically correct for Fuze and the other not, it's just down to personal taste how you lay them out.

          I think mine tend to look perfectly nice enough. Not the same as in Java which is my usual language, but I accept that as fine because this simply isn't java.

          For me, I don't see anything wrong with either of these. Note, I add brackets simply because if I don't then when I go back to my Java IDE it moans at me a lot. They are not required in Fuze as we know...

          if (line == single) then noNewLines = fine endif
          
          if (abc == 123) then
              // Blah
          else
              if (code == 456) then
                  // Blah
              endif
          endif
          

          Oh, just to add - I'd qualify the "single line is fine" with "as long as it isn't too long". How long is too long? No more than a screen width but in old terms, probably around 100 to 120 characters. But again, no hard rules here.

          _ 1 Reply Last reply Reply Quote 2
          • _
            _JKDOS F @Martin last edited by

            @Martin Probably just need to wait for Fuze to add switch cases. The way we're nesting the if statements will be a heachache when we start to add a multitude of them, with each one being more indented. This is why I opted not to indent mine. I just have to deal with multiple endif statments being stacked for each else if

            *shudder *

            if (abc == 111) then
                // blah
                // blah
            else 
            	if (abc == 222)
            		// blah
            		// blah 
            	else
            		if (abc == 333)
            			// blah 
            			// blah
            		else
            			if (abc == 444)
            				// blah 
            				// blah
            			else
            				if (abc == 555)
            					// blah 
            					// blah
            				else
            					//blah
            					//blah
            				endif			
            			endif
            		endif
            	endif
            endif
            

            compared to

            if (abc == 111) then
            	// blah
            	// blah
            else if (abc == 222)
            	// blah
            	// blah 
            else if (abc == 333)
            	// blah 
            	// blah
            else if (abc == 444)
            	// blah 
            	// blah
            else if (abc == 555)
            	// blah 
            	// blah
            else
            	//blah
            	//blah
            endif   
            endif    
            endif    
            endif    
            endif
            
            N 1 Reply Last reply Reply Quote 0
            • N
              Nisse5 F @_JKDOS last edited by Nisse5

              @_JKDOS FWIW, there's no need for using "else" in the example, since none of these are in conflict (unless "abc" is changed within the "if" block). But if you want it structured like a case statement, you can do:

              while true loop
                 if (abc == 111) then
              	// blah
              	// blah
                      break
                 endif
                 if (abc == 222) then
              	// blah
              	// blah
                      break
                 endif
                 if (abc == 333) then
              	// blah
              	// blah
                      break
                 endif
                 if (abc == 444) then
              	// blah
              	// blah
                      break
                 endif
                 if (abc == 555) then
              	// blah
              	// blah
                      break
                 endif
                 break
              repeat
              
              _ 1 Reply Last reply Reply Quote 4
              • T
                TheDearHunter F last edited by

                I agree it is a bit of a mess, it would be nice if they just gave us an elseif instead of having to nest subsequent cases inside of our else.

                1 Reply Last reply Reply Quote 0
                • pianofire
                  pianofire Fuze Team last edited by

                  If you feel strongly about this please add something to the Wishlist post https://fuzearena.com/forum/topic/11/wishlist/168

                  Personally I would like to have a switch/case statement

                  1 Reply Last reply Reply Quote 0
                  • Aceba1
                    Aceba1 last edited by

                    An elseif keyphrase (or elif) could simplify the process as well

                    1 Reply Last reply Reply Quote 1
                    • A
                      Alexander last edited by

                      I like it the way it is.... but if you added an elseif would you still need an additional endif? and would it break the original coding, because, that would be a boring day of editing broken code...

                      1 Reply Last reply Reply Quote 0
                      • Jongjungbu
                        Jongjungbu F last edited by Jongjungbu

                        My preference with IF statements is is more like:

                        if ( a >5 ) then bDone = 0 else bDone = a endif

                        I like less lines hahaha

                        1 Reply Last reply Reply Quote 0
                        • Hitomi
                          Hitomi F last edited by Hitomi

                          The point of an elseif keyword is that it would be used in a statement such as if condition then statement elseif condition 2 statement elseif condition 3 statement endif. That would make it one long statement that doesn't need pairing off for each condition. That means nested statements aren't necessary as often. That's the way lua and some versions of basic do it. I'd like to see it in FUZE, but it's not necessary, just helpful. In C like languages like Java and Javascript you don't need another syntax for it because a single statement doesn't need an extra set of braces, so you can use the two separate words else if and it will still balance out and be a valid statement.

                          1 Reply Last reply Reply Quote 2
                          • Jonboy
                            Jonboy Fuze Team last edited by

                            I can't see IF statements changing but we will be adding SWITCH for sure.

                            Discostew 1 Reply Last reply Reply Quote 2
                            • Discostew
                              Discostew F @Jonboy last edited by

                              @Jonboy Switches in my Switch? Switchception!

                              1 Reply Last reply Reply Quote 1
                              • Jonboy
                                Jonboy Fuze Team last edited by

                                Just wait till the XBOX version!

                                Discostew 1 Reply Last reply Reply Quote 1
                                • Discostew
                                  Discostew F @Jonboy last edited by

                                  @Jonboy said in Is it me, or are if statements messy?:

                                  Just wait till the XBOX version!

                                  Wait, wha?

                                  1 Reply Last reply Reply Quote 0
                                  • _
                                    _JKDOS F @Nisse5 last edited by

                                    @Nisse5 said in Is it me, or are if statements messy?:

                                    @_JKDOS FWIW, there's no need for using "else" in the example, since none of these are in conflict (unless "abc" is changed within the "if" block). But if you want it structured like a case statement, you can do:

                                    That is really neat.

                                    1 Reply Last reply Reply Quote 0
                                    • PB____
                                      PB____ last edited by PB____

                                      I also feel like if statements have room for improvement. I've expressed some ideas below. Even though it might not 100% align with the ideas of the Fuze team, I hope my chain of thought is useful anyway:

                                      inline if statements IIF
                                      If the arguments to a function call are conditional, I currently do not have many options. Basically I see two patterns:

                                      pattern 1:

                                         value = 1
                                         if condition then
                                            value = 2
                                         endif
                                         stuff(value)
                                      

                                      pattern 2:

                                         if condition then
                                            stuff(2)
                                         else
                                            stuff(1)
                                         endif
                                      

                                      I would however like to use the inline if statement pattern (from other languages: stuff(condition ? 2 : 1)).
                                      In F4NS I'd think a syntax like this would work:

                                         stuff(IIF condition THEN 2 ELSE 1 ENDIIF)
                                      

                                      IIF standing for 'inline if', and ending with ENDIIF in stead of ENDIF.

                                      Fundamentally the difference is that an IIF must always decide on a statement that returns a value. Opposed to IF that executes conditionally.

                                      keyword to prevent nesting ELSEIF

                                      ELSEIF could easily be a keyword, that helps reduce the amount of nesting for correlated if statements (keeping the cognitive load of the code lower). This way only one ENDIF keyword would be needed for correlated conditions. It would be possible to follow up IF with multiple ELSEIF's and even one optional ELSE after those (ELSEIF would not be possible after ELSE).

                                      For example:

                                         string value
                                         IF i < 0 THEN
                                             value = "negative"
                                         ELSEIF i > 0 THEN
                                             value = "positive"
                                         ELSE
                                             value = "neutral"
                                         ENDIF // using ELSEIF would not require an additional endif
                                         stuff(value)
                                      

                                      combining the two keywords
                                      The following code could be reduced if we were allowed to combine those two keywords. Currently in F4NS you would write something like this:

                                         string value
                                         if i < 0 then
                                             value = "negative"
                                         else if i > 0 
                                             then 
                                                 value = "positive"
                                             else
                                                 value = "neutral"
                                             endif
                                         endif
                                         stuff(value)
                                      

                                      A possible option would be to introduce ELSEIIF (as an inline ELSEIF):

                                         stuff( IIF i < 0 THEN "negative" ELSEIIF i > 0 THEN "positive" ELSE "neutral" ENDIIF)
                                      

                                      Or otherwise IIF could be nested like so:

                                         stuff(
                                            IIF i < 0 THEN
                                               "negative" 
                                            ELSE IIF i > 0 
                                               THEN 
                                                  "positive" 
                                               ELSE 
                                                  "neutral" 
                                               ENDIIF 
                                            ENDIIF
                                         )
                                      

                                      Type agnostic short circuiting

                                      I guess you could name the following pattern "type agnostic short circuiting". It can be addictive, and this would work in JavaScript, but for F4NS this would probably be a bad idea:

                                         stuff( (i < 0 && "negative") || ( i > 0 && "positive") || "neutral")
                                      

                                      I know F4NS has the AND and OR keyword, but in my opinion it's more intuitive if those keywords would always resolve in a boolean value, where as && and || would then be able to resolve to other types (as shown in the example above).

                                      1 Reply Last reply Reply Quote 0
                                      • First post
                                        Last post