Win a copy of OCP Oracle Certified Professional Java SE 11 Programmer I Study Guide: Exam 1Z0-815 this week in the Programmer Certification forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Junilu Lacar
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • Devaka Cooray
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Ron McLeod
  • Carey Brown
Bartenders:
  • Paweł Baczyński
  • Piet Souris
  • Vijitha Kumara

java.lang.StackOverflowError caused by recursion

 
Master Rancher
Posts: 3399
33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Strong agreement with Stephan and Junilu here.  Of course it's appropriate to return 0 for the sum of no numbers, or 1 for the product of no numbers.  These are inherent in the definitions of sum and product, as far as I can see.  Think: how would we write this code without recursion?



As Junilu says, we can always put in custom checks for isEmpty() if we want.  Just as we can put in custom checks for negative numbers, or for exceeding someone's credit balance - if we need to, for a given use case.  But as far as the straightforward implementations of sum and product, 0 and 1 are the only things that make sense, and throwing an exception would just complicate the life of someone who wants the obvious correct behavior.
 
Marshal
Posts: 7271
492
Mac OS X VI Editor BSD Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That first variant just had written in my IDE before saw your post. I think I have no other arguments.

And no, I don't regret killing a dead horse once again. Took something out from it.
 
Marshal
Posts: 14373
239
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But then again, Optional can open up the possibility for some nice alternatives:
 
Liutauras Vilda
Marshal
Posts: 7271
492
Mac OS X VI Editor BSD Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just for the record, just tried few other languages how they implemented their built-in sum functions: Scala, Kotlin, Racket, Haskell they all return 0 for an empty array or list.

hats off guys
 
Mike Simmons
Master Rancher
Posts: 3399
33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Liutauras Vilda wrote:Just for the record, just tried few other languages how they implemented their built-in sum functions: Scala, Kotlin, Racket, Haskell they all return 0 for an empty array or list.


Not to mention Java
 
Liutauras Vilda
Marshal
Posts: 7271
492
Mac OS X VI Editor BSD Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Mike Simmons wrote:Not to mention Java


To be brutally honest, didn't even come up to mind to check that one
 
no wonder he is so sad, he hasn't seen this tiny ad:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!