Jump to content

reuse function and simplify this


espace
 Share

Recommended Posts

hi,

i'm searching to simplify my code and use a lot of possible the reuse principle. but in this case i don't see how could i do to achieve that . Any suggestions ?

thanks.

 

	var logic_position=function(sprite){
		if (debug_position){
			console.log("retablissement des paramètres")
			hero.grid.visible=false	
			var _table
			var _name_json
			switch(sprite.name){
				case 'canon':
					_table=c
					_name_json='canon'
					console.log('sprite.angular',sprite.angular,_name_json)
					//var sprite.number=sprite.number
					_table[sprite.number] = {
						number:sprite.number,
						delay:sprite.delay,
						x:sprite.x,
						y:sprite.y,
						speed:sprite.speed,
						frequency:sprite.frequency,
						variance:sprite.variance,
						angular:sprite.angular,
						_flag:sprite._flag,
						kill_with_world:sprite.kill_with_world,
						special_color:sprite.special_color,
					};
					break
					//asteroid = function(posx,posy,speed,radius)
				case 'asteroid':
					_table=a
					_name_json='asteroid'
					_table[sprite.number] = {
						x:sprite.x,
						y:sprite.y,
						speed:sprite.speed,
						radius:sprite.radius,
					};
					break
					//neon = function(delay,posx,posy,speed,posx_in_tween)
				case 'neon':
					_table=n
					_name_json='neon'
					_table[sprite.number] = {
						delay:sprite.delay,
						x:sprite.x,
						y:sprite.y,
						speed:sprite.speed,
						posx_in_tween:sprite.posx_in_tween,
					};
					break
					//
					//pulsar = function(delay,time,posx,posy,speed,scale_factor)
				case 'pulsar':
					console.log('sprite.name',sprite.name)
					console.log(sprite.x)
					_table=p
					_name_json='pulsar'
					_table[sprite.number] = {
						delay:sprite.delay,
						time:sprite.time,
						x:sprite.x,
						y:sprite.y,
						speed:sprite.speed,
						scale_factor:sprite.scale_factor,
					};
					break
				case 'dalle':
					_table=d
					_name_json='dalle'
					_table[sprite.number] = {
						delay:sprite.delay,
						x:sprite.x,
						y:sprite.y,
						speed:sprite.speed,
					};
					break
			}
			localStorage.setItem(_name_json+sprite.number+'lev0', JSON.stringify(_table[sprite.number]));
		}
	}

 

Link to comment
Share on other sites

You fundamentally can not create a reuse function for different use-cases, although you might be able to extract common parts and refactor some bits together to be able to extract commonality.

I'm not quite clear on the:

_table = a
_table[sprite.number] = {...}

I'm assuming 'a' (and the other ones there) are arrays stored outside the function, if this is so, then why create the `_table` variable, just set directly on the array.

Are you grabbing specific members of the different types of object you have? Or just grabbing everything that exists on the object? If the latter then you can create a generic function that copies over all sprite attributes to an array entry (like you're doing), if the former then somewhere there will be a piece of code for each type of object you're dealing with.

I'm not sure why you explicitly name things again, when it seems to copy the sprite.name variable.

You can get rid of some unnecessary transient state, and you can refactor out of the switch statement (note this isn't becoming DRY, it just makes this function more readable):

const tableAssociation = {
  canon: c,
  dalle: d
  ...
}

function getTable (sprite) {
  const {name} = sprite
  return tableAssociation[name] || null 
}

function createDebug (sprite, table) {
  let table = getTable(sprite)
  table[sprite.number] = sprite
  return table[sprite.number]
}

function logic_position (sprite) {
  if (debug_position) {
    hero.grid.visible = false
    const debug = createDebug(sprite)
    localStorage.setItem(
      sprite.name + sprite.number + 'lev0',
      JSON.stringify(debug)
    )
  }
}

I'm made some fairly sweeping assumption here:

* I've used just one `createDebug` function and assumed you want all props on a sprite, you might want to track enumerable props, or some other metric

* You might want to extract different values for each object type, you could create separate functions for this or use something like the `tableAssociation` object I used which specifies which attributes to pull from the object and then you'd have just the one createDebug function again.

* createDebug returns a value but also creates side effects, given that your code snippet is riddled with side effects I'm guessing you're ok with that

* there's quite a bit of stuff that comes from outside of the function, I've tried to keep with your style, assuming that all that stuff is necessary and desired

The key thing is that if you're treating each object type differently then you can't really create just one function to deal with it, but, you might be able to refactor and extract some commonality to achieve some reuse.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...