Hacker News new | ask | show | jobs
by copperx 3812 days ago
> But it's also the same when coding, the hard parts are not always where I expect them to be.

As you may know, this also applies to optimizing code. The 'hot' parts of the code are very often things that you don't expect.

1 comments

The hard part of optimizing code is to find areas were your code is weak at and taking too long to do something.

In my best practices and I learned this from a video by Grace Hopper, is to use less code to do the same thing. Less code takes up less memory and usually runs faster.

Just writing code and then compiling it without any errors doesn't mean it is done. You still have to optimize it and do security checks as well as quality checks. You still have to check for bugs and side effects. You have to use it and think like a user would to see how the user would see it.

Less code is not really that important anymore (within sensible bounds) Clarity to whatever poor shmo has to maintain it is often much more important than saving 5 lines with some clever bit twiddling hack.

Unless of course you are having performance issues and you have identified this code block as the issue.

>> Less code is not really that important anymore (within sensible bounds) Clarity to whatever poor shmo has to maintain it is often much more important than saving 5 lines with some clever bit twiddling hack.

I don't think many people will advocate reducing line count by stuffing the same processing into fewer lines using clever tricks. What should always be considered a good thing though, is cutting down on the line count by changing the algorithm/strategy, using better suited data structures, by trying to 'do less' and get the same result, not trying to support every thinkable corner case you will never run into, by preferring multiple simple implementations of the same thing for different corner cases, over one single complex kitchen-sink solution, by not re-inventing the wheel and using tried & tested libraries/frameworks, etc.

I know it sounds like a terrible cliche, but any line you don't write, is a line that can't contain bugs ;-). Over the years I've learned that almost always, simplicity is preferred over complexity, even if it implies a little bit of duplication, or an implementation that is 'less awesome' because it 'only' does what is needed for the application.