Philip Potter

  • 'lvalue' considered harmful

    Posted on 06 June 2010

    If there is one term I would like to erase from computing terminology forever, it is ‘lvalue’. It is a confusing and distracting term, and far from making a discussion more precise, it makes it less so.

    The problem is that there are multiple competing definitions for lvalue.

    1. A storage location: The C Standard definition

    A C lvalue is an expression which refers to a location which can store a value.

    This is the definition I am most familiar with. Given the C code:

    int a[30];
    int i=3,j=5,k=10;
    a[i] = k;

    the following are all lvalues:

    a
    i
    j
    k
    a[i]
    a[j]
    a[k]

    because every one of these expressions refers to a memory location which holds a value. The standard suggests in a footnote that it is helpful to think of “lvalue” as short for locator value.

    2. Something which can be assigned to: The C Standard “modifiable lvalue”

    In C, a modifiable lvalue is an expression which may appear on the left-hand side of an assignment.

    Note that this is not contingent on there being such an assignment; only that assignment is possible. As a result, given the same C example code, the following are modifiable lvalues:

    i
    j
    k
    a[i]
    a[j]
    a[k]

    Notably missing is a. a is an lvalue but not a modifiable lvalue, because it refers to a location in memory but can not appear as the left side of an assignment. In C, you cannot assign to a whole array, only to one cell at a time.

    In contexts outside of C, this is often simply referred to as an lvalue, but when talking about C you should stick to Standard terminology to be as precise as possible. Especially if you’re asking questions on comp.lang.c.

    3. Something on the left of an assignment: the “left-hand side” definition

    This definition is of a totally different character to the C Standard definitions. In the perl code:

    my $array = [];
    my $i = 3;
    my $j = 5;
    $array->[$i] = $j++;

    this definition states that the only lvalue is $array->[$i] because it’s on the left-hand side. $array->[$i] is an lvalue only in the context of this assignment; in another assignment (say, $foo = $bar) $array->[$i] is no longer an lvalue, $foo is instead.

    There is an obvious problem in this example that $array->[$i] is not the only expression which gets written to: $j is also written to by the autoincrement operator.

    I believe when people use this definition, they really mean definitions 4 or 2 but are using sloppy language. Nevertheless it is a definition which people use.

    4. Something which is written to

    This is similar to the above argument, but states more precisely that an lvalue is something which is written to in an expression. In the above example, there are two lvalues: $array->[$i] and $j, both of which are written to as a result of the code. I like it more than definition 3 for this reason, but you can start to see how thinking of ‘lvalue’ and ‘rvalue’ as analogous to ‘left’ and ‘right’ breaks down.

    Mutual incompatibility

    These four definitions are mutually incompatible. The biggest problem is that definitions #1 and #2 are absolute, while #3 and #4 are contingent on a particular assignment. Under #3 and #4, a variable can be an lvalue in one line of code, but on the next line it is not an lvalue because it is not written to. Under #1, a variable is always an lvalue because it always refers to a location in memory. Under #2, a variable is always an lvalue unless it is of some type unsuitable for assignment — a constant, for example.

    In other words, definition #2 says an lvalue can appear on the left-hand side of an assignment, while definition #3 says an lvalue does appear on the left-hand side of an assignment.

    Definitions #1 and #2 are incompatible in programming languages which have storage locations which cannot be assigned to. Such a location is a #1-lvalue but not a #2-lvalue. The chief problem requiring separate definitions in C is the existence of array and structure types — these cannot be directly assigned to. In modern programming languages, arrays and structures can typically be assigned, so this problem is moot.

    The other main type of location which cannot be assigned to is a variable with constant type. There are many languages in which you can declare variables constant. A reasonable modification can be made to definition 2 to say “an lvalue is an expression which could be on the left-hand side of an assignment, ignoring constant qualifiers”; but then there exist lvalues which cannot be assigned to. Nevertheless outside of the context of C there are situations in which it is reasonable to conflate #1 and #2.

    A survey of definitions

    I did a search of various programming resources I know for definitions of lvalue. Here is what I came up with:

    • Strachey’s seminal paper in which he coins the term L-value: “An L-value represents an area of the store of the computer.” pretty explicitly #1
    • Wikipedia: “A value (computer science) that has an address” #1 as of time of writing
    • Wiktionary: “A value that can be treated as an address or storage location.” #1 as of time of writing.
    • Computer Programming and Precise Terminology, a Dr Dobbs article: #1
    • C Standard (read a free draft here): #1 (and #2 for modifiable lvalue).
    • Foldoc: “A reference to a location, an expression which can appear as the destination of an assignment operator indicating where a value should be stored.” #1 and #2 in the same sentence.
    • about.com: #3 (top google hit for lvalue definition)
    • c2.com: Direct quoting of Strachey; #1
    • msdn: “An lvalue refers to an object that persists beyond a single expression. You can think of an lvalue as an object that has a name.” #1
    • perldoc perlglossary: #2
    • The Ruby Programming Language: page 92, #2
    • The Camel Book, 3rd edition: page 52, #2

    I must be honest here and say I fully expected more instances of definitions #3 and #4 than I found. I have come across people using #3 and #4 on mailing lists and in fora enough for the term to be a source of confusion — in fact, just today I saw somebody using definition 4. The mnemonic value of ‘lvalue’ and ‘rvalue’ versus ‘left’ and ‘right’ encourages people to think of lvalues as something to do with assignment, rather than as something to do with persistent storage locations.

    In any case, in my experience there are enough people confusing the term ‘lvalue’ that I feel that it should not be used at all. I would instead recommend the term ‘location’. If you must use the term lvalue, please use definition #1.

    On rvalues

    I haven’t mentioned the term rvalue at all here. It doesn’t get used nearly as much, for which I am thankful; I believe the reason it isn’t used is because the term value does the same job much better, or where more precision is required, value of an expression.

  • Dear East Walworth council candidates

    Posted on 06 May 2010

    None of you are trying hard enough.

    I tried to find out about you online, I tried to read your leaflets to
    understand who *you* are, I tried to find out what you would do with
    my council. But none of you seem to be trying to tell me. The only
    person who has any real Google visibility is Kirsty McNeill, who is
    moving on to better things as Gordon Brown's speechwriter.

    Your leaflets say nothing about local elections, only about the
    Bermondsey and Old Southwark general election.

    As a result, only two candidates stand out for me. Martin Seaton, who
    helped chair a meeting with developers when our local park was
    threatened with being developed into flats, came across as a very good
    facilitator and though he was a neutral chair at that meeting, he came
    across as genuine and concerned. And Ben Johnson, who is the only
    candidate I have spoken to during this entire process, gave me a call
    and put up with my complaints about dishonest Lib Dem leaflets. As a
    young and enthusiastic candidate, I feel he's a much better bet than
    old and apathetic alternatives.

    The rest of you must try harder if you want to get my vote.

  • On political spectra

    Posted on 29 April 2010

    Some very common phrases you hear in political debate is "so-and-so is
    very right wing" or "he's a socialist" or "she's all about big
    government". You also get refinements to the left/right divide, from
    "far left" (communist party), "left" (old labour), "left of centre"
    (lib dems), "right of centre" (new labour), "right" (conservatives),
    "far right" (bnp). But what does it all mean? Are they useful labels,
    and do they form a coherent continuous line from far left to far
    right?

    Here are some policies I associate with "left": big government, higher
    taxes, equality, government provided services (education, health,
    social security, pensions), regulated markets, redistribution of
    wealth, an "equal society", relaxed attitude to immigration, relaxed
    attitude to the EU,

    And some I associate with "right": small government, lower taxes,
    individual freedom, privately chosen and paid-for services (education,
    health, social security, pensions), free markets, wealth as an
    incentive to betterment, the "American Dream", restrictions on
    immigration and asylum claims, euroskepticism,

    When we use terms such as "left" or "right" we assume several things:
    that somehow, these baskets of policies are associated with one
    another; that left and right policies are diametrically opposed; and
    that these policies are the only ones which matter. I don't believe
    any of these to be the case.

    First, are these policies associated with each other? Well, I would
    say tight immigration controls are diametrically opposed to free
    markets. If Polish plumbers are prepared to work for less money than
    their British counterparts, in a free market I should be able to
    choose for myself which one I want to fix my bathroom. But immigration
    controls remove that choice and mandate that I pay a British plumber,
    by preventing the Polish plumber from entering the country. "British
    jobs for British workers" also inevitably means "less choice for
    British consumers".

    Are left and right policies diametrically opposed? If we look at free
    markets versus regulated markets, while it initially sounds like they
    are totally opposite, it is not as simple as that. A market cannot
    exist without some rules: for example, without rules, why would I
    trade with you when I can just steal from you? We need property laws
    and regulations to punish those who break the rules. I want to employ
    someone, but I want to know they are competent at mathematics -- we
    need a system of regulations around qualifications to give me the
    freedom to choose the candidate I want. Some types of market freedom
    require regulation.

    And are these the only policies that matter? I haven't mentioned the
    environment, Scottish independence, ID cards, policing, housing or
    defense (including nuclear weapons), because I have no idea what the
    generic "left" or "right" think about them. But they are important
    policies.

    In the end, it's far better to choose the policies you care about the most and find the politicians who support those policies. TheyWorkForYou doesn't try to work out whether each MP is "left" or "right", but instead tells you how each MP voted on a range of issues. See for example Malcolm Bruce (LD, Gordon), Ed Miliband (Lab, Doncaster North), Eric Pickles (Con, Brentwood & Ongar). Once you've seen that, you might not be able to divide these three into simple positions on the left-right spectrum, but you cannot come to the oft-heard conclusion that "they're all the same". No, they're all different. With 650 MPs, you have 650 different opinions on the way the country should be run, and it would be optimistic to hope they all fit onto one line from "left" to "right".

  • The lib dems and bankers

    Posted on 13 April 2010

    So the lib dems have declared war on banker's bonuses [1].

    I don't like banker's bonuses. They have two problems: 1) Someone is
    getting paid lots of money to do something which can't be that hard,
    and 2) Someone is incentivised to take more risks than is good for the
    bank, since greater risk gives greater potential reward and greater
    potential bonus. The solution to 1) is to get over it; it's the way
    the world works, and bankers are hardly unique in this position
    (footballers, opera singers, chief executives also spring to mind).
    The solution to 2) is to have a system where the banks aren't
    incentivising excessive risk-taking.

    Consider that I am a banker, with a bonus scheme where I am rewarded
    more for earning more for the bank. Consider that in a given year, I
    have a 1/4 chance of losing £1m and a 3/4 chance of making £1m for the
    bank. If I am lucky and make the £1m, I get a bonus of £X; if I make a
    loss for the bank, I get nothing.

    But suppose I also have the option to gear up my trading strategy by a
    factor of 10. Then I have a 1/4 chance of a £10m loss and a 3/4 chance
    of a £10m gain. If I have made £10m for the bank, I should be getting
    quite a bit more than £X bonus, while if I make a £10m loss I can't
    get a smaller bonus than zero, so I'm quids in either way. But the
    bank is now running the very real risk of losing £10m on my trades. It
    might have considered the £1m risk acceptable, but the £10m risk as
    unacceptable, but the bonus structure does not reflect this fact at
    all.

    This is what is meant when it is said that bonuses encourage excessive
    risk-taking. The problem is exacerbated by the fact that there is so
    much randomness involved -- if I might make a huge loss due to a
    market crash beyond my control, I'm not going to be terribly motivated
    to work hard on a marginally better strategy unless the potential
    bonus is pretty huge [2] -- and in many cases, bankers' bonuses are as
    big as or bigger than their salaries.

    What is the solution? Well, if you curtail banker's bonuses, the banks
    will maintain remuneration packages by pushing salaries up instead.
    And without bonuses, bankers have no incentive to take excessive risks
    -- but they also have no great incentive to make money for the bank
    either. If you made £10m for your employer, wouldn't you want a slice
    of that pie?

    The lib dems' proposals just seem to be a grand "up yours" to the
    bankers without actually trying to understand and solve the problems
    represented here.

    [1]http://www.bbc.co.uk/blogs/thereporters/robertpeston/2010/04/lib_dems_smaller...
    [2]http://www.forbes.com/2006/05/20/executive-compensation-tournament_cx_th_06wo...