Beautiful Code: Leading Programmers Explain How They Think (Theory in Practice (O'Reilly))

Category: Programming
Author: Andy Oram, Greg Wilson
3.5
All Stack Overflow 11
This Year Stack Overflow 3
This Month Stack Overflow 1

Comments

by akkartik   2022-09-02
I kinda dislike the premise of your question, but https://www.amazon.com/Beautiful-Code-Leading-Programmers-Pr... has some good candidates. Chapter 1 for example is a regular expression matcher that appeared on HN recently: https://news.ycombinator.com/item?id=32589311
by drivers99   2020-02-16
I thought I had seen it before. Verified you can actually see the whole chapter in a nicer (book vs web) layout in Amazon's "Look inside" feature since it's at the beginning of the book.

https://www.amazon.com/Beautiful-Code-Leading-Programmers-Pr...

by anonymous   2017-08-20

Should I using other patterns?

No, you should not insist on a single pattern.

No design pattern books will ever advise you to use a single pattern. Just like you cannot chop all ingredients in one single way (are you going to dice the spaghetti?), you cannot organise all logic in one single pattern.

Sure, you can make all your Objects use the initialiser pattern, and don't use constructors at all. This is ok. Been there, done that. I like it.

But these objects can be used with Builder or Abstract Factory (if it make things simpler). As long as the builders/factories themselves have initialiser, and that they properly initialise the created objects, then your use of the initialiser pattern will be consistent. Outside of creational patterns, it is usually good to organise objects with structural and behavioural patterns. They do not conflict with initialiser at all.

For example, look at DOM. All nodes are created by the Document object - elements, text nodes, comments, even events. This is the Factory pattern.

Yet the Document object is also a Facade! From it you access the whole system's status, objects, data, you can even write to it! Every DOM operation starts from the Document, it is the Facade of the DOM system.

DOM nodes also implements multiple patterns. They are organised in Composite, let you listen to events with Observer and Command, and handle events in a Chain of Responsibility. They are certainly parsed by an Interpreter, DocumentFragment is a Proxy, svg elements are implemented as Decorators, and createNodeIterator obviously gives you an Iterator.

The point is, good object-oriented design will yield multiple design patterns as a result, intentional or not.


What are the secrets for good code appearance

I think the best looking code is the one that is easiest to understand to you, and the way you read code changes as you gain more experience.

For example my style is too condensed for most programmers, but to me it strikes a good balance. So do develop your own style - you are not me, and you are not yesterday's you either.

Remember this as we go through the styles.

At the lowest level we have coding style - most importantly indent and bracket.

This one is simple, pick the one you like and stick with it. There are language specific styles, and they are often good starting points. Configure your IDE's formatter so that you can format all your code with hotkey.

Above the code syntax we have comment style and naming convention.

Setting rules on comment is fine, sometimes it is necessary for documenting tools. Avoid too much comment in practice. You may also want to decide your namespace and your stand on naming function expressions.

Above these structures, we have logic conventions.

The same code logic can often be done in many ways, some more 'beautiful' than the others in your eyes. Look at this example.

I picked the second style on first sight: no duplicate, logic is sectioned cleanly, format is not my style but reasonable. But many programmers would prefer the first style: logic is plain as day, a few duplications is worth it. While abstract, this level is quite deep - present your logic the wrong way actually increase the chance an experienced programmer read it wrong.

Finally, we arrives at the level of design pattern, about as far as code beauty goes.

The key to keep your code structure beautiful, is using the right patterns at right level to consistently accomplish loose coupling and code reuse, while avoiding pitfalls and over-design.

There are quite some books about beautiful code, and then there are even more books about designing and implementing beautiful software. (Decide for yourself which are beyond your level.) Knowledge is as important as experience, and you can gain them only by spending your time to study, to write, and to revise/refactor your apps.

Feel free to change your mind as you explore and experiment with your code. Changing your mind is a good sign of learning.

But first, familiarise yourself with design patterns. Just don't forget, they are the generic result of applying object-oriented principals to common tasks. It is still up to you to do the design.

Design Patterns Are Not Silver Bullets.

by anonymous   2017-08-20

I was a self taught coder not so long ago so I can relate to your position. Since then I've shipped 10 Millions LOC applications, so I had to develop discernment. 3 things helps me to get software engineering : books, code and people.

First of all, good engineering is about beauty, the beauty of an elegant design, of an efficient mecanism. You need to develop that sense for beauty and to do that you need to see it and discuss it.

Books like SICP, Refactoring, Head First Design Patterns and Beautiful Code are all about beautiful/better code. Most of the time they are not about a language per se (or about exotic languages). Code complete is also interesting but somehow more low level.

Online reading of code is also a good way to educate your taste. I have no example in mind but this is a common question, you should easily find some ideas. Also, building an ambitious application (a game, 3d modeler, web server, database) is a good practive : you're likely to redo it 2 or 3 times to have a better architecture.

People is also very important. If you find a/a few passionate software architect(s) to discuss with, you will be able to confront your view on what is beauty. I am still digesting some discussion I had with my manager a few years ago.

This "enlighment" is an ongoing process, so don't get discouraged and just move forward. One day, you'll see ugly code with an obvious alternative implementation, one you could discuss and compare objectively as better than the original.

by anonymous   2017-08-20

You can try this book - Beautiful Code

The author has collected some experience-sharing articles of open source projects. e.g. Python's Dictionary Implementation, Subversion's Delta Editor, etc.

by anonymous   2017-08-20

I recommend you read Beautiful Code. It's a great example of what you're after.

alt text

by anonymous   2017-08-20

[Edit 16-Jan-2015] I recently encountered this beautiful book Beautiful Code. I recommend you go through Chapter 1, "A Regular Expression Matcher" by Brian Kernighan.

You can read the classic paper by Ken Thompson, "Regular expression search algorithm" ... http://portal.acm.org/citation.cfm?doid=363347.363387 ... this paper should give you a good understanding on how regular expressions are matched using finite automata.

This is another page giving some detailed information by Russ Cox ... http://swtch.com/~rsc/regexp/

Hope these help you get started.

by olalonde   2017-08-19
> How to write English (non-native language)

I suggest writing a blog in English. Practice is probably the best way to learn. There is also http://english.stackexchange.com if you have specific questions.

> Program well in c in userspare (Currently reading 'The Linux Programming Interface')

In my experience, reading other people's code is the best way to get better at a language. I don't have much experience with C but I guess there are a few projects that would be interesting to read here: https://github.com/search?o=desc&q=language%3A%22c%22&ref=si...

Relevant links: http://stackoverflow.com/questions/925754/resources-for-lear..., http://www.amazon.com/Beautiful-Code-Leading-Programmers-Pra...

> Embedded development (I don't even know where to start that one...)

I am currently learning embedded development (maybe we could help each other?).

by deutronium   2017-08-19
You might be interested in http://www.amazon.com/Beautiful-Code-Leading-Programmers-Pra...

I believe one of the examples they gave in C was the diff algorithm from Subversion.

by jamii   2017-08-19
Have a look at the book 'Beautiful code'

http://www.amazon.com/Beautiful-Code-Leading-Programmers-Pra...