DEV Community

Daniel Brady
Daniel Brady

Posted on • Updated on

What's your favorite line of code?

Abstract answers and actual code snippets are both welcome 😄

Discussion (9)

Collapse
bittnkr profile image
bittnkr • Edited

My favorite lines of code is those which saves me typing and makes my code cleaner. Like:

global log = console.log

Over the time that line grow to a full module, but I think that saved me hundreds of console dots.

Collapse
daniel13rady profile image
Daniel Brady Author

I love doing things like that with my output statements! It lets me swap out the implementation or even disable output entirely without modifying every log statement in my code. 👌

Collapse
bittnkr profile image
bittnkr • Edited

To be honest, that line was later refactored to:

global.log = function (...p) {
  log.data.push(p)
  log.console(...p)
}

log.data = []
log.console = console.log
console.log = log
...

To create a log history to be flushed later, and redirect all console.log() to log(). But lost a little bit of its beauty.

Collapse
downey profile image
Tim Downey

Maybe not the most elegant example, but this line in Cloud Foundry's deprecated v2 API let us implement rolling "no downtime" app deployments on v3 without breaking the legacy stuff.

It basically let have more than a single "web" process for an app at a time, but let v2 continue to only concern itself with the newest.

The beauty of Ruby is that we could mix it in to all of the v2 controllers that needed it!

Collapse
daniel13rady profile image
Daniel Brady Author • Edited

A wise old wizard I used to work with once said,

My favorite line of code is the one I get to delete.

It's taken me a few years, but I've finally caught on to that appreciation.

Collapse
daniel13rady profile image
Daniel Brady Author

And a wise old wizard I currently work with offers up a bottle of Scotch during our company's annual Hackathon to the dev who creates a PR which deletes the most code while maintaining bug-for-bug compatibility without adding new code. 😍😅

Collapse
bittnkr profile image
bittnkr • Edited

My favorite line of code is that I will never delete, because is perfect and cannot be replaced.

Collapse
daniel13rady profile image
Daniel Brady Author

That sounds great, but I'm not sure I'll ever be good enough to write a line of code that never gets modified before being deleted. Seems like a good goal to strive for!

Thread Thread
bittnkr profile image
bittnkr

Sorry for the 'never'...

I mean, a line of code to be "perfect" must:

  1. Solve the problem
  2. Be reliable
  3. Be easily tested or (pardon the heresy) dispense testing
  4. Be as terse as possible
  5. I can forget about it.

But my really favorites, "the perfect ones" are those that beyond that, yet saves me the typing of another lines. :D