• Post Reply Bookmark Topic Watch Topic
  • New Topic

Code check for a newb  RSS feed

 
Neil Van Styn
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

New to Programming and have always wanted to learn Java. So I've taken up Barry Burds "Java for Dummies" and am about half way through it. Loving it so far.

I just wanted to ask someone if I am on the right track though. I wrote the below just as a silly little random victory thing to test out a few things I've learnt so far. I'd like to know if its a good way to set it out? It complies and works fine etc. My concern in the way I've written it revolves around the immense use of the "else if" statements to work out who the winner is. I guess I'm wondering if there is a more efficient way of going about it. Thanks!

=D

 
Michael Gomez
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Instead of storing each character individually in their own variable I would use an array.
Then I would sort the character array from highest to lowest, there's quite a few algorithms available for sorting.
The main problem with your approach is that it will not scale very well.
What would you do if you had 20 characters? Would you write 20 else if clauses?
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66304
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Variable names should start with lowercase. Alos avoid using variable names that are the same as other items; in this case the enum values.
 
Anayonkar Shivalkar
Bartender
Posts: 1558
5
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Neil,

Welcome to CodeRanch!

Good to see code tags in your first post itself.

If your assignment is not specific to writing your own sorting logic, then you may take advantage of ready-made APIs provided by Java (hint: how about Collections?)

I hope this helps.
 
Neil Van Styn
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Michael Romero wrote:Instead of storing each character individually in their own variable I would use an array.
Then I would sort the character array from highest to lowest, there's quite a few algorithms available for sorting.
The main problem with your approach is that it will not scale very well.
What would you do if you had 20 characters? Would you write 20 else if clauses?


I see so there is a way then, good. I thought my approach was a bit tedious. I haven't gotten to arrays and such yet, getting ahead of myself it seems. Looking forward to learning about that and revising the whole thing.

Bear Bibeault wrote:
Variable names should start with lowercase. Alos avoid using variable names that are the same as other items; in this case the enum values.


Noted. I'll take that on-board as it makes good sense.

Thanks guys.
 
Paul Clapham
Sheriff
Posts: 22819
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's also traditional to use ALL CAPS for enum names. So instead of



you would write



As you see your version compiles and runs perfectly well, so technically there's nothing wrong with it, but it does help to write code which other Java programmers will find a little bit easier to understand.
 
Neil Van Styn
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Anayonkar Shivalkar wrote:Hi Neil,

Welcome to CodeRanch!

Good to see code tags in your first post itself.

If your assignment is not specific to writing your own sorting logic, then you may take advantage of ready-made APIs provided by Java (hint: how about Collections?)

I hope this helps.


Bookmarked it, thanks. When I'm finished this book, I'll really work on taking advantage of APIs and such. Sounds like a good way to go.

Paul Clapham wrote:
As you see your version compiles and runs perfectly well, so technically there's nothing wrong with it, but it does help to write code which other Java programmers will find a little bit easier to understand.


Ah, cool I didn't pick up on that one. Will keep in mind, Cheers.
 
Campbell Ritchie
Marshal
Posts: 56520
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear Bibeault wrote: . . . avoid using variable names that are the same as other items; in this case the enum values.
I would go a lot farther than that; I would say that enum constants are full‑blown objets. Each can therefore have fields. So you can put those random numbers as fields of the enum constants, along with constructors and getXXX methods.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12562
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell let me down. Usually he is immediately on top of someone who puts too much code in their main method - which you have done.

much of this code should be broken down and split out into methods. Pretty much any time you see the same line of code over and over, you should think "hmmm....".

and as others have said, your code to determine the winner is horrible (sorry, there's no other word for it).
 
Neil Van Styn
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's okay, I can take it. If I'd been doing this for more than a week or so I'd probably take offence. I'm flailing around in the shallow end with regards to my knowledge on the subject, so any advice/ criticism is much appreciated. I'm just a sponge here to absorb everything.

I'm kind of glad my code sucks because if that was "good code" I probably wouldn't be interested in learning Java anymore. ;-)
 
Robert D. Smith
Ranch Hand
Posts: 221
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Neil Van Styn wrote:I'm kind of glad my code sucks because if that was "good code" I probably wouldn't be interested in learning Java anymore. ;-)
Actually, your code doesn't suck. It compiles, runs, doesn't crash, and does exactly what you want it too. It's good to get feedback on your code. i remember my first program that went through testing/critique. it was a simple login screen. How hard could that be to write, right? User name, password, click ok. By the time they (fellow programmers) got done beating on it -- let's just say that once I got over the humiliation I realised that I learned quite a lot.

You have received plenty of good suggestions on improvements, so I won't waste your time with that. Too much advice is like adding salt when cooking. I will add another link or two that you might add to your reading list. The first is to the Code Conventions for Java Programming from Sun. And the second is JavaRanch Style Guide.

Regards,
Robert
 
Neil Van Styn
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Robert D. Smith wrote:
I will add another link or two that you might add to your reading list. The first is to the Code Conventions for Java Programming from Sun. And the second is JavaRanch Style Guide.

Regards,
Robert


Brilliant, thanks very much. I'll have a read during my lunch break at work. Thanks for your time mate.
 
Campbell Ritchie
Marshal
Posts: 56520
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Both rather old, but well worth a read. What if they differ from each other?
A lot of people disagree with the JavaRanch guide about “constructs to avoid”. I prefer the indenting suggestions in the JavaRanch guide.
 
Charles D. Ward
Ranch Hand
Posts: 99
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kudos for the Final Fantasy X references btw.
 
Neil Van Styn
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Charles D. Ward wrote:Kudos for the Final Fantasy X references btw.


First thing that came to my mind ;-)

Probably doesn't help that I listen to this most of the time when I'm studying: http://youtu.be/6pKWeVL6270

Actually it does help. It puts me in a good frame of mind to learn somehow. *zen*
 
Neil Van Styn
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay.

This was way easier:



Neil is learning lots...
 
Ajitesh Shukla
Greenhorn
Posts: 4
Java PHP Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is a concept called as McCabe Cyclomatic complexity (http://en.wikipedia.org/wiki/Cyclomatic_complexity ) that could be used to determine one's code complexity irrespective of programming language. In short, it suggests that you could suggest the code complexity as a function of number of decision points + 1.

Code Complexity = Number of Decision Points + 1.

The way to calculate number of decision points is calculating if, while, do...while, for, switch, && etc. From that perspective you made use of large number of if..else if and also && leading to a very high code complexity. Code like these may not be termed as code of high quality.
 
Campbell Ritchie
Marshal
Posts: 56520
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In true OO fashion, each of the enum elements should map to the winner. You can even have the winner as the value of one of the enum element's fields.
 
Neil Van Styn
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yeah I'm going to have to get my hands on a copy of a good Object Oriented Programming book. To say I even have a basic grasp on the concept would be stretching the truth a bit.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!