File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

May code writing style affect the grade?

 
Shengshuo Wu
Greenhorn
Posts: 6
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Roberto Perillo
Bartender
Posts: 2271
3
Eclipse IDE Java Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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>
 
Shengshuo Wu
Greenhorn
Posts: 6
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Roberto,

Thank you for the prompt response.
I think I'll look more into the coding convention.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64188
83
IntelliJ IDE Java jQuery Mac Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"Shengshuo", please check your private messages for an important administrative matter.
 
Shengshuo Wu
Greenhorn
Posts: 6
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Btw, does the Style1 violate the convention really large?
Or it just seems confusing?
 
Ulises Pulido
Ranch Hand
Posts: 81
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Jethro Borsje
Ranch Hand
Posts: 100
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ulises Pulido
Ranch Hand
Posts: 81
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic