aspose file tools*
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes May code writing style affect the grade? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Bookmark "May code writing style affect the grade?" Watch "May code writing style affect the grade?" New topic
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: 2266
    
    3

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: 61413
    
  67

"Shengshuo", please check your private messages for an important administrative matter.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by 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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: May code writing style affect the grade?