Win a copy of Serverless Applications with Node.js this week in the NodeJS 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
  • Bear Bibeault
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Junilu Lacar
  • Paul Clapham
  • Knute Snortum
Saloon Keepers:
  • Stephan van Hulst
  • Ron McLeod
  • Tim Moores
  • salvin francis
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Vijitha Kumara

Candy Bars Program  RSS feed

 
Greenhorn
Posts: 28
Eclipse IDE Java Redhat
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello, ranchers!

I was reading through this thread and thought it might be fun to try to solve it myself. Here's the code I came up with:


Your comments or questions go here
 
Marshal
Posts: 63843
209
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Looks good, but . . . .
Most of that code should be in a different method from the main method. Don't close your Scanner; simply let it go out of scope. Scanners reading everything other than System.in do need to be closed, which is best done with try with resources.
 
Marshal
Posts: 6634
443
BSD Linux Mac OS X VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Steven, formatting and indentation of the code is exemplary. Well done on that!
 
Steven Fraser
Greenhorn
Posts: 28
Eclipse IDE Java Redhat
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mr. Ritchie,
I see the comment "Most of that code should be in a different method from the main method" a lot. As a new beginner, I kind of see where one should desire the "main" to be just the entry point or ignition for the rest of the program. Does this apply regardless of the amount of code? Perhaps a best practice no matter what?
I was thinking what I would do different in this instance and perhaps I would create a class CandyBarCartons and then move 98% of what is in there to that class. I would then call that from Main? I will try to come up with a real instance and repost it later... hopefully today.
Thank you for the feedback.
I am ready and willing to take all the feedback. Really love Java even though I am not a programmer by trade.
Steve
 
Campbell Ritchie
Marshal
Posts: 63843
209
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, a short main() method would be best practice whatever else happens; you would write. . . and really feeling like Mr Belt‑and‑Braces, I have given that class a private constructor for reasons explained in the Java® Language Specification (=JLS).You obviously know what you are doing, so I'll let you work out for yourself what line 8 means. By writing that sort of code, you are getting used to creating objects, and that is what object‑oriented programming is all about
 
Steven Fraser
Greenhorn
Posts: 28
Eclipse IDE Java Redhat
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"You obviously know what you are doing"  Scares the junk out of me, because I don't think I do.
As life would have it, I was given the gift of tenacity, so I will certainly give it a whirl. Thank you again for your support!
Steve
 
Sheriff
Posts: 13391
221
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There are many ways to solve that problem. Your solution is pretty good, considering your experience.

Here's your code with a few tweaks (not getting into OO yet):

Read this code and compare with what you wrote. Do you think this code is more expressive? Do you see the difference in how it's organized?
 
Bartender
Posts: 690
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You should also add ode to handle the possibility that the user types something other than an int and nextInt() fails.
 
Junilu Lacar
Sheriff
Posts: 13391
221
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The code I suggested before,

could also be written as:

This code is clearer than the previous version and only requires a few seconds more of refactoring. Many people question me about code like this, particularly whether or not it's "too much". I don't think it's too much. Anyone concerned with saving a few milliseconds overhead managing the call stack is worrying about the wrong thing. The amount of time a person reading the code spends trying to understand naked formulas, e.g. (hasExtra(candyBars)) ? 1 : 0 instead of boxForExtra(candyBars) far exceeds the time the computer would spend making another method call.

Generally, I prefer saving people some effort in reading my code over trying to save a few milliseconds by avoiding a method call. In fact, there are articles out there that demonstrate that refactoring to make that extra method call might actually allow the compiler to generate faster bytecode.

Small private methods have a good chance of getting inlined by the Java JIT compiler so why should I preempt that at the cost of readability? If I write code like the above, readers of my code have it easier and I also make it easier for the compiler to make optimizations like inlining of one-line return statement methods like those in boxForExtra() and hasExtra().

Anyway, in a program like this, readability is a bigger concern than performance. If you're going to tune your code for performance, use a profiler, not your gut.

See when does the Java compiler optimize and inline methods?
 
Steven Fraser
Greenhorn
Posts: 28
Eclipse IDE Java Redhat
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My brain exploded. As soon as I am done with the clean up I will post something of value, or at least make the attempt. Love the assistance one can get here and the vast knowledge and sharing. Awesome!!
Cheers,
Steve
 
Junilu Lacar
Sheriff
Posts: 13391
221
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Steven Fraser wrote:My brain exploded.


What made it explode, the idea that you can move your code around and make it so it actually reads like a story?

This is what I find disappointing about how programming is taught these days. It seems like it's all about syntax. Nothing about problem solving or writing clear, expressive code. To me, programming is mostly about the latter; syntax is just a detail. If nobody can understand your program, it's not much better than useless. Code spends most of its lifetime being read, not written. When it does need to be changed, then code that is easy to read is far easier to change than code that's difficult to read and understand.

As Martin Fowler (the author of "Refactoring: Improving the Design of Existing Code") says, "Any fool can write code that a computer can understand. Good programmers write code that humans can understand."

 
Liutauras Vilda
Marshal
Posts: 6634
443
BSD Linux Mac OS X VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Junilu Lacar wrote:


I like very much everything else in your refactored version and how you approached it, apart from the boxForExtra(candyBars). In particular its name. This name somehow indirectly implies in my mind two things: [1] there are extras [2] there will be a guaranteed box. Looking to its implementation all doubts get cleared though.

Currently my mind would understand it better if method would read as:

So from this method name, I personally can better understand that might be needed an extra box or might not without looking to its implementation.

But maybe it just now I feel that way.
 
Steven Fraser
Greenhorn
Posts: 28
Eclipse IDE Java Redhat
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Liutauras Vilda,
Which would lead me to ask; when writing programs, how descriptive do we want to be? Should be?
So an end user would either have to know up front what this application does and its purpose.
Is that built in through the app itself? I know we are dealing with Candy Bars and Cartons needed
and in real life someone can just whip out a calculator.
However I could see something like this in a factory and probably on a much larger scale.
But do we say: This program will calculate how many Cartons need to be manufactured based
on the number of Candy Bars we will product today. Each Carton holds 24 however, every Candy
Bar MUST be in a carton.
Babbling and I apologize but do you understand what I am saying/asking?
Thank you ALL for your thoughts.
Steve
 
Junilu Lacar
Sheriff
Posts: 13391
221
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In the real world, code should be reviewed for clarity and feedback like Liu's should be considered. Then there would be a discussion and some experimentation. On critical functionality that would require fine-grained expressiveness that Liu suggested, so that people don't stumble over any kind of misunderstanding of the code, Liu's point would be well taken. In this context, not so much, at least for me. The name extraBoxAsPerNeed does not appeal to me as much and the distinction it has over boxForExtra isn't that much. However, I might experiment for a minute with similar names with like intent such as boxForAnyExtra(candybars). My goal is to make the code read like a casual conversation, which I find much more pleasing. "Plus an extra box as per need for candy bars" just isn't consistent with the way I talk in real life. However, I would say "plus a box for any extra candy bars" because to me that already implies that if there are in fact no extras, I won't need another box.

Unfortunately, there are very few programmers even in professional settings who care to spend even a minute considering these kind of things and they keep writing code that makes other people and themselves stumble and fumble over what the code they're working with means. This literally costs companies and society billions of dollars per year.
 
Junilu Lacar
Sheriff
Posts: 13391
221
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'd be ok with aBoxForAnyExtra(candybars) or aBoxForAnyLeftover(candybars). When reviewing code, have someone read it to you while you're not looking at the code. Our ears alone can give us a lot of good feedback about whether or not code is expressive and understandable.
 
Steven Fraser
Greenhorn
Posts: 28
Eclipse IDE Java Redhat
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This conversation and information is invaluable.
I remember working on a database project and we began to say; "There is never time to do it right, but there is always time to do it over."
So I agree with the point of the importance of doing it right and doing it in way that is understandable; for past, present and future.
Cheers,
Steve
 
Liutauras Vilda
Marshal
Posts: 6634
443
BSD Linux Mac OS X VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Steven Fraser wrote:Liutauras Vilda,
Which would lead me to ask; when writing programs, how descriptive do we want to be? Should be?


I think Junilu answered that way better than I could have. After all, he's one of those who made one of the biggest impacts for my career. As you see, I've made a remark about his refactored version, and all that lead even further, so even better version been suggested (read further for more). So if you think whether ask something or not - ask, remark, always - it pays back.

Junilu Lacar wrote:The name extraBoxAsPerNeed does not appeal to me as much and the distinction it has over boxForExtra isn't that much. However, I might experiment for a minute with similar names with like intent such as boxForAnyExtra(candybars). My goal is to make the code read like a casual conversation, which I find much more pleasing. "Plus an extra box as per need for candy bars" just isn't consistent with the way I talk in real life.


Again, I'm glad I joined the conversation. We always talk about the "better name", and sometimes indeed is hard to distinct what is better (apart from obvious). "...isn't consistent with the way I talk in real life" - I found this as a very good metric to consider. While I myself fine with my suggested version, I must admit I like yours boxForAnyExtra(candybars) way more. In the subsequent post suggested names also great.

So I think if the lately suggested method name(s) didn't make anyone feel worse about them, while at least one (myself) person found such method name boxForAnyExtra(candybars) more readable, I think that qualifies as - "more readable code" as odds increased.

 
A wop bop a lu bop a womp bam boom! Tiny ad:
global solutions you can do at home or in your backyard
https://www.kickstarter.com/projects/paulwheaton/better-world-boo
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!