Readability vs efficiency
Andrew Bennett <andrew.bennett@unsw.edu.au>, COMP1511 18s1 Course Forum
Readability vs efficiency
Student, follow-up question:
If you have several statements that are over difference conditions but result in the same outcome, and you can simplify them into one if statement - is that really not recommended?
I've done it a few times under the idea that less lines is better, but it makes the code completely unreadable for a third party. Should I be emphasizing how other people can read my code, over how short or mathematically sound it is?
Andrew
Could you give me an example of what you mean?
Should I be emphasizing how other people can read my code, over how short or mathematically sound it is?
So, programming style is subjective, and some people emphasise/value certain aspects/preferences over others.
In my opinion, yes you should definitely be trying to make your code more readable. For others, but also for yourself weeks or months later.
If you end up programming in "the real world" -- at a big software company like Google (and others I'm sure, but I don't have first hand experience), you will have to work with code that many many other people have interacted with, and many others will need to work with your code.
Having consistent and readable code in that sort of situation is very important.
Two interesting/relevant things from my experience at Google:
#1
They have a style guide, which is significantly larger than ours (because it covers C++ rather than C, and much much more syntax etc than we see in this course), but which is very indepth and... not "restrictive" exactly, but forces you to do things in a very specific way. https://google.github.io/styleguide/cppguide.html
#2
At one point, I was working on writing a piece of code and was getting advice/feedback from a more senior engineer -- I wasn't sure whether to use a certain more-optimised data structure for this code I was writing, which would be a bit faster (O(log n) rather than O(n), which will have meaning to you after COMP2521 :)), but more complex to read/understand.
They told me something that has had quite a profound impact on the way I think about code:
CPUs are cheap. If this code is only going to run over relatively small data sets (giant by our standards, but tiny by Google standards -- maybe hundreds of concurrent users), then does it actually make enough difference to care about optimising the algorithm?
On the other hand, engineer time is finite, and valuable. If your code is slightly less efficient (and costs more CPUs), but significantly easier for you and others to write and understand -- the trade-off is entirely worth it, even just looking at it from a $$$$ perspective.
Basically, is it better for a computer to "waste time" (in a way that doesn't have any real/noticable impact) running slightly inefficient code, or for a human to "waste time" trying to understand code that's more efficient, but harder to understand.
-------
Of course, there is a time and a place for trying to optimise your code -- again this is something we look at a lot more in future courses rather than this course, but here's a super quick overview:
O(n) refers to code that has to do 1 thing per piece of input. So,
something like going through a single loop across an array, and adding
all of the values together, or something.
O(n^2) refers to code that has to do "n" things per piece of
input. So, something like nested loops that run through an array
comparing every array element to every other array element, for example.
(note: n refers to the size of the input, so if your array was of
size 100, n would be 100; if you had an array with 100,000 ints,
n would be 100000)
If you have to work with say a million values -- an O(n) algorithm/approach means it would take a million steps; on the other hand, an O(n^2) algorithm would take a million squared steps, which is a very very big number.
In that sort of situation, then yes by all means it's very important to choose the "more efficient" algorithm.
But, there's a time and a place for optimising your code, and generally "readability" / ease of understanding is more important than making your code run 0.00001% faster.
Student:
This was really interesting, thank you.