6

I was started programming in 6 months ago in college. I learned Python, C and Java. I want to create a project and using these languages. For example to searching part should use C because it's faster than the others. To design java etc ... How do you use different languages in same project? Is there any special method to optimize them? How can I research this subject. Is there any keyword?

NOTE: For instance , The Steam ,which downloading game, was created C++ , Objective C and Java. Why use different languages in same program like Steam or other any programs?

claymorehack
  • 73
  • 1
  • 5
  • 2
    Possible duplicate of [How to make support for bindings for a scripting language](https://softwareengineering.stackexchange.com/questions/193521/how-to-make-support-for-bindings-for-a-scripting-language) – Basilevs May 21 '18 at 04:40
  • Another possibility is to split things into web services. You could use C for the "search" web service, and you are free to write the front end (be it a web front end or installed GUI) in Python or Java. Gives you experience in service oriented architecture, though to be honest not many web services get written in C... – Greg Burghardt May 21 '18 at 15:47
  • 2
    Basically, it's pretty common to use multiple languages in the same project/solution/ecosystem, but they usually interact via web services. – Greg Burghardt May 21 '18 at 15:51
  • Not to forget the "natural" language in comments, or worse in names of variables, functions, classes,... While you might write them in English, Asok will prefer Tamil, Hans German or Denglish, Yuri Russian, ..., and everythings will turn out to be an easily understandable mess... "Clean Code" teaches us to keep things simple for the ease of reading the code somewhen later. – Bernhard Hiller May 30 '18 at 08:24

3 Answers3

9

When you don't have to, don't do it! It will become very painful, especially if the languages cross boundaries like you suggested (read Clean Architecture about the boundaries).

If you really want to do it, there are are so called interop layers in each software (NDK for Java for example). But you always have to care about the representation of variables and concepts in memory and the underlying platform.

The worst here: You have three completely different kind of programming languages: C is compiled and very (very) near at the hardware, Java is a mix between compiled and interpreted and is a little bit more away from the platform (it is leaking through here and there) and finally you have an interpreted language with python which hides the hardware.

Your argument for doing it sounds for me like premature optimisation: You should only improve performance when you measure a problem with performance in your current application. In my opinion Java is really fast through the JIT (See Kafka for example - it is incredible).

Note: When you really want to use different languages make sure they are all on the JVM like Java with Groovy and Jython for example.

[EDIT]: Was mentioned in the comments: There are use-cases where different languages makes sense. The best example is a web-application, where you have HTML, Java-Code and SQL-Code in one project (even with three different paradigms: Just Markup, Imperative, Declarative). But you have VERY clear boundaries (HTML has an different file-extension). Even in this clear scenario, it is hard to not mix the languages an cross boundaries. You should for example push all SQL-Code awai from your business-logic and keep the HTML separate. In short: Try to avoid it, but when you have to do it, be aware of your boundaries.

  • While C is definetly low level compared to the others saying it is very (very) close to hardware is somewhat misleading, if anything its very close to some sort of virtual PDP-11 machine, not actual hardware – jk. May 21 '18 at 16:29
  • thanks your answer Thomas but it still uncertain to me . For instance , The Steam ,which downloading game, was created C++ , Objective C and Java . As your answer this situation is very complex as i understand . Why use different languages in same program like Steam or other any programs? – claymorehack May 22 '18 at 00:27
  • There are cases where you can get big advantages from using different languages. Especially when you want to solve different problems. The best example is a web-application. You use HTML/CSS for the frontend-design, maybe java for the backend and for the database-queries SQL. But you can see a pattern here. There are very clear boundaries between all the layers. In the HTML/CSS example you see it very well. You even use a different file extension. In short: Different languages for different languages can make sense, but only when you can draw a VERY clear boundary. – Thomas Deniffel May 29 '18 at 17:20
2

You might try to refine your concept of "one project". If you're using the languages along clearly defined boundaries it sounds like you have multiple projects that work together. If that is the case then it's probably best to split them into separate projects. As distribution or deployment you can add the combining or deploying into the build process. This approaches offers some clear advantages. It allows you to keep a clear distinction of dependencies, it's maintains a separation of concerns, and it makes your code more portable/modular.

You see this approach a lot in web and mobile applications. For example, one software I work on has a PHP backend, JS app front end, and a nodejs application to handle some pub/sub routines. To keep things organized the nodejs is completely separate (and even parts of that are in their on repos). The PHP and JS are together at the moment, but we're working on separating those also to maintain a clear separation. Another advantage this offers us is the ability to deploy individual parts of our app separately without interference to the others.

Another example that comes to mind is mobile apps, especially iOS. In that stack you can use C, Objective C, or Swift in the same project. However in practice most developers prefer to keep them separate and instead create libraries that in other languages at are child projects. They're maintained view dependency managers, and combined at build time.

TLDR: Just keep it organized.

zquintana
  • 254
  • 1
  • 10
  • 1
    I agree with this. I would add that, if you have different areas of a larger project that are so different that you feel like you need different programming languages, then you should probably have a *very* well-defined interface between them. That could be a DLL, an HTTP API, or whatever, but keep some separation. – Andrew May 29 '18 at 19:58
1
  • As top language, GUI and all, you could use java.
  • For mass data processing one could use C.
  • For scripting one could use Python, maybe using the java Scripting API.

That would make sense, and has the responsibilities and areas well-defined. Python on the JVM could of course do a bit more than just scripting.

However it makes developing tortureous slow, because of the switching. Concerning oneself with extending an API on two language sides is awkward. Debugging the more. Best do Test-Driven Development, developing parts in isolation of a unit test.

Best (most fluent) would be to adapt an all-java development to C and Python, replacing parts.

As say java 9 with FX for GUI would already require a steep effort, do not be over-enthusiastic. The more limited the project is, the more one can be creative, concentrating on the business at hand, rather than technical programming features.

Joop Eggen
  • 2,011
  • 12
  • 10