views:

421

answers:

11

Hi SO-Followers,

What are some situations where languages should be mixed?

I'm not talking about using ASP.NET with C# and HTML or an application written in C accessing a SQL database through SQL queries. I'm talking about things like mixing C++ with Fortran or Ada with Haskell etc. for example.

[EDIT]

First of all: thank you for all your answers.

When I asked this question I had in mind that you always read "every language has its special purpose".

In general, you can get almost everything done in any language by using special libraries. But, if you are interested in learning different languages, why not take the programming language that serves your purpose best instead of a library that solves a problem your language wasn't originally designed for?

+1  A: 

The biggest reason you would mix langauges is because one language has advantages in certain areas, where another has advantages in another. So, you try to harness the capabilities of both by throwing them together. A common example is using C and ASM, because C is more abstract and makes it easy to do the more complex stuff in a program, however, you would want to use ASM to do base level stuff with hardware and the processor. So, they are often mixed, given the nature of C and its use in embedded systems.

Peter
+1  A: 

Depending on what paradigm a language falls under, it has its pros and cons. For example, one might want to use the GUI capabilities of C# but use the efficient backtracking or Artificial Intelligence capabilities of Prolog (a language in the logical paradigm) to compute some details before displaying them.

A simplified example

Imagine trying to write a program that allows a user to play Chess against a computer.

(Potentially) I would:

1.) Create the visuals with Windows GUI Libraries.

2.) Calculate the possible moves using Prolog, and choose the best/most viable move.

3.) Retrieve the results from step 2 from Prolog in my C# code, and render the results.

4.) Allow for the gameplay and rapid development of visuals and UI in C# and rely on the Prolog for the calculations/backtracking

Sev
A: 
  • When 2 heads are better than one.

    I've commonly seen games where flash was embedded with C# - with the AI and other heavy code running off a C++ DLL.

  • When forced to

    Writing new code to augment a new system supported by an old framework

McAden
+4  A: 

I know in your question you sort of ruled this out, but different languages are used for different domains.

Right now I am working on a data visualizer, the data is in a database so of course there is some SQL, but that hardly counts because it's small and required frequently. The data is turned into a series of graphs, I'm using R, which is like MATLAB but open source. It is a unique statistical language with some advanced plotting features.

A data visualizer isn't just a graph generator, so there needs to be a way to browse and navigate this pile of image files. We opted to use html with embedded javascript to build an offline "application" that can be easily distributed. It's offline in the sense that it is self contained, that html is carefully generated and the js inside it is carefully crafted to allow the user to browse thousands of images sorting or filtering by a number of criteria.

How do you carefully craft javascript and html based on a database structure that changes as the rest of my team makes progress? They are made by a perl program (single pass script really) that reads into the db for some structure and key information, and then outputs over 300 kilobytes of html/js. It's not entirely trivial html either, imagemaps that are carefully aligned with the R plots and some onclick() javascript allow the user to actually interact with a plain image plot so this whole thing feels like a real data browser/visualizer application.

That's four 'languages', five if you count SQL, just to make a single end product.

I dont think doing this in a single language would be a good choice, because we are exploiting the capabilities of a real web browser to give us a free GUI and frontend.

Karl
+11  A: 

For example, in video games we use different languages for different purpose :

  • Application (Game) code : have to be fast, organized and most of the time cross-platform (at least win, MacOS is to be envisaged), often on constraint-heavy platforms (consoles), so C++ (and sometimes C and asm) is used.
  • Development Tools : level design tools generates data that the game code will play with. Those kind of tool don't need to run on the target platform (but if you can it's easier to debug) so often they are made with fast-development languages such as C#, Python, etc.
  • Script system : some parts of the games will have to be tweaked by the designers, using variables or scripts. It's really easier and cheap to embed a scripting language instead of writing one so Lua or other similar scripting languages are often used.
  • Web application : sometimes a game will require to provide some data online, most often in a database accessed with SQL. The web application then is written in a language that might be C#, Ruby(R.O.R.), Python, PHP or anything else that is good for the job. As it's about the web, you then have to use HTML/Javascript too.
  • etc...

In my game I use HTML/Javascript for GUI too.

[EDIT]

To answer your edit : the language you know the best is not always the most efficient tool for the work. That's why for example I use C++ for my home-made game because I know it best (I could use a lot of other languages as the targets are Win/Mac/Linux, not consoles) but I use Python for everything related to build process, file manipulation etc. I don't know Python in depth but it's fare easier to do quick file manipulation with it than with C++. I wouldn't use C++ for web application for obvious reasons.

In the end, you use what is efficient for the job. That's what you learn by working in real world constraints, with money, time and quality in mind.

Klaim
That scripting is called Lua, not LUA.http://www.lua.org/about.html#name
GogaRieger
I know, it's a typo I'll fix but it's not important........
Klaim
@Klaim OK, I just complained because I'm from Brazil and that way sounds bad.
GogaRieger
Ok, No problem :)
Klaim
+6  A: 

Well, the most obvious (and the most common) situation would be when you use some high level language to make most of your program, reaping the benefits of fast development and robustness, while using some lower level language like C or even assembly to gain speed where it is important.

Also, many times it is necessary to interface with other software written in some other language. A good example here are APIs exposed by the operating system - they're usually written with C in mind (though I remember some old MacOS versions using Pascal). If you don't have a native binding for your language-compiler infrastructure, you have to write some interface code to "glue" your program with "the other side".

There are also some domain-specific languages that are tuned specifically to efficiently express some type of computation. You usually don't write your entire program in them, just some parts where it is the appropriate tool. Prolog is a good example.

Last but not least, you sometimes have heaps of old and tested code written in another language at hand, which you could benefit from using. Instead of reinventing the wheel in a new and better language, you may simply want to interface it to your new program. This is probably the most usual (if not the only) case when languages geared for similar uses are mixed together (was that C++ and Fortran you mentioned?).

Generally, different languages have different strengths and weaknesses. You should try to use the appropriate tool for the job at hand. If the benefits from expressing some parts of the program in a different language are greater than the problems this introduces, then it's best to go for it.

stormsoul
I agree... I use OCAML and C, and even call outside packages written in fortran. Works wonderfully.
nlucaroni
A: 

For instance in games (which are pretty hardcore applications) you usually have a very tight c++ engine that does all the heavy lifting and a scripting language (such as Lua) that's accessible and suited for making that collection of special cases that we call 'game' happen.

Rodrigo Lopez
+1  A: 

This most often happens when you already have code written in two or more different languages and you notice that it makes sense to combine the programs. Rewriting is expensive and takes time.

In the financial world you often have to keep programs alive (or replaced) 50 years. With technology replacement every 10 years, new contracts (mortgages, life insurance) are created in the newest language/environment. The four older ones just handle the monthly payments and changes to existing contracts. To know how the company is doing, you need to integrate data from all five systems.

I suppose keeping the old ones alive is cheaper than migrating each time to the newest technology. From a risk avoiding point of view it makes sense.

Web applications should probably not be mixed language for the developer. Smalltalk does just fine, with Gemstone for persistence and Seaside as web application framework. The multiple languages (javascript) can be hidden in the framework.

Stephan Eggermont
+2  A: 

An excellent current example would be to write methods for creating XML documents in VB.NET, which has an "XML Literals" feature, which C# lacks. Since they're both .NET languages, there's no reason not to call one from the other:

Public Function GetEmployeeXml (ByVal salesTerritoryKey As Integer) As XElement
    Using context As New AdventureWorksDW2008Entities
        Dim x = <x>
                <%= From s In context.DimSalesTerritory _ 
                Where s.SalesTerritoryKey = salesTerritoryKey _
                Select _
                <SalesTerritory 
                    region=<%= s.SalesTerritoryRegion %>
                    country=<%= s.SalesTerritoryCountry %>>
                    <%= From e in s.DimEmployee _
                        Select _
                    <Employee firstName=<%= e.FirstName %> lastName=<%= e.LastName %>>
                         <%= From sale in e.FactResellerSales _
                             Select _
                        <Sale 
                            orderNumber=<%= sale.SalesOrderNumber %>
                            price=<%= sale.ExtendedAmount %>/>  %>
                    </Employee> %>
                </SalesTerritory> %>
                </x>
        Return x
    End Using 
End Function
John Saunders
A: 

People here have most of the reason. I'll just have this one:

There is also the case of graceful degradation. For instance I am working on a legacy intranet, and we're changing little by little from a language to another so at this point we have different languages in the same system.

marcgg
A: 

Device Programming - typically you would want to program the UI with the OS's native UI language (Java for Android for example) but would need to program the device drivers with something that gets more into the low level (like C).

stevedbrown