AlexMorley-Finch
AlexMorley-Finch

Reputation: 6955

design patterns for code that breaks regularly?

I have this class that tries multiple methods of getting data from Google maps web services API.

If one method fails, it tries another. etc.

Something like this (pseudo code):

FUNCTION FIND_ADDRESS( house_number, postcode )

    get location co-ordinates for postcode from local database

    if location returns false, try getting location from maps service

    if map service fails, return "postcode not found", exit

    get address components using location co-ordinates

    if address components doesn't contain street name, return street name not found, exit

    if street name exists, get all address_components + location for house number, street_name and postcode

    if no results, try again without the postcode,

    if still no results, return location co-ordinates for postcode found earlier in code

END

As you can see, This is very procedural!

I'm trying to think of ways to improve the code, and I've externalised all re-usable code, added exception handling to know exactly where the code fails if it does.

But I was wondering if anybody knows of a design pattern or similar solution.

Because I'm basically trying something, if it fails trying something else, if it fails trying something else and so on until I get a full address

Any ideas?

Upvotes: 3

Views: 143

Answers (5)

Joseph
Joseph

Reputation: 119837

i would try something around like:

public function getAddress($houseNumber,$postCode){

    // as far as i know, if the function returns a LOOSE true, the condition is true
    if($data = location.coordinates()){ 

        // $data was returned a value,  do additional parsing here

        // if you need to return early because of an error, you can in here

        //if all proccesses deem your data valid, return the data you want returned
        if(processedAsValid){
            return $some_value;
        }
    }

    //if the previous didn't return, it goes here. $data is overwritten
    //or you can use some other variable name
    if($data = maps.service()){

        //some more parsing

        return $some_other_data;
    } 

    // if non of the above was satisfied (and thus none returned yet), return a FALSE
    return FALSE;

}

Upvotes: 2

Gordon
Gordon

Reputation: 316969

You might want to look into Chain of Responsibility.

In Object Oriented Design, the chain-of-responsibility pattern is a design pattern consisting of a source of command objects and a series of processing objects. Each processing object contains logic that defines the types of command objects that it can handle; the rest are passed to the next processing object in the chain. A mechanism also exists for adding new processing objects to the end of this chain.

So instead of having many if/else or try/catch blocks, you do something like

$finderChain = new AddressFinder;
$finder
    ->add(new LocalFinder)
    ->add(new MapsService)
    ->add(…);

$result = $finder->find($houseNo, $postCode);

Internally, you will send $houseNo and $postCode to the LocalFinder. If it doesn't find the desired data, the next element in the chain is tasked with trying to find the desired data. This is repeated until either the end of the chain is reached or the desired data was produced.

Upvotes: 5

zaf
zaf

Reputation: 23244

Its not a question of it being procedural/oop/whatever.

If you understand and maintain the code easily then great. If you can do it in 6 months time then even better.

Your function block looks fine as it is - just watch out how deep your nesting is. The shallower the better.

Upvotes: 2

OhDude
OhDude

Reputation: 225

You have only 6 "if"'s which isn't much at all, I used to work with code that needs up to 50 if's and the program works just fine. There is no software pattern concept that fits your current problem. DP is not a solution to a particular problem, it is a concept to solutions of a recurring problem. Reading patterns helps me personally learn more solutions to even social problems, coding patterns, process modelling, social issues in Pair Programming and more, it offers lots of thoughtful ideas.

Upvotes: -1

OM The Eternity
OM The Eternity

Reputation: 16204

Use nested try{} catch{} block

Upvotes: 0

Related Questions