views:

551

answers:

17

I was reviewing this morning a piece of code written by a beginner, when I found these lines:

If
...
ElseIf
...
ElseIf
...
Endif

I told him "Please, Do Not Use This". And the other guy sitting aside (more experimented) said: "Oh no DoN't Use This!". I think there are many reasons explaining this reaction:

  • a bad experience when we first discover the instructions ("Great, it's like a SELECT but it allows to compare more than one variable") and were finally very disappointed trying to put together multiples overlapping elseifs that no one could ever never debugg properly ...
  • The elseif trick does not allows you to align your lines of code as you've been doing it for the last 175 years, where you can visually check and adjust the quantity of endifs to add at the end of your procedure.
  • But most of all "what is the interest of something that is already correctly done by the traditionnal IF/ELSE/ENDIF or SELECT CASE statements?"

Another example of a DNUT/NDT item was this "cascade update" thing in SQL SERVER, which seemed to me at first glance a great trick ("Wohh! my keys are automatically updated anywhere in my database"), until I began to think that a really effective key should never be updated, so I banned the use of this cascade update thing in my databases a few hundred years ago, and, until today, nobody complains about that!

I am sure that each one of us has its own DNUT/NDT list, and I am sure there is plenty of time to spare by sharing this kind of knowledge!

+2  A: 

GoTo.

Rem instead of ' for comments in VB.

Separating fields in a SQL SELECT with commas at the end of the line instead of the beginning:

(way I used to do it:)

SELECT
    Field1,
    Field2,
    Field3

vs. (post-enlightenment:)

SELECT
    Field1
    ,Field2
    ,Field3

I am told Cursors in sprocs are to be on my NDT list.

cfeduke
Why is 'Separating fields in a SQL SELECT with commas at the end of the line instead of the beginning' not such a hot idea?
Hyposaurus
So you can delete/comment-out lines -- even at the end, without having to change the previous line to make the comma right. Of course, all you've done is moved the troublesome one from the end to the beginning, which is not nearly worth making your code that ugly.
James Curran
Shift+Home, Delete is better than Shift+Home, Delete, End, Up Arrow, Backspace in my book. Additionally commas at the start of the line are easier to locate than the end of the line with variable length field names.
cfeduke
+6  A: 

DON'T DO: Exception suppression

NEVER EVER

try
{
    // stuff 
}
catch(Exception e)
{}

even if you think you are going to fix that later.

EDIT: the 'code smell' here is twofold: first the exception is masked, neither thrown nor logged, so you will have a hard time finding out what went wrong, then you catch a "generic" Exception without any chance to address the specific problem and attempting to solve it or provide the user with a meaningful error message which in turn may help him/her to address it.

Manrico Corazzi
What if you intend to catch all errors so that you can log them and then end the program? I'm asking because I'd like to know what the best solution is. :)
Jason Baker
Be as specific as you can when catching errors. Catching an Exception is being lazy and could lead to problems later on with maintenance and actually doing something useful when the error happens. This is what Manrico was getting at.
Hyposaurus
Well, either you add all your logging in the "catch", or you use something like AOP (AspectJ for Java, for example) to add logging logic to your method calls.
Manrico Corazzi
Also in the CLR, code within a try { } can't be fully optimized in relation to the code outside of it so wrapping it just to wrap it hurts performance.
cfeduke
+1  A: 

Do not use:

zaca
Goto's aren't inherently evil. It's just that sometimes people use them in bad ways. Use them sparingly, and only when they make your code more understandable and faster. Jumping backwards (or into the middle of a loop) are things that you really should avoid (al la spaghetti code).
CodeSlave
I've frequently made good use of fall-through, although a make note of it in a comment.
James Curran
Yes...goto and fall-through can be great tools, but"with great power comes great responsibility" .
zaca
I use fall-through, but every time there is a comment to the effect that it is intentional. It would actually be someone convenient to force a keyword there (say... break exits the switch block, continue does an explicit fall-through?).
Tom
I never ever use goto, for the simple reason that it never even occurs to me. Therefore I wouldn't know of any situation in which it would be useful. Dijkstra has had a large impact on my education, I guess...
Thomas
+6  A: 

DON'T DO: Commit broken code

Manrico Corazzi
+3  A: 

In C, always put parentheses around bitwise operators (&, |, ^) when used in a complex condition. For example instead of

if (a & 1 == b & 1)

use

if ((a & 1) == (b & 1))

Because bitwise operators have surprisingly low precedence.

Ted Percival
+1  A: 

DON'T: Change other people's code without running the test suite to ensure you did not break anything

Manrico Corazzi
Wow, you have test suites? So jealous...
Greg D
I'd make that stronger: DON'T Change other people's code without understanding it first.
Tom
But if you refactor the code to understand it (as it is often the case, and as Martin Fowler advocates) you must at least make sure that the code yelds the same results after changing it.
Manrico Corazzi
+12  A: 

DON'T: Copy and paste code

Don
I have seen this get more devs in trouble (students and professionals alike) than any other single principle. Never, ever, ever copy and paste code!
Greg D
I was had a project which had 25 copies of the same block of code --- each with the same typo (it didn't cause a syntax error, but it did prevent it from working....)
James Curran
+2  A: 

DON'T: Eat yellow snow.

DON'T: Leave test pop-up messages or prints to the screen in checked in code...

Ian Devlin
+1  A: 

Don't use uninhibited globals in a multi-threaded program. Unless it absolutely, positively will not under any circumstances change beyond initialization.

If you have to use a global, STM is a lifesaver.

Jason Baker
"uninhibited"? Link please.
MSalters
Uninhibited = something that any part of the program can change at any time without consideration for anything else that may be using it.
Jason Baker
+6  A: 

DON'T do tests in a production environment.

Ace
and DO make it hard to _mistakenly_ run tests on production
Richard Levasseur
+6  A: 

Don't make your boss' boss look bad.

If you do, you may find yourself on the receiving end of the "Not being a team player" speech, which sets you one step closer to not getting paid for programming.

Greg D
Elaborated. I won't elaborate further because, in spite of appearances, I do still want to keep my job. ;)
Greg D
I have to keep reminding myself of this one on a daily basis!
Ric Tokyo
http://c2.com/cgi/wiki?NotaTeamPlayer
mannicken
+2  A: 

NEVER manually ref count a COM object. One of two things will happen

  1. You will get it wrong
  2. The next guy will screw it up
JaredPar
+2  A: 

DON'T: Assume it works because you know it can't possibly go wrong

Chris B-C
I should listen you. Oh yeah I should :-)
Luc M
+3  A: 

DON'T do a an ad-hoc delete on a Production system without wrapping it in a transaction. Run it once with a rollback, and then run it again and commit. No-one wants to restore a database because you forgot your where clause.

Even Mien
Ditto for updates. Trust me, I found out the hard way when I was young and stupid ;)
Giovanni Galbo
A: 
  1. Don't do anything in a hurry!

    Go as fast as you can without compromising:

    • security

    • quality

    • performance

    • presentability of the behind the screen magic (source code, SQL, etc)

    If upper management wants something done yesterday, try to educate them, and get them used to having things ready at the right time (done properly).

    Rather than solving something Mcguyver style in no-time, but horrible to maintain.

    another big Don't for me, but not so immediately programming related perhaps for some:

  2. Don't stop reading and educating yourself.

  3. Don't forget to delete old development environments or whatever you don't need.

  4. Don't replace source code versioning/control with full machine backups :^) they do not service the same purpose.

Ric Tokyo
+4  A: 

Do not put anything in the code that you wouldn't want a customer to see

Hamish Smith
A: 

DON'T use the token-concatenation operator in C++.

'##' is evil, evil, evil, evil, evil.

Greg D