Live data from Hacker News

Avoid Else, Return Early (2013)

blog.timoxley.com

451–460 of 601 posts

Re: Avoid Else, Return Early (2013)

#451
post #119

Earlier quoted context omitted.

Only "brilliant" code ends up needing comments. Plain code organized into understandable methods (usually no more than half a page of code), with good naming for variables & method names reduces the need for comments. It's also easier to scan/read code if there's a minimum of comments in the way.

> Only "brilliant" code ends up needing comments. Code shows what is being done. Comments should explain why it's being done.

Code shows how it is being done. Comments should explain what is being done.

Re: Avoid Else, Return Early (2013)

#452
post #280

Earlier quoted context omitted.

> (very large) side projects How big once you remove all the extra newlines?

LOL. This is the part of the Silicon Valley episode-bar-scene where the fight breaks out. Thank god OC didn't mention "Just like you move from tabs to spaces."

I never understood this part of the show. I'd think that the genius inventor of a breakthrough compression algorithm whose WHOLE POINT was high quality lossless compression would prefer spaces over tabs. After all, the compression algorithm can deal with it... without the issues that result from embedding hard tabs in a source file.

Re: Avoid Else, Return Early (2013)

#453
post #420

Earlier quoted context omitted.

I've actually gone from avoiding else to requiring else, which is sort of a necessity when you're in a functional immutable environment where the result of a logical expression is a value (and often assigned as such), since it avoids undefined situations. (This example comes from Elixir.) value = if boolean, do: this_value, else: other_value is undefined in some circumstances unless an "else" is included. This patter…

That's just a ternary operator. I use that in Java all the time. value = boolean ? this_value : other_value; If the branching logic gets too complicated, I usually move it into a function (private method) with a return in each branch.

And in Javascript, once do-expressions go from a proposal to a common part of the language, this will be a thing:

  let value = do {if (boolean) { this_value } else { other_value }}
...which is even closer to the Elixir style than JS's ternary operator (which is identical to Java's).

Re: Avoid Else, Return Early (2013)

#454
post #358
post #345

Earlier quoted context omitted.

That's the bad part of not having braces. If boundaries exist they need to be clear. One shouldn't have to count the tabs that make up the level of indentation. Its already a challenge reading code. Counting invisible tabs makes it even worse.

Well, you don't have to literally count them, you use the spatial reasoning hardware built in to your retina and visual cortex to just see that this bit of code is further over to the right than that bit of code. But of course that only works for pieces of code that are small enough to fit in your field of view.

All code pieces should be small enough to fit in your field of view :)

Re: Avoid Else, Return Early (2013)

#455
post #106

Programmers with lots of hours of maintaining code eventually evolve to return early, sorting exit conditions at top and meat of the methods at the bottom. Same way you evolve out of one liners. Same way comments are extra weight that should only be in public or algorithm/need to know areas. Same way braces go on the end of the method/class name to reduce LOC. Same way you move on from heavy OO to dicts/lists. Same w…

> Same way braces go on the end of the method/class name to reduce LOC. Screw that. I've been writing code for 15 years, and Allman style braces make it so much easier to mentally parse code into blocks that they're worth every single LOC. I can't speak for anyone else but I'm not working on an 80x24 terminal anymore.

Ok. I've been writing code for 25+ years, and disagree.

Re: Avoid Else, Return Early (2013)

#456
post #354

Earlier quoted context omitted.

I don't think throwing void at people is generally going to make anything clearer.

why

It’s an obscure operator. Many people are unaware of it and many of those that are have a vague notion of exactly what it does.

So people reading this code may have to stop and puzzle out this little pattern.

Here you can replace “ void “ with a new line and have code anyone with basic familiarity with a c-like language can read and understand intuitively.

Re: Avoid Else, Return Early (2013)

#457

Earlier quoted context omitted.

If you didn't want a pointer you could have just used something like: template struct FooWrapper { TFoo foo; const TDestr destroyFoo; template FooWrapper(Tinit initializeFoo, TDestr destroyFoo) : destroyFoo(destroyFoo) { foo = initializeFoo(); if (!foo) throw FooException(...); } ~FooWrapper() { destroyFoo(&foo); } } Although really that should have been part of the "Foo" class itself, but if you need to deal with ex…

Oh yes, I forgot to make it clear that `Foo` is a C struct from a C API. There are tens of thousands of such libraries. An issue with your wrapper is that it is not generic enough and thus must be written for each Foo-like object. That could likely be fixed with more template arguments, but of course there are multiple ways to initialize C objects like - `fooInitialize(Foo foo); // expects that foo is pre-allocated`…

Well you probably have your reasons for needing an absurdly general method of handling things. That said I think it would be a lot cleaner to use locally define a class that takes care of both initialisation and destruction when possible, rather than one that only takes care of the destruction. I wouldn't mind too much if you used macros to simplify the boiler plate somewhat.

What worries me is that using 'defer' leaves absolutely no possibility for reusing the code, other than by literally copy pasting it, which just rubs me the wrong way. It goes completely against the DRY principle.

Re: Avoid Else, Return Early (2013)

#458
post #454
post #358

Earlier quoted context omitted.

Well, you don't have to literally count them, you use the spatial reasoning hardware built in to your retina and visual cortex to just see that this bit of code is further over to the right than that bit of code. But of course that only works for pieces of code that are small enough to fit in your field of view.

All code pieces should be small enough to fit in your field of view :)

Try to tell that to my current client :-(

Re: Avoid Else, Return Early (2013)

#459
post #334

This is one of the reasons why I grew to really love function overloading, especially in combination with pattern matching (I learned both first with Clojure, perhaps, but have only become comfortable with them now with Elixir). I can often avoid if/then statements altogether! It's also interesting how error handling becomes much less of an issue. I do a lot of javascript programming, and it's often quite frustrating…

> While I by no means hate Javascript, I have to admit I feel a bit silly about being defensive about it in the past. I just didn't know what I was missing, or didn't see what the big deal was.

I'm glad to hear a perspective like this. I'm fortunate enough to not come across too many people like this, but I'm always utterly baffled by people who get defensive about the smallest complaints about Javascript (of which there are many valid ones that have nothing to do with irrational hatred).

Re: Avoid Else, Return Early (2013)

#460
post #425
post #418

Earlier quoted context omitted.

K&R isn't that terrible when code is neat and clean, but it starts to have trouble IMO particularly when declarations or conditions wrap to multiple lines. Compare the following examples... I personally have to stop and read the code to find the blocks with K&R braces, vs being able to see them at a glance with Allman. void MyLongMethodName(SomeLongParamType param1, SomeOtherLongParamType param2, YetAnotherLongParamT…

Emacs autoindent to the rescue: void MyLongMethodName(SomeLongParamType param1, SomeOtherLongParamType param2, YetAnotherLongParamType param3) { if (longContrivedVariableName1 == longContrivedVariableName2 && longContrivedVariableName1 != longContrivedVariableName3) { // do stuff } }

IME the more complex your code formatting, the less likely people are to do it, especially after your code has been touched by dozens of different people with a dozen different editors.
Post reply on HN