What is good code?
-
"good code is code that works " NO! This is a mistake many managers make, who can't tell their good programmers from their bad programmers. Bad code will APPEAR TO work, so this heuristic doesn't tell you much. Way more time and money are spent on code maintenance than development, so MAINTAINABLE code is good code. Bad code is difficult to maintain. It's hard to figure out how it works, and modifications have a high probability of breaking the code is some remote location. But it will appear to work. The defects in bad code are as invisible as a limp in a sitting man. Good code is CLEAR. Many factors influence clarity. Good code rarely has long methods, which reduces complexity since a method's complexity increases faster than linear as the length grows. Minimizing dependencies on code in distant locations is also a factor in clear code. Simple code is clear. Good code also has sufficient comments to quickly figure out how it works. For good code, strive for clarity.
"Microsoft -- Adding unnecessary complexity to your work since 1987!"
I'll make a point of saying "I do not disagree with your statement", because clarity is a *very* important part of what I believe defines good code. But I will also point out that good code is, indeed, code that works. This may seem obvious, but having maintainable code that doesn't do the job is of little use; it simply means fixing it is unlikely to take as long as fixing broken spaghetti code. The notion of what makes good code generally falls under the purview of software engineering, à la Gang of Four design patterns and the like. I don't think anyone here has given an incorrect opinion, as all are valid measures of good code; meaningful variable names, concise functions with well-written comments/documentation and low-cohesion to aid maintainability and extensibility are just a few measures of code quality.
Sometimes a fist in the face says more than a thousand honeyed words.
-
Slacker007 wrote:
Who determines if your code is good?
Myself, hence my code is bad.
Slacker007 wrote:
What is good code and what are the precise criteria for giving it a status of "good"? Since most of your code will never be reviewed by your peers (outside of work) then how do you know it is worth a damn, really?
If I think your code sucks but the next guy thinks it is good, then who is right?There's no precise criteria. It is hard to recognize good code (and it is also matter of personal taste). On the other hand bad code is pretty recognizable at a glance.
Slacker007 wrote:
This kind of goes in line with my other thoughts about the "greatness" of an individual programmer. Who determines that a person is a great programmer and that person writes some damn good code? You? Me? None of the above?
Myself? :-D Great programmers are like great artists. You may say they are great with some confidence. For instance we may argue on a particular Picasso's painting. But no one will doubt about Picasso greatness (well, the dumb will do, but that's a point in favour).
Veni, vidi, vici.
CPallini wrote:
On the other hand bad code is pretty recognizable at a glance.
Oh, if only that were true. 15 seconds after I first saw "if ((Max-Thread_Count_Allowed + Thread_Count_Increment) <= Current_Thread_Count) Get_New_Threads();" I knew that was bad code. It took me a week to convince my managers there was a problem with that logic. (So, how long did it take for you to spot the bug?) If anything, it certainly makes a good case for using self documenting variable names. That was the good part of the code.
-
V. wrote:
good code is code that works
I like where you are going with this. One of the reasons why I posted the question. I was up last night thinking about this after reading about someone who thinks they know what good code is.
"the meat from that butcher is just the dogs danglies, absolutely amazing cuts of beef." - DaveAuld (2011)
"No, that is just the earthly manifestation of the Great God Retardon." - Nagy Vilmos (2011) "It is the celestial scrotum of good luck!" - Nagy Vilmos (2011) "But you probably have the smoothest scrotum of any grown man" - Pete O'Hanlon (2012) -
CPallini wrote:
On the other hand bad code is pretty recognizable at a glance.
Oh, if only that were true. 15 seconds after I first saw "if ((Max-Thread_Count_Allowed + Thread_Count_Increment) <= Current_Thread_Count) Get_New_Threads();" I knew that was bad code. It took me a week to convince my managers there was a problem with that logic. (So, how long did it take for you to spot the bug?) If anything, it certainly makes a good case for using self documenting variable names. That was the good part of the code.
I was NOT talking about buggy code, about ugly code instead. Your sample code, however, you should (probably) be something like
if ( Current_Thread_Count + Thread_Count_Increment <= Maximum_Thread_Count)
Get_New_Threads();I used
Maximum_Thread_Count
beacuse is not very clear the meaning of bothMax
andThread_Count_Allowed
.Veni, vidi, vici.