How to find the bracket pairing in long sketch?

Hi all.
I don't know what kind of question here is.
met error occasionally when compiling: 'xxx' was not declared in this scope. and found the reason is just missing a brackets after a long time check.

any easy way to do?
Thanks
Adam

Ctrl T

1 Like

Hi @shanren. In addition to madmark2150's suggestion of doing a Tools > Auto Format and then using the resulting indentation as a visual indicator of the bracket structure of your sketch, a couple of other useful tools are available for this use case:

  • The "Go to Bracket" keyboard shortcut (Ctrl+Shift+\ for Arduino IDE 2.x users, Ctrl+[ for IDE 1.x) will move the cursor to the opening or closing bracket of the block.
  • Fold the block by clicking the "˅" icon ("-" icon for IDE 1.x users) in the left margin of the editor panel at the opening of the block.
2 Likes

yo @shanren

Along with the advices presented so far, I would ask you to start learning how to write code in such a manner that a closing brace is never really that far away from an opening brace.

Aim for most of the time being able to see both the open brace and the closing brace of whatever it is you are embracing.

Usually possible. If not possible, it may be a sign of something. :expressionless:

a7

5 Likes

Always put each curly bracket on its own line.

void aFunction()
{
      // statements
}

It makes blocks of code more apparent and mismatches stand out more.

As mentioned, use auto format often.

In the IDE, if you place the cursor to the right of a curly bracket or parenthesis the matching curly bracket or parenthesis will be indicated.

6 Likes

Agreed. A good programming style required writing a short functions and methods. As a rule, the every block of code won't be longer than one screen in used IDE.

3 Likes

What works for me is i copy the entire code, then open a new sketch. then paste it.
i then start in the middle identifying where blocks start and finish in the void setup
deleting the code from curly bracket to close, eventually getting to

void setup
{
}

void loop
{
}

This also works

1 Like

I'm curious, how is that possible ?

Please smell my code and assess my code smell

void loop() { 
    delay(100);

    for (int i = 0; i < 16; i = i + 1) {

      if(i - 6 >= 0) {
        leds[i - 6] = CRGB::White; //Original (32, 128, 20)
        leds[10] = CRGB::Black;
        leds[11] = CRGB::Black;
        leds[12] = CRGB::Black;
        leds[13] = CRGB::Black;
        leds[14] = CRGB::Black;
        leds[15] = CRGB::Black;
      }
      if(i - 5 >= 0) {
        leds[i - 5] = CRGB::White; //Original (32, 128, 40)
        leds[10] = CRGB::Black;
        leds[11] = CRGB::Black;
        leds[12] = CRGB::Black;
        leds[13] = CRGB::Black;
        leds[14] = CRGB::Black;
        leds[15] = CRGB::Black;
      }
      if(i - 4 >= 0) {
        leds[i - 4] = CRGB::White; //Original (32, 128, 70)
        leds[10] = CRGB::Black;
        leds[11] = CRGB::Black;
        leds[12] = CRGB::Black;
        leds[13] = CRGB::Black;
        leds[14] = CRGB::Black;
        leds[15] = CRGB::Black;
      }
      if(i - 3 >= 0) {
        leds[i - 3] = CRGB::White;
        leds[10] = CRGB::Black;
        leds[11] = CRGB::Black;
        leds[12] = CRGB::Black;
        leds[13] = CRGB::Black;
        leds[14] = CRGB::Black;
        leds[15] = CRGB::Black;
      }
      if(i - 2 >= 0) {
        leds[i - 2] = CRGB::White; //Original (32, 128, 70)
        leds[10] = CRGB::Black;
        leds[11] = CRGB::Black;
        leds[12] = CRGB::Black;
        leds[13] = CRGB::Black;
        leds[14] = CRGB::Black;
        leds[15] = CRGB::Black;
      }
      if(i - 1 >= 0) {
        leds[i - 1] = CRGB::White; //Original (32, 128, 40)
        leds[10] = CRGB::Black;
        leds[11] = CRGB::Black;
        leds[12] = CRGB::Black;
        leds[13] = CRGB::Black;
        leds[14] = CRGB::Black;
        leds[15] = CRGB::Black;
      }
        leds[i] = CRGB::White; // Originally (32, 128, 40)
        leds[10] = CRGB::Black;
        leds[11] = CRGB::Black;
        leds[12] = CRGB::Black;
        leds[13] = CRGB::Black;
        leds[14] = CRGB::Black;
        leds[15] = CRGB::Black;

      FastLED.show(); // SHOW THE LEDS
      delay(DelaySpeed1);
      FastLED.clear();
      FastLED.show();

    } 
  }

I understand how it works with something like this

if(i - 6 >= 0) {
        leds[i - 6] = CRGB::White; //Original (32, 128, 20)
        leds[10] = CRGB::Black;
        leds[11] = CRGB::Black;
        leds[12] = CRGB::Black;
        leds[13] = CRGB::Black;
        leds[14] = CRGB::Black;
        leds[15] = CRGB::Black;
      }

But ultimately you cant keep this on the same screen

void setup() { 
}

and even if we forgive that and take it as a "given" how would you keep this on the same screen

for (int i = 0; i < 16; i = i + 1) {

} // where this close is way down?
1 Like

I might be misding the point of the question?

Put the block in a function and pass i-n as an argument.

1 Like
void loop() { 
    delay(100);

    for (int i = 0; i < 16; i = i + 1) {
      int j =0;
      while ((i - j >= 0) && (j<7))  {
         leds[i - j] = CRGB::White; 
         j++;
      }
      leds[10] = CRGB::Black;
      leds[11] = CRGB::Black;
      leds[12] = CRGB::Black;
      leds[13] = CRGB::Black;
      leds[14] = CRGB::Black;
      leds[15] = CRGB::Black;

      FastLED.show(); // SHOW THE LEDS
      delay(DelaySpeed1);
      FastLED.clear();
      FastLED.show();

    } 
  }
1 Like

Your code contains a lot of repetition and this immediately indicates that this is not a good style.

It can be more shortened after the variant of post #11:

void loop() { 
    delay(100);

    for (int i = 0; i < 16; i = i + 1) {
      int j =0;
      while ((i - j >= 0) && (j<7))  {
         leds[i - j] = CRGB::White; 
         j++;
      }
      
      for (j=10; j < 16; j++) leds[j] = CRGB::Black;
      
      FastLED.show(); // SHOW THE LEDS
      delay(DelaySpeed1);
      FastLED.clear();
      FastLED.show();

    } 
  }
2 Likes

... and here begins the Holy Brace Placement War ...

:scream: :military_helmet: :crossed_swords: :bomb:

(disclosure: yes, I am on the side of the "brace always on its own line" - for the reason stated)

7 Likes

Indeed it does , and that's why i asked you , i suspected you had a trick up your sleeve.
So.. my code stinks then, ok.. i'll incorporate this
Thanks

1 Like

I now feel like Michael in "Underworld" when Selene tells him
"You are now part of a curly bracket blood war that has been raging for centuries" :stuck_out_tongue:

2 Likes

One may set autoformat to put curly brackets on their own lines. https://www.instructables.com/Arduino-Auto-Formatting-Listings/

1 Like

Note that the article is for IDEs before IDE 2.x :wink:

2 Likes

I never use 2.0.

Here is instruction for 2.0.

1 Like

Perhaps more important than consistently using the style that one person or another recommends, there will always be differences of opinion, is to…

pick one and consistently use it and…

read enough code so that you can handle any reasonable defensible style.

I do gotta say, however, that coming out of the box

void loop() {

}

is just wrong. :expressionless:

I wrote my own new sketch default the day I discovered it was possible how.

I like to use as little ink as possible and am generous past, maybe, a fault with white space.

a7

2 Likes

many of today's editors can match things like braces or parenthesis and some will even starting warning when there's a mismatch.

the Arduino IDE supports the use of external editor (see preferences) to disable changing the file thru the IDE.

of course the use of editors is religious. i've continued to use vi/vim over my career and many of the tools i've used over the years (e.g. git) use it or its command. it's features have evolved over time (e.g. split screens, ctags, which evolved from cscope)

2 Likes

assumed this is referring to the braces or is it the loop*)

some companies (especially one that developed most of the software tools we take for granted today) had a policy that if the file couldn't fit within 2 screens (40x80), the code was split into separate files. one rational for doing such things is it reduces the chances of making an unintentional change in a different part of the code. (one reason for the development of diff).

with limitations on screen size, there are practices that help condense the code onto fewer lines so that more of it is visible on one screen, such as opening braces on a single line

a similar practice is to limit line lengths to 80 chars which would be a limit for code printed on sheets of paper, again with the goal of condensing the code into a smaller area

some might note that verbose variable names may make this difficult to achieve

may be worth looking at the coding guidelines for Android

but good practice is always to maintain whatever style an existing file has. many organizations used files from other organizations.

one further comment, i reformat all the code i get from this forum using a script which formats it in a style i'm comfortable with and which easily identifies missing braces because of improper indentations

1 Like