You are not logged in.
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
'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
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
"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
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
Are you familiar with our Forum Rules, and How To Ask Questions The Smart Way?
BlueHackers // fscanary // resticctl
Offline
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
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
Good code is code that doesn't make you wonder what you were thinking when you read it the next morning.
Offline
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
In addition, good code is scalable.
Offline
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
-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
If your code is published on thedailywtf.com...
Offline
Then it's... Good code?
Are you familiar with our Forum Rules, and How To Ask Questions The Smart Way?
BlueHackers // fscanary // resticctl
Offline