r/linux • • Dec 28 '21

Tips and Tricks Rules of Programming

/r/computerscience/comments/rq60d4/rules_of_programming/

[removed] — view removed post

0 Upvotes

9 comments sorted by

View all comments

2

u/Columbus43219 Dec 28 '21

Rule 3.

Fancy algorithms are slow when n is small, and n is

usually small. Fancy algorithms have big constants. Until you know

that n isn't frequently going to be big, don't get fancy. (Even if n

does get big, use Rule 2 first.)

Is that a typo?

1

u/AegorBlake Dec 28 '21

no that is not a typo. Fancy algorithms work best when n is big (when compared in speed to simpler ones)

1

u/whosdr Dec 28 '21

Until you know

that n isn't frequently going to be big, don't get fancy.

This says that you should only rework the algorithm if you know n is mostly small. Which yeah, that's definitely backwards.

1

u/lostparis Dec 28 '21

This rule is just crapply written - what even counts as fancy in this context?

A better rule is that easily understood algorithms are usually the best, but that sometimes a fast one is important. Also make your algorithms pluggable.

2

u/[deleted] Dec 28 '21

For example don't implement comb sort when built in algorithm for sorting is good enough for sufficiently small number of items. If you have fewer than 100 or 1000 items, sorting them will be fast regardless of algorithm used.

Implementing something else means adding bugs to the code and increasing complexity for not much benefit.

That said, I can't say rules are well written.

1

u/lostparis Dec 28 '21

For example don't implement comb sort

I think don't reinvent the wheel is more the issue here :)

1

u/[deleted] Dec 28 '21

That's the "can't say rules are well written" part of my answer. :) It's essentially trying to reinvent KISS principle but going around it.