105

How does a Function Programming, such as Elm, achieve "No runtime exceptions"? That's easy. You simply don't write functions that fail. That might sound simplistic, but that's the gist of it. Take division, for example. We can simply define that anything divided by 0 is 42. Boom. Now, division no longer throws a runtime exception, it just ...


16

How the Functional Paradigm or programming approach eliminates runtime exceptions? Elm does it by encoding return values as Maybe or Result instead of causing a runtime error. type Result error value = Ok value | Err error Are runtime exceptions a great disadvantage of OOP over Functional Programming? As I understand it, the goal of Elm is to make the ...


15

"I realize this might be too trivial of an example" That is the point. Programs start usually simple and small, but become more and more complex over time. Without introducing more structure during this process, one will easily produce a big ball of unmaintainable mud. OOP provides tools for solving this problem - giving programs more structure. ...


15

It doesn't remove errors. It just uses the type system to force you to handle all errors explicitly. To understand Maybe types, you had to go back to older languages like C that doesn't have Exception. In C, errors are usually indicated by returning status/error code that you have to remember to check every time you do an operation. The problem with this ...


7

Two things occur to me that look ugly to my eye. IMHO the things you mentioned look quite less ugly to me than they were before. in the constructor to my Matrix class -- at the point when I know what the fixed size of the matrix will be -- I won't be calling the vector's constructor, but rather the function elem.resize() It depends on the point where you ...


6

Run-time exceptions are not related to the programming paradigm chosen, but to the fact that whatever the language chosen, something can go wrong at run-time: Exception handling emerged as a special control flow mechanism before OOP, and appeared in the context of structured programming in PL/1 in the 70s. Functional programming languages handle exception ...


5

A pure function is a function that: for the same arguments received it always returns the same result; running the function has no side effects. So these are properties of the function. It doesn't matter if the parameter you send to the function is a complex structure or not. If the function respects the above two conditions then it's a pure function. See ...


2

If we remove the context/meaning and just look at the code, yes, this is a valid way of doing it. However, what you're trying to do is to take a shortcut by trying to bypass the typing system that events tend to rely on. What you're trying to do here is no different from using object as method parameter types so you could pass any kind of object into it. ...


2

Pass the board to the piece you want a move list from. You’re correct that the board data needs to be where the moves are. But the board is mutable so it’s a poor choice to make into an objects state. So just pass it as a parameter to a method. Speaking of immutable, pieces don’t need to know where they are. Let the board keep track of that. All the pieces ...


2

I would create a separate class (e.g. RS485Channel) that represents the RS485 communication channel. This class would be responsible for multiplexing multiple sensors on the RS485 bus. The ISensor classes that represent/communicate with the sensors on RS485 would be constructed with an RS485Channel instance and the ID of the sensor on the RS485 bus. The ...


2

The core idea of functional programming is choosing the right types for your function. I will borrow the example of an easy and most evident example. Divide // Note: Scala Code // Here you cannot avoid the exception, // because your function is tied to a primitive data type def divide: (Float,Float) => Float = (a, b) => a / b // Here the function ...


2

It's less of a violation, but not fully solved yet. You did correctly fix that the render() method is now properly being overridden instead of adding different signatures, which was an issue in the beginning. If both your regular parser and your xml parser are part of the base type, and are not just being pushed into the base type because a derived type ...


2

what are the exact steps that take place and how is memory allocated to the member functions and the attributes of the class and in what order? This is language (and in some cases, implementation) specific. In a fair number of languages (e.g. Python, JavaScript), the allocation of memory is not a concept which is exposed by the language so your question ...


2

Short answer: OOP principles make code more maintainable. In your example however, there isn't much need to use a generic interface if you only have 2 types of InstantNotification. But what if you start to add more kinds of Notifications? You'll have to test and define them all individually even though their behavior is essentially the same. This goes back ...


1

As was already mentioned in the previous reply, the fact that you can do something does not necessarily mean that you have to :) In your case, it depends on whether you need to instantiate your classes and actually work with their object containing various data/states, or not. Using static classes/methods is in a certain way a bit against OOP principles ...


1

As others have pointed out, the core difference here is in the way the effect of an exceptional situation is handled. In functional programming, it is somewhat common to encode the possible exception states into the return type. This can get a bit conflated when you start having both business errors and technical exceptions encoded into the same error ...


1

You seem to have a basic misunderstanding of how instantiation works and how a program goes from human-readable text to a format that can be executed by a processor. First, when a source file with a class in it is read by a compiler or interpreter, the entire class is read and converted to a data structure that can be used by either the runtime environment ...


1

Logically, a valid move is not something determined by an individual piece, but by the application of the rules to an overall board (consisting of the current placement of many pieces), and also (in the case of castling) by an analysis of previous moves. You could dispense with the history, by keeping a separate account of castling rights, but this absence ...


1

Your code is in dire need of some SRP optimization. The responsibilities you're trying to mash into a single class (even in the current situation) should be distributed among several classes. Your question is suggesting to start making use of generics, which would further complicate an already too complex class definition. Part of the reason why you're ...


1

The pattern I would use would not be far removed from the service + repository pattern common in ASP.NET. Except instead of exposing web functionality, you are exposing an API for other programs to work with, and instead of accessing a database, you are accessing a hardware device. Repository layer Provides data access to the hardware Its single reason to ...


1

Expressiveness is in the eye of the beholder. (Many famous programmers have used the word "expressiveness" to mean different things, sometimes in contradictory ways.) To appreciate Bjarne Stroustrup's approach for C++, one has to be armed with intimate knowledge with the lower level working of C++, such as memory allocations, the standard library, ...


1

Pure functions, as discussed by Bogdan, have two important features: They give the same result for the same arguments. That means their return value cannot depend on global variables, external input and the like. A function of type (say) MyObject -> int can be pure as long as only the attributes of influence the return values. They do not have any (...


1

If you wider the context a little bit, you can find a pretty good use-case for this, like dependency injection. Imagine having a class that depends on another one and calls its method. You want to implement it as a loose coupling code with a minimum dependency between those two classes, so you can replace the dependent class with some other, without breaking ...


Only top voted, non community-wiki answers of a minimum length are eligible