r/learnprogramming • • Apr 05 '22

Name one programming/comp sci concept you never understood and if you understand it, try to explain it to them

Name a programming concept such as virtual functions, pointers, certain algorithms, etc that you always had a hard time understanding and why.

Someone else who reads yours and does understand it, try to explain it to them to help them learn

1.6k Upvotes

738 comments sorted by

View all comments

9

u/rashnull Apr 05 '22

“Functional programming has no side effects” Neither does OOP if you code it not to!

4

u/tedharvey Apr 05 '22

It's like when you have two study guides. One for math and one for English. One guide will have math formulas and one will have grammar rules. There's nothing stopping you from study math and writing math formulas in your English book and vice versa. Same thing with programming functionally. When doing functional programming with a functional language, the standard library are geared toward not having side effects in your code. And it's not just about reducing side effects, the standard library will have many functions that assist you when following this workflow which non functional language will lacks since most will not be programming in that style.

2

u/warlaan Apr 06 '22

One of the reasons OOP is so popular - because it's easy to define strictly functional or strictly procedural programming, but with OOP you can just write code that is formally object oriented but effectively functional (when everything is immutable) or procedural (when you just put everything into one class).

Interestingly the guy who coined the term OOP (Alan Kay) also created the programming language Smalltalk, and judging by the standard library of Pharo Smalltalk it is usually written in an almost functional style.

He is also famous for saying that when he came up with the term he did not have C++ in mind (which goes the exact opposite direction).

2

u/SHA65536 Apr 05 '22

This. I never understood what it means. If side affects mean unintended consequences then obviously you don't want to have those. If by side affects you mean mutating data, then aren't side affects a really crucial thing? How would you interact with a file or make a web request without it?

And it never explains how there are no aide affects, what mechanism makes it so. Idk man, just confusing to me, from what I saw online, functional programming is just writing code in a mathy-fancy way... I would love someone to explain it to me like I'm 5

6

u/[deleted] Apr 05 '22

From what I understand its more so about separating the side effects into one part of your code base, and having all other functionality as "pure functions". pure functions are just functions that have a clear input/output and don't rely on any external I/O. Like for example instead of having a function that computes a sum and prints it, you have a function that computes a sum and another function that prints, so you keep the external I/O separate from the actual computations. Reason why you would want to do this is so that when you are debugging/testing/refactoring your pure functions it is easier to understand what they are supposed to do and very easy to write test cases. Having small, simple, pure functions makes it easy to compose them together to do greater things, and then pass the result of that to external I/O when needed.

2

u/plastikmissile Apr 05 '22

And it never explains how there are no aide affects, what mechanism makes it so.

It achieves it through immutable data. In a pure functional language everything is immutable. Meaning that once a value is set it can never be changed.

1

u/ExtraFig6 Apr 06 '22

If by side affects you mean mutating data... How would you intact with a file

Interacting with files in normal computers is stateful. But a lot of the data transformations can be done without side effects. So you do as much as you can that way.

Then you either just do the side effects only when you need, or the runtime gives you a way to express this functionally but injects the non functional bits. That depends on the language.

Once you're mostly using pure functions, the appropriate design patterns have already been developed for mathematics

1

u/ExtraFig6 Apr 05 '22

That's true. That would be functional and OO at the same time

1

u/plastikmissile Apr 05 '22

That's true, however the distinction here is that (pure) functional programming forbids you from having side effects. You can't have side effects even if you wanted to. It guarantees that code written in it will not have side effects. This means, among other things, is that you can parallelize this code without fear of conflicts.

1

u/toastedstapler Apr 06 '22

in functional programming it's explicitly opt in. in java you can't tell if someObject.someMethod(); does side effects or not from looking at its signature void someMethod(), but in haskell printStrLn has a signature of

ghci> :t putStrLn  
putStrLn :: String -> IO ()  

because of this it's only possible to execute it in certain places in your codebase

1

u/__Fred Apr 06 '22

One difference concrete between functional languages and imperative/OOP languages is that you don't have variables, you only have constants.

In Java you can write "x = x + 2", in a functional language you'd write "new_x = old_x + 2".

I think it helps the compiler find bugs and optimize stuff(?). I think functions without side-effects are easier to parallelize sometimes. You can also reorder calls or repeat them (think of RPC) without it impacting the result. (More practical advantages would be up to someone else to explain. I don't think FP is superior in every way. At least people seem to be able to understand OOP easier.)

It's true that OOP code doesn't need to have mutable variables, but when a programming language doesn't allow mutable variables, you can be sure there aren't any.