Shop OBEX P1 Docs P2 Docs Learn Events
Indent sensitivity of Spin is dangerous! — Parallax Forums

Indent sensitivity of Spin is dangerous!

This bug has made me waste three days on debugging and ruined my reputation for at least two customers. Magically, the following source code

PRI GearCog()
  repeat
    repeat while gearStop
    GearLoop()

turned into this:

PRI GearCog()
  repeat
    repeat while gearStop
      GearLoop()

The first is correct. It waits until gearStop is equal to zero and repeatedly calls GearLoop() as long as gearStop is zero. The second calls GearLoop() as long as gearStop is NOT equal to zero which is the exact opposite. Of course, I double checked the code many times but always have overlooked the tiny difference, which is only human. But to my suprise both Total Commander and GIT also haven't noticed any difference! Unless you set a special flag the default is "ignore spaces".

And I swear I haven't done anything! So where did the change come from? I can only guess that I have used different editors to cut/pase some of the code back and forth. So tabs could be automatically converted to spaces or vice versa.

So be warned! If you use any editor other than Propeller Tool be sure to turn on the option "always convert tabs to spaces" if available. If there are only spaces in the source files there is no possibility for different interpretations of how many position ticks a tab is translated to.

Comments

  • Feel the pain. Similar thing cost a lot of time when putting together code examples from different IDE's. I try to stick to just one IDE per project now. I thought the situation had been improved since; for sure I'd be happy to find such a "tab to spaces" option-- and even better see it enabled by default :)

  • I think it was a mistake for Python (a language I love and work a lot with, and without problems) and other languages to accept anything but space. Tabs should be an error, or at least tabs and spaces mixed within one source file.

    OTOH I don't doubt the dreaded curly braces elusion of C/C++ has had similar bad effects. Heartbleed anybody?

  • In VS Code, the indent setting is always available on the bottom status bar. You can use "Convert Indentation to Spaces" from the command palette (ctrl+shift+p) to convert an entire file to spaces (also check out "trim trailing whitespace", which gets rid of extra spaces that PropTool sometimes leaves).

    Recent versions of flexspin will warn you when space/tab is mixed in an indent-sensitve context.

  • ManAtWorkManAtWork Posts: 2,088
    edited 2023-04-19 11:06

    I do not complain about languages being sensitive to single character typos. C is famous for that. I don't know how many times I stumbled across the "=" versus "==" trap. Sh*t happens. Any language which tends to use abbreviations is sensitive to this more or less. And we all are lazy to write, aren't we. But if you encounter a bug you normally look at the last thing that changed or has been freshly implemented. But it takes a long time until you look at the code that has already worked perfectly for years. "Spooky" automomous self-modification turning working code into buggy code is very irritative and should be avoided at all cost.

    I think throwing an error message if tabs and spaces are mixed in a single file is a good idea. If anyone responsible for editors reads this (@cgracey, @ersmith ...) please consider implementing this.

  • @Wuerfel_21 said:
    Recent versions of flexspin will warn you when space/tab is mixed in an indent-sensitve context.

  • __deets____deets__ Posts: 193
    edited 2023-04-19 12:22

    To be clear: this is a language feature. The compiler should refuse the input. Then it becomes irrelevant which editor and which settings within that editor you're using.

  • ElectrodudeElectrodude Posts: 1,627
    edited 2023-04-19 13:41

    @ManAtWork said:
    But to my suprise both Total Commander and GIT also haven't noticed any difference! Unless you set a special flag the default is "ignore spaces".

    That's certainly not the default in Git. Make sure you don't have some funny settings set in your ~/.gitconfig. You can also enable some extra whitespace checking features as described here.

    EDIT: Git doesn't seem to actually have any settings to make it ignore whitespace changes by default - only flags like -w and -b that you must pass in explicitly every single time, since this is a dangerous feature, as you've found out. Are you using some sort of GUI wrapper that's being a little too helpful?

  • Proptool does not suffer from this problem. It shows you if it is indented or not.

  • Yes. The last paragraph from the first post says so.

    ..If you use any editor other than Propeller Tool ...

  • For Mac and Linux users, @macca's Spin Tools IDE also shows indents nicely.

  • pik33pik33 Posts: 2,352
    edited 2023-04-20 07:42

    @ManAtWork said:
    This bug has made me waste three days on debugging and ruined my reputation for at least two customers. Magically, the following source code

    PRI GearCog()
      repeat
        repeat while gearStop
        GearLoop()
    

    turned into this:

    PRI GearCog()
      repeat
        repeat while gearStop
          GearLoop()
    

    The first is correct. It waits until gearStop is equal to zero and repeatedly calls GearLoop() as long as gearStop is zero. The second calls GearLoop() as long as gearStop is NOT equal to zero which is the exact opposite. Of course, I double checked the code many times but always have overlooked the tiny difference, which is only human. But to my suprise both Total Commander and GIT also haven't noticed any difference! Unless you set a special flag the default is "ignore spaces".

    And I swear I haven't done anything! So where did the change come from? I can only guess that I have used different editors to cut/pase some of the code back and forth. So tabs could be automatically converted to spaces or vice versa.

    So be warned! If you use any editor other than Propeller Tool be sure to turn on the option "always convert tabs to spaces" if available. If there are only spaces in the source files there is no possibility for different interpretations of how many position ticks a tab is translated to.

    I have this experience, too. Over 3000 lines of Spin code for controlling the robot and long (tens of) hours wasted to debug indent problems, several of them invisible because of tabs vs spaces.

    So when the FlexBasic became mature enough, I rewrote all this code into Basic. Several days of work, but it was worth it. The code is now much shorter, much more readable and much more serviceable.

    No more Spin in bigger projects. We have a very good Basic and several Cs.

Sign In or Register to comment.