| Author |
May code writing style affect the grade?
|
Shengshuo Wu
Greenhorn
Joined: May 28, 2008
Posts: 6
|
|
Hi, I am wondering which style of coding below is preferred. Can the coding style affect the exam result? What if in real business? Style 1 <blockquote>code: <pre name="code" class="java"> String[] theArray = {"scjd", ""}; int emptySlot = theArray[0].equals("") || theArray[1].equals("") ? (theArray[0].equals("") ? 0 : 1): -1; </pre> </blockquote> Style 2 <blockquote>code: <pre name="code" class="java">int emptySlot = -1; String[] theArray = {"scjd", ""}; if (theArray [0].equals("") || theArray [1].equals("")) { if (theArray [0].equals("")) { emptySlot = 0; } else { emptySlot = 1; } } else { emptySlot = -1; } </pre> </blockquote> Thanks a lot. [ July 11, 2008: Message edited by: Shengshuo ] [ July 11, 2008: Message edited by: Shengshuo ] [ July 11, 2008: Message edited by: Shengshuo Wu ]
|
SCJP, SCWCD, SCJD
|
 |
Roberto Perillo
Bartender
Joined: Dec 28, 2007
Posts: 2212
|
|
Hey, partner. Yes, the way you code certainly affects your grade (I believe you lose some points in the "General Considerations" section of the marking criteria). You must follow the Java Coding Conventions. In your example, according to the Java Coding Conventions, your second example is almost correct. It should be: <blockquote>code: <pre name="code" class="java"> int emptySlot = -1; String [] theArray = {"scjd", ""}; if (theArray[0].equals("") || theArray[1].equals("")) { if (theArray[0].equals("")) { emptySlot = 0; } else { emptySlot = 1; } } else { emptySlot = -1; } </pre> </blockquote>
|
Cheers, Bob "John Lennon" Perillo
SCJP, SCWCD, SCJD, SCBCD - Daileon: A Tool for Enabling Domain Annotations
|
 |
Shengshuo Wu
Greenhorn
Joined: May 28, 2008
Posts: 6
|
|
Hi Roberto, Thank you for the prompt response. I think I'll look more into the coding convention.
|
 |
Bear Bibeault
Author and ninkuma
Marshal
Joined: Jan 10, 2002
Posts: 56185
|
|
|
"Shengshuo", please check your private messages for an important administrative matter.
|
[Smart Questions] [JSP FAQ] [Books by Bear] [Bear's FrontMan] [About Bear]
|
 |
Shengshuo Wu
Greenhorn
Joined: May 28, 2008
Posts: 6
|
|
Btw, does the Style1 violate the convention really large? Or it just seems confusing?
|
 |
Ulises Pulido
Ranch Hand
Joined: Jul 24, 2008
Posts: 81
|
|
Hello, about the code that you have: I believe you should be able to refactor it to: I believe it does what you wanted in your example [ August 01, 2008: Message edited by: Ulises Pulido ]
|
SCJP 5.0, SCWCD 5.0, SCBCD 5.0, SCJD, SCEA in progress
www.ulisespulido.com
|
 |
Jethro Borsje
Ranch Hand
Joined: Jul 22, 2008
Posts: 100
|
|
Originally posted by Ulises Pulido: I believe you should be able to refactor it to: I believe it does what you wanted in your example
I believe it is even better to do it like this: In order to prevent NPE's when theArray[0] or theArray[1] are null.
|
SCJP, SCJD
|
 |
Ulises Pulido
Ranch Hand
Joined: Jul 24, 2008
Posts: 81
|
|
Totally agree.
Originally posted by Jethro Borsje: I believe it is even better to do it like this: In order to prevent NPE's when theArray[0] or theArray[1] are null.
|
 |
 |
|
|
subject: May code writing style affect the grade?
|
|
|