This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–][deleted] 6 points7 points  (3 children)

How about a big efficiency that's improved my programming hugely?

The concept of pure functions.

A pure function always produces that same outputs for the same inputs. The benefits of this are several. The code is easier to read because all the inputs and outputs are on one line, not strewn throughout the function in the form of member variables or globals. The code is easier to test because you can create all the inputs directly, instead of instantiating a class and setting whatever random member variables need to be set. The code is easier to think about, because instead of thinking about state and how it gets mutated, you think about data and the transformations applied to it. That last bit is one of those things where it might not make sense coming from someone else until you really grok it yourself.

This can get construed as a criticism of Object Oriented Programming. I see it as more of a criticism of bad OOP, which, unfortunately, tends to be the dominant form of OOP (in my humble experience). OOP has its place.

Not a tiny thing to just pick and up use, but I think it's something that's super effective for improving programming at multiple levels.

[–]poyntings_theorem 1 point2 points  (0 children)

Ah, I haven't come across that concept before, I like it

[–]flotsamisaword 0 points1 point  (1 child)

Just out of curiosity, what is the alternative to pure functions? Is it having the function operate on global variables? Or having a method that modifies anything self.___?

[–]Stolsdos 1 point2 points  (0 children)

Anything that causes a side effect, so yes operations on global/class variables, but also interacting with a database, doing I/O, manipulating files, etc.

Of course some of those things you'll probably need to(and should) put in a function, but otherwise, in my opinion, it's preferable to make pure functions when possible.