A description like
"I went off the platform and then on the train and then I looked at the nearest seat and then I saw it was occupied and then I looked at the next seat and then I saw that was occupied too snd then I repeated that and then I saw an empty seat and then I walked toward it and then I sat down and then ..."
is begging for structure by wrapping up into procedures
"I boarded the train, and then I located the closest free seat, and then I sat down in it ..."
with the obvious definitions. That can in turn can be further improved to
"After boarding the train I sat down in the first free seat ..."
where the relations between the things are more useful than "and then".
I often wish for a more rich way to layout a complex 2-level decision tree. Dividing it up into sub-functions/classes scatters the logic, while putting it all together is too heavy. However a 2D decision table would often be perfect.
I’ve just tracked it down. Here’s something about it by the same author Roedy Green (author of How to Write Unmaintainable Code) http://mindprod.com/project/scid.html (look for the words ‘decision table’)
How about Python? It lets you put your procedures in tables - dispatch tables, that is:
def eat(): print "eating"; def walk(): print "walking"
t = {"eat": eat, "walk": walk}
# now call the reqd. function via string s read from somewhere: keyboard, file, etc. ...
t[s]() # error handling omitted
Kidding apart:
>Dividing it up into sub-functions/classes scatters the logic
What is the issue with scattering the logic? if you break up your problem/solution into different logical units, with good judgement as to the points of breakup (i.e. coupling, cohesion, etc.), then what is the issue? Not clear.
A good point of comparison is technical writing. Think back to the best textbooks you read: they didn't have the flourishes and sophistication of literary writing, but they still had a significantly richer structure and organization than, say, a children's book. They needed this richer structure to get their ideas across effectively. You can't write a coherent textbook in the style of Where's Spot?
The same goes for code. You can be too clever, sure, but you can also not be clever enough. On a scale from "Salman Rushdie" to "Clifford the Big Red Dog", you don't want to be at either extreme.
Is this hard? Yes! Writing readable code is a skill unto itself, distinct from writing working code. People don't always agree on what is and isn't readable, but that's true for writing too. Still a worthy goal, just one that doesn't lend itself to simple rules. You just have to build up the right skills from experience.
Not sure what level of clever Salman Rushdie is acceptable for?
We don't talk about c-groups and compressed files with metadata, we talk about containers and images. We don't talk about individual machines running a number of highly specialized daemons with configuration files, cryptographic keys, and networks-in-networks, we talk about clusters and pods and gateways.
Notwithstanding that much of the code which has built up these sweeping generalizations is built of a tangle of red dogs, the abstractions are very high level and mean we rarely have to discuss infrastructure at a very low level.
Of course, there's a twist to that as well, that hearkens back to an earlier statement: the understanding have been limited to an extremely high level as well. The number of people who can troubleshoot a container cluster is small, and growing smaller. We've intentionally created cliff notes of "Salman Rushdie" novels and believe that's all the populace needs.
Salman Rushdie style is reserved for libraries that make a massive impact. Learning abstractions like that is like learning a new part of the language so it has to really pay off. Rare but not impossible.
The Haskell lens[1] library is a great example. It's almost too clever for its own good and learning it is like learning a new programming language, but it is such an improvement for the entire codebase it's worth it. I use it widely at work and it pays for its own difficulty almost immediately. (It's also important that it can't achieve some of its core functionality without the "clever" things it does.)
The documentation could use a bit of work though :/.
I'm not sure if it is really appropriate for any code to be more than Robert Frost clever, sometimes think that should be clever enough for anyone. But this is only a periodic opinion.
on edit: I guess that means I'm saying no language needs to be more clever than Python.
I believe the the art of programming is actually writing the code in a way that transitions the reader through context changes keeping the amount of context necessary to understand what's going on to a minimum.
One of the best things about humans is how easy it is for us to work at multiple levels of abstraction simultaneously, as long as we're guided to and between them properly. Good code - hell, good engineering - exploits this to create things which otherwise wouldn't fit in a person's head.
The problem is, the location of the sweet spot depends on the reader (or at least, on their familiarity with the idioms used...)
I jest, but I agree with your statement.
Programming is specialized work (I'd call it a discipline, but then we'd have to argue whether it's more an engineering discipline or art discipline). You have to gain knowledge and experience to understand it.
A bridge blueprint is not understandable for all. It is understandable for all who took time and effort to gain education necessary to understand it.
(The motivation between this comment is to counter the increasingly noticeable sentiment in our industry that everything needs to be dumbed down to the lowest common denominator; it's an understandable sentiment if you're selling something and want the biggest market, but it goes against what's needed to build great things.)
That kind of ought to be the default for most code. However, it's the natural tendency of code to become abstruse and unnecessarily complicated.
Since market forces will often tend to exploit rather than reward the work done by programmers to "dumb their code down" and inadvertently reward "clever magic understood by a limited number of experts" there's more of an incentive to amplify this effect than to work against it - especially where money is involved.
I'm a bit conflicted because on the one hand I don't want to help developer compensation get ground down by billionaires or other business owners with an entitlement complex who consider developers to be "spoiled brats", but I do prefer working with clean, straightforward code.
For example, take code generation (I actually have in mind a SQL query generator, but a compiler back end would do just as well). The problem is already abstract - the input is code, you're naturally writing code that manipulates code, like you would with macros or reflection. The problem is complex: you can generate simple code, but it won't perform well. There's irreducible complexity here that cannot be simplified away.
You'd also like the code generation itself to be fast. That puts a limit on how much you can break it up into parts that can be understood individually and in isolation. Optimization works in opposition to abstraction because the optimal often requires steps that span multiple abstraction layers, and often requires multiple instances of the specific over few instances of the general.
Personally, I think the two biggest reasons code becomes unnecessarily complicated these days are (a) testing and (b) local modifications. Unit testing in particular encourages over-parametrization so that dependencies can be replaced for the purposes of testing; while normal software maintenance under commercial pressure leads to local modification because nobody has time to understand the whole. People instead make conservative local changes by adding parameters, extra if-statements, local lookup maps, etc.
I've found elegant solutions are often on the other side of a hill from over-engineering. You write specific solutions, then you climb the a hill of abstraction as you add layers, indirections, parameters etc., until you reach a summit, where you can see the whole, and can then start boiling things back down again, only retaining abstraction where it's actually necessary, or perhaps replacing multiple abstractions with a single more powerful abstraction (I've found this to happen a lot with monads; another one is converting control flow into data flow).
Yeah sometimes you do, and that is exactly the kind of problem that is irreducibly complex, but I think new kinds of problems like this don't tend to crop up in the wild very often and when they do they tend to show up in subtle and non-obvious ways.
The problem you've described is far from a new problem - it's the same problem space that is covered by ORMs. Furthermore, if I were working on a team where a developer has uttered the words "I've created my own ORM" (or something to that effect), my face has probably already landed in my palms.
The rest of what you wrote I'm in vigorous agreement with though- especially the parts about unit testing, local modifications and "the other side of the hill". Seen all of that.
This is digression.
I have in mind something I wrote, the most complex piece of code I've written in the past couple of years. It isn't actually well covered by ORMs. ORMs are usually tuned for (a) static schemas, and (b) graph navigation in OO-style. Give them a problem like "here's a filter in the form of a syntax tree, please give me the top 100 results from this 10 million line table" - where the table schema is determined at runtime - well, most ORMs can't even answer this question because schemas are assumed to be static. And if you want to control the join order using nested subtable joins, with predicates that don't need joins pulled out of the filter expression and pushed down, because MySQL's optimizer observably doesn't reliably do the right thing, ORMs don't give you that control.
It wasn't an ORM kind of problem; think more something like a read-only Excel spreadsheet, but with typed columns, and a rich autofilter, on the web, scaling to millions of rows. The output of the database query is a page of tuples, not objects in any behavioural sense.
In some ways a database seems like the wrong solution, but morphologically it's exactly right: the user data is rectangular, relational, has foreign keys to other tables, and needs to be sorted, filtered and joined with other user-defined schemas. Modelling the user schema as database columns performs better than any other database solution, and database solutions are preferred because shared state, transactions, etc. Because the product is closer to being an actual database than a program using a database, ORMs aren't tuned for it.
Your rather unique use case certainly falls outside of the remit of what ORMs provide (cutting down on generic SQL boilerplate) but I'm unconvinced that what you built is in need of especially powerful language features to build it.
I've seen hard to read code that solves simple problems and I've seen easy to read code that solves hard problems.
Why are you advocating for the hard to read code?
Code can be hard to read because it's badly written, or it can be hard to read and understand because it deals with a hard problem.
> I've seen hard to read code that solves simple problems and I've seen easy to read code that solves hard problems.
I've seen easy to read code that solves simple problems that seem hard because of things like combinatorics (e.g. Sudoku solvers and the like). Actually hard problems don't have simple solutions; there's a complexity that doesn't go away no matter how you express the solution. This is doubly true when there are constraints on the solution in execution time and space, because such constraints limit how much you can break the problem down.
> Why are you advocating for the hard to read code?
Where did I do that?
The objective is to solve business problems, not write tests. Warping design to introduce unnecessary abstractions and indirections for testing is what leads to Java-itis, with factory factories.
I don't think this is over-engineering though. Even if unit tests didn't warp designs they'd be a bad idea. I think it's just bad engineering based upon stupid dogma spread by the likes of J.D. Rainsberger and Uncle Bob.
Software is not like that. We may be experts in programming languages, algorithms and data structures, but the things we create and the problems we solve keep changing all the time.
We're not usually experts in these problem domains and in some cases there are no such experts at all. So our code needs to be a lot more descriptive and written for readers that may not be very familiar with the problem at hand.
We are more like lawyers supporting law makers or even like law makers themselves.
Bridges are unique to their locations and their scale: what works over a creek will not work over a ravine. What works for a pedestrian will not work for a car. What works for a car will not work for a train. What works for a train will not work for a marching army.
I don't doubt that each bridge comes with unique challenges. But the purpose, user interface and constraints of bridges have remained stable enough for long enough to allow specialisation. That is not the case in many areas of software development.
Greatness is highly dependent on context. I think your statement on greatness would apply to code that helps you learn and think.
This is, however, NOT the kind of code I'd want to see at work. Great code in a business setting is simple, easy to understand, and has absolutely no subtlety.
If I was looking for a literary example, great code in a business setting would be like the writing style you'd see in a newspaper: written for easy consumption by the greatest number of people possible.
Great code should be predictable, obvious and easy to change.
Bravo.
Preferably at 2 AM on a Saturday, when production is down ;)
To add to p2t2p's comment, procedural programming doesn't offer very powerful high-level abstractions. OOP on the other hand is so powerful that it's easy to do wrong.