You are not logged in.

#1 2009-02-24 18:19:50

ihavenoname
Member
Registered: 2006-01-09
Posts: 198

Difference between "Good Code" and a "hack"

Basically I wanted to get opinions on the following things.

1. what makes something (a solution) a "hack"

2. What makes something "great code"

And everything in between.

Is iit "debugablility"? Is it conciseness? Speed?


In this land of the pain the sane lose not knowing they were part of the game.

~LP

Offline

#2 2009-02-24 18:30:32

Dusty
Schwag Merchant
From: Medicine Hat, Alberta, Canada
Registered: 2004-01-18
Posts: 5,986
Website

Re: Difference between "Good Code" and a "hack"

'debugability' and maintainability are symptoms of good code. Good code is elegant, clean, and beautiful, much like poetry. Also much like poetry, it is very subjective. Good code doesn't contain duplicate logic (DRY -- don't repeat yourself) and has simple elegant interfaces between disparate parts. Above all, good code is easy to read, some would say a pleasure to read.

hacks aren't always bad code, but assuming you mean a 'messy hack', its usually a quick few lines that you introduce into a program to solve a specific problem, knowing full well that a truly elegant solution to that problem would entail rewriting substantial portions of the code. Sometimes this is ok in the interest of development speed, but in the long term, if your code is just a bunch of hacks glued together, it slows down progress of the project until eventually its simply unmaintainable. Hacks are rarely self-documenting and usually require a comment plus a thorough inspection of the few lines of code in question to understand what's going on, why it works, and most often, the reason its not working in the current case you're trying to debug.

Dusty

Offline

#3 2009-02-24 19:09:05

freakcode
Member
From: São Paulo - Brazil
Registered: 2007-11-03
Posts: 410
Website

Re: Difference between "Good Code" and a "hack"

It's simple:

If you wrote code that solves the problem, it's good code. If you wrote code that solves the problem but creates another one, it's a hack.

Unless you're a hobbist, avoid hacks by all means, they are evil. You always write it, put a comment like "this is an ugly hack", but guess what? You'll never touch it again, because it's working code and you'll forget it, until it turns back to you in the form of a huge headache.

So, spend time and effort writing correct, atomic and clear code. Once it's done, you won't need to touch it anymore.

Offline

#4 2009-02-24 19:19:32

techprophet
Member
Registered: 2008-05-13
Posts: 209

Re: Difference between "Good Code" and a "hack"

"Good Code" (for me) is something clearly divided into structs, functions, classes, and such so that the code easy to read, modify, and fix.

A "hack" involves adding code that duplicates something already done elsewhere, or is implimented messily.

A Good Program is like Good Code, modular and (reasonably) easily modifiable. A Bad Program is like a hack, it has only one way to perform it's function, with no options, or options that are very hard to change.

Offline

#5 2009-02-24 21:49:27

fukawi2
Ex-Administratorino
From: .vic.au
Registered: 2007-09-28
Posts: 6,217
Website

Re: Difference between "Good Code" and a "hack"

freakcode wrote:

If you wrote code that solves the problem, it's good code. If you wrote code that solves the problem but creates another one, it's a hack.

That's a good way to put it, I like it smile

Offline

#6 2009-02-24 21:57:02

Barrucadu
Member
From: York, England
Registered: 2008-03-30
Posts: 1,158
Website

Re: Difference between "Good Code" and a "hack"

To me, good code is code which is nicely commented and broken up into logical blocks (functions, different files, et cetera), whereas a hack is a couple of lines thrown in to fix something quickly, however hacks can be good code if done properly.

Offline

#7 2009-03-08 02:01:05

Lexion
Member
Registered: 2008-03-23
Posts: 510

Re: Difference between "Good Code" and a "hack"

Good code is code that, written in one language, can be read by someone who programs in another.  A good example would be c/c++ and python.  I agree with Barrucadu in that hacks can be good code, but then I wrote some good code that broke my system.  Yay!


urxvtc / wmii / zsh / configs / onebluecat.net
Arch will not hold your hand

Offline

#8 2009-03-08 06:04:45

pauldonnelly
Member
Registered: 2006-06-19
Posts: 776

Re: Difference between "Good Code" and a "hack"

Good code is code that doesn't make you wonder what you were thinking when you read it the next morning.

Offline

#9 2009-03-08 06:27:22

Killa B
Member
From: United States
Registered: 2008-10-28
Posts: 42
Website

Re: Difference between "Good Code" and a "hack"

Good code is pretty straightforward to explain, but hacks can be hard to wrap your head around.

Here's one shining example of a terrible hack I just did yesterday:
1. Take perfectly good source code
2. Add a commandline argument that sets a newly added global variable
3. Add a bunch of if statements to skip various pieces of code if that variable is set
4. Completely disregard that variable once the main loop has started

The global variable and all the if statements are terrible practice, but I don't care because I just wanted to get the job done quickly.

Another very common form of hack is breaking for special cases in the middle of loops, and, similarly, returning early for special cases in the middle of functions.

Offline

#10 2009-03-08 10:54:04

Nepherte
Member
From: Singapore
Registered: 2008-09-09
Posts: 427
Website

Re: Difference between "Good Code" and a "hack"

In addition, good code is scalable.

Offline

#11 2009-03-11 03:25:02

big_gie
Member
Registered: 2005-01-19
Posts: 637

Re: Difference between "Good Code" and a "hack"

Good code:
-is well organized: using a modular approach, you can easily add, remove or displace some elements without breaking things
-has failing test: for example, always test when you open a file. Else you risk of having a problem _elsewhere_ in the code. You'll pull your hear out!!!
-has NO global variables! EVIIIIIIL!
-respect formating style: decide on a style and _stick to it_ whatever it is. If you start from scratch choose a good style. If you inherited some code, take the same style as the code's. I'm working on an inherited code which is a mess: around 10 people have put some hacks in it. It is a real Jenga game. Each time I need to add/change something, it breaks at 10 different places.
-is fun to work with: when you want to throw your computer by the window, there is something wrong wink
-is easy to read: a non-programmer should be able to understand it. Else, the next morning you wont understand it
-has references: In my field of study (physics) you need to know where things come from. A formula needs to come with a citation. The article will tell you much more information then the plain equation (approximations, field of validity, units, etc)
-has a good balance of comments: not too much, but more then none.
-has a testing suite accompanying it

Hacks:
-are easy to identify: they work when they should not, or not when they should.
-are always a way to put an exception in the general case (the code)
-when you don't need them they work and you forget about them. Then when you're in a critical situation, they break
-do not come with self-consistency test: they are more often just thrown-in in the code.

I recommend "The Practice of Programming" from Kernighan and Pike. There's a good review at lwn: http://lwn.net/Articles/293037/

Offline

#12 2009-03-11 09:35:18

iphitus
Forum Fellow
From: Melbourne, Australia
Registered: 2004-10-09
Posts: 4,927

Re: Difference between "Good Code" and a "hack"

If your code is published on thedailywtf.com...

Offline

#13 2009-03-11 21:29:39

fukawi2
Ex-Administratorino
From: .vic.au
Registered: 2007-09-28
Posts: 6,217
Website

Re: Difference between "Good Code" and a "hack"

Then it's... Good code? tongue

Offline

Board footer

Powered by FluxBB