Live data from Hacker News

Ifs and &&s and Plan 9's Source Code

computationallyendowed.com

61–70 of 150 posts

Re: Ifs and &&s and Plan 9's Source Code

#61
Perhaps the compiler wasn't relied upon to provide "short circuit" boolean eval? This code will compile that way no matter if it's available in the compiler or not.

Honestly for the sake of being more robust, I'd add if(a != nil) after the first test of b.

if(b != nil) if(a != nil) if ...

Re: Ifs and &&s and Plan 9's Source Code

#62
post #12

One downside to non-braced conditionals is that a semicolon accidentally placed after the conditional will cause the block to always run, e.g.: if (null != foo); bar(); This is valid code in C and Java, and bar() will always run in this case. Having seen people waste hours on such a semicolon, I always use braces, even in one-liners, because I never know when someone is going to break it out into multiple lines later…

I personally have never put a semicolon after a condition and being mainly a C# developer I have written a lot of them. Is this really something that happens more than once or twice in a lifetime?

Re: Ifs and &&s and Plan 9's Source Code

#63

Earlier quoted context omitted.

For what it's worth, Vim handles it just fine.

What? Not for me, not with cindent on. It indents each if statement by one level, just like it "should". How would it somehow know to not indent them?

An auto-indenter could easily detect the idiom if hit backspace after the auto-indent it produced after the first 'if'.

Once you have your two ifs at the same indent, the indenter can use that info to keep them at the same indentation level. It could even assume you mean (a&&b) vs (!a&&!b) and not the three-way (a&&b), (a&&!b), (!a) when you follow it with an else block (I think that would be the best heuristic)

Implementing this will get a bit hairy, but if you are writing an auto-indenter for C, you should be used to that.

So, all you would have to do is hit one extra backspace to signal your intent.

Alternatively, one could type

  if(a) if(b) {
To signal that one wants

  if(a)
  if(b) {
Same number of keystrokes, and, IMO, that space is slightly easier to type than the return it replaces.

Re: Ifs and &&s and Plan 9's Source Code

#65
post #41
post #11

Earlier quoted context omitted.

^ this is a detail which boggles my mind. why are our debuggers still line based? they're clearly not in every respect since you can basically always 'step in'to an && sub-expression, but nothing displays progress as you step through such things, nothing lets you put a breakpoint at some sub-expression, nearly every feature of every tool is delineated by lines as if they're the important part of a program.

Very interesting point! There is an argument that lines are a unit of human comprehension and complicated expressions should be broken onto different lines already, as a matter of readability. It makes some sense to let the granularity at which you debug reuse the granularity at which you read.

If you're debugging though, presumably there's something you don't understand, so a debugger should be more granular than what you normally deal with since reading wasn't enough. In many ways they are, since you can check other things in scope (or any parent scope) which is not always visible in source (e.g. a callback has multiple stack entries outside the code that defined it, even if inline).

Re: Ifs and &&s and Plan 9's Source Code

#66

Good luck using an auto formatter on a code base that uses this technique.

I believe that the layout of code is done by humans for humans. An auto code formatter destroys all of that meaning. I blogged about this more here: http://www.databasesandlife.com/do-not-use-automatic-code-re...

Yeah, I saw that. I think it's misguided. The amount of time spent on arguing about style far exceeds the time savings of the changes you propose. People can get used to different formats quite easily, but time spent arguing about and maintaining formats can never be recovered.

Also, your formatter looks pretty brain-dead. clang-format formats all of those cases (in C++) as in your green examples with Google style turned on. Even the Eclipse formatter should do a better job than what you've posted with the appropriate settings. When I advocate an auto-formatter, it doesn't make sense for you to respond with a blog post talking about a formatter seemingly designed to make everything worse.

Re: Ifs and &&s and Plan 9's Source Code

#67
post #47

Earlier quoted context omitted.

I still prefer this: if( b != nil && b->qid.type == a->qid.type && b->qid.path == a->qid.path && b->qid.vers == a->qid.vers && b->dev == a->dev && b->type == a->type ) { fprint(2, "cp: %s and %s are the same file\n", an, bn); ret = 1; }

We can go deeper... if( b != nil && b->qid.type == a->qid.type && b->qid.path == a->qid.path && b->qid.vers == a->qid.vers && b->dev == a->dev && b->type == a->type ) { fprint(2, "cp: %s and %s are the same file\n", an, bn); ret = 1; } (Lined up the "a"s to make it obvious that they're all the same.)

[deleted]

Re: Ifs and &&s and Plan 9's Source Code

#68
post #63

Earlier quoted context omitted.

What? Not for me, not with cindent on. It indents each if statement by one level, just like it "should". How would it somehow know to not indent them?

An auto-indenter could easily detect the idiom if hit backspace after the auto-indent it produced after the first 'if'. Once you have your two ifs at the same indent, the indenter can use that info to keep them at the same indentation level. It could even assume you mean (a&&b) vs (!a&&!b) and not the three-way (a&&b), (a&&!b), (!a) when you follow it with an else block (I think that would be the best heuristic) Impl…

None of the existing auto formatters that I'm aware of do this.

Re: Ifs and &&s and Plan 9's Source Code

#69
post #31

Oh god that code is fucking hideous. If someone who worked with me wrote that I'd talk to them about it and make sure they never did anything like that ever again. From the terrible argument names to the abuse of the single line if syntax (which should never be used anyway, always use curly braces.)

It is always the worst developers who fixate on arbitrary rules because they never developed the ability to read code. Go practice reading code, that's the only way to get better at it.

Re: Ifs and &&s and Plan 9's Source Code

#70
post #24
post #11

Earlier quoted context omitted.

^ this is a detail which boggles my mind. why are our debuggers still line based? they're clearly not in every respect since you can basically always 'step in'to an && sub-expression, but nothing displays progress as you step through such things, nothing lets you put a breakpoint at some sub-expression, nearly every feature of every tool is delineated by lines as if they're the important part of a program.

Because something you do relatively early in the compiler is throw away all of the structure of the source, including flattening nested expressions into a linear IR. Mapping back to line numbers in the debugger is a bit hacky to begin with, and mapping back in an even more fine-grained way would be more complex still.

True (currently, I see no reason this is a necessary step), but that suggests you could perform trivial expansion of lines like `if (a.what() && b == c && (d == f || d exact same hack to get those pseudo-lines into the final stages, and into your debugger. You could even explode each piece into extra variables, so you can see the results of `a.what()` without re-evaluating it.

Honestly, even if you had to hit an 'expand this statement' button in your debugger to see `if x() && y()` spread into:

  x_val = x()
  if x_val
    y_val = y()
    if y_val
      ...
it would completely remove the necessity to write strange things to get around this limitation. Why do we have compilers and a huge variety of languages if not to stop writing strange things unnecessarily?
Post reply on HN