Programming style abominations

Yeah, you Python doodz have done the world a great service by replacing the holy war over brace style with a holy war over spaces vs. tabs. :slight_smile:

Ultimately, indenting style is irrelevant. It can easily be “corrected” to whatever style you prefer by your local ‘indent’ program or a source control filter. If you’re complaining about indenting and braces, you haven’t seen bad programming. (I also recommend reading The Daily WTF. A lot.)

It’s nice to see another person in defense of the noble Javascript. It’s a great high-level language. Lots of people could learn a lot from simple prototype-based OO.

But I must disagree with you about C#. If you need to do some object-oriented systems programming, which would you rather use? Bjarne’s abomination, or a clean super-set of C with bounds checking and garbage collection?

Personally, though, I really like Objective-C.

Ah … the religious wars, as we call them in my office. The users of this style:



if (cond) {
   <statements>
}


give many reasons for their behavior:

This is probably due to some early childhood trama or poor potty training. They just need understanding and re-education. :wink:

We don’t even discuss when to use IF blocks vs. CASE statements anymore.

I don’t understand this at all. The spacing is irrelevant if you’re using an IDE made any time in the last 15 years or so. Granted, I’ve done a non-insignificant amount of my coding without a modern IDE, and that’s where the real objection to the style comes in… who the hell blocks together that many variable declarations without any sort of organization? If I need to declare that many variables in a block, which is not often, I will organize them in a meaningful way and comment if necessary.

And speaking of variables, give them f’n meaningful names, will you? I know someone mentioned seeing someone name a variable after himself, but it’s just as horrible when all of the variables have only slightly more than meaningless names. I’m okay with using things like i, j, k for loops and such because it’s established style, but using a bunch of variables like int1, int2, int3, etc. is impossible to read. I’m fine with abbreviating things, but make sure the abbreviations make sense.

I’m constantly reading code written by other people, mostly Microsoft developers, and invariably (NPI) making improvements or fixing bugs, and their style is impossible to read. I suppose, at least it’s consistent.
And to comment on the


if (cond) {
   <statements>
}

vs.

if (cond)
{
   <statements>
}

The first is unconscionable to me, perhaps because I learned to program in PASCAL where it would look completely horrendous.


if (cond) then begin
   <statements>
end

The only time I’d ever put the {}'s on the same line as code is for the do-while loops because the only way to remove the visual ambiguity of the end of a do-while loop and the beginning of a while loop is with


do{
   <statements>
}while (cond)

Consequently, I avoided the do-while loop like the plague.

I couldn’t rewrite it until I deconstructed it. Nobody knew what the program did, except that you fed it these two files and the personnel database, “then a miracle occurs”, and a report and a paycheck file fit for printing came out. The existing program, such as it was, was the ONLY documentation on how the paychecks were calculated. I tried contacting the accounting department, but they had no idea what the requirements were. Even the people I contacted at the state revenue department couldn’t tell me what the rules were for payroll taxes. Seemed insane to me, but that’s the way it was.

I’d conjecture it was the first COBOL program the original coder had ever written. There were no ELSEs to go with IF-THENs (and no DO blocks of any kind)…just branch here via a GOTO, branch back, branch hither and yon. In fact, the GOTO targets were labels like GOTO-TARGET-001, GOTO-TARGET-002, etc. (So yeah, you’d see a statement that said GOTO GOTO-TARGET-001.) Loops were ‘manually’ controlled by explicitly coded counters and GOTO statements and IFs for the loop exit control… (Evidently the guy wasn’t aware that you could achieve a DO loop in COBOL using a PERFORM-VARYING combo.) Heck, the guy never even indented a single statement.

The variable names were done as though there was an 8-character limit. Besides, it was hard to write, it should be hard to understand, no? I love the fact that COBOL is still around; I swear it’ll outlive humanity.

And since I forgot this on my previous post… Hey you kids! Get out of my field!!

Why is that? What do modern IDE’s do with statements such as the one you quoted?

Agreed. What I gave was an artificial example with too many variables being initialized in the same spot, just to show that aligning them improves readability (in a basic text editor, like vi or emacs)

If you notice, the title of the thread is “programming style abominations”, not
“programming abominations”.

Of the latter, I’ve seen too many to count here, but this was more about coding style (or rather space-brace-and-indentation style).

BTW, the OP was a bit tongue-in-cheek, because it is obviously an exaggeration to call a badly-placed brace an “abomination”.

This reminds me one of my huge pet peeves about programming languages.

Why does every goddamn language have to invent its own way of saying if-then-else?

Each language should just concentrate on adding its own unique features (e.g. OOP) and leave the damn if statement alone (BTW that goes for ‘for’ loops too, and don’t get me started on the comment character)

Let’s see, we have
C


if(bla)
{
   foo;
}
else if (test)
{
   bar;
}


Perl


if(bla)
{
   foo;
}
elsif (test)
{
   bar;
}


Csh


if(bla) then
   foo;
else if (test)
   bar;
endif


Matlab


if(bla)
   foo;
elseif (test)
   bar;
end


and, now, based on Blaster Master’s example, I can add PASCAL, which I guess would look like



if (bla) then 
   begin
     <statements>
  end
else if (test) then
  begin
       <statements>
  end
end


and the list goes on and on.

Hey, jackass language developers, you are not adding anything to the programming paradigm by inventing your own little way of doing a simple if statement, or for that matter, a comment, or a for loop, and with all the different ways of saying ‘break’ and ‘continue’ in all the languages.

Ah yes Apple LOGO, my first language. Many cycles ago.
When I was a TA I constantly had a fight with my prof of Variable names, he would teach them “be as descriptive as possible in variable names, point will be taken off for nondescriptive names” with no further instruction, then I had to grade then damn things.



int[] arrayToStoreTheTemperatureIn;
int variableToStoreTheXAxisOfTheArray =0;
int variableToStoreTheYAxisOfTheArray =0;
int variableToStoreTheXAxisOfTheArray =0;

.....

for(int variableToControltheXAxisOfTheArray =0;  variableToControltheXAxisOfTheArray ;
 variableToControltheXAxisOfTheArray ++){
      for (int variableToControltheYAxisOfTheArray =0; variableToControltheYAxisOfTheArray ;
 variableToControltheYAxisOfTheArray ++){

        for (int variableToControltheZAxisOfTheArray =0;  variableToControltheZAxisOfTheArray ;
 variableToControltheZAxisOfTheArray ++){

            if (arrayToStoreTheTemperatureIn[variableToControltheXAxisOfTheArray] == variableToStoretheXAxisOfTheArray)
          {
          killProfessorWithDullImplement(myFoot, hisSpleen);}
            }
        }
 }


One word: INTERCAL

I think 3acres knows what I’m talking about.

So when you coded it to give programmers three times their current pay, no one would know that it wasn’t supposed to?

Here’s a little abomination for all of you C++ folks:



const int c = 3;
int* x = &c;

*x = 5;

if ((&c == x) && (*x != c))
{
    printf("Everything you know is wrong!");
}


Visual Studio 6.0 and 7.0 will actually print that out. I’ve not had the chance to try it in other compilers.


-> g++ -o tst tst.cc
tst.cc: In function `int main()':
tst.cc:8: error: invalid conversion from `const int*' to `int*'


In my experience with Visual Studio, Eclipse, Borland, and who knows what else, reserved words are generally colored, bolded, or otherwise differentiated from identifiers so the type would be easily visually distinguishable from the variable name. For instance, it might look something like this (a simple Visual Studio-esque example):


int i = 1
string s = "foo"
double x = 3.14

Understood, of course, I’d consider that bad style too. :stuck_out_tongue:

Nitpicks: the last end would cause an error. In that sense, it is like C, such that if only a single statement follows the if, you don’t need the {}'s. Also, IIRC, there is no “else-if” equivalent, so you would’ve had to do nested ifs instead. And it’s a nitpick because…who uses PASCAL anymore?

Mostly agreed here. It’s annoying when I’m constantly switching between languages, especially during certain semesters in Grad school when I had one class in C++ and Java, one in Matlab, one in Lisp, and another with random tools like Yakk, Prolog, etc. But at the same time, a certain syntax just may not make sense in another context (Lisp being an excellent example). And, of course, we’re stuck with the whims of the designers’ preferences… should the “begin” be explicit or implicit, etc.

Is that a list of globals, or are they locally declared? If the latter, then I would recommend splitting the function up - it’s too long. :smiley:

My personal pet peeve is when I see something like this:



bool someFlag = false;

...

if(checkBoxIsChecked)
{
    someFlag = true;
}
else
{
    someFlag = false;
}


For cryin’ out loud - if you love your unnecessary conditional so much, why don’t you marry it?

LilShieste

If you explicitly cast it however (int* x = (int*) &c;), it does exactly as ultrafilter describes. That is a weird one, all right.

A K&R brace style war? You’re kidding me. Can we debate the Brady Bunch next?

In any case, that if/else clause should be pounded down to:


foo = (blah) ? bar1 : bar2

Happy? Yeah, well I don’t care, either. Twatwaffles.

Wow, that’s pretty obnoxious. Also, Hungarian Notation sucks. :stuck_out_tongue:

LilShieste

I use reverse hungarian notation. Especially useful when writing a bathroom application and you have a hand dryer subobject with an activation control called PushBut.

I’m guilty of using the ? operator even though I would probably forbid its use by anyone else.

I like to do this:


public void setJohnsonLength(Long value){
  _johnsonLength = (value == null) ? DEFAULT_JOHNSON_LENGTH : value;
}

It’s a short-and-sweet way to insulate from nulls (especially important in this example).

And about those braces… Go ahead and put them wherever you want. The automated build will slam them into shape with a sledgehammer and there’s nothing you can do about it :D.

The ? operator is fine, as long as the three elements are variables or very brief. If “blah” is some long-ass function with multiple params, then a ? operator would be horrid.

Another concise way might be:


foo = bar1;
if(blah)
{
 foo = bar2
}

so you avoid the ‘else’ altogether.

I am firmly in the



if (foo) {
   doSomething();
} else if (blah) {
   doOther();
} else {
   doNothing();
}

doMore();


camp. The braces just clutter up the code, so I don’t waste any space on them. I can tell what block something belongs to through indentation.

It drives me nuts when people put braces on a separate line - why are you wasting so much space?