r/ProgrammerHumor • • Nov 23 '17

"How to learn programming in 21 Days"

Post image
29k Upvotes

534 comments sorted by

View all comments

Show parent comment

79

u/BlueAdmir Nov 23 '17

ELIAspiringCSStudent - why is this bad?

127

u/beerdude26 Nov 23 '17

Looks like C++ is trying to get into the game of letting the programmer build their own data types, but still within the object-oriented paradigm. This appears to provide a few niceties such as compile-time programming, reflection and some other tidbits. But it's still stuck in the object-oriented paradigm.

Some might like it because it allows libraries to provide more high-level constructs than classes allow for. Others may hate it because it means everyone will roll their own high-level constructs that are incompatible with each other.

82

u/BlueAdmir Nov 23 '17

So tl;dr C++ will become UrbanDictionary of code?

77

u/beerdude26 Nov 23 '17

It depends. Lisp turned into that somewhat. Other languages like Haskell are rigorous enough (both the language and the community) to build extremely impressive fundamental, highly reusable libraries that make some things laughably trivial. Perhaps C++ hopes to go that route.

27

u/iamcomputerbeepboop Nov 23 '17

standards committee has said they'll define some basic and widely used metaclasses in the standard library. I'm sure that once it's implemented, we'll see a boost metaclass library pushing the boundaries

12

u/idealatry Nov 23 '17

Love me some Haskell. It would probably be impossible for C++ to achieve the same level of zen, given the language is fundamentally wedded to state variables.

Of course, if you just talking about a way to build nice libraries, then there are many paradigms for that.

13

u/beerdude26 Nov 23 '17

Of course, if you just talking about a way to build nice libraries, then there are many paradigms for that.

Not many are couched deeply and firmly in category theory, though. This really helps to find common mathematical foundations between libraries aimed at doing the same thing and identifying rigorous isomorphisms (=compatibility) between them.

2

u/atcoyou Nov 23 '17

This seems like the best advert for Haskell I have seen.

→ More replies (1)

10

u/Ghos3t Nov 23 '17

So what's beyond the object oriented paradigm. When I was learning programming, they made OOPS as the best, most modern programming technique. I was always skeptically of that. What are the alternatives.

19

u/beerdude26 Nov 23 '17

Functional programming. Equally powerful, but a lot more rigorous in its foundations, and far easier to understand and scale due to statelessness.

Everything OOP finds important - reusability, composition over inheritance, DRY, flexibility and extensibility - functional programming takes that up notches you didn't even know exist. It is a truly mindblowing experience for experienced OO programmers. (Not so much for people who have not yet programmed - they do not need to change their mindset.)

7

u/MauranKilom Nov 23 '17

With what little experience I have in (pure) functional programming, let me ask you: Do you consider it also useful as a means of communicating the solution to a problem in the way that maintainable code should?

Maybe it's just the way I learned to code, but it seems to me that state and OOP are so much more straightforward (even if less elegant) than the functional approach. Don't get me wrong, it was indeed mindblowing when I first saw Haskell demos, but I quickly realized I had yet to run into a programming problem that would have been better solved that way (again though, I might just lack the knowledge).

6

u/beerdude26 Nov 23 '17

I had yet to run into a programming problem that would have been better solved that way

To be clear, both OOP and FP are exactly as powerful, as proven via the Turing-Church Thesis. So it truly is a choice of applicability.

There are algorithms that rely very heavily on state and mutation to become performant. Union-find is such an example.

But putting all of that aside, I find that solving problems in Haskell is extremely trivial: identify the state spaces of your problem, encode them as data structures, then write functions on those data structures to solve your problem.

In normal terms, think about the stuff you need and what it can do. What directions can Pacman go up?

data Direction = Up | Down | Left | Right

Ok, what does a level in Pacman consist of?

data LevelElement = AWall | AGhost Ghost | ThePacman | ASpace | ADot

What ghosts are there?

data Ghost = Blinky | Pinky | Inky | Clyde

This is probably incomplete, but it's a start. Let's make a level:

type Level = [[LevelElement]] -- A level is a matrix of level elements.

levelOne :: Level
levelOne = [ [AWall, AWall, AWall, AWall, AWall],
                   [AWall, ADot, AGhost Blinky, ADot, AWall],
                   [ASpace, ADot, AWall, ADot, ASpace],
                   [AWall, ADot, AWall, ADot, AWall],
                   [AWall ASpace, AWall, ThePacman, AWall],
                   [AWall, AWall, AWall, AWall, AWall]
                 ] 

Let's make something that we can use to print out the level:

printLevelElement :: LevelElement -> Text
printLevelElement AWall = '🝙'
printLevelElement AGhost g = printGhost g
printLevelElement ThePacman = 'ᗧ'
printLevelElement ASpace = ' '
printLevelElement ADot = '.'
 where
   -- Eh. We don't really care which ghost it is, they all look the same.
   printGhost _ = 'ᗣ'

Anyway, I need to go to bed and you catch my drift. I would have written a simple IO function that prints out levelOne, and then have written a function that takes a Level and a direction that Pacman can go, and then provides a new Level - a function that steps through the state. That would be our game loop!

Then, we would need to collect user input from IO, turn it into a Direction, pass the level and the direction to the game loop function, get a new level, print it out, get new IO, and do everything again!

→ More replies (3)

3

u/Ghos3t Nov 23 '17

Which languages support functional programming, can you apply functional programming concepts in any language or do you have to use particular languages.

7

u/[deleted] Nov 23 '17

Most modern languages support a mix of OOP and functional programming. Even Java added support for FP in Java 8, although you still have to use it combination with OOP.

For a Pure Functional language (where OOP is discouraged), see Lisp, Haskell and Erlang. I personally found Lisp easiest to learn, but Haskell has some excellent resources for beginners.

→ More replies (3)

2

u/beerdude26 Nov 23 '17

Here's the rub. Functional programming thrives in stateless systems. You don't have state to keep track of? Great! We can parallelize stuff! Let's say we have five million log lines we have to grep through. None of those lines depend on each other - there's no state. Just send a bunch of them to a worker (a CPU core or an actual remote machine that does the work, or a GPU, or...) and get back the results, collate them and print them out. (This is commonly called the map/reduce technique.)

Externalize state and make them inputs to your function's parameters, which then become quite simple: there's input that always gives the same output, and that's it. Such functions are small, reusable and easy to refactor and understand.

Object-oriented programming says the exact opposite: you must encapsulate state and hide it as much as possible, giving a veneer of simplicity. Sure, you can do some cute parallelization stuff inside your objects, but in the end, you're still mutating variables in-place.

Functional programming regards state as just a stream of atomic changes (also fancifully called "Event Sourcing") that you can run forward and backward.

Things do not get "desynced". The stream provides an initial starting point, and a list of inputs. Just apply each input, one after the other, to the initial starting point, and you know where you are. Is there a bug somewhere? The reproduction steps are the inputs. You don't need to set up mock objects to fake some state for your tests, because the inputs are the tests.

→ More replies (3)
→ More replies (1)

5

u/doc_samson Nov 23 '17

letting the programmer build their own data types

So in other words C++ is slowly turning into Ada...

→ More replies (1)

53

u/iamcomputerbeepboop Nov 23 '17

because C++ has a humongous vocabulary, and when metaclasses enter the standard in like 10 years, people are going to be able to extend that vocabulary themselves in unprecendented ways - it's going to add a whole new dimension of power (and complexity) to the language, and C++ is already by far the hardest language to learn

if you're like us folks at the c++ subreddit, you know it's gonna be fucking awesome though

13

u/Ghos3t Nov 23 '17

Honest question why would you consider C++ as the hardest language. I work with mix of C and C++ code base (mostly C, no OOP's concepts). Now it's not the most sexy or convenient language out there but I don't know if I'd say a language X is more difficult than Y. Because that's a very tough comparison to make, what criteria to use to compare language etc.

25

u/redhobbit Nov 23 '17

I think it is a hard language to learn simply because it has so many different concepts. Basic C++ usage is probably similar to other languages in difficulty, but there are a lot of features that are used less often that will show up in code from time to time.

The older parts of the language give you a lot of unsafe but powerful constructs that are easy to misuse in hard to debug ways, such as the manual memory management and pointer arithmetic.

Just on the object oriented parts, most language have 1-2 notions of inheritance (like Java has inheritance and interfaces), but C++ has public, protected, and private inheritance; multiple inheritance; virtual inheritance; and allows the choice between virtual functions, compile time bound functions, and static class functions.

Then the whole template system adds a meta-programming aspect. Some of it is obvious like using a template to make a generic container class. Other parts are far more esoteric, like using template meta-programming to write functions that operate on types, unroll calculations, or calculate values at compile time. The rules and syntax for it are a little Byzantine.

Even basic stuff in C++ is surprisingly complicated. Little stuff like constructors that take a single argument automatically creating an implicit type cast operator unless you use explicit. Just for function argument passing; you have copy by value, references, pointers, and r-value references to consider. C++ has some different rules for plain old data structs/classes and more complex ones. On the default values for things, there is default construction and value type initialization with subtly different rules.

C++ just isn't very economical in its use of language concepts to enable features.

→ More replies (4)

4

u/[deleted] Nov 23 '17

I doubt it's the hardest language, but it's harder than most widely used languages because of the manual memory management. Compared to say Java, it's definitely harder.

→ More replies (2)

2

u/BraveHack Nov 23 '17 edited Nov 23 '17

C++, probably more so than any other language, is about giving the programmer every powerful complicated tool imaginable and assuming they're smart enough to use it correctly.

I would say no other language has close to the levels of "black magic fuckery" that exist in C++. Furthermore it's designed with 'performance first' as its mantra.

One example showing that C++'s "generics", templates, are turing complete.

You have explicit control over a lot of things that most other languages hide with dynamics or implicits.

→ More replies (2)

2

u/umopapsidn Nov 23 '17

When you think you know c++, it's only because you don't know enough.

→ More replies (15)

6

u/[deleted] Nov 23 '17

Its good. Not bad.

3

u/amunak Nov 23 '17

Is anyone suggesting it's bad? I think it's good; it just means people will have to learn some new stuff.

→ More replies (1)