views:

207

answers:

5

How does one choose if someone justify their design tradeoffs in terms of optimised code, clarity of implementation, efficiency, and portability?

A relevant example for the purpose of this question could be large file handling, where a "large file" is "quite a few GB" for a problem that would be simplified using random-access methods.

Approaches for reading and modifying this file could be:

  1. Use streams anyway, and seek to the desired place - which is portable, but potentially slow, and is not clear - this will work for practically all OS's.
  2. map the relevant portion of the file as a large block. Eg, mmap a 50MB chunk of the file for processing, for each chunk - This would work for many OS's, depending on the subtleties of implementing mmap for that system.
  3. Just mmap the entire file - this requires a 64-bit OS and is the most efficient and clear way to implement this, however does not work on 32-bit OS's.
+2  A: 

Without constraints, this question rationally cannot be answered rationally.

You're asking "what is the best color" without telling us whether you're painting a house or a car or a picture.

Constraints would include at least

  • Language of choice
  • Target platforms (multi CPU industrial-grade server or iPhone?)
  • Optimizing for speed vs. memory
  • Cost (who's funding this and is there a delivery constraint?)

No piece of software could have "ultimate" portability.

An example of this sort of problem being handled using a variety of methods but with a tight constraint both on the specific input/output required and the measurement of "best" would be the WideFinder project.

lavinio
I am more concerned about 'best practices' - for example, it is considered best practice to use portable POSIX libraries even when only writing for Linux, rather than using OS-specific functions if the two happen to be equivalent.
Arafangion
+4  A: 

Not sure what you're asking, but part of the design process is to analyze requirements for portability and performance (amongst other factors).

If you know you'll never need to port the code, and you need absolutely the best performance, then you adjust your implementation accordingly. There's no point being portable just for its own sake.

Note also that if you want both performance and portability, there's nothing stopping you from providing an implementation for each platform. Of course this will increase your cost, so really, its up to you to prioritize your needs.

Justicle
"There's no point being portable just for its own sake" - An interesting concept, but isn't it considered best practice to design your application so that it is portable, even if you defer actual testing on alternate OS's for when you actually need it?
Arafangion
Portability has a cost, and developers are finite resources. "Agile" development says don't do more than you need; of course, we are wise not to take that too literally. But finding the balance still depends on the project and the resources. For example, I might develop a Win32-only app, knowing that it "might" run with WINE, or assuming it can be run on OS/X under VMware. So in that sense, I'm pushing portability off to someone else.
lavinio
@Arafangion - No, it isn't considered best practice to do anything if you don't need it.
Justicle
@Justicle: It is best to make a program portable so that there can be as many people that can use the program as possible. If you design your code to never be portable good luck when it comes to upgrading your program for the next version of any OS. ;)
Partial
@Partial - Of course! Its also best to make your program run as fast as possible, use as few resources as possible, have the least amount of bugs possible, and be developed in the shortest amount of time possible. Do you get my point? :-)
Justicle
@Justicle: I do see your point. On another hand, making a program portable does not mean that it will be slow and that it well take that much resources. It might take a bit more time to develop and be harder to program. In the end, you will gain more by designing it to be portable. The extra time you will have passed to make it portable will save time and money in the future. Seeing a program life on a longterm perspective is sometimes better.Back to your point, it is true that not all programs need to be portable, but when you can, try to do so.
Partial
"Agile" development says don't do more than you need. However, in many ways it's wise to plan for the future. That requires predicting the future, which isn't foolproof of course, but it's worth making an educated guess and perhaps taking a contingency analysis (risk management) approach.
Craig McQueen
+1  A: 

It really depends on the drivers for the project. If you are doing in-house enterprise dev, then do the simplest thing that could work on your target hardare. Mod for performance reqs as needed.

If you know you need to support different hardware platforms on day 1, then you'll clearly need to choose a portable implementation, or use multiple approaches.

Scott Weinstein
How would you choose between coding for a single OS, ie, make your program heavily dependant on win32, or design your application so that only a minimal amount of your code depends on win32, just in case you might want to use it on Mac OS X or Linux? I would, at the very least, attempt to use the MVC architecture (or similar) even if I never intend to change any of the components.
Arafangion
@Arafangion: You can design your code to become portable in the future without implementing that portability at the beginning. OOP can help a lot and using interfaces too. The Pimpl idiom is really a great way to make this happen.
Partial
+2  A: 

Basically, you need think first before coding. Every project is unique and an analysis of the needs could help decide what is primordial for it. What will make the best solution for any project depends on a few things...

First of all, will this project need to be or eventually be multiplatform? Depending on your choice, choosing the right programming language should be easier. Then again you could also use more than one language in your project and this is completely normal. Portability does not necessarily mean less performance. All it implies is that it involves harder work to achieve your goals because you will need quality code. Also, every programming language has its own philosophy. Learn what they are. One thing is for sure, certain problems frequently come back over and over. This is why knowing the different design patters can make a difference sometimes, but some languages have their own idioms and can be very relevant when choosing a language. Another thing that needs some thought is the different approaches that you can have for your project. Multithreading, sockets, client/server systems and many other technologies are all there for you to use. Choosing the right technology can help to make a project better.

Knowing the needs and the different solutions available today is what will help decide when comes the time to choose for the different tradeoffs.

Partial
Good point - portability does not necessarily mean slow.
Justicle
A: 

Portability for portability's sake has been a marketing spiel for Java since inception and is a fact of life for C by convention, and I believe most people who abide by it "grew up" with Java or C will say that.

However true, absolute portability will only be true for the most trivial to at most applications with medium complexity -- anything with high complexity will need specialized tweaks.

Jon Limjap