4

Some of the legacy code I've inherited uses the fact that C# supports multiple assignment to write code like:

void DisableControls()
{
    ddlStore.Enabled                =
    ddlProgram.Enabled              =
    ddlCat.Enabled                  =
    btnSaveProxy.Enabled            =
    btnCancelProxy.Enabled          =
    btnViewMassManagerProxy.Enabled =
    grdPromo.Enabled                = false;
}

It's a very pretty style, with all the = lined up neatly, and the value being assigned down at the bottom, but it's also a rather annoying one to work with. (And yes, those are tabs lining things up, not spaces).

I would like to know what, if anything, this style implies about the original coder, since I've only seen it on a few occasions before. Does it indicate a C++ background? .NET 1.1? Just someone's personal preference?

Is there any reason that the chained assignment like this would be more efficient than individually setting each control's .Enabled? Less so? (This part was answered by @gnat's comment: This way makes it easy to insert btnWhatever.Enabled = as new buttons are added and delete as some buttons are deleted)


Edit: Other elements of this person's style, assuming the whole file was written by one person (likely but not certain), include putting function arguments on the next line, and putting commas at the beginning of a line instead of the end of the previous line. Example:

PromoUpdate[] GetPromoUpdates(
                            StateField field)
{
    List<PromoUpdate> updates   = new List<PromoUpdate>();
    foreach (PromoOptInState p in field.States.Where(
                                                    x => x.IsDirty))
    {
        updates.Add(new PromoUpdate() {
            promoCode   = p.Code 
            , optIn     = p.IsOptedIn
        });
    }
    return updates.ToArray();
}

It's very weird.

14
  • 2
    this way makes it easy to insert btnWhatever.Enabled = as new buttons are added and delete as some buttons are deleted. Sort of cargo cult - makes it easier to copy-paste stuff instead of having a list and iterating over it
    – gnat
    May 12, 2014 at 19:49
  • 2
    Group Multiline Alignment - seems to have certain popularity in PHP
    – gnat
    May 12, 2014 at 20:02
  • 1
    What's the point of lining up all those equals signs if nothing follows them? May 12, 2014 at 20:37
  • 11
    I believe this style is known as Too Much Time On One's Hands.
    – Kyralessa
    May 12, 2014 at 20:44
  • 2
    On the picky side, this isn't will return the result of an assignment as the value. This is just multiple assignment, found in heaps of languages that don't do the assignment-is-value thingie.
    – david.pfx
    May 13, 2014 at 4:47

4 Answers 4

10
/*********************************************************************
 * People who do indent that way also tend to write pretty comments. *
 * It looks so very pretty, so very professional. Except it's not.   *
 *********************************************************************/

Suppose a variable needs to be renamed for some reason. That simple change might mean lots of superfluous changes because the original author succumbed to the "pretty code" programming style. You as the maintainer have to look at the entire body of code to see if you have broken the prettiness of the code.

Suppose the pretty commentary is flat out wrong. It doesn't describe why the code exists or what the code does. It's pretty, but wrong. Perhaps the original author rethought the design but didn't change the pretty comment. Perhaps some previous maintenance programmer fixed a bug but didn't fix the pretty comment. Perhaps that happened over and over.


There's a big difference between beautiful code and overly pretty code. Beautiful code doesn't need tons of makeup.

Update

That code aligned on the equal signs certainly does look prettier, and is perhaps easier to read, particularly when the targets on the left hand side have only slightly dissimilar lengths. This alignment also serves as a form of self-documenting code to show that that block of assignment statements are somehow related to one another. For these reasons, Steve McConnell advocated aligning the equals signs in a block of assignment statements in the first edition of Code Complete.

However, he withdrew this recommendation in subsequent editions. Instead he explicitly admonished against this style:

Do not align right sides of assignment statements.

His rationale for completely reversing his recommendation in this regard was that the huge maintenance cost associated with this style does not justify that slight increase in readability and comprehensibility.


Regarding the comma-first style, I've seen it and I don't like it. I put it in the same class as yoda conditions (if (42 == foo) do_something(); and if ("foobar".equals(baz)) do_something_else();). To me, both the comma-first and yoda conditions are too jarring to how I normally read and think.

All of this is of course a programming religious issue. Some people love their block formatted assignment statements, others love that comma-first style, yet others love their yoda conditions. My own rule: When I'm maintaining someone else's code I had better adapt to that author's style. New code? If the project give some leeway on style, I'll write that new code in my own preferred style.

1
  • 2
    That, at least, is one problem I don't have to deal with in this code...
    – Bobson
    May 13, 2014 at 13:29
4

Putting commas at the beginning of a line rather than at the end of the previous one is common in SQL, so perhaps a database background?

It helps you comment out sections you don't want more easily, even if it is the last item in the list you are commenting out.

SELECT Name
       , Address
       -- , Email
FROM Customers
WHERE Address IS NOT NULL
  -- AND Email IS NOT NULL

(Note the commented out lines pertaining to Email)

Yes, you could argue that commenting out the first line in the list now has an inconsistency, but that seems to be less of an issue.

2
  • I prefer separators first because they're important especial in sql.
    – JeffO
    May 13, 2014 at 20:30
  • Note that this doesn't apply to many parts of C#, code like new PromoUpdate() { promoCode = p.Code, } is perfectly valid, so you can just comment out the last line of the object initializer and it will still work.
    – svick
    May 14, 2014 at 16:19
3

I'm gonna go out on a limb and say Haskell, of all things.

Take a look at Johan Tibell's style guide here, or the code samples here. You'll see some of the same features there, like deep indentation on new lines or commas going before list elements.

I'm working in a project that has a fair share of people with Haskell background, and it's not hard to come across similar styling.

Agreed that it looks weird in a 'braces' language.

2
  • I've seen this "align on the equals sign" x in ancient Fortran code. It goes way back. May 13, 2014 at 17:19
  • Sure it does, and it can be just a coincidence. But that combination of features feels familiar to me. Aligning on equals sign is the least conclusive, wouldn't bring it up without seeing the second sample. That said, you'll see plenty of aligning on 'stuff' going on in Haskell/OCaml/F#.
    – scrwtp
    May 13, 2014 at 18:16
2

That's a somewhat unique style. I have seen somewhat frequent alignment of assignment operators in C, but not in a chained manner. So you would see something like:

int foo    = 1;
int bar    = 2;
int bazbiz = 3;

With the idea of calling out all assignments in one block and making it explicitly clear "this is where I'm assigning things."

Some would argue it improved readability if the compiler didn't provide default values for your variables.

Using it with chained assignments seems a bit more unusual. Most cases I have seen of that looked like:

int foo = bar = bazbiz = 0;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.