• Post Reply Bookmark Topic Watch Topic
  • New Topic

Tips for improvement?  RSS feed

 
Daniel Dagenhart
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys..

I have a question about a program I wrote for my Java class.  It runs fine as far as I can tell, but I know there are tons of ways to write code, but I want to see if i could have wrote this code more efficiently.
I am turning in what I wrote because that is my work that I did by myself with no help.  Any help you give me is just for learning.

One of my homework projects was to write a program that intakes 2 numbers and then will give the sum, the difference, the product, the average, the distance, max, min of both numbers.

Now remember this is the simplest of Java for me, like kindergarten stuff.  My college professor is a very old guy and just talks about for loops for the entire last semester, and this semester is talking about strings.. So i am pretty much teaching myself from you tube tutorials and a Java book.
Any input on my code would be great to help me see it in another way.

 
Tobias Bachert
Ranch Hand
Posts: 86
18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A few things:
Class names should start with an uppercase letter (and your class should be renamed, as there isn't a single int in your code )
The variable names for the two input-doubles could be clearer (they are called sum but aren't the sum of anything)
You don't have to import anything of the java.lang.* package -> import java.lang.Math; can be removed
Your comparison for the smaller/bigger value is a bit too verbose in my opinion
(-> the isSmaller output could be added to the corresponding blocks of the isBigger output, alternatively System.out.println("The bigger of both numbers is " + Math.max(suma, sumb)); and the same with min for the smaller value)

[You are solving currently everything in the main method, normally you should try to abstract problems and move them in own methods/classes (but would be a bit overkill for this task as the code fits currently in ~11 lines)]
Personal opinion: I prefer to have the initialisation in the same line instead of having a line break inbetween (for sum, diff, prod and avg); moreover I would not store any of these calculated values in locale variables as they are only required once (or twice) and not expensive to calculate

The step after these changes would be: What, if you suddenly have to print the sum, average, max./min.-value for three (four, five, ...) values -> you could rewrite it in a way, that the count of numbers is easily changeable.
 
Junilu Lacar
Sheriff
Posts: 11486
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As a beginner, you should focus on clarity and organization of your programs, not so much on efficiency. Premature optimization is root of all evil.  A well-written program is easier to analyze and make efficient than a messy, buggy program.

The first thing that stands out to me about your program is your poor choice of names. Names should reflect the purpose/intent of the ideas that they represent.  Take your class name for instance.  Does the name IntSum reflect what the program is about or is it actually misleading? When you look at the program, you're actually using doubles, not ints. And you're not just calculating the sum but also the result of a number of other arithmetic operations.  What might be a better name for your class that gives the reader a better sense for what it does?

The same kind of issue exists for the suma and sumb variables names.

There's a book by Robert C. Martin, aka Uncle Bob, called Clean Code.  This is a very good book and all programmers should read it. Chapter 2 is about names. There's a blog article that goes over the main points of that chapter. Study these and incorporate them into your practice. It will make you a much better programmer.

 
Daniel Dagenhart
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the input..
TO answer one of both your questions is why is the class named intsum?

When i first starting writing this code, I did have it in all Int's but after research i read I would get an error if the number that was returned had a decimal in it.. So i switched to double. Also at the time I was just going to work on the sum. The problem in the book is near the bottom of the page, and it said to enter 2 values and have it print sum.. I didnt know at the time the problem continued onto the next page, then I seen it wanted all of the other stuff.
As for math.max and min..  I had no idea those existed. Like I said, I am extremely new to java. Even though I have a semester under my belt, all we did was focus on for loops, loops inside of loops, and the understanding of how many times the loops would run, and the value of an Int at the end of the program..
The professor did not teach us any syntax at all.  I didnt even know how to start off a basic java code until this semester.
Even then, I wouldnt know how to change anything if I had to do another kind of project. 
I found some good youtube videos from Nathan Schutz, who explains some basic and some stuff that I can understand.
I will see if I can get that clean code book from amazon and download to my kindle/computer.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
damon greenfield wrote:When i first starting writing this code, I did have it in all Int's but after research i read I would get an error if the number that was returned had a decimal in it.. So i switched to double.

You should re-read Tobias' post, and get into the habit of writing things down correctly.

1. There's no such thing as an 'Int' in Java. There is, however, an 'int' ... and an 'Integer'.
2. Just to remind you again - class names should begin with a CAPITAL letter, so yours should be 'Intsum' - or probably even better: 'IntSum'.

It may sound like nitpicking, but accuracy is very important in programming. Even though compilers are very good these days, they can't read your mind - ie, they have no idea what you intended to write when you get it wrong; all they can tell you is that something is wrong.

As to the rest of your post: you might want to have a look at this thread, which seems to cover much of the same ground.

HIH

Winston
 
Liutauras Vilda
Sheriff
Posts: 4917
334
BSD
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Indentation and formatting are equally important as other issues mentioned earlier by other Ranch members.

1. There should be an empty line between lines 2 and 3.
2. Your indentation is inconsistent. i.e.: line 6 vs line 28.
3. When you write if statement, before you place opening parentheses you should add space character, so it would read "if (something)" rather than "if(something)".
4. On line 5, main method, where you specify parameter and its type, you should place array type identifiers right after the data type, not after the variable. In case there would be variable arguments instead, you'd get compiler error in the way you did now.
5. '=' sign should be surrounded by space characters in all cases, same as '+', '-', '*', '/', '%'...
6. Either line 20 or line 21 needs to be removed, it makes formatting inconsistent too.
7. There is a lack of methods in your program too. Have you been taught about it already?
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am afraid that what strikes me about that code is that it doesn't seem to contain any objects. Java® is an object language, so you shou‍ld be thinking in objects.
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Getting the number of iterations of a loop wrong can cause serious problems.
 
Carey Brown
Saloon Keeper
Posts: 3315
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Liutauras Vilda wrote:3. When you write if statement, before you place opening parentheses you should add space character, so it would read "if (something)" rather than "if(something)".

Some of your suggestions are good but here I feel you are wading into religious-war territory. Most firms I've consulted for required a space after opening parens and again before the closing parens making the space after an 'if' unnecessary. But other than dictated style this is a personal preference and not a "should".
 
Aline Galea
Greenhorn
Posts: 22
2
IntelliJ IDE Java Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
damon greenfield wrote:The professor did not teach us any syntax at all.  I didnt even know how to start off a basic java code until this semester.
Even then, I wouldnt know how to change anything if I had to do another kind of project. 
I found some good youtube videos from Nathan Schutz, who explains some basic and some stuff that I can understand.
I will see if I can get that clean code book from amazon and download to my kindle/computer.
Hi, I'm fairly new to java too and I know what you mean. All I learned at university was a few hours of theory on algorithms, with no practice in programming whatsoever.
I learn java in my own, mainly from this online course from the university of Helsinki: http://mooc.fi/courses/2013/programming-part-1/
It's free, fairly easy to understand, and more importantly comes with more than a hundred exercises that you can download and then let their server test automatically for you (and once you're done with those, you can start part 2 and solve a hundred more). After solving an exercise, you get access to a model solution that you can compare your own code with (and spot maybe a few things that you could have done in a simpler way). It's the easiest way to practice java basics that I've found so far.
 
Junilu Lacar
Sheriff
Posts: 11486
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Carey Brown wrote:But other than dictated style this is a personal preference and not a "should".

I disagree! An if should always be followed by a space. Let the space and bracket wars begin! (tongue firmly planted in cheek) 
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Junilu Lacar wrote:I disagree! An if should always be followed by a space. Let the space and bracket wars begin!...

I have to admit, I tend to agree; and my reason is that it highlights the fact that 'if' is a Java construct, and not a method call.

But then, I'm very fond of factory methods called 'of()' (especially with Enums), and you also run into the corollary - you shouldn't put spaces after method names.

Phew...coding ain't easy.

Winston
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!