Prototype pattern in angularjs

Hi ,

I have following sample code in angularjs and was trying to refactor using prototype pattern.Can anyone guide me how to implement it ?

Thanks in advance

foo.controller.js

function fooControler($scope,d1,d2)
{

var vm=this;
vm.updateFoo=updateFoo;
vm.deleteFoo=deleteFoo;

function updateFoo(form,obj,value)

{    
      if(some cond) {
              showIt(form,obj,value)
           }
}

function deleteFoo(form,obj,value)

{    
      if(some cond) {
              showIt(form,obj,value)
           }
}

function showIt(form,fooObject,value)
{
  if(cond1)
  
  {
    something
  }

   else
  
  { 
  do something else
  }

if(cond2)
  
  {
    something
  }

   else
  
    { 

  do something else

    }

} // showIt

} //Foo controller

“Prototype” is an object creation pattern, but I don’t see you creating any objects. Can you clarify what you’re trying to achieve and what made you think the prototype pattern is the solution?

Hi Jeff,

Thank you for your response.In the above code snippet i am invoking showIt() function in a couple of other functions and would like to make function as property.I am not sure prototype pattern is the right pattern here to use, basically trying to refactor the code.So suggest me if there is better way (reusable,readable ,maintainable etc) of writing the above code ?

Okay - functions should be no more than 5 lines of code.

Is the deleteFoo statement a missing function parameter, accessed as a global variable, or does it appear further down? The functions should appear before the code that uses them, so that a person reading through the code trying to understand it doesn’t have to scan up and down trying to work out if something is missing.

Are the something and something else parts in cond1 and cond2 the same? If so then further optimisation can occur there.

The updateFoo and deleteFoo functions can be simplified further too, depending on their content.

Your request for reusable and maintainable code cannot be processed much further until further actual code is provided.

It’s been a little while since I’ve used AngularJS, but I believe you’re supposed to attach methods to the $scope object.

$scope.showIt = function () {
    // ...
};

$scope.updateFoo = function () {
    // ...
    $scope.showIt();
    // ...
};

Ouch. I mean… I get that shorter is better, but… 5 lines… ouch.

Yes, that doesn’t include closing braces either.

It comes from our friend in coding, Uncle Bob. He deals primarily with Java, but his advice on structure and refactoring is universal.

From page 34 of Clean Code

Off Topic:

Dammit windows 10, I would like your to respond to my typing at a faster pace than just one character per second, if I may?? Background activity after starting your web browser and being lied to about its availability is a real hoot.

Thank you all for your suggestions

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.