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
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:
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!
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
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.
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,
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.
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.