Page 67 - Programming the Photon Getting Started With the Internet of Things
P. 67

code  tidy  is  always  best—it  makes  decoding  bugs  quicker  and  allows  someone  else  to
        easily read your code if you collaborate with others.





        Indentation



        You can see in some of the example sketches that we always use some sort of indentation
        for the code in the left margin. Indentation is usually determined by the curly braces and
        provides some sort of hierarchy structure to the whole code. Using the following example

        we can see that we have void loop() as our top level and within this we have a small
        amount of code followed by another sublevel starting with if:


        Void loop()
        {
                   int count = 0;

                   count = ++;
                   if  (count == 10)
                              {

                                         count = 0;
                                         delay(1000);
                              }
        }


             If we added another if statement within the first, then we would simply increase the
        indentation by a further 1 or 2. To indent from the left margin, you can simply press TAB

        on the keyboard to move across. You might find this a bit tedious, but when reviewing
        your code later on it will become apparent how useful this is.





        Commenting Your Code


        Comments  in  your  code  is  text  that  the  compiler  does  not  read  and  simply  ignores.

        Comments  can  provide  additional  information  to  either  you,  as  the  programmer,  or
        someone who is reading your code. If your program has a lot of code divided into many
        sections, then you can also use comments as titles or headers; this can be useful when

        debugging code so you can easily find the section you need to edit or change. There are
        currently two forms of syntax in which you can write comments in your code:


               The single-line comment, and probably the most common one that you will use,
               starts with two backslashes together // until the end of the line. So you cannot insert
               a comment and then carry on with code on the same line, as the code will be ignored
               by the compiler because it thinks it is part of the comment.
   62   63   64   65   66   67   68   69   70   71   72