6

This might be a super nerdy, OCD-like, silly question, but... I love it when related variables have names of equal length, such that when written below one another, it lines up nicely. Eg:

  • min/max:

    int min;
    int max;
    
  • index/length: idx/len

  • inner/outer:

    SpaceTime inner;
    SpaceTime outer;
    

However, there are many words that do not fit, like:

  • begin/end
  • start/stop
  • width/height
  • top/bottom
  • left/right
  • first/last
  • open/close

Are there tricks, or known alternatives for pairs of words like these that do align?

I just hate to see something like this:

double openPrice = lerpPrice(openTime, previousCloseTime, currentOpenTime, priceStart, priceStop);
double closePrice = lerpPrice(closeTime, previousCloseTime, currentOpenTime, priceStart, priceStop);

A long line, that would align perfectly if open and close would have had the same number of characters:

double opennPrice = lerpPrice(opennTime, previousCloseTime, currentOpenTime, priceStart, priceStop);
double closePrice = lerpPrice(closeTime, previousCloseTime, currentOpenTime, priceStart, priceStop);

Adding spaces could work, if my formatter didn't remove them automatically.

closed as primarily opinion-based by Christophe, amon, Robert Harvey, Greg Burghardt, gnat Jul 13 '18 at 5:27

Many good questions generate some degree of opinion based on expert experience, but answers to this question will tend to be almost entirely based on opinions, rather than facts, references, or specific expertise. If this question can be reworded to fit the rules in the help center, please edit the question.

  • 9
    double opennPrice -- And your pretty alignment has already cost you more in code clarity than the alignment is worth. – Robert Harvey Jul 9 '18 at 19:30
  • 3
    Sometimes I just add a space to make things line up. – user949300 Jul 9 '18 at 19:30
  • 2
    This is tough. For people who have difficulties reading text this can improve the readability of code. Other people may find it harder to read. To be honest I found code that lines up like that easier to read. What I found hard to read was "lerpPrice" -- what on earth is that!? – Greg Burghardt Jul 9 '18 at 19:34
  • 3
    @GregBurghardt: lerp is often used as jargon term for linear interpolation – Doc Brown Jul 9 '18 at 19:49
  • 4
    Adding spaces could work, if my formatter didn't remove them automatically. But what you're asking here is for the purpose of formatting. You're approaching it the wrong way - format your code to use spaces for aligning and you'll be happier. – Peeyush Kushwaha Jul 9 '18 at 21:22
11

Oh my God I wish I didn't know exactly what you're talking about.

It's SO tempting to line things up in a visually pleasing way. But you are going about it in one of the most damaging ways you could.

If you must line up

double openPrice = lerpPrice(openTime, previousCloseTime, currentOpenTime, priceStart);
double closePrice = lerpPrice(closeTime, previousCloseTime, currentOpenTime, priceStart);

for the love of readability don't rename openPrice to opennPrice! It looks like a typo. Don't torture yourself coming up with name pairs. If alignment means that much to you use whitespace.

double openPrice  = lerpPrice(openTime,  previousCloseTime, currentOpenTime, priceStart);
double closePrice = lerpPrice(closeTime, previousCloseTime, currentOpenTime, priceStart);

Your style guide might frown on it, your IDE might keep trying to fix it, and your fellow coders are unlikely to respect it. But at least you haven't let your OCD create the most evil thing in software: a bad name.

You're in good company. Josha Bloch of Java fame is afflicted with this obsession as well. But he expresses it like this:

enter image description here

and this

enter image description here

Effective-Java

No goofy name mangling required.

Now please don't take this as an endorsement of visually coupling lines using whitespace. I've struggled with this myself and I know the downside. Once you start being pleased by them lining up you waste time trying to keep them lined up. You forgo good refactoring opportunities that would move you forward just because you don't want to spoil your accidental symmetry. But worse than that you start having edit wars with unsuspecting coders who were naively trusting their IDE's default style correction to fix their code. They accidentally change your code without knowing they touched it. They'll swear up and down in a code review that they only changed one line. Yet there it is in source control. 105 lines changed. Sigh.

I like visually pleasing code as well. But this not a hill worth dying on. If you have this kind of energy to spare come join me in the fight against heathens who use unholy tabs rather than god given spaces.

Though I will admit I slightly mangle the default names to pair them in my unit tests.

expect
actual

May the spaces forgive me.

  • The openn is definitely not what I'm doing. It was for the sake of demonstrating that if you could come up with a name of 5 chars, it would line up nicely. I'm not that crazy. 😝Very nice answer though, but not exactly something I didn't know already. I guess a good formatter plugin for your IDE could do the trick. – Martijn Courteaux Jul 9 '18 at 23:05
  • 3
    There are many auto formatters. The trick isn't getting them. It's getting everyone one the team using the same one. – candied_orange Jul 9 '18 at 23:51
1

People tend to have such issues because they repeat code. If you could write it so:

var pi = new Interpolator(previousCloseTime, currentOpenTime, priceStart, priceStop);
double openPrice = pi.interpolate(openTime);
double closePrice = pi.interpolate(closeTime);

There'd be much less annoyance.

  • I like the creativity of the solution. In Haskell, this could make sense (using currying). But in a language like Java or C++, I would never consider this, because of the allocation of a new object on the heap, just to avoid passing 4 doubles the two calls have in common. – Martijn Courteaux Jul 10 '18 at 8:44
  • @MartijnCourteaux I don't think that is an issue, as good JVM (or CLR for .NET) will be able to effectively optimize around it. IMO the problem is even in creating the specific class just for this purpose. – Euphoric Jul 10 '18 at 11:09
  • @candied_orange could be as well but why "mutable", pi is not changed by interpolate() method, it returns the result – max630 Jul 10 '18 at 19:30
  • @max630 wow, you're right. I completely misread that. – candied_orange Jul 10 '18 at 19:59
-3

I recently saw a recommendation on formatting like this: move importent information as most left as possible.

An average method would look like this then:

public void doSomething(
            String firstParamter,
            int secondParameter
) throws Exception {
   double localVariable =
       getValueForLocalVariable(
            firstParamter, 
            secondParameter);
   if(isConditionMet(
            localVariable,                
            secondParameter)
   ){
       throw new Exception(
            String.format(
                 "reason %s %d %f",
                 firstParamter,                
                 secondParameter, 
                 localVariable));
   }
}

The point is that all the parameters (declaration and usage) are in the same column (within the same block).

Yes, some accustoming is needed but I think it has some benefits, especially when we keep SCMs in mind that might have more possibilities for automatic conflict resolutions.

  • This is very hard to read because nothing is placed where I expect it to be. – Alex Reinking Jul 9 '18 at 23:00
  • I don't see how this addresses the question. – Martijn Courteaux Jul 9 '18 at 23:33
  • @AlexReinking "nothing is placed where I expect it to be." Yes, changing conventions never comes without pain. But think a while about having all parameters at same indentation regardless of the length of the method name or the parameter names. With the current convention you quite often end up with the first parameter at the far right and all other parameters on the left. – Timothy Truckle Jul 10 '18 at 7:01
  • @MartijnCourteaux As fahr as I understood the OP the main problem is that the (almost) equal right part of the assignment is somewhat "displaced". Having an mandatory linebreak after (or before) the equal sign would solve this. – Timothy Truckle Jul 10 '18 at 7:03

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