my dog learned polymorphism*
The moose likes Beginning Java and the fly likes Tidying up my code? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Tidying up my code?" Watch "Tidying up my code?" New topic
Author

Tidying up my code?

Dylan Cassidy
Greenhorn

Joined: Dec 17, 2012
Posts: 2
Hi, im very new to java and just looking for a way to tidy up my code a bit before handing up my assignment.
The object of this code is to draw a big 'X' using alot of the characters 'X', as you can see it does do the trick but surely shouldnt be so long.....
If anyone has any suggestions that would be great
Thanks

fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 10916
    
  12

If this were production code, the first thing I'd tell someone is they need comments. I won't even read one line if there aren't any.

Now...are you really going to tell me that in six months (heck, in six DAYS), you will be able to explain what EXACTLY this line is doing?



Quite frankly, my brain hurts just looking at this much of it:


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36508
    
  16
Welcome to the Ranch

The first thing to do with tidying up code is to use the code button, which I have done. It makes your code look a lot better (‍), but shows up some formatting inconsistencies. We have some suggestions about style, which I suggest you follow. You will see in that page, links to other style guides. Remember these are suggestions, not rules, but you should get one style guide (you can even create it for yourself) and stick to it strictly.
So you want me to go through your code? You asked for it!
  • Lines 2 13 15 17 19 25 26 28: unnecessary blank lines. It is particularly confusing when you have blank lines in the middle of an expression, i.e. after your || operators.
  • Inconsistent indenting: compare lines 9&11 and 13-18 and 20&22.
  • Declaration of the two Strings is redundant; you can simply write System.out.print("X"); or System.out.print(" ");
  • If you still declare the Strings, x is a good name for the String, but y might better be called space or similar. Then the names will match the intent.
  • Your if and else should be indented to the same level and should have brace pairs: {}.
  • Look in the Sun guide (linked from previous link) and find out about how to break that long line after if into smaller parts. Each successive part should be indented twice compared to the beginning.
  • Put single spaces before and after all binary operators including < and =.
  • All these things will make the code much easier to read and understand.
    You have one level of () more than you strictly need, after the if. Remember && has a higher precedence than ||. Decide whether the additional () make the code easier to read or harder. Breaking the lines so those () form a line to themselves, which I think you are already doing, will probably increase legibility.
    Remember there are exercises in books and exam websites where they intentionally indent the code incorrectly. A lot of people think the indentation is significant to the syntax, and misinterpret the code. Which is exactly what the people writing the exercise hoped for
    Paul Clapham
    Bartender

    Joined: Oct 14, 2005
    Posts: 18135
        
        8

    Campbell Ritchie wrote:Remember there are exercises in books and exam websites where they intentionally indent the code incorrectly. A lot of people think the indentation is significant to the syntax, and misinterpret the code. Which is exactly what the people writing the exercise hoped for


    Let me just expand on this a little. There are many of these exercises which ask questions about badly-written code, but they are intended to induce you to learn particular things about the Java language. In fact from what I've seen of the certification exams, they are pretty much a litany of examples of how NOT to write good Java code. So be cautious about learning from examples such as those.
    Dylan Cassidy
    Greenhorn

    Joined: Dec 17, 2012
    Posts: 2
    Thanks alot guys, really appreciate the help!
    I followed your advice and I think this does look better, do the comments make sense?

    Is there any way to shorten up that 'if' statement, it seems a bit long winded....
    For my next program Im going to have to let the user set the parameters for the width of the X so it would be ridiculous to keep going this way.
    Im here to learn as much as I can so you can be quite vague or even point me in the general direction with something to reference,
    thanks again!

    Winston Gutkowski
    Bartender

    Joined: Mar 17, 2011
    Posts: 7064
        
      16

    Dylan Cassidy wrote:Is there any way to shorten up that 'if' statement, it seems a bit long winded....

    Yes, but in order to work it out you need to StopCoding (←click).

    Writing out those comments is probably a good start, but now you have to sit down and write out in English WHAT it is you're doing. Your problem right now is that you're 'thinking in Java', so everything is all about "I add one to x, and subtract y, then divide by z...".

    STOP, and try and describe WHY you're doing that, because that's what will will help you trim down that horrendous if statement.

    Winston


    Isn't it funny how there's always time and money enough to do it WRONG?
    Artlicles by Winston can be found here
    Joanne Neal
    Rancher

    Joined: Aug 05, 2005
    Posts: 3169
        
      10
    Dylan Cassidy wrote:I followed your advice

    You didn't follow the advice in the first sentence (other than the welcome) of Campbell's post.


    Joanne
    Stephan van Hulst
    Bartender

    Joined: Sep 20, 2010
    Posts: 3378
        
        9
    Hi Dylan,

    Let me give you a few tips. You can make the code more generic by writing a method that takes the thickness of the legs of the X. You can build up each line by creating a char[] and filling it first, and then convert it to a String for printing.
    Campbell Ritchie
    Sheriff

    Joined: Oct 13, 2005
    Posts: 36508
        
      16
    Joanne Neal wrote:
    Dylan Cassidy wrote:I followed your advice

    You didn't follow the advice in the first sentence (other than the welcome) of Campbell's post.
    Thank you for noticing, Joanne. It looks even worse now I have added code tags and the inconsistent indentation reappears.
    Winston Gutkowski
    Bartender

    Joined: Mar 17, 2011
    Posts: 7064
        
      16

    Campbell Ritchie wrote:I have added code tags and the inconsistent indentation reappears.

    @Dylan: And your lines are far too long. I've broken them up this time.

    Please read the DontWriteLongLines page.

    Winston
    Campbell Ritchie
    Sheriff

    Joined: Oct 13, 2005
    Posts: 36508
        
      16
    Now I know you are supposed to write a large X, which I didn’t know until the comments appeared, I can see you are going to have to apply six operations to that if statement:-
  • 1: Highlight it.
  • 2: Push the key marked d‑e‑l
  • 3: Remove a piece of hardware which is obstructing you: that black wire between the computer and the power supply
  • 4: Paper
  • 5: Pencil
  • 6: (Very very important ‍) Eraser.
  • Write down on the piece of paper how you can get from 5, “ ” and “X” toAs Stephan has hinted, there is a much much simpler way to do it than that big if.
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: Tidying up my code?
     
    Similar Threads
    how to do an update an application
    where to start on assignment
    Fetch 10K rows from table
    Creating an Array list
    Need help in understanding JMockit Usage