Readable Code is Code That Looks Like the Problem Statement
Join the DZone community and get the full member experience.Join For Free
in a recent blog post i showed how you could use java's labels as a way to clarify the given, when and then sections of a test. i think that this can result in more readable and comprehensible tests. obviously since its a matter of style this isn't an opinion that will be shared by everyone - that's just natural. its also good that we can all share different opinions on an approach like that. when my blog post got syndicated on the excellent dzone i noticed a few quite strange comments were made by people.
they didn't just dislike the approach in question - they disliked the idea of trying to make code read naturally. these counter-points could be broken down into two different groups:
- developers who didn't like their java code to read like english.
- developers who thought you should try and use groovy or scala if you wanted your code to read like english.
i wouldn't normally take the effort to reply to any old comment on the internet, but i'm of the opinion that this is an important enough topic to comment on. in this blog post i'll be explaining why i think that no matter what programming language you're using readable code is code that looks like the problem statement .
what is programming?
programming, coding, hacking - whatever you want to call it - is the subset of software engineering that deals with actually writing code. we want to produce a working application that solves some kind of problem. we don't necessarily know what the problem is when we start - but hopefully our understanding of it will improve along the way. so we're trying to convince the lump of silicone sitting in front of us that it really wants to solve the problem for us - rather than be converted back into a large sand pit.
the problem is that there's one heck of a gap between what humans understand and what machine understand. i'm not talking about how cute cats are - what i mean is that there's a large conceptual impedance mismatch. all cpus really do is basic arithmetic and logical operations. they can be used to do other things like map the human genome, spread democracy or drive cars but really all we've shown is that the computational part of those problems is equivalent to basic arithmetic and logical operations.
a better way
over the past 50 years we've been developing programming languages that try to bridge the gap between what the machine does and the problems that we're trying to solve are. at first this involved providing more abstract machine-oriented constructions - the ability to loop rather than just conditionally branch and the ability to name blobs of code and parameterise them with different arguments.
by the time we hit the 1980s two competing sets of evangelists were trying to tell us to throw away structured programming and embrace abstractions that are orthogonal to machines. object oriented programming focussed on the idea that we should structure our code to model real world objects, whilst declarative approaches, such as functional programming, concentrate on telling the computer what to compute and not how to compute it.
whilst there's still considerable debate about the merits of these different approaches its pretty clear that everyone wants to try and make the gap between the problem and the machine smaller. the smaller the gap, the less you actually have to program. the simpler they are to write and the easier they are to read. if you have a problem to solve that's written in a natural language by using the same vocabulary as the problem domain you're making that gap smaller. if you try and model the operations that exist in the real world - in either functional or object-oriented fashion - you're making that gap smaller.
is java the problem?
neither perfect nor correct
we're all human. we all make mistakes. we all write ugly code. but as an industry we should be looking to try and identify ways to improve ourselves. one principle that i hope we can all get behind is that the more your code looks like the its problem statement, the easier it is to read.
thanks to tom fitzhenry, martijn verburg and daniel bryant for feedback on this blog post.
* bash isn't a programming language - its an embrace-and-extend interpreter.
Published at DZone with permission of Richard Warburton, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.