InfoQ

InfoQ

News

My Bookmarks

Login or Register to enable bookmarks for unlimited time.

The content has been bookmarked!

There was an error bookmarking this content! Please retry.

Communicating Intent through Idiom and Paradigm Selection

Posted by Sadek Drobi on Feb 15, 2008

Sections
Process & Practices,
Architecture & Design,
Development
Topics
Ruby ,
Architecture ,
Agile ,
Programming ,
Teamwork
Tags
Functional Programming ,
Languages ,
Language Features

Making code more readable and more explicit? Reg Braithwaite believes that correctly choosing programming idioms can be instrumental for achieving that goal. He stresses that there are often several ways to get to the desired outcome and the choice should not be random. One should prefer the tool that signals in a most obvious way the intent of the piece of code that is being built. Syntax choices, for instance, can bear useful information about code’s intent:

Consider blocks in Ruby. Some people use do … end when the block needs multiple lines and { … } when it fits on one line. A popular (AFAIK) and superior idea is to use do..end and {…} to disambiguate between blocks that are executed chiefly for their side effects and blocks that are executed for their return values:

foo.map { |x|
# I care about the result
}
foo.each do |x|
# I care about the side effects
end

The computer doesn’t care, of course, but it signals an extra piece of information, the fact that you are chiefly interested in side effects in one case and chiefly interested in the result in another case.

Braithwaite believes that this approach is not limited to syntax idioms but can scale up to paradigms. In some languages, e.g. Ruby, “there are several entirely different philosophies the language supports.” This can be used as a means to declare intent by borrowing idioms from paradigms that correspond the best to what one is trying to achieve.

By making functional things look functional, you clearly signal that you are performing a calculation strictly for the result. Using imperative syntax like for and while should be reserved for the times you need to mutate variables or cause other side effects. […] Likewise, OO is a great paradigm—when you are modelling entities in the real world or when you need long-term persistent state. Languages like Ruby force you to use objects behind the scenes, your code shouldn’t look OO unless you are trying to signal your colleagues that there are entities involved.

Even though it is obvious that, sometimes, the choice of paradigm may be imposed by performance requirements, using this kind of conventions, when possible, allows providing additional information to programmers thus making code more readable and more expressive.

Some argue that using undocumented and unvalidated conventions would only result in more confusion. Reg argues, however, that any project involves programming conventions of some kind, e.g “using Bang! method names to indicate state changes (sort vs. sort!)”, and that nothing prevents teams from documenting established conventions, if necessary.

What do you think about using idioms as signals and means to declare intent?

  • This article is part of a featured topic series on Agile
Makes sense but... by Matthias K. Posted
be careful by ding jack Posted
makes me think of new syntax by John DeHope Posted
  1. Back to top

    Makes sense but...

    by Matthias K.

    ... I think that if you already bother using specific programming idioms to make your code more readable, maybe that's an indicator that it is lacking in more obvious ways for increasing readability, such as proper naming of identifiers.

    Another problem is that there has to be a shared understanding of what the usage of certain idioms in certain situations actually means to the programmer. This will differ not only with languages, but also with the people who actually read and write the code.

  2. Back to top

    be careful

    by ding jack

    conventions are everywhere, but widely recognized conventions are rare. so, be careful.

  3. Back to top

    makes me think of new syntax

    by John DeHope

    I'd prefer something a little more explicit. If we're going to communicate, why not communicate clearly and specifically? Why rely on subtle clues about our meaning? Why would anyone use 'map' unless they wanted the result set? Why would anyone use 'each' if they wanted the result? I'd prefer to see 'each' have a void return value (I am not a ruby programmer so maybe it already does). That would make it crystal clear. 'map' gives you back a return set, 'each' gives you back void. Nothing spells intent like a void pointer :)

    On the subject of functional programming... I wonder if we'll be able to specify read-only-ness at the method and argument level? That would make it really clear when you were being functional and when not. If there was a token of some kind you could attach to a method that said "hey I will not touch the 'this' object's data" and also a token on arguments that said "I will not touch this argument, and I promise not to call any of it's methods that touch it". Now we can program in a functional way without anyone being confused about what we're doing.

    I realize I am thread-jacking here. The original post is talking about using existing semantics to convey intent. I am talking about conveying intent with new semantics (and syntax) that enforce that intent. I hope that's okay.

Educational Content

New-age Transactional Systems - Not Your Grandpa's OLTP

John Hugg discusses high volume transaction processing applications with high and low frequency profiles, and how VoltDB can be used for that purpose.

Cool Code

Kevlin Henney examines code samples to see what can be learned from them starting from the premise that one won’t write great code unless he knows how to read it.

Collaboration: At the Extremities of Extreme

Jason Ayers share the observations he made watching a team of developers collaborating in real time on the same code base, pushing XP, pair programming and continuous integration to their extremes.

Yesod Web Framework

Michael Snoyman presents Yesod, a web framework written in Haskell and containing a web server, templating, ORM, libraries (templating, gravatar, etc.).

Transactions without Transactions

Richard Kreuter and Kyle Banker on how to avoid classical RDBMS transactional systems by using compensation mechanisms, transactional messaging or transactional procedures.

Attila Szegedi on JVM and GC Performance Tuning at Twitter

Attila Szegedi talks about performance tuning Java and Scala programs at Twitter: how to approach GC problems, the importance of asynchronous I/O, when to use MySQL/Cassandra/Redis, and much more.

10 tips on how to prevent business value risk

One category of risk that project teams need to ensure they address is business value failure – delivering a product that fails to provide value for the business investor.

Interview: Software Systems Architecture: Working With Stakeholders Using Viewpoints and Perspectives

InfoQ spoke to the authors of Software Systems Architecture on a couple of new topics, the System Context viewpoint and Agile, which have been added to the second edition.