How to replace functions or methods?


The simplest way to replace a function or method is to refactor the existing code so that it no longer calls the function or method being replaced. This is a common approach in test-driven development: you make the test fail first, then write the new code to pass it. Once it’s passing, you can remove the old code.

This approach works even in cases where you can’t see the call sites. For example, you might have an iterator that returns all objects of a given type from an object graph (e.g., an in-memory database or DOM tree). You could simply add a “skip these objects” method to your iterator, and then refactor all clients of the iterator to skip over those objects that should no longer be returned.

Let’s say you want to replace a function in your code with another function. You have to search the codebase and replace every instance of the function call.

Now, let’s say that you want to replace a method in your code with another method. You have to search the codebase and replace every instance of the method call.

That seems simple enough. But if you’ve ever had to do it, there’s a good chance you ran into trouble somewhere along the way. In all likelihood, there was some place in the code where it made sense to use the old function or method but not the new one, and so you had to make an exception for that case. And then you might have found some place where it made sense to use both versions of the thing, so you had to create yet another version that could handle both cases (or possibly something more complicated).

I think the point of this is that your code should be able to evolve in steps: small changes should be possible.

So, you have a function or method which does something, and you want to replace it with something different, or move its definition to another place.

The solution is to leave behind a “stub” — a function or method with the same name, which knows how to call the old version.

If you’re using OOP, you can use class hierarchies and/or simply move the methods around:

function doSomething() { // old code}class OldClass {// …method doSomething() {// old code}}class NewClass extends OldClass {// …method doSomething() {// new code}}$obj = new NewClass();$obj->doSomething();

When you want to replace a function or method in object oriented programming, it is easy and fast if you only need to replace the code that implements the function. In this case, a simple subclass with your new method will do the trick.

However, if you want to change any of the following things, you will have to do more work:

– The class of objects returned by your function/method

– Any references held on to your instances (these are hard to find)

– The number or order of parameters

– The calling convention (e.g., if one or more of your parameters becomes optional)

The problem is that, when you override a method, there are often multiple callsites. You have to be sure that all of them can handle whatever changes you make.

The most common function in programming is the function that replaces other functions. You write a function which, when called, creates and returns another function, as in:

function make_adder(x) {

return function(y) {

return x + y;

};

}

var add5 = make_adder(5);

var add10 = make_adder(10);

print(add5(2)); // 7

print(add10(2)); // 12

The second-most-common function is the one that replaces methods. In Python you’d write something like:

class Adder:

def __init__(self, x):

self.x = x;

def __call__(self, y):

return self.x + y;

class Adder:

def __init__(self, x):

self.x = x;

def __call__(self, y):

return self.x + y;

add5 = Adder(5);

add10 = Adder(10);

print add5(2);

Replacing an operation with one that is more efficient is called optimization. It is critical to understand the difference between optimization and premature optimization.

Premature optimization is the root of all evil (or at least most of it) in programming.

— Donald Knuth

The key to writing fast software is not to make the software fast. Rather, the key is not to make it slow. The first rule of optimization is Don’t do it. The second rule of optimization (for experts only!) is Don’t do it yet.

— Michael A. Jackson

Often, when you write some new code, you’re not sure if it’s faster or slower than what you had before. You can’t just ask the computer, because that would require running both versions and seeing which one finished first. But they might finish in a different order each time! So how can you tell? There’s a simple solution: don’t worry about it until you have reason to think one version really is faster than the other.

That’s premature optimization: worrying about making something fast before you know whether you need to. Premature optimization may be the root of all evil, but we can’t avoid it entirely, because we do sometimes know when one thing will be faster than another.

We are in the midst of a technology shift. A few years ago, if you wanted to write software for a phone or tablet, you had to use the tools the vendor provided – Objective C for iOS and Java for Android. But now you can use another language: JavaScript.

This is a huge change. Because JavaScript is so different from the languages that were used before, it enables a new approach to building software. This approach is known as reactive programming.

Reactive programming has been around since the 90s, but this is the first time it has become mainstream. One reason is that we finally have browsers that are fast enough to make it work. Another is that the frameworks currently available are much better than they used to be. And finally, companies like Netflix, GitHub and PayPal have adopted it and proven its real-world value.

How does reactive programming differ from traditional approaches? The most significant difference is that whereas traditional programming uses functions or methods, reactive programming uses events and data streams as its basic building blocks. I’ll explain what these mean in more detail soon, but for now just think of them as lists of things happening at different times or things changing over time (for example, your GPS coordinates).


Leave a Reply

Your email address will not be published.