News for hackers. Stuff that matters.
Re:Avoid Else, Return Early (2013) by jrimbault on Monday March 26, 2018 @09:13AM (#16678587)
>braces go on the end of the method/class name to reduce LOC

You could argue the placement of braces with lots of valid arguments both way, but this... to reduce LOC ? Doesn't feel like a valid reason in any language using braces...

Re:Avoid Else, Return Early (2013) by fyi1183 on Monday March 26, 2018 @09:29AM (#16678673)
Oh but it is, independent of the language. Fewer LOC means more code on the screen, which means you can more easily grasp the functionality of some piece of code, which makes it easier to maintain.
Re:Avoid Else, Return Early (2013) by jcelerier on Monday March 26, 2018 @11:17AM (#16679512)
> Fewer LOC means more code on the screen, which means you can more easily grasp the functionality of some piece of code,

uh ? for me the less code on each line and the easier it is to read

Re:Avoid Else, Return Early (2013) by Dylan16807 on Monday March 26, 2018 @10:57PM (#16684814)
When it comes to bracket placement, we're talking about empty lines. They don't affect the amount of code on a line.
Re:Avoid Else, Return Early (2013) by collyw on Tuesday March 27, 2018 @04:17AM (#16686376)
That depends. Python's pep guidelines has an 80 char wide rule, but I personally find that a lot harder to read than one long line (most of the time).
Re:Avoid Else, Return Early (2013) by extralego on Tuesday March 27, 2018 @04:34AM (#16686455)
I find the 80 chars too short but, and this is highly objective, I notice I am more relaxed and usually having more fun when working with shorter lines. The best theory a I have as to why is just that most lines in code are short so long lines require making my eyes deviate from the code structure. If there is a long line, it will probably be out of place. So, my personal rule is to avoid that one or two lines that juts out beyond the lines above and below it. If all lines in a block are long, they are often similar commands/expressions which makes them orderly, and not so discomforting.
Re:Avoid Else, Return Early (2013) by redblacktree on Wednesday March 28, 2018 @07:32PM (#16703088)
It's pretty sweet when you need to read code in a console that's limited to 80 characters. Also, it makes it easy to have two files opened side-by-side.
Re:Avoid Else, Return Early (2013) by mden on Monday March 26, 2018 @09:30AM (#16678677)
Eh, all things equal, the less the number of lines the more readable.
Re:Avoid Else, Return Early (2013) by bipson on Monday March 26, 2018 @09:35AM (#16678709)
I disagree. Consider the absurd example where every single statement is crammed into a single line. Not exactly readable to me?

Sometimes more verbose code is more readable, and sometimes it is the presentation, and sometimes this is "superfluous lines", that increase LOC.

Re:Avoid Else, Return Early (2013) by jorams on Monday March 26, 2018 @10:56AM (#16679347)
That's covered by "all things equal".

The argument against placing braces on their own line is that this:

    if (a)
    {
        print(a)
    }
conveys exactly as much information as this:

    if (a) {
        print(a)
    }
while taking up more space. The thing the brace tells you is already told by the indentation, so the brace is on a superfluous line.
Re:Avoid Else, Return Early (2013) by robot01 on Tuesday March 27, 2018 @01:10PM (#16690597)
Maybe it's just me, but I find the first version easier to parse visually. When the coding convention forces me to put the brace on the same line, I end up doing:

    if (user.exists()) {

        destroyHuman(user);
        hideRemains(user);
    }
Re:Avoid Else, Return Early (2013) by pdonis on Monday March 26, 2018 @11:20AM (#16679540)
> The thing the brace tells you is already told by the indentation

Which is true, and of course begs the question: why do you even need the braces?

Re:Avoid Else, Return Early (2013) by MaulingMonkey on Monday March 26, 2018 @11:59AM (#16679830)
1) I frequently tell my diffs to ignore whitespace so I can see my structural changes without getting drowned in a sea of indentation changes when introducing new scopes. Not viable when diffing python.

2) I semi-frequently make indentation mistakes when resolving merge conflicts. In braced languages this is fixed by an autoreformat. In unbraced languages, I have to take a lot more care with merging, lest I end up in yet another lengthy debugging session ending in facepalms - or worse, check it in.

3) Redundancy in the face of typos and whitespace destroying mediums such as many pieces of web technology - comments sections, forums, etc.

Re:Avoid Else, Return Early (2013) by isostatic on Monday March 26, 2018 @02:43PM (#16681346)
In this specific case

    if (a) {
        print(a)
    }
and if (a) { print(a) }

Could just be if (a) print (a)

Except in perl, where it's

    print (a) if (a)
And if you want to add a second line you have to change it, thus I got into the habit of

    if (a) { print(a) }
But I don't code for a living, any more than I run network cables for a living, or screw things into bays for a living. I code as a tool to get the job done, most recently that was writing some perl to parse the output of a tcpdump which was outputting rtp sequence number discontinuities to see where packet loss was occurring, what jitter was going on, etc. Fast, to the point, isolate the problem (crappy juniper srx), fix the problem (replace with mikrotik ccr), job done, drink beer.
Re:Avoid Else, Return Early (2013) by MaulingMonkey on Tuesday March 27, 2018 @03:02AM (#16685980)
I do like one-liner conditionals (at least short ones for early bailout) but sadly my coworkers and their coding standards do not.
Re:Avoid Else, Return Early (2013) by mekoka on Tuesday March 27, 2018 @03:17AM (#16686067)
Off-topic and pedantic pet peeve, would you mind replacing "begs the question" with "raises the question". The former might not mean what you intended.
Re:Avoid Else, Return Early (2013) by defertoreptar on Monday March 26, 2018 @10:29AM (#16679152)
Let's say we can see 30 lines on our screen and the average function takes 5 lines with OTBS. That means we can see 6 functions at once with OTBS and 5 with Allman. That makes a difference at least in my case.
Re:Avoid Else, Return Early (2013) by RobertRoberts on Monday March 26, 2018 @11:07AM (#16679448)
Not being a pest here, but how often are you comparing more than 2 functions at a time? I can't say that I have ever dealt with code in over 20 years where I had to deal with more than 2 functions at once.

Maybe I am mentally disabled, but I can only focus on one logical task at a time. Can other programmers multi-task their coding?

Re:Avoid Else, Return Early (2013) by defertoreptar on Monday March 26, 2018 @11:43AM (#16679692)
All the time, but to be honest, it's mainly side by side when I do. This is useful when building one function that acts on some other part of the code and I don't want to scroll / swap files back and forth.

I more meant it in the sense that it's nice to be able to soak in more of the code at once and avoid unnecessary scrolling.

Re:Avoid Else, Return Early (2013) by MaulingMonkey on Monday March 26, 2018 @12:11PM (#16679933)
Refactoring might have you adding a parameter to a function, and then feeding that additional parameter to the function at it's call sites. Even when you only have one call site and thus are in the "2 functions" case - these two functions are often separated by multiple "irrelevant" functions.

The less scrolling, tab management, and other context switching I have to do to look at those "2" functions, the easier it is on my memory.

Re:Avoid Else, Return Early (2013) by RobertRoberts on Monday March 26, 2018 @01:15PM (#16680564)
>The less scrolling, tab management, and other context switching I have to do to look at those "2" functions, the easier it is on my memory.

I don't disagree, but the sentiment seems to be "fit everything on the screen at all times", which seems like one of those rules that may take more time to implement than it saves in actual time.

I work on projects with upwards of 100k lines of code consistently, the very idea that anything relevant will fit on one screen is absurd, so I am always curious what kinds of projects people work on where the relevant code is literally within a half screen of each other.

Re:Avoid Else, Return Early (2013) by MaulingMonkey on Tuesday March 27, 2018 @02:59AM (#16685966)
I tend to work on gamedev codebases roughly near a 1mloc (both above and bellow.) In one recent refactoring I recently managed to get a class declaration (that was previously local to a single .cpp and had all functions defined inline in one go) to fit on a screen. I use small fonts, 4k resolution monitors, portrait orientation, and I still had to cull some "GetFoo gets a foo" style worthless doc comments to accomplish it. (I kept the comments that actually added anything of meaning.)

Given that above description, you can probably guess I don't have everything on screen at all times, not even close. But I'll refactor off small files of related functionality where I can, subdivide things that are getting unwieldy, etc. - put private/static functions near the public/exported functions that call them, break things up into (sub)modules, etc. and get good grouping.

> I don't disagree, but the sentiment seems to be "fit everything on the screen at all times", which seems like one of those rules that may take more time to implement than it saves in actual time.

Taken to the extreme, any such rule absolutely will. I certainly don't have that as a hard and fast rule. Or even as a guideline per se - it's more a faint but distant dream, or perhaps a feverish hallucination, a reminder of codebases past. But there's often plenty of cruft and low hanging fruit that can be fixed up in these codebases. Everyone has their "just one small change" to check in, so things build up and they don't jump to "okay, this is one line too many - time to refactor."

I practice pain driven development - when it's getting to be a pain to keep enough of something my head to do whatever sweeping changes or overhauls I might need to do, see if some pure or near-pure refactoring changes it. No expected behavior changes, often not even class or structural changes - often literally just regrouping code by topic and category via cut and paste. Horrific diffs, but low-risk checkins, and often a lot easier to reason about afterwards, and suddenly related code is often on the same screen.

(Although far from always.)

Re:Avoid Else, Return Early (2013) by RobertRoberts on Tuesday March 27, 2018 @12:39PM (#16690233)
It sounds like we are on the same page. It's hard to tell with brief forum comments what people mean. I am just used to the reality that you _must_ switch contexts all the time, and it's _normal_ simply because the code bases are too big.

Yes, it's nice when things are close together, but in my case it's so rare as to not be point to consider trying to achieve, but more like a mental joke like "Hey, Bob. Come here and check this out! Two functions and template I am working on all fit on the same screen!" And Bob's like "wooooah, dude". And then we take a group selfie with my monitor and have a laugh, print out a poster of the selfie for the bulletin board for a few months, then throw it away during a cleanup and move on...

But to have people say "It's important to save whitespace so stuff can all be on the screen to avoid context switching" seems completely and utterly pointless.

Re:Avoid Else, Return Early (2013) by lokedhs on Tuesday March 27, 2018 @09:38PM (#16694458)
I've always wonder why no C or Java code formatter tool allows me to use Lisp-style formatting. It's clearly superior, and the popularity of Python should prove that people are OK with not having closing braces on their own lines:

    void foo(int x) {
        if(x == 0) {
            bar() }}
Re:Avoid Else, Return Early (2013) by jrimbault on Wednesday March 28, 2018 @02:43AM (#16695895)
After having seen a meme, I gave a teacher a project formatted like this:

    public class Main                                        {
        public static void main(String[] args)               {
            System.out.println("Hello world!")             ;}}
We had a good laugh. (that was fine with him, of course as long as it's a joke)

PS: getting back to the original topic, imo, formatting style should be project-wide rules set at the beginning or during refactoring. Don't get too hung up on style as long as it stays consistent.

Re:Avoid Else, Return Early (2013) by kazinator on Friday March 30, 2018 @12:24AM (#16713652)
I've written a bit of code formatted rather like this:

    void foo(int x)
    { if(x == 0)
      { bar(); } }
That is workable. I have adhered to it (mostly) in this Yacc grammar file: http://www.kylheku.com/cgit/txr/tree/parser.y

Just in the grammar; not in the supporting functions.

It's an attractive style for C statements embedded in another language, because they look more like capsules somehow.

Re:Avoid Else, Return Early (2013) by Turing_Machine on Monday March 26, 2018 @09:40AM (#16678749)
"this... to reduce LOC ? Doesn't feel like a valid reason in any language using braces..."

It's not really a valid reason. Unfortunately, there are still a lot of people who think LOC is a valid metric.

Re:Avoid Else, Return Early (2013) by loup-vaillant on Monday March 26, 2018 @10:23AM (#16679105)
Here's an extreme, yet very real example: mandatory Doxygen comments on accessors:

  /**
   * Get the foo
   *
   * @return the foo
   */
  int getFoo();

  /**
   * Get the bar
   *
   * @return the bar
   */
  float getBar();

  /**
   * Set the foo
   *
   * @param foo the foo
   */
  void setFoo(int foo);

  /**
   * Set the bar
   *
   * @param bar  the bar
   */
  void setBar(float bar);
Compare with what any sane project would do:

  int   getFoo();
  float getBar();

  void setFoo(int   foo);
  void setBar(float bar);
Multiply that by a dozen such attributes, and what should fit on a single screen spans now hundreds of lines. What you could see at a glance, you have to search for. There's simply no way those utterly redundant comments increase readability. (Yes, the real comments in the real code were just as redundant.)

LOC is more valid as a metric than most care to admit.

Re:Avoid Else, Return Early (2013) by edejong on Monday March 26, 2018 @04:25PM (#16682223)
Or you use scala:

case class Baz(foo: Int, bar: Float)

Gives you a free hashCode, equals and copy as well.

Re:Avoid Else, Return Early (2013) by redwolf2 on Monday March 26, 2018 @12:29PM (#16680091)
Also: Why use get and set accessors for private fields at all, when you can just use:

int foo; float bar;

That can later be changed, when the need arises.

Re:Avoid Else, Return Early (2013) by macspoofing on Monday March 26, 2018 @12:20PM (#16680007)
Meh. It's noise but what's the harm? It was generated by an IDE so nobody had to physically type that. And usually no developer will be dwelling on this object or this area of the object. It just sits there doing no harm to anyone.

>LOC is more valid as a metric than most care to admit.

It's not.

Re:Avoid Else, Return Early (2013) by loup-vaillant on Monday March 26, 2018 @05:15PM (#16682663)
> It was generated by an IDE

Not all of it, actually.

> nobody had to physically type that

We totally had to read that.

> And usually no developer will be dwelling on this object or this area of the object.

Well… yes, usually. Sometimes however we did want to pay more attention than usual. At that point the original dev would have made a real comment. And that comment was lost in the noise, so you couldn't even distinguish between real comments and useless boilerplate.

Re:Avoid Else, Return Early (2013) by taeric on Monday March 26, 2018 @12:46PM (#16680263)
I seem to recall that LOC correlated just as heavily with bugs and other problems as any other measure it was put up against. And, just like every other measure, gaming it leads to a degenerative case. (Can't remember where I saw this, or I would provide a citation.)

I'll repeat my current gut feeling. Intrinsic measures of software quality are terrible as guides for how to write code. They are decent at predicting quality, but are not usable in a prescriptive way to write quality code.