• Linux
  • January 2019
    M T W T F S S
     123456
    78910111213
    14151617181920
    21222324252627
    28293031  
  • Meta

Looking at FnLoC Code

This afternoon I took a look at my FnLoC source code and saw a couple of lines of code that I thought I could modify a bit.

While the program is parsing a line of source code, it goes through the line character by character, changing the state of the line as it goes. I use a switch statement that looks at every possible state and potentially changes the state based on the state set by the previous character.

One of the possible states is NewLineNC. It’s a transition state that’s usually entered when a newline character is reached from certain states. I’m not exactly sure of its purpose anymore though I’m sure I knew when I wrote the original program back in 1998. I really no longer fully understand how the processes work but I’ve figured out some of it as I’ve been working on it over the past few months.

When I first entered the source code (after 20 years), I got an error or warning from the compiler about NewLineNC state not being among the possible cases. It’s not a state that can be entered until the very end of a line so there’s no function to process it. To satisfy the compiler, I added a case for it that doesn’t do anything.


     switch (state)
     {
        case (NewLineNC) :
            break;
     }

Later in the program, after the line has been parsed, I have a conditional statement that check to see if the final state of the line is NewLineNC. If it is, the state is changed to NewLine which is the initial state of a new line of code.


     if ( state == NewLineNC )
            state = NewLine;

I wondered it I could change the state from NewLineNC to NewLine in the switch case and eliminate the if statement so I gave it a try. After compiling the code, I ran it against my source code file and compared it to the previous LOC information. I immediately noticed a considerable difference.

The total LOC count increased from 245 lines to 276 lines. Counts for several functions increased by one line. The main() function’s count increased by 6 lines and lines of code outside of functions (declarations and compiler directives), increased from 5 lines to 24 lines.

I looked at the code and physically counted the logical lines in several functions, verifying the counts before the changes. I wasn’t able to identify what lines had been added to the count but it was apparent that with the change, the line state was being changed from NewLineNC to NewLine when it shouldn’t have, thus adding lines of code that weren’t really code such as comments. It became quite apparent that the place to change between those states was after the entire line had been processed.

NewLineNC seems to be a possible final state for a processed line while NewLine is an initial state for the line. As I said earlier, I probably understood the code a lot better when I originally wrote it. I’m pretty sure that I had manually parsed many lines of code to determine all of the potential states that could occur in a possible line of code. Since rediscovering the code, I’ve been slowly relearning it and figuring it out.

I undid the changes, leaving it the way it was. I considered adding more comments to help clarify some sections of the code but decided against it. I didn’t really want to recompile it and make a new deb package. I’d do that if there were actual changes to the code itself.