OPC (Other People's Code)

  • (6 Pages)
  • +
  • 1
  • 2
  • 3
  • 4
  • 5
  • Last »

84 Replies - 9719 Views - Last Post: 23 October 2012 - 07:36 AM

#31 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3089
  • View blog
  • Posts: 19,137
  • Joined: 14-September 07

Re: OPC (Other People's Code)

Posted 09 February 2009 - 09:10 PM

This one:


for(int i=0;i<10;i++)


Was This Post Helpful? 0
  • +
  • -

#32 no2pencil  Icon User is offline

  • Head MFIC
  • member icon

Reputation: 5068
  • View blog
  • Posts: 26,450
  • Joined: 10-May 07

Re: OPC (Other People's Code)

Posted 09 February 2009 - 09:16 PM

View PostKYA, on 9 Feb, 2009 - 10:10 PM, said:

This one:

Are you being serious, or are you just saying that because of what I said?

Tthe way I understand it, variables should be declared before any code.
Was This Post Helpful? 0
  • +
  • -

#33 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3089
  • View blog
  • Posts: 19,137
  • Joined: 14-September 07

Re: OPC (Other People's Code)

Posted 09 February 2009 - 09:40 PM

If I only need 'i' in the scope of the for loop, the correct way is to do what I posted. There's no reason to declare it earlier if it will not be used after the for loop.

I don't see what's wrong with it. If other people hate it, that may explain some of my homework scores :)
Was This Post Helpful? 0
  • +
  • -

#34 Tom9729  Icon User is offline

  • Segmentation fault
  • member icon

Reputation: 180
  • View blog
  • Posts: 2,641
  • Joined: 30-December 07

Re: OPC (Other People's Code)

Posted 09 February 2009 - 09:40 PM

View Postno2pencil, on 9 Feb, 2009 - 11:16 PM, said:

View PostKYA, on 9 Feb, 2009 - 10:10 PM, said:

This one:

Are you being serious, or are you just saying that because of what I said?

Tthe way I understand it, variables should be declared before any code.

I think the only exception I make is with the for-loop, since the 'i' is essentially a throw-away variable.
Was This Post Helpful? 0
  • +
  • -

#35 Gloin  Icon User is offline

  • Expert Schmexpert...
  • member icon

Reputation: 235
  • View blog
  • Posts: 4,489
  • Joined: 04-August 08

Re: OPC (Other People's Code)

Posted 10 February 2009 - 01:47 AM

I would always do the loop in the way KYA did. Like he said, the variable is not to be used afterwards so it pretty clearly shows the scope.
Was This Post Helpful? 0
  • +
  • -

#36 LaFayette  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 43
  • View blog
  • Posts: 326
  • Joined: 24-November 08

Re: OPC (Other People's Code)

Posted 10 February 2009 - 01:55 AM

Declaring 'i' before the loop when you wont need it outside of it is a bit like declaring a variable global (or whatever one scope up would be) when it's only used within one function.

In some languges you have to do this though (in C before C99).

This post has been edited by LaFayette: 10 February 2009 - 01:57 AM

Was This Post Helpful? 0
  • +
  • -

#37 thepeoplescoder  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 17
  • View blog
  • Posts: 97
  • Joined: 02-February 09

Re: OPC (Other People's Code)

Posted 10 February 2009 - 01:56 AM

If I'm being quick and dirty, I'll declare the variable in the loop, but since I come from C, usually, I prefer to declare my variables before I start doing any executable statements/function calls, as opposed to declaring it inside of the loop. What if I need that value outside of the loop?

// Another reason is because in the event that I'm going to use the loop variable
// more than once, it prevents  the compiler from generating the following instructions
// several times.
//
// sub esp, 4 ; Create space for local variable
//; do loop stuff here
// add esp, 4 ; Get rid of local variable

// Doing it this way means the variable is created once, and discarded once.
// Unless, however, the compiler is smart enough to catch something that normally
// would result in the aforementioned.
//
// I guess I'm stingy when it comes to CPU cycles;)
int index, len;

for (index = 0, len = someFunc(); index < len; index++)
{
   doStuffHere();
}

for (index = 0, len = string("some BS").length(); index < len; index++)
{
   doStuffHere();
   andDoSomeMoreStuff();
}


This post has been edited by thepeoplescoder: 10 February 2009 - 02:02 AM

Was This Post Helpful? 0
  • +
  • -

#38 LaFayette  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 43
  • View blog
  • Posts: 326
  • Joined: 24-November 08

Re: OPC (Other People's Code)

Posted 10 February 2009 - 03:19 AM

Quote

What if I need that value outside of the loop?


then you declare it outside.. Otherwise you dont! :)

And as far as local variables and stuff go, you don't allocate and discard memory for them in sequence.
Was This Post Helpful? 0
  • +
  • -

#39 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5643
  • View blog
  • Posts: 12,359
  • Joined: 16-October 07

Re: OPC (Other People's Code)

Posted 10 February 2009 - 05:13 AM

View PostKYA, on 9 Feb, 2009 - 10:40 PM, said:

If I only need 'i' in the scope of the for loop, the correct way is to do what I posted. There's no reason to declare it earlier if it will not be used after the for loop.


Agreed. Strangely, I just recently on DIC recommended to someone the "declare and define" practice with variables. In "Effective Java", Bloch gives a pretty good justification for the practice under the item "minimize the scope of local variables". ( I've been hunting about, but can't find it offered as a sample chapter. )

In C, you should declare first and then assign all the values later. You pretty much have to, anyway. However, from C++ forward, and particularly in languages like Java, declaring a variable without an initial value is a poor practice. It's acceptable if scope forces it, like variable defined in constructors or assigned in a sub block. You should never leave a variable value hanging, even if you just have to assign it null.

So I'm afraid I have an opposite peeve to some. :P Poor scoping always makes me twitch, actually.
Was This Post Helpful? 0
  • +
  • -

#40 gabehabe  Icon User is offline

  • GabehabeSwamp
  • member icon




Reputation: 1376
  • View blog
  • Posts: 10,951
  • Joined: 06-February 08

Re: OPC (Other People's Code)

Posted 10 February 2009 - 05:57 AM

I hate no2pencil's code. :P

View PostKYA, on 10 Feb, 2009 - 03:10 AM, said:

This one:
for(int i=0;i<10;i++)
Concur.
Was This Post Helpful? 0
  • +
  • -

#41 BenignDesign  Icon User is offline

  • holy shitin shishkebobs
  • member icon




Reputation: 5755
  • View blog
  • Posts: 10,078
  • Joined: 28-September 07

Re: OPC (Other People's Code)

Posted 10 February 2009 - 06:50 AM

When people do THIS:

body
{
background-color:#000000;
background-image:url(pic.jpg);
background-repeat:no-repeat;
}



instead of:

body {background: #000000 url(pic.jpg) no-repeat;}



Or:

$query="SELECT * FROM table";
$result=mysql_query($query);
$row=mysql_fetch_array($query);



rather than:

$query=mysql_query("SELECT * FROM table");
$row=mysql_fetch_array($query);



I like code as condensed as possible and as clear as it can be within said condensation. Why add extra steps when you don't have to?
Was This Post Helpful? 0
  • +
  • -

#42 thehat  Icon User is offline

  • awake ? web();
  • member icon

Reputation: 106
  • View blog
  • Posts: 951
  • Joined: 28-February 08

Re: OPC (Other People's Code)

Posted 10 February 2009 - 07:17 AM

View PostBenignDesign, on 10 Feb, 2009 - 01:50 PM, said:

When people do THIS:

body
{
background-color:#000000;
background-image:url(pic.jpg);
background-repeat:no-repeat;
}



instead of:

body {background: #000000 url(pic.jpg) no-repeat;}


This one has made me unnecessarily angry before. Also, people who feel they have to define things like padding and margin using four lines makes me want to tear their code from the internet. I accept that I may have issues :)
Was This Post Helpful? 0
  • +
  • -

#43 crummydo  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 223
  • Joined: 06-January 09

Re: OPC (Other People's Code)

Posted 10 February 2009 - 09:32 AM

In NetBeans, right clicking on your code will give you the option to "Format" and it automatically formats your code... properly. In my Java class people were still after two months not formatting correct apparently. So In our DB I mentioned that and it was like I just invented fire for some of those guys.
Was This Post Helpful? 0
  • +
  • -

#44 supersssweety  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 22
  • View blog
  • Posts: 373
  • Joined: 16-March 07

Re: OPC (Other People's Code)

Posted 10 February 2009 - 10:01 AM

500 line functions with NO COMMENTS WTF! PLEASE TELL ME WHAT YOU ARE TRYING TO ACCOMPLISH!
Was This Post Helpful? 0
  • +
  • -

#45 bodom658  Icon User is offline

  • Villiage Idiom
  • member icon

Reputation: 113
  • View blog
  • Posts: 1,123
  • Joined: 22-February 08

Re: OPC (Other People's Code)

Posted 10 February 2009 - 12:16 PM

i'd do it the way KYA did it, no reason to have some variable sitting around in memory somewhere if it isn't going to be used. waste of space. (it being the whole for loop thing up about 5 posts)

This post has been edited by bodom658: 10 February 2009 - 12:18 PM

Was This Post Helpful? 0
  • +
  • -

  • (6 Pages)
  • +
  • 1
  • 2
  • 3
  • 4
  • 5
  • Last »