Sign in to follow this  
Wush

How do i get flexible/easily interchangeable code?

Recommended Posts

Imagine for example you have a population with birth, death and illness however all these things are highly influenced/modified by species, environment, support structures. Even worse your species might be custom made from a list of traits and some traits might even change the whole way things work (undeads, hibernation etc). So any programming techniques or tips to create such a free system?

Share this post


Link to post
Share on other sites
Hmm that pattern feels incomplete would it not make sense to have a kind of index class that keeps track of whats the newest wrapper to use for each method,so you do Not have to replace every reference to a wrapped object? And how does this influence performance once you have wrapped something 10 or 12 times? Also might be good to do this like a linkedlist to easier remove wrappers.(demolished buildings)

Share this post


Link to post
Share on other sites

So any programming techniques or tips to create such a free system?
The ultimate implementation of a free system is a general purpose programming language (although it has boundaries too).

 

Anything you add reduces the free-ness. On the other hand, if the addition is in the direction you want to go anyway, the additional restriction is not a problem, it saves work.

This is the exchange you get with a game engine. It pushes you in a framework, and in return you get a lot of functionality pre-implemented. As long as you have no desire to step outside the limitations set by the engine, the benefits are usually bigger than the costs, ie it's a good trade.

"Design" in that respect is deciding what restrictions are acceptable in the context of the problem that you're solving.

 

The result of this is that you cannot have "flexible/easily interchangable code" in the general sense. Rather than trying to capture the world, it's often better to chop of some of the world that is not likely to be of interest, and design for that smaller world, increasing the added value that you can provide.

Share this post


Link to post
Share on other sites

I would add that in terms of code design, it's often a bad idea when you try to be as flexible as possible.

 

I wish I could give you more likes. I've seen this problem strike more than a few times professionally. Just about every big major "flexible" system I've ever seen ends up _also_ needing to be rewritten every time a new feature comes online, only it takes 5x longer to rewrite than the simpler "inflexible" code would have been.

 

 

That said, I think the OP is asking a more reasonable question closer to: http://gamedev.stackexchange.com/questions/29982/whats-a-way-to-implement-a-flexible-buff-debuff-system

Share this post


Link to post
Share on other sites

First my apologies for the late answer I needed some time to consider things carefully. Indeed when I talk about flexibility I mean "modular" and "easy to modify", the system does not have to provide all functionality on its own but its has to move out of the way when something new or different is added.

The buff system or any event system are a good example of things i have considered in the past and indeed difficulties tend to run rampant, so much I even looked up a specialized programming language at some point(I think it had something to do with autonomous agents, i honestly forgot).

It is exactly to avoid to overly complex solutions I am asking for tips,

 

P.S.: I think I was searching for code that instead of listening for parameters with if checks could activate or deactivate based on triggermessages at that point.

 

Also Sean considering the link you gave me I still need to read everything carefully, right now I lack some concentration to take in the information completely

Edited by Wush

Share this post


Link to post
Share on other sites

The simplest approach to get flexibility is to not prepare for any future stuff.

 

Instead, make it work for the needs you have today, and nothing more.

When you have a new addition (and not earlier), refactor the code to make it all fit nicely again, and add the required new functionality, and nothing more.

You can repeat this as often as needed.

 

It always gives you the simplest possible code (there is no code you can remove while keeping all functionality), and it executes quickest, as it doesn't take non-existing cases into account.

 

 

The rationele behind this approach is that you cannot predict the future. Either it doesn't happen (you changed your mind about some feature), or it doesn't happen as you planned (at the time of actually adding it, new ideas and approaches exist that you didn't take into account during the design).

 

This also means you don't have to consider future additions while designing, which makes the design a lot simpler and faster.

Share this post


Link to post
Share on other sites

The most flexible systems I've seen in use started out in their first generation as being non-flexible.  

 

After the first project worked out, a second generation was created which reused many pieces and threw out many pieces, fixing problems along the way.

 

After the second project worked out, a third generation was created which reused many pieces and threw out fewer pieces, fixing problems along the way.

 

After six or seven generations, the tool has a great set of options, a library of reusable pieces, and a great amount of flexibility.

 

 

You cannot start seven generations out, you need to start with the first.  Get something out that is non-flexible yet gets the job done.

Share this post


Link to post
Share on other sites

Thanks for all the answers so far , ofc the answers of "do it simple" are not quite reason for me to jump of joy, but I appreciate them nonetheless.

After taking a good look at the buffsystems let me explain my current thought and problems:

 

I am splitting modifiiers of variables into +x1,+x2%,*x3 and provide an double[3] modifiers for x  instead of x itself to access from outside the method and I have a variable that points to an outermost wrapper(Object starter) so I can modify and overwrite the method. Now I seperate simple variable modifications and keep them in a list to reduce the amount of wrappers needed, each of these simple modifications will have an apply and remove method so I do not add up modifiiers every time I call the method. 

 

The big problem for me is how to handle the removal, will each wrapper or simple modifier need a remove  boolean to check  each time its executed, do I trust for example a buildings demolish instruction and instead of a list I use a map to easier find the modifiers to be used. Do I keep a seperate list for timed modifiers so I can check each time if they expire?  

Edited by Wush

Share this post


Link to post
Share on other sites

The big problem for me is how to handle the removal
At some point you have to trust your environment. The problem of how much can be a tough one.

 

You may want to consider a sort of risk analysis.

 

1. How likely is it that the other code messes up? (by accident mostly, intentional bad behavior is pretty much not solvable)

2. How hard is it going to be, to find the above code if a problem arises?

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this