• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

Is this ok code?

 
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I was wondering if the code below is proper coding. I know it functions as how I want it*. However I'm not sure if this way off coding opens me up to nasty bugs. The book I'm currently using does not go into a lot of detail whether this is good or bad coding.
Thanks for the replies.



*Note: This is not the exact code as the exact code has dutch comments en dutch named variables. So this code could have some type errors in the variable names.
 
Stijn Rensen
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I know that there is an alternative for this code in the following:



And I feel that this is probably better coding. However I cannot grasp what risks I'm running in the first version. Where I do think it is easier for me to follow "the story of the code" in the first version.

Kind Regards,

Stijn
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your original code is OK, but it's a bit verbose. If you don't want to use the one line solution, then you could at least get rid of the local variable isLeapYear which isn't really necessary:

 
Stijn Rensen
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ah thank you!

That is a good one to take out of this.
 
Sheriff
Posts: 16767
281
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Stijn Rensen wrote:... I do think it is easier for me to follow "the story of the code"


Definitely good to think about the story your code wants to tell.

This one is a borderline case and really boils down to a judgement call by the programmer. If you were programming with a buddy or bunch of buddies, I would try to see which version other people preferred, then go with the general consensus, if there is one. In this case, I would actually prefer the more succinct version. I'd probably move all those end-of-line comments into the JavaDoc for the method.

My own preference would be:

If you're really concerned about bugs sneaking into code like this, then write a comprehensive set of unit tests.

Edit: For me, the story is already told well by the method name and the details are explained by the supporting annotations and examples in the JavaDoc.
 
Stijn Rensen
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Junilu for your comment. Yes there is a lot of merit to the more compressed version. And the more I think of it the more I get enticed to follow you in this one. Especially when moving the comments to the top of the method explaining the logic (liking the www reference btw).

Maybe it also comes down to some experience. Having a lack of that I'm more inclined to write things out more to be sure I will understand it later when reading it back. Though I should trust myself and others reading my code to understand some degree of abstraction in the actual code.
 
Junilu Lacar
Sheriff
Posts: 16767
281
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My rule of thumb is to make high-level code tell the story, let low-level code hide the details and the geeky explanations. There can, of course, be varying degrees of abstraction in between, especially if the logic is very complex/complicated. Experience does help but you know what they say about that: "Experience is what allows you to recognize a mistake when you make it again."
 
Stijn Rensen
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Junilu Lacar wrote:"Experience is what allows you to recognize a mistake when you make it again."



wow the wisdom spouts of this quote
 
WHAT is your favorite color? Blue, no yellow, ahhhhhhh! Tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic