• Post Reply Bookmark Topic Watch Topic
  • New Topic

When writing debug-friendly code affects readability.  RSS feed

 
Avor Nadal
Ranch Hand
Posts: 156
Java Netbeans IDE Postgres Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello:

For the last months I've been writing code that is easy to debug. By "easy to debug" I mean code with many temporal variables that allow me to check the state of the objects easily, without using some of the aids that the debuggers offer. I don't use them just to hold values from simple getters of course.

However, I've realized that this strategy is affecting the readability often. Although it may seem a contradiction. This is specially true when I create variables which type declaration is very long, such as those that use nested generics. For example: Map <ColumnOrder <FoundStudent>, Boolean> columnOrder = ...

Also many debuggers (or IDEs) come with a feature called "watch expressions". So in the majority of the cases I wouldn't need many of these extra variables. Obviously, if a method has external side-effects using this aid may be dangerous. But these are very specific and known cases in my applications.

In this scenario, if you were someone who were going to edit my code, What would be more important for you, readability or this debug aid? I'm thinking in changing my habits and using temporal variables for readability purposes only, in a restrained manner.

Thank you.
 
Tim Cooke
Marshal
Posts: 4048
239
Clojure IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Readability is very important and should be given the due care and attention it deserves.

It looks to me that you are using debugging as a tool to verify the code's behaviour. you should be using Tests to do this instead. If you had a good test framework with a good test suite then you would use those to tell the story of your code as opposed to expecting future readers of your code to have to debug it manually.
 
Avor Nadal
Ranch Hand
Posts: 156
Java Netbeans IDE Postgres Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tim Cooke: Thank you for your impressions. Really I don't debug very often. Only when something fails and I'm unable to find the cause after having checked the code several times. I believe that I've never used it as a replacement of tests. Although I admit that it may sound like that.

It's only that I've been preparing my code for the possibility that me or someone needs to debug any line, at any moment. But I believe that I've gone too far, and have taken this approach to an unrealistic point. Unrealistic because there are pieces of code that do very simple operations that unlikely will need debugging.

So I guess that I'll focus more on readability, as you suggest.


PS: By the way, I agree with your recommendation about test cases. My life would be happier if I used them, he he he. I still have to get used to that way of programming.
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 37507
548
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would use watch expressions to avoid the excessive temporary variables. When there are too many "helper aids", it is hard to figure out what is important in the code.
 
Avor Nadal
Ranch Hand
Posts: 156
Java Netbeans IDE Postgres Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeanne Boyarsky: That's just what I was feeling these days, that my code was becoming less concise :S . In some extreme cases I was spending more lines doing something complementary that doing the main operation.

I'll investigate what other aids the debugger offers. Sincerely I discovered the "watch expressions" feature only by a chance, after reading other people comments in another threads.
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I hardly *ever* have to use the debugger and here's what I think contributes to this:

1. I practice Test-Driven Development
2. This results in lots of unit tests
3. This also results in very small, concise methods with very readable code
4. The unit tests become my detailed design specifications, clearly showing and demonstrating the important aspects of the design
5. If something is wrong with the code, I look for a failing test. If there is no failing test, then I write one to show that there *is* something wrong with the code
6. Failing tests, short and focused methods, and readable, well-factored code makes it a lot easier to find bugs quickly.
 
Tim Cooke
Marshal
Posts: 4048
239
Clojure IntelliJ IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's my somewhat simplistic take on writing code with and without tests:

With Tests
"Dear Customer, here's my application that performs to your specification, and here's some evidential proof to back that up. That'll be two million dollars. Thanks"

Without Tests
"Dear Customer, here's my application that performs to your specification, I think, I haven't actually tested it, you'll just have to take my word for it. That'll be two million dollars. Thanks"

A bit tongue-in-cheek I know but essentially that's what you're saying to your customers. Without tests, you're just winging it, and that's not professional.
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Then there's the other way to view tests, as E. W. Djikstra said: "Program testing can be used to show the presence of bugs, but never to show their absence!"

When writing tests, it's useful to keep this in mind and ask yourself "What bug do I want this test to reveal when it fails?"
 
Campbell Ritchie
Marshal
Posts: 56570
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The problem about showing the absence of bugs is that it will need calculation of the weakest‑predicate effects of every method and how they affect the class invariants. That takes a long time, and for some things, like the termination problem, might never give an answer.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!