What is good code?

A place to discuss the implementation and style of computer programs.

Moderators: phlip, Moderators General, Prelates

User avatar
Berengal
Superabacus Mystic of the First Rank
Posts: 2707
Joined: Thu May 24, 2007 5:51 am UTC
Location: Bergen, Norway
Contact:

What is good code?

Postby Berengal » Sun Jun 13, 2010 5:23 am UTC

How do we write it?
How do we recognize it?
It is practically impossible to teach good programming to students who are motivated by money: As potential programmers they are mentally mutilated beyond hope of regeneration.

User avatar
WarDaft
Posts: 1583
Joined: Thu Jul 30, 2009 3:16 pm UTC

Re: What is good code?

Postby WarDaft » Sun Jun 13, 2010 9:28 am UTC

I would say one of the criteria is that it is easy to write, and easy to recognize. As for how do we write it? I have a hunch you could probably tie any "complete" description to the halting problem, so we could only approximate it with practice and decent standards. What constitutes the best of the latter will often be a matter of personal taste.
All Shadow priest spells that deal Fire damage now appear green.
Big freaky cereal boxes of death.

Timtu
Posts: 21
Joined: Sat Feb 06, 2010 5:58 am UTC

Re: What is good code?

Postby Timtu » Sun Jun 13, 2010 12:48 pm UTC

Do you mean aesthetically (layout and commenting and stuff) or the actual set of instructions itself?

User avatar
You, sir, name?
Posts: 6983
Joined: Sun Apr 22, 2007 10:07 am UTC
Location: Chako Paul City
Contact:

Re: What is good code?

Postby You, sir, name? » Sun Jun 13, 2010 1:26 pm UTC

Berengal wrote:How do we write it?
How do we recognize it?


The questions are incomplete without a context in which the code is judged. An incomplete list of commonly accepted code-virtues are:

  • Maintainability
  • Readability
  • Terseness
  • Hack value
  • Portability

Though most of these are in the eye of the beholder.
I edit my posts a lot and sometimes the words wrong order words appear in sentences get messed up.

User avatar
Berengal
Superabacus Mystic of the First Rank
Posts: 2707
Joined: Thu May 24, 2007 5:51 am UTC
Location: Bergen, Norway
Contact:

Re: What is good code?

Postby Berengal » Sun Jun 13, 2010 2:44 pm UTC

Timtu wrote:Do you mean aesthetically (layout and commenting and stuff) or the actual set of instructions itself?
You tell me. Which of those are important to good code and how much?

You, sir, name? wrote:The questions are incomplete without a context in which the code is judged.
Well that's the main problem (apart from the actual problem I'm trying to solve. This discussion is about meta stuff) I have when writing code as well. If I knew the context the code would be judged in, I could optimize my writing for that. I'm also not too sure about how important context really is. I'm starting to think it isn't very, and that code that is really excellent is excellent in all contexts, like e.g. TeX.

I've been having quite a lot of thoughts on this subject lately, but I'm having trouble crystallizing them into concrete concepts. That's why I'm asking you what you think good code should be, to get some new perspectives on things.

To put at least something on the table I can at least tell you what I'm pretty sure about. I'm pretty sure that good code is first and foremost correct. Any code that isn't correct cannot be good, in any context. Quite a lot of literature exists on how to write correct code as well, so it's perhaps the easiest of them all to get right. There is one issue however, in that many problems aren't fully defined, so it can be hard to tell the difference between incorrect code and code that's just solving the wrong problem. Some might say that code solving the wrong problem is incorrect, but I think that's a higher-level incorrectness not directly related to code. Good code is still good code even if the requirements (and hence the problem) change, something that happens all the time.

I can't even start to decide on the second most important thing. I've got several candidate concepts lining up like simplicity, discoverability, readability and isomorphism with the problem domain, but I feel that what I really want is something common to all those that I've somehow missed.
It is practically impossible to teach good programming to students who are motivated by money: As potential programmers they are mentally mutilated beyond hope of regeneration.

User avatar
Steax
SecondTalon's Goon Squad
Posts: 3038
Joined: Sat Jan 12, 2008 12:18 pm UTC

Re: What is good code?

Postby Steax » Sun Jun 13, 2010 3:39 pm UTC

My benchmark: If I make a project, let it be, and come back 6 months later and I still understand how it works (to the point of being able to fix problems, add features etc), it's good code.
In Minecraft, I use the username Rirez.

User avatar
Thesh
Made to Fuck Dinosaurs
Posts: 6598
Joined: Tue Jan 12, 2010 1:55 am UTC
Location: Colorado

Re: What is good code?

Postby Thesh » Sun Jun 13, 2010 6:11 pm UTC

My criteria is this:

1) Works
2) Easy to follow and understand
3) Not obviously inefficient
4) Easy to change if the requirements change (basically, try not to copy and paste the same logic all over the place)
5) Well organized (if you are familiar with the files, you should be able to look at a function you never saw before and know where it is defined)


However, there isn't just one school of thought, someone I work with feels that aesthetics are more important than efficiency or how easy it is to follow. Basically, if you have a function that looks ugly, start breaking the ugly parts out into separate functions until it looks pretty.
Summum ius, summa iniuria.

qbg
Posts: 586
Joined: Tue Dec 18, 2007 3:37 pm UTC

Re: What is good code?

Postby qbg » Sun Jun 13, 2010 8:14 pm UTC

Really good code should make the solution obvious, and the code should be a clean implementation of the solution.

User avatar
Cleverbeans
Posts: 1378
Joined: Wed Mar 26, 2008 1:16 pm UTC

Re: What is good code?

Postby Cleverbeans » Sun Jun 13, 2010 8:15 pm UTC

Formally verifiable code is good code, and can be recognized as such in the obvious manner. The rest of code is bad, not bad like Hitler but bad like adding the cheese powder to Kraft Dinner before draining the water you cooked the noodles in.
"Labor is prior to, and independent of, capital. Capital is only the fruit of labor, and could never have existed if labor had not first existed. Labor is the superior of capital, and deserves much the higher consideration." - Abraham Lincoln

Timtu
Posts: 21
Joined: Sat Feb 06, 2010 5:58 am UTC

Re: What is good code?

Postby Timtu » Mon Jun 14, 2010 5:22 am UTC

Good code is like elegant mathematics.
Good code IS elegant mathematics, why not.

0xBADFEED
Posts: 687
Joined: Mon May 05, 2008 2:14 am UTC

Re: What is good code?

Postby 0xBADFEED » Mon Jun 14, 2010 2:05 pm UTC

Cleverbeans wrote:Formally verifiable code is good code, and can be recognized as such in the obvious manner. The rest of code is bad, not bad like Hitler but bad like adding the cheese powder to Kraft Dinner before draining the water you cooked the noodles in.

What? Formal verification (currently) quickly spirals out of reach in almost any non-trivial piece of software. What you're saying is silly. There's tons of "good" code that is not necessarily formally verifiable. Good code works. Who cares if it's verified by formal analysis, rigorous test suites, or decades of reliable service.

User avatar
hotaru
Posts: 1045
Joined: Fri Apr 13, 2007 6:54 pm UTC

Re: What is good code?

Postby hotaru » Mon Jun 14, 2010 3:02 pm UTC

0xBADFEED wrote:Formal verification (currently) quickly spirals out of reach in almost any non-trivial piece of software.

are operating systems non-trivial? according to wikipedia, two operating systems were formally verified as of 2009.

Code: Select all

factorial product enumFromTo 1
isPrime n 
factorial (1) `mod== 1

User avatar
styrofoam
Posts: 256
Joined: Sat May 08, 2010 3:28 am UTC

Re: What is good code?

Postby styrofoam » Mon Jun 14, 2010 8:05 pm UTC

hotaru wrote:are operating systems non-trivial? according to wikipedia, two operating systems were formally verified as of 2009.

[citation needed]

[ps] from you, not Wikipedia.
aadams wrote:I am a very nice whatever it is I am.

User avatar
hotaru
Posts: 1045
Joined: Fri Apr 13, 2007 6:54 pm UTC

Re: What is good code?

Postby hotaru » Mon Jun 14, 2010 11:03 pm UTC

styrofoam wrote:
hotaru wrote:are operating systems non-trivial? according to wikipedia, two operating systems were formally verified as of 2009.

[citation needed]

[ps] from you, not Wikipedia.

http://en.wikipedia.org/wiki/Formal_ver ... stry_usage

edit: and here's wikipedia's source: http://www.embedded.com/columns/breakpoint/220900551
Last edited by hotaru on Tue Jun 15, 2010 1:45 am UTC, edited 1 time in total.

Code: Select all

factorial product enumFromTo 1
isPrime n 
factorial (1) `mod== 1

User avatar
Cleverbeans
Posts: 1378
Joined: Wed Mar 26, 2008 1:16 pm UTC

Re: What is good code?

Postby Cleverbeans » Tue Jun 15, 2010 12:54 am UTC

0xBADFEED wrote:Formal verification (currently) quickly spirals out of reach in almost any non-trivial piece of software. What you're saying is silly.


I understand that our current collective standards are low for practical reasons, that doesn't change my opinion however. The end game should be all verifiable code, all the time and I don't see any good reason for giving up on the notion entirely.
"Labor is prior to, and independent of, capital. Capital is only the fruit of labor, and could never have existed if labor had not first existed. Labor is the superior of capital, and deserves much the higher consideration." - Abraham Lincoln

User avatar
Berengal
Superabacus Mystic of the First Rank
Posts: 2707
Joined: Thu May 24, 2007 5:51 am UTC
Location: Bergen, Norway
Contact:

Re: What is good code?

Postby Berengal » Tue Jun 15, 2010 2:34 am UTC

All code is formally verifiable if it's correct, but I don't think that having been formally verified is a sufficient, or even necessary, condition for being good code. Simply being correct should be necessary, and as I said, it's probably the most important quality of good code, but it is by no means sufficient by itself. Now, formal verification is an excellent way of proving correctness and I truly believe that in the future it will become a simpler and easier process than it is today, but being correct is not the same as being proven correct.
It is practically impossible to teach good programming to students who are motivated by money: As potential programmers they are mentally mutilated beyond hope of regeneration.

0xBADFEED
Posts: 687
Joined: Mon May 05, 2008 2:14 am UTC

Re: What is good code?

Postby 0xBADFEED » Tue Jun 15, 2010 1:05 pm UTC

hotaru wrote:
0xBADFEED wrote:Formal verification (currently) quickly spirals out of reach in almost any non-trivial piece of software.

are operating systems non-trivial? according to wikipedia, two operating systems were formally verified as of 2009.

Operating systems are not trivial. But:
seL4 is a third-generation microkernel of L4 provenance, comprising 8,700 lines of C and 600 lines of assembler. Its performance is comparable to other high-performance L4 kernels.
....
About 30 person months (pm)
went into the abstract speci cation, Haskell prototype and
C implementation (over all project phases), including design,
documentation, coding, and testing.
...
The cost of the proof is higher, in total about 20 person
years (py). This includes signi cant research and about 9 py
invested in formal language frameworks, proof tools, proof
automation, theorem prover extensions and libraries. The
total e ffort for the seL4-speci fic proof was 11 py.
We expect that re-doing a similar verifi cation for a new
kernel, using the same overall methodology, would reduce
this figure to 6 py, for a total (kernel plus proof) of 8 py

[seL4: Formal Verification of an Operating-System Kernel, Communications of the ACM, May, 2010 Klein et al]

This is pretty small by OS (or any software) standards and the effort to verify the < 10KLOC was immense. Also, embedded OS's are a very narrow class of software that benefit from having well-understood semantics and conceptual models that exist in a fairly isolated ecosystem. This can also be said of compilers and most other software that has been formally verified in the past.

Perhaps "non-trivial" was too strong a classification, but my main point stands. Formal verification is (currently) out of reach, and not suitable for the overwhelmingly vast majority of software projects. Especially considering that formal verification only checks that software is doing what you think it should be doing. There is no validation that your semantics are correct.
Cleverbeans wrote:I understand that our current collective standards are low for practical reasons, that doesn't change my opinion however. The end game should be all verifiable code, all the time and I don't see any good reason for giving up on the notion entirely.

I'm not saying we shouldn't strive for formal verification of programs, or that people should give up on formal verification research. I think it's something that could be very important in the future. I'm saying that requiring "good code" == "formally verified code" (right now) is ridiculous and arbitrary. It's overly strict while at the same time providing no actual proof that the software behaves correctly, only that it behaves as expected.

If a bug never occurs is it a bug?
Last edited by 0xBADFEED on Tue Jun 15, 2010 11:35 pm UTC, edited 3 times in total.

User avatar
styrofoam
Posts: 256
Joined: Sat May 08, 2010 3:28 am UTC

Re: What is good code?

Postby styrofoam » Tue Jun 15, 2010 6:23 pm UTC

0xBADFEED wrote:This is pretty small by OS (or any software) standards and the effort to verify the < 10000 lines of code was immense. Also, embedded OS's are a very narrow class of software that benefit from having well-understood semantics and conceptual models that exist in a fairly isolated ecosystem. This can also be said of compilers and most other software that has been formally verified in the past.

Anyone want to verify Debian? :mrgreen:
aadams wrote:I am a very nice whatever it is I am.

Parsifal
Posts: 113
Joined: Thu Feb 28, 2008 1:35 am UTC

Re: What is good code?

Postby Parsifal » Thu Jun 17, 2010 10:00 am UTC

Good code is SOLID code. What does this mean? SOLID is an acronym I only recently learned, but it describes a set of properties common to 'good code' I have learned over the course of years reading and debugging BAD code.

S - single purpose, a class does one thing only
O - open/closed, if you open resources, close them locally
L - Liskov substitution principle, you should be able to substitute a subclass for a superclass without the user needing to know about it
I - Interface separation, or don't force the user to depend on interfaces that are irrelevant to the task at hand
D - Dendency injection. Use interface in the code and a DI mechanism to configure implementation.

This is a partial description of what makes good code. Of course, there are many other concerns as well, particularly making code human-readable, not repeating oneself in the code and optimizing for performance only when it is appropriate.

stephentyrone
Posts: 778
Joined: Mon Aug 11, 2008 10:58 pm UTC
Location: Palo Alto, CA

Re: What is good code?

Postby stephentyrone » Thu Jun 17, 2010 2:07 pm UTC

Parsifal wrote:Good code is SOLID code. What does this mean? SOLID is an acronym I only recently learned, but it describes a set of properties common to 'good code' I have learned over the course of years reading and debugging BAD code.

S - single purpose, a class does one thing only
O - open/closed, if you open resources, close them locally
L - Liskov substitution principle, you should be able to substitute a subclass for a superclass without the user needing to know about it
I - Interface separation, or don't force the user to depend on interfaces that are irrelevant to the task at hand
D - Dendency injection. Use interface in the code and a DI mechanism to configure implementation.

This is a partial description of what makes good code. Of course, there are many other concerns as well, particularly making code human-readable, not repeating oneself in the code and optimizing for performance only when it is appropriate.


This might be good object oriented design, but I don't think it's what Berengal is talking about. I could be wrong, of course.

I would say that a necessary (but not sufficient) condition for good code is that a skilled programmer who doesn't know the language in which the code is written can easily read the code and understand the program's function.

Edit: I originally claimed that "an immediate corollary is that there is no good code in Brainfuck of Unlambda; all code in those languages is evil." Upon reflection, either this is true for each of those languages, or it is true that only someone who knows said language can be a "skilled programmer".
Last edited by stephentyrone on Thu Jun 17, 2010 11:01 pm UTC, edited 1 time in total.
GENERATION -16 + 31i: The first time you see this, copy it into your sig on any forum. Square it, and then add i to the generation.

User avatar
Berengal
Superabacus Mystic of the First Rank
Posts: 2707
Joined: Thu May 24, 2007 5:51 am UTC
Location: Bergen, Norway
Contact:

Re: What is good code?

Postby Berengal » Thu Jun 17, 2010 5:36 pm UTC

stephentyrone wrote:This might be good object oriented design, but I don't think it's what Berengal is talking about. I could be wrong, of course.
Well, object-oriented design is important, but I believe there are some underlying principles that are common to all good code irrespective of language or paradigm.

stephentyrone wrote:I would say that a necessary (but not sufficient) condition for good code is that a skilled programmer who doesn't know the language in which the code is written can easily read the code and understand the program's function.
This sounds good. I'd allow a language cheat-sheet explaining the syntax though, and I don't think any programmer, no matter how skilled, would easily be able to understand a programmer written in a paradigm he's unfamiliar with.

Timtu wrote:Good code is like elegant mathematics.
Good code IS elegant mathematics, why not.
I used to be pretty sure about this, but not anymore. Clever hacks are sometimes mistaken for elegant code, and I've read code that was so incredibly simple and obvious that it seemed trivial and not at all elegant. This is not to say that elegance isn't a good measure, because most of the time good code is elegant and vice versa, but not always.

Simplicity, on the other hand, is a quality I've always found in both good and elegant code. It also requires deep thought and hard work to get right, but it's hard to spot when you get it wrong. I've still to discover just what simplicity really is; for now I just go by my gut feeling and how large my brain-stack grows during parsing.
It is practically impossible to teach good programming to students who are motivated by money: As potential programmers they are mentally mutilated beyond hope of regeneration.

User avatar
Kow
Posts: 195
Joined: Sat Apr 19, 2008 12:37 pm UTC

Re: What is good code?

Postby Kow » Sun Jun 20, 2010 12:46 am UTC

Berengal wrote:Good code is still good code even if the requirements (and hence the problem) change, something that happens all the time.

I have to agree wholeheartedly with this. It's the prime reason interfaces are good. You can change the way something works behind the scenes to account for some change elsewhere and not have to go through every place you've used that code to update it.
Image

User avatar
MHD
Posts: 630
Joined: Fri Mar 20, 2009 8:21 pm UTC
Location: Denmark

Re: What is good code?

Postby MHD » Wed Jun 23, 2010 1:39 am UTC

Good code must be:

Easy to read
Easy to understand
Easy to maintain

Good code should be:

Well Commented
Efficient
Idiomatic

Good code can be:

Elegant
Optimized
Hackingly Awesome

Triple Easy - Good code.

Reverse the order and you get Fun code

Fun code - Elegantly Optimized Hacks
EvanED wrote:be aware that when most people say "regular expression" they really mean "something that is almost, but not quite, entirely unlike a regular expression"

User avatar
You, sir, name?
Posts: 6983
Joined: Sun Apr 22, 2007 10:07 am UTC
Location: Chako Paul City
Contact:

Re: What is good code?

Postby You, sir, name? » Wed Jun 23, 2010 4:09 am UTC

MHD wrote:Good code must be:

Easy to read
Easy to understand
Easy to maintain

Good code should be:

Well Commented
Efficient
Idiomatic

Good code can be:

Elegant
Optimized
Hackingly Awesome

Triple Easy - Good code.

Reverse the order and you get Fun code

Fun code - Elegantly Optimized Hacks


I don't agree that good code necessarily needs to be well commented. I would like to replace it with "self-explanatory". Which by all means -may- be through comments, but not necessarily.
I edit my posts a lot and sometimes the words wrong order words appear in sentences get messed up.

User avatar
Thesh
Made to Fuck Dinosaurs
Posts: 6598
Joined: Tue Jan 12, 2010 1:55 am UTC
Location: Colorado

Re: What is good code?

Postby Thesh » Wed Jun 23, 2010 4:42 am UTC

You, sir, name? wrote:I don't agree that good code necessarily needs to be well commented. I would like to replace it with "self-explanatory". Which by all means -may- be through comments, but not necessarily.

I agree. Honestly, I find that overcommenting is worse than undercommenting. I hate it when I see stuff like this:

Code: Select all

//increment x by 1
x++;
Summum ius, summa iniuria.

cathrl
Posts: 427
Joined: Tue Jan 30, 2007 9:58 am UTC

Re: What is good code?

Postby cathrl » Wed Jun 23, 2010 11:11 am UTC

I don't agree that good code necessarily needs to be well commented. I would like to replace it with "self-explanatory". Which by all means -may- be through comments, but not necessarily.


I'd even go further than that. Any code which is incomprehensible without comments is not good code. (That's not to say that you can never have a line of code which needs a comment to help make it understandable. But in general you should be able to see roughly what's going on. Comments should be additional assistance, not necessary in order to have any hope of deciphering it.)

User avatar
Jplus
Posts: 1721
Joined: Wed Apr 21, 2010 12:29 pm UTC
Location: Netherlands

Re: What is good code?

Postby Jplus » Wed Jun 23, 2010 12:29 pm UTC

If we change "well commented" into "self-explanatory", I think MHD's "triforce" would be a good candidate for some kind of intersubjective definition of good code. However, there's also a qualitative, phenomenological aspect to code being good which I think you can't really express in language. Within the first few seconds looking at some piece of code, I get a feeling which tells me, "this is (not) good code". I doubt whether introspection upon such a sensation would always lead me to characteristics of the code that can be expressed like in MHD's "triforce".

Probably whether something is good code can also depend on the goal of the code.
"There are only two hard problems in computer science: cache coherence, naming things, and off-by-one errors." (Phil Karlton and Leon Bambrick)

coding and xkcd combined

(Julian/Julian's)

User avatar
MHD
Posts: 630
Joined: Fri Mar 20, 2009 8:21 pm UTC
Location: Denmark

Re: What is good code?

Postby MHD » Thu Jun 24, 2010 5:06 pm UTC

Thesh wrote:
You, sir, name? wrote:I don't agree that good code necessarily needs to be well commented. I would like to replace it with "self-explanatory". Which by all means -may- be through comments, but not necessarily.

I agree. Honestly, I find that overcommenting is worse than undercommenting. I hate it when I see stuff like this:

Code: Select all

//increment x by 1
x++;


Well, "well commented" and "ridiculously over-commented" is two very different things...
EvanED wrote:be aware that when most people say "regular expression" they really mean "something that is almost, but not quite, entirely unlike a regular expression"

User avatar
Pesto
Posts: 737
Joined: Wed Sep 05, 2007 5:33 pm UTC
Location: Berkeley, CA

Re: What is good code?

Postby Pesto » Sat Jun 26, 2010 12:15 pm UTC

Berengal wrote:Clever hacks are sometimes mistaken for elegant code, and I've read code that was so incredibly simple and obvious that it seemed trivial and not at all elegant.

This is an idea I was going to put forward, and I'd sum it up in two points. Good code is elegant. Good code is not "clever".

Something I've recently learned is that good code should be appropriate. For example, let's say you're validating some input to a class. You are passed ten integers and an email address. In stead of having integer validation in ten different places, it makes sense to have an integerValidation() function that is called from ten places. However, it may not be appropriate to write an emailValidation() function. Since email validation is only needed in one place, it may be most appropriate to put the validation in line the one place it's needed.

Good code should use tools the way they were intended to be used. If you have a wrench and a screwdriver, don't put your app together with nails. Or, go get yourself a hammer.

lgw
Posts: 437
Joined: Mon Apr 12, 2010 10:52 pm UTC

Re: What is good code?

Postby lgw » Thu Jul 15, 2010 11:10 pm UTC

Pesto wrote:This is an idea I was going to put forward, and I'd sum it up in two points. Good code is elegant. Good code is not "clever".


The essense of good code is clarity. Your intention should be clearly expressed. It should be easy for a maintainer to understand what you wanted. It should be easy for the compiler/optimizer to understand what you wanted. "Elegant" is occasionally the wrong thing: I occasionally find a terse, beautiful way to solve a problem, but it's not obvious why it works, or it needs some rarely-used language feature, or it would otherwise be hard for a junior programmer to maintain. This is where art and engineering stop overlapping.

This is the same as good prose in any language. It's when you start mistaking code for poetry, instead of prose, that "clever" starts being a problem (which is fine for code to print out and hang on the wall, but not so much for production).

Also: proving code correct is a crock. Yes, code review is always good, and proving correctness is useful in that way, but all your "proof" does is move the problem. Now you have to prove that you proof-of-correctness is correct, and I bet that's a lot less clear than the code.
"In no set of physics laws do you get two cats." - doogly

User avatar
Pesto
Posts: 737
Joined: Wed Sep 05, 2007 5:33 pm UTC
Location: Berkeley, CA

Re: What is good code?

Postby Pesto » Thu Jul 15, 2010 11:22 pm UTC

lgw wrote:
Pesto wrote:This is an idea I was going to put forward, and I'd sum it up in two points. Good code is elegant. Good code is not "clever".


The essense of good code is clarity. Your intention should be clearly expressed. It should be easy for a maintainer to understand what you wanted. It should be easy for the compiler/optimizer to understand what you wanted. "Elegant" is occasionally the wrong thing: I occasionally find a terse, beautiful way to solve a problem, but it's not obvious why it works, or it needs some rarely-used language feature, or it would otherwise be hard for a junior programmer to maintain. This is where art and engineering stop overlapping.

This is the same as good prose in any language. It's when you start mistaking code for poetry, instead of prose, that "clever" starts being a problem (which is fine for code to print out and hang on the wall, but not so much for production).

As with most anything in programming, you will have different dimensions at odds with each other. In this case, clarity and elegance. That's where you need to draw on your experience to make a decision how much of one you're going to give up for the other. If you're giving up clarity in exchange for something else, I think the solution is to add comments.

Also: proving code correct is a crock. Yes, code review is always good, and proving correctness is useful in that way, but all your "proof" does is move the problem. Now you have to prove that you proof-of-correctness is correct, and I bet that's a lot less clear than the code.

I don't think this is correct. I've never actually done any proofs of code, but I don't think you have to "proove your proof" in coding any more than you do in mathematics. This is probably a discussion for another thread, though.

User avatar
WarDaft
Posts: 1583
Joined: Thu Jul 30, 2009 3:16 pm UTC

Re: What is good code?

Postby WarDaft » Fri Jul 16, 2010 1:56 pm UTC

I don't think this is correct. I've never actually done any proofs of code, but I don't think you have to "proove your proof" in coding any more than you do in mathematics. This is probably a discussion for another thread, though.
What you do is prove that the code computes some exact mathematical model.

You really do have to be sure the model is the one you want however.
All Shadow priest spells that deal Fire damage now appear green.
Big freaky cereal boxes of death.

User avatar
tetsujin
Posts: 426
Joined: Thu Nov 15, 2007 8:34 pm UTC
Location: Massachusetts
Contact:

Re: What is good code?

Postby tetsujin » Fri Jul 16, 2010 8:20 pm UTC

qbg wrote:Really good code should make the solution obvious, and the code should be a clean implementation of the solution.


...Except that's worthless if the code in question doesn't run fast enough. :)

I have to put "works" as the first criterion - and whether code "works" in this case includes consideration of whether it's fast enough. (Can the codec decode video in real-time? When I run my program on a real-world kind of input, does it take hours, days, weeks, or months to run? Or does memory fragment to the point that running such examples is simply impractical?)

There are times when breaking encapsulation, or writing code that's below typical comfort levels for "clean" code, is worthwhile. Admittedly, I'd say the vast majority of cases don't call for this, but at times it is very important.
---GEC
I want to create a truly new command-line shell for Unix.
Anybody want to place bets on whether I ever get any code written?

User avatar
WarDaft
Posts: 1583
Joined: Thu Jul 30, 2009 3:16 pm UTC

Re: What is good code?

Postby WarDaft » Sat Jul 17, 2010 12:29 am UTC

...Except that's worthless if the code in question doesn't run fast enough.
I was thinking about various things, and I thought, "you know what would be nice? If there was a distributed computation project that continually optimized code."

You set up a language with emphasis on simplicity, readability, and conciseness. The code is compiled dynamically with the fastest known optimizations for any functions used. Every computer with the platform installed spend some of it's free time searching for equivalent but more optimized forms of commonly executed functions (this could include the optimizing function) - this would probably work best if the language was almost but not actually Turing complete, so that we can actually decide more things about the functions. Then, when coding, you code the simplest form of the type you're actually aiming for. If you want a sort, you code a bubble sort, for example. When the code is run, it is dynamically complied with one of the best known equivalents of the bubble sort, depending on the situation. Equivalents would be stored and shared online, and the source code for an equivalent to a function would simultaneously be a proof that it is an equivalent and more optimized form. The optimizations would not have to entirely be computer generated, so long as it is provable that any optimization that claims to be an optimized equivalent is discernibly either so or not so by the compiler.

There are problems obviously, but the up sides include that a "properly" written program could continue to be optimized gradually but indefinitely (depending on how often the functions it utilizes are used) without any further input from the program's original developers, and that an arbitrarily optimized program could have the same level of readability as the simplest way to write the program.
All Shadow priest spells that deal Fire damage now appear green.
Big freaky cereal boxes of death.

Notch
Posts: 318
Joined: Tue Dec 12, 2006 5:52 pm UTC
Location: Stockholm, Sweden
Contact:

Re: What is good code?

Postby Notch » Sat Jul 17, 2010 8:17 am UTC

Another important factor of good code is the terminology used. It should be carefully considered, so that names are immediately clear and understandable to anyone who understands the problem space. If you're making a Buffy the Vampire Slayer simulation that takes place in a forest, the Willow class is probably not a tree (which could be WillowTree instead, perhaps).

User avatar
Pesto
Posts: 737
Joined: Wed Sep 05, 2007 5:33 pm UTC
Location: Berkeley, CA

Re: What is good code?

Postby Pesto » Sun Jul 18, 2010 8:37 am UTC

WarDaft wrote:If you want a sort, you code a bubble sort, for example. When the code is run, it is dynamically complied with one of the best known equivalents of the bubble sort, depending on the situation.

I don't know that this is really feasible. This would require that the computer be able to determine the intent of a piece of code.

User avatar
hotaru
Posts: 1045
Joined: Fri Apr 13, 2007 6:54 pm UTC

Re: What is good code?

Postby hotaru » Sun Jul 18, 2010 2:49 pm UTC

Pesto wrote:
WarDaft wrote:If you want a sort, you code a bubble sort, for example. When the code is run, it is dynamically complied with one of the best known equivalents of the bubble sort, depending on the situation.

I don't know that this is really feasible. This would require that the computer be able to determine the intent of a piece of code.

it'd probably be doable with a language like prolog...

Code: Select all

factorial product enumFromTo 1
isPrime n 
factorial (1) `mod== 1

troyp
Posts: 557
Joined: Thu May 22, 2008 9:20 pm UTC
Location: Lismore, NSW

Re: What is good code?

Postby troyp » Sun Jul 18, 2010 10:59 pm UTC

Like WarDaft said, it would be possible in a non-Turing-complete (Turing-incomplete?) language. One that works with function composition based on a set of primitive functions would be good, since you could, in principle, test functions for equality and other relationships.

For instance, J can do things like automatically find inverses of functions (as long as they're defined using J's primitive functions and compositional operators, rather than the optional lambda expressions).
I'm curious to know if languages like Joy and Cat have anything like this.

IDrinkIamThunk
Posts: 12
Joined: Tue Dec 09, 2008 11:52 am UTC

Re: What is good code?

Postby IDrinkIamThunk » Wed Jul 21, 2010 12:05 pm UTC

How do we write it?
How do we recognize it?

If we take Knuth's tex, for example, the current version is stable (ZERO defects) and has been so for a while and has great performance and does its job extremely well and it is well maintainable(if it needs maintenance). And it is written with amazing clarity.
BUT, if some one (especially one without the reputation of Knuth) develops a program like it today, would it still be considered a good program? Am not so sure.

Gregory Chaitin said to the effect of "program = frozen thought" in MetaMath(http://www.amazon.com/Meta-Math-Quest-Gregory-Chaitin/dp/1400077974).
Maybe we have to evaluate code using the same yardsticks as those we use for evaluating 'thinking'.
A good thought is something that has clarity, consistency and some sense of economy (in terms of underlying assumptions) and being as close to current reality as possible. And it also implies, goodness is an ephemeral property especially when it comes to code. (Perhaps that is how it links to maintainability).

But then there are er.. some schools of thought (Robert Pirsig is an example of such, I think) have very strong opinions on whether 'goodness' is a property of the object or not.
Perhaps, to paraphrase the wise tortoise Master Oogway (of 'Kung fu' panda), there is no good code or bad code. There is only code.


Return to “Coding”

Who is online

Users browsing this forum: No registered users and 10 guests