• Post Reply Bookmark Topic Watch Topic
  • New Topic

Searching through lines of code  RSS feed

 
Tim Mullenbach
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you were given a problem to search for an error in a section of code but it is incredibly long, what are some ways you can go through the lines of code quickly?
 
Paul Clapham
Sheriff
Posts: 22817
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Tim, welcome to the Ranch!

To me it would depend on what the error was. For example if it was an error which would prevent the code from compiling, then I would just compile the code and see what the compiler had to say. Otherwise, there are lots of other possibilities for errors. Did you have something specific in mind?
 
Campbell Ritchie
Marshal
Posts: 56518
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

I would start by asking what the error is. Unless you know what you are looking for, it is very difficult to find errors.
 
Tim Mullenbach
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What if its an error that doesn't cause compiling issues but is still an error?
 
Junilu Lacar
Sheriff
Posts: 11476
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just out of curiosity, how long is "long"? 100+ lines, 1000+ lines, 10K+ lines?
 
Tim Mullenbach
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Say a thousand lines or so. Something that would take too long for a person to scan each line for errors. Would you do some kind of testing?
 
Paul Clapham
Sheriff
Posts: 22817
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Really, if somebody told me there was an unspecified error there, but nothing else besides that, I would do something unspecified to find it.
 
Tim Mullenbach
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How can you best answer a question like this?

"How do you find an error in a large file with code that you cannot step through?"
 
Ben Ooms
Ranch Hand
Posts: 47
Debian Eclipse IDE Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The answer is very simple, it depends on the type of error.

Compile error: read the error thrown by the compiler.
Runtime error: follow the stacktrace
functional error: you have to read and check the code logic against the intended behavior
 
Campbell Ritchie
Marshal
Posts: 56518
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ben Ooms wrote:The answer is very simple, it depends on the type of error.

Compile error: read the error thrown by the compiler.
Easy-peasy
Runtime error: follow the stacktrace
Easy-peasy. But how do you know the Exception will be thrown; some Exceptions only occur under certain circumstances?
functional error: you have to read and check the code logic against the intended behavior
Bl**d* difficult that will take ages to do.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12562
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Spelling error - use the "search and replace" function
 
Junilu Lacar
Sheriff
Posts: 11476
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:
functional error: you have to read and check the code logic against the intended behavior
Bl**d* difficult that will take ages to do.

It is the bane of a programmer's life.

Robert Martin hints at a way to break the problem down a bit: Look at the code's geography. What's that? First, format the code properly if it isn't already formatted according to some standard convention. Many IDEs will do that for you with a single command. Then look at the left margin. If you tilt your head to the left and scroll up and down, you'll see the code's "jagged horizon". There will be mountains and valleys, plains, plateaus, hills and vales, glens and gullies, etc. Hopefully, there will be some clear demarcations between features in the form of whitespace. Take each major (or not) geographical feature as a possible grouping of code that takes on one of the many responsibilities that the ginormous method has. As you go through the code and begin to understand, take every opportunity you can to either document each section you have already analyzed or extract it to another method. This way you can start breaking down the task of going through a thousand lines of code into smaller chunks.

It's still going to be tough work but sooner or later someone has to do it. Just imagine yourself as an explorer who has set out to map out an unknown continent for others who will follow. This will be your Lewis & Clark expedition.
 
Campbell Ritchie
Marshal
Posts: 56518
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Junilu Lacar wrote: . . . This will be your Lewis & Clark expedition.
As long as it isn't like Capt Scott's expedition.
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:
Junilu Lacar wrote: . . . This will be your Lewis & Clark expedition.
As long as it isn't like Capt Scott's expedition.

"I'm going to look at some code...I may be some time"
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!