OPC (Other People's Code)

  • (6 Pages)
  • +
  • « First
  • 4
  • 5
  • 6

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

#76 rjdegraff  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 26-May 07

Re: OPC (Other People's Code)

Posted 18 February 2009 - 07:18 AM

if (condition)
{
	statements;
}



or

if (condition) {
	statements;
}



The only justification I've ever seen for the first form (I prefer the second, more compact form) is that the braces for each level left-align. I never trust alignment when I am looking at someone else's code. I use an editor that supports "find matching bracket".

C and C++ support omitting the brace brackets if a simple (rather than compound) statement follows the construct. I advise against using this form. ALWAYS use brace brackets even for constructs that have only a simple statement. It avoids ambiguity and if you need to add another statement you don't need to add the braces.

Always write your comments first (a header in each file that includes a description AND an audit trail). The reason you need to write your comments first is to force you to think about what the code should be doing (instead of how). After all, if you don't know what the code should do then you shouldn't be writing it.

White space (but not too much). Code should be pleasing to the eye as well as functional. If you took a great work of literature and removed all of the white space, it would still be great, but much more difficult to read. Same with code.

Break up your code into small, functional modules. I had to modify a REXX program written by a co-worker. It was your basic "stream-of-consciousness" code. One monolithic block (no whitespace - no indentation). Also, all variables were of the x, xx, iii, j1, j2, etc type (before you ask - he wasn't an engineer). It was easier to just rewrite the mess.

Don't get clever. A piece of code that is simple and clear is almost always better than code that uses clever (and usually obscure) tricks. If the trick provides massive savings in time or resources then use it. But make sure you thoroughly document the trick.

Don't patch bad code - rewrite it.

When reading someone else's code, don't assume anything. I once spent hours trying to find a bug in a thousand-line piece of FORTRAN code (on a system that did not have a source level debugger). The problem ended up being in an include file in which the constant NINE was initialized to the value 5.

This post has been edited by rjdegraff: 18 February 2009 - 07:50 AM

Was This Post Helpful? 0
  • +
  • -

#77 computerfox  Icon User is offline

  • straight vegetarian kid

Reputation: 50
  • View blog
  • Posts: 3,772
  • Joined: 29-January 09

Re: OPC (Other People's Code)

Posted 27 February 2009 - 12:13 PM

wow that's making my head spin....


correct indentation should be key to the code because it would be easier to debug and when others read it, they can follow it and make sense of it.

2. i agree duplication is redunkulous. if you already wrote a function that does that, then just utilize that function.

3. i don't care so much about comments.
Was This Post Helpful? 0
  • +
  • -

#78 Hyper  Icon User is offline

  • Banned

Reputation: 108
  • View blog
  • Posts: 2,129
  • Joined: 15-October 08

Re: OPC (Other People's Code)

Posted 27 February 2009 - 12:16 PM

Comments are what make code READABLE QUICK!
To quote my black hero, "Don't take my word for it, read it!"

Now to break into song and dance...
"Take a look, read a book, reading rainbow! I... can... go... anywhere! Take a look! Read a book! The reading rainbow!"

This post has been edited by Hyper: 27 February 2009 - 12:17 PM

Was This Post Helpful? 0
  • +
  • -

#79 Michael26  Icon User is offline

  • Radar love
  • member icon

Reputation: 332
  • View blog
  • Posts: 1,419
  • Joined: 08-April 09

Re: OPC (Other People's Code)

Posted 13 September 2012 - 08:30 AM

View PostIrishCereal, on 10 February 2009 - 01:14 AM, said:

I honestly do not understand why people do this:
if (condition) //This if condition is done mostly by C# programmers
  {
  statements
  }



I like it better and think it looks a lot nicer like this:
//And this is done mostly by JAVA programmer
if (condition) { 
  statements
}



Just my observation.
Was This Post Helpful? 0
  • +
  • -

#80 modi123_1  Icon User is offline

  • Suitor #2
  • member icon



Reputation: 8371
  • View blog
  • Posts: 31,099
  • Joined: 12-June 08

Re: OPC (Other People's Code)

Posted 13 September 2012 - 08:33 AM

Avoid digging up posts from 2009.
Was This Post Helpful? 0
  • +
  • -

#81 BenignDesign  Icon User is offline

  • holy shitin shishkebobs
  • member icon




Reputation: 5753
  • View blog
  • Posts: 10,077
  • Joined: 28-September 07

Re: OPC (Other People's Code)

Posted 13 September 2012 - 10:18 AM

haha... holy flashback! :wub:
Was This Post Helpful? 0
  • +
  • -

#82 DarenR  Icon User is offline

  • D.I.C Lover

Reputation: 385
  • View blog
  • Posts: 2,651
  • Joined: 12-January 10

Re: OPC (Other People's Code)

Posted 13 September 2012 - 10:24 AM

my biggest complaints are:

code not having comments
aliased items...I hate searching through code to try to find out what the alias means.
no white space
Was This Post Helpful? 0
  • +
  • -

#83 Ghlavac  Icon User is offline

  • D.I.C Addict

Reputation: 84
  • View blog
  • Posts: 515
  • Joined: 14-January 09

Re: OPC (Other People's Code)

Posted 22 October 2012 - 11:21 AM

Code files with 30k lines that is mostly hard-coded translations with switches in switches rather then utilizing localization technologies.

(Hint: Its terraria.)

This post has been edited by Ghlavac: 22 October 2012 - 11:21 AM

Was This Post Helpful? 0
  • +
  • -

#84 cfoley  Icon User is offline

  • Cabbage
  • member icon

Reputation: 1906
  • View blog
  • Posts: 3,952
  • Joined: 11-December 07

Re: OPC (Other People's Code)

Posted 23 October 2012 - 04:21 AM

Here are some that really get me.

// Doesn't understand booleans
boolean answer = (someVariable == true);
if (answer == true) {
    return true;
} else if (answer == false) {
    return false;
}


// Worse than doing it without the loop.
for(int i = 0; i < 3; i++) {
  if (i == 0) {
    someMethod(0);
  } else if (i == 1) {
    someMethod(1);
  } else if (i == 2) {
    some method(2);
  }
}

Was This Post Helpful? 0
  • +
  • -

#85 wordswords  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 76
  • View blog
  • Posts: 272
  • Joined: 17-December 11

Re: OPC (Other People's Code)

Posted 23 October 2012 - 07:36 AM

I don't like:

* Dynamically typed languages where variables are declared without any indication of their proper type.
* Global variables that are not declared to be explicitly included in each file.

In PHP:

C.php
<?php 

$X = 2;

?>



B.php
<?php
include('C.php');
...
$X = 1;
...
?>



A.php
<?php
include('B.php');
echo $X;

?>



If you imagine this, except spread across half a dozen files, with hundreds of lines in each file.. nasty.

This post has been edited by wordswords: 24 October 2012 - 10:25 AM

Was This Post Helpful? 0
  • +
  • -

  • (6 Pages)
  • +
  • « First
  • 4
  • 5
  • 6