Live data from Hacker News

The Shapes of Code

fluentcpp.com

21–30 of 71 posts

Re: The Shapes of Code

#21
High indentation has always been a good heuristic for high complexity.

More indentation equals more mental context required.

But although these shorter, less-indented functions are inherently easier to understand, they're not necessarily freer of bugs...

Re: The Shapes of Code

#22

While the patterns themselves are interesting, the idea that all code must be rearranged in to smaller functions to be “refactored” seems a little juvenile.

I can agree "code should be in small functions" by itself is a bad guideline. I think it's suggested due to other principles around making code easier to read / use.

It'd be better to ask if the code is more complex than it needs to be.

I liked the terms John Ousterhout uses. For some module of a program (e.g. a method, class, package, etc.), it has an interface and an implementation. The interface is "what you need to know to use the module", the implementation is "how it's done".

Complexity comes from dependencies (e.g. more interfaces you need to know about), and from obscurity (e.g. things you need to know about, which aren't obvious from the interfaces given).

In some cases, many small methods would be an increase in complexity overall. In some cases, one big method may be an increase in complexity.

Re: The Shapes of Code

#23
> ... not so good code: if it was, we wouldn’t need a comment...

How much longer will this rumor persist that "good code" doesn't need commenting? Good comments explain things that aren't immediately obvious from the code.

Re: The Shapes of Code

#25

I'm glad I'm not the only one who pays attention to this. This is completely unsubstantiated and coming from a relatively inexperienced programmer, but it's been my experience that better code tends to produce nicer shapes. An obvious example not mentioned in the article arises when code is nested too deeply and too thinly, creating jarring and disproportionate peaks.

I review a lot of code for work and I agree with you that you can usually tell the quality of the code simply by looking at its outward appearance. Rough edges and bizarre shapes almost always indicate poor, unstructured thinking in the code itself.

Not saying I go by shape alone, but if it looks messy, that's a warning flag.

Re: The Shapes of Code

#26

> ... not so good code: if it was, we wouldn’t need a comment... How much longer will this rumor persist that "good code" doesn't need commenting? Good comments explain things that aren't immediately obvious from the code.

Like what? It can’t be the “how” - the code should be able to express that clearly. It can’t be the “what”, again that should be apparent from some combination of code and configuration / metadata.

So that leaves the “why”. To my mind, code comments are the worst place to record why a particular implementation exists. The why often needs collaboration with non-coders.

Re: The Shapes of Code

#27

While the patterns themselves are interesting, the idea that all code must be rearranged in to smaller functions to be “refactored” seems a little juvenile.

https://news.ycombinator.com/item?id=8374345

Since that blog seems to be down at the moment here's a cached version of the article/email.

https://webcache.googleusercontent.com/search?q=cache:http:/...

Re: The Shapes of Code

#28

> ... not so good code: if it was, we wouldn’t need a comment... How much longer will this rumor persist that "good code" doesn't need commenting? Good comments explain things that aren't immediately obvious from the code.

Most code should be boring and immediately obvious. I don't run into lots of real world code that needs much explaining. Sometimes you do something weird or not-obvious for good reason, but I see plenty of comments explaining things that only need to be explained due to poor design. Also poor comments that don't really explain anything that isn't obvious by looking.

Re: The Shapes of Code

#29

> ... not so good code: if it was, we wouldn’t need a comment... How much longer will this rumor persist that "good code" doesn't need commenting? Good comments explain things that aren't immediately obvious from the code.

A rule of thumb I follow is that code is twice as hard to read as it is to write so comments may of course help but in the other case comments that have to overly explain what the code is doing or why it's doing it also indicate that the code ought to be revised

My process of writing code is to of course code a proof of concept in order to get a feel for the problem domain this could be considered a spike in agile parlance

Then the primary and alternate flows can be coded against a basic test suite

followed by a refactor and then a comments pass

Depending on what comments arise perhaps another refactor may be in order

I typically try to follow the spirit of TRUE software

Re: The Shapes of Code

#30

> ... not so good code: if it was, we wouldn’t need a comment... How much longer will this rumor persist that "good code" doesn't need commenting? Good comments explain things that aren't immediately obvious from the code.

[deleted]
Post reply on HN