Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

for() identifiers question

 
Zkr Ryz
Ranch Hand
Posts: 187
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have recived a feed back from queiroz

Saying the next:

<pre>
for( int i = 0, j = 0 ; i < n ; i++, j++ )
{
//body here
}
</pre>

"This is something I prefer to avoid. I like to see each identifier on its own line"

But Im not quite sure to understand what does this means.

Would this be a satisfactory "for" statement construction?

<pre>
for( int i = 0,
j = 0 ;
i < n ;
i++,
j++ )\
{
//body here
}
</pre>


Any suggestion?

Thanks.
 
Marilyn de Queiroz
Sheriff
Posts: 9063
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How about two separate if statements?

or a for statement and j stuff inside

 
Zkr Ryz
Ranch Hand
Posts: 187
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh Marylin I see you're there, well, I don't really get this one, what's the real problem about having compound identifiers on the same line. This is the first time someone have mention that is a bad coding style.

Well I gues it's for my own good, but I still would like to know why ?

C'ya
 
paul wheaton
Trailboss
Pie
Posts: 21484
Firefox Browser IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It looks like the values are going to be the same. Is there a way to leave one out?
The key is that the for loop is pretty damn ugly in its simplist form.
When you are doing the nitpicking and trying to understand what is going on - or if you are just trying to figure out why your own code works, it sure helps to have clean, readable, obvious code.
If you start getting fancy, and then work in lots of little fancy tricks, your code becomes really hard to read.
For the cattle drive, Readability is more important than anything else.
 
Zkr Ryz
Ranch Hand
Posts: 187
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul:

Well, actually the 'j' is evaluated inside the "for", and reach another value.

Im understandig the reason why to keep code on its simplier way is better.( I remenber the day I have to read someone else code , grrrrr!!!)

So, I suppose that in cattle drive it won't be accepted a one line statement like this:

<pre>
Sistem.out.println( a > b ? " a > b " : "a<=b"); <br /> </pre><br /> And I should use this other instead:<br /> <br /> <pre><br /> if( a > b )
{
System.out.println("a > b");
}
else
{
System.out.println("a <= b");
}
</pre>
More lines, but everyone can understand it . . . .

Ciao
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic