Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Is this ok code?  RSS feed

 
Stijn Rensen
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • 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
  • 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
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16028
87
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
Ah thank you!

That is a good one to take out of this.
 
Junilu Lacar
Sheriff
Posts: 11152
160
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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
  • 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: 11152
160
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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
  • 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
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!