What are the best practices for the Middleware API? [closed] - java

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 5 years ago.
Improve this question
We are developing a middleware SDK, both in C++ and Java to be used as a library/DLL by, for example, game developers, animation software developers, Avatar developers to enhance their products.
What I would like to know is this: Are there standard "Best Practices" for the development of these types of API?
I am thinking in terms of usability, readability, efficiency etc.

My two favourite resources on the subject: http://mollyrocket.com/873 and http://video.google.com/videoplay?docid=-3733345136856180693

From using third party libraries on Windows I've learned the following two things:
Try to distribute your library as a DLL rather than a static library. This gives way better compatibility between different c compilers and linkers. Another problem with static libraries in visual c++ is that the choice of runtime library can make libraries incompatible with code using a different runtime library and you may end up needing to distribute one version of the library for each runtime library.
Avoid c++ if possible. The c++ name mangling differs alot between different compilers and it's unlikely that a library built for visual c++ will be possible to link from another build environment in windows. When it comes to C, things are much better, in particular if you use dll's.
If you really want to get the good parts of c++ (such as resource management through constructors and destructors), build a convenience layer in c++ that you distribute as source code that hides away your c functions. Since the user has the source and compiles it locally, it won't have any name mangiling or abi issues with the local environment.
Without knowing too much about calling c/c++ code from Java, I expect it to be way easier to work with c code than c++ code because of the name mangling issues.
The book "Imperfect C++" has some discussion on library compatibility that I found very helpful.

The video from Josh Bloch mentioned by yrp is a classic - I second that recommendation.
Some general guidelines:
DO define your API primarily in terms of interfaces, factories, and builders.
DO clearly specify exactly which packages and classes are part of the API.
DO provide a jar specifically used for compiling against the API.
DO NOT rely heavily on inheritance or the template method pattern - over time this becomes fragile and broken.
DO NOT use the singleton pattern or at least use it with extreme caution.
DO create package and class level javadoc explaining usage and concepts.

Take a look at Framework Design Guidelines. I know it is .NET specific, but you can probably learn a lot of general information from it too.

There are lots of ways to design apis, depending on what you are solving. I think a full answer to this question would be worthy off a whole book, such as the gang of four patterns book. For Java specifically, and also just OO programming in general, I would recommend Effective Java 2nd Edition. The first is general and a lot of popular programming patterns, when they apply and their benefits. Effective Java is Java centered, but parts of it is general enough to apply to any programming language.

Related

Statically-typed cross-OS Web platforms: Java, Mono, and what else? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 7 years ago.
Improve this question
I appreciate this question may get closed due to its open-ended nature, but I'm curious none-the-less.
If I wanted to develop a cross-platform Web application using a statically-typed language, what would my choices be? I'm aware of Mono and Java, but what else is there?
(When I say cross-platform, I mean Windows and at least one common flavour of Linux)
You can use single language for the both client and server parts:
Java
C#
Google's Go
Scala
GWT(Java framework)
Haskell web frameworks
There are several high-performance Haskell web frameworks that emphasise strong static correctness:
snap
yesod
happstack
The goal is to lean on the type system to provide highly optimzied code, and zero chance of runtime failure.
To quote the Yesod web site:
Turn runtime bugs into compile-time errors
Yesod believes in the
philosophy of making the compiler your ally, not your enemy. We use
the type system to enforce as much as possible, from generating proper
links, to avoiding XSS attacks, to dealing with character encoding
issues. In general, if your code compiles, it works. And instead of
declaring types everywhere you let the compiler figure them out for
you with type inference.
If you mean statically typed, there isn't that much choice unfortunately. Google's Go language seems to begin growing into the web application space (there is Go support for Google's App Engine and projects such as GoWeb), but these efforts are probably not very mature yet.
Scala might be another possible option. While it also runs on the JVM, its web frameworks are very different due to the language's advanced features (see e.g. Lift) and might be worth a separate look.
In addition to Java and C# (via mono), you could also use (modern) C++ in the form of the
Wt (C++ Web Toolkit).
Now the C backwards-compatibility part of C++ is not strongly typed, but otherwise C++ programs are type safe.
If you want really strong type safety, you can use Haskell with Yesod. The goal of Yesod is to use strong types to prevent common errors in programming web applications. URLs, for instance, are type checked. You cannot construct an intra-application link without supplying the correct parameters. Escaping of user-content when rendering the web page is another situation where the Haskell compiler is used to ensure that strings are properly escaped before being included in a rendered page.
I believe Ruby is stongly typed, so Ruby on Rails be a good choice for this.

Is there a more modern, OO version of "Let's Build a Compiler"? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 4 years ago.
Improve this question
Is there a more modern, maybe object-oriented, equivalent to Jack Crenshaw's "Let's Build a Compiler" series?
A while back I stumbled across "Let's Build a Compiler" and could just not resist writing some code. I wrote a recursive-descent C compiler in C# that output .NET CIL. "Write once, leak everywhere" was my slogan.
Too bad I did not realize until too late that parsing C is a nightmare.
I am now interested in writing a Java compiler in Java that outputs .NET CIL or assemblies with the goal of being self-bootstrapping. I was hoping there might some newer tutorials kicking around.
As an aside, would you spend more time with up-front design or would you just write a ton of tests to support the ability to mercilessly refactor. Thinking back, I am leaning towards the latter. The compiler worked but the code was really awful.
It sounds like you completely missed the point of Crenshaw's tutorials. LBC isn't about writing pretty, clean, or efficient code. It's all about bringing something that's steeped in formal theory down to a level where the casual coder can easily and rapidly hack out a rudimentary (but working!) compiler.
When I read through LBC years back, I rewrote the examples in C#. I'm sure the class layout isn't the best, or tasks segregated properly, but it's comparable to his Pascal. I'd be happy to share the code with you if you like-- let me know and I can post it online and share the link.
In my spare time I've been hacking out some writing with the aim of unifying the philosophies of LBC and Basics of Compiler Design together-- walkling away with practical, working code at the end of each unit/chapter, with also discuss some theoretical stuff after exploring the ideas so the reader understands why things are the way they are. But it took Crenshaw years to write his incomplete series, so mine my be a pipe dream... and I use C (exactly because it's not C++ or Java).
Take a look at Terence Parr's "Language Implementation Patterns". He wrote ANTLR - a parser generator for Java - so knows his stuff. It explains the principles of compiler design really well and builds up gradually.
Martin Fowler's "Domain Specific Languages" is also good. It has a slightly different agenda than being a pure compilers course, but is a good reference on the key concepts of language design.
I'm a fan of "MiniJava" and associated work based on the "Modern Compiler Implementation in Java" family of books. This doesn't quite meet all the requirements you mention as a MiniJava implementation will, generally, generate native code - but the backend can easily be changed to emit MSIL or whatever.
I have recently built a compiler at my company using BNFC, at first I was instructed to use Flex and Bison (C/C++) but I found them to be a pain so I used BNFC to generate the Flex and Bison files.
Can't say I liked the code, my grammar was pretty big and so was the generated visitor but nothing I couldn't handle, I TDDed from the beginning so I always had enough tests to refactor and but I also kept a UML diagram to help me think about the additional classes I wrote.
There actually is a book called Implementing Programming Languages self described as "a self-study book, and to some extent, a manual to the BNFC tool" had I read it I would probably have struggled less with implementation decisions but overall I found BNFC to be intuitive enough to be able to use it by only reading the manual and the tutorial
Last but not least, it can also be used with other languages including Java (with Cup and JLex)
Have you taken a look at the PyPy project? It is a Python implementation of the Python language. Maybe it can provide some inspiration for your goal of self-bootstrapping Java?
What about Writing Compilers and Interpreters: A Software Engineering Approach by Ronald Mak?
When thinking of learning this stuff, you should have a look at book language-implementation-patterns and antlr-reference
If you like to learn by example, the code for Finch, a little programming language of mine:
Is written in object-oriented C++.
Is very clean.
Includes a bytecode compiler.
How about Watt & Brown's Programming Language Processors in Java. It demonstrates what OO patterns to use in (simple) compiler design. I used it with C# successfully.

Why should a Java developer learn an additional JVM language? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 3 years ago.
Improve this question
I am a java developer and I want to know,
what is the main benefit from learning a language such as Scala or Groovy?
You can get the same benefit from learning another JVM language as learning any new language. It increases your understanding of programming in general and more importantly, it adds another tool to your toolbox.
So the next time you have to solve a problem, you may reach for a nail gun instead of a hammer.
To be more specific, Groovy is a good language for mocking up code quickly, and Scala, while I've never used it, is suppose to great for writing concurrent applications due to it's functional approach. As others have mentioned, the JVM languages can interact with Java code. Which can be useful for adding onto legacy systems or for mocking up pieces of a application quickly.
Some good reasons that come to mind:
They have features that Java doesn't have and that you may find useful in certain circumstances.
They use different programming paradigms, different way of thinking.
Learning other languages opens your mind.
Learning languages makes you conscious about their respective strengths and weaknesses.
They are getting more and more attention and better JVM support (with Java 7).
Actually, maybe ask yourself the reverse question:
Why not learn an addiitional JVM language?
The other answers here have very good points, but there's one thing I'm missing.
A good coder rarely identifies himself as a 'Java developer', a 'Python developer' or any ' developer'. Learning another language (be it a JVM language or not) will make you understand there's a lot more in the world to learn.
If you're satisfied with only one language, it usually means you're oblivious to the problems it has, and that there are many tasks that are better suited for other languages.
This is why the Pragmatic Programmers encourage every programmer to learn a language a year.
The languages you've mentioned practice a different programming paradigm that could help you be more productive. They are also more fun to work with.
Languages such as Scala and Clojure run on the JVM and exhibit great performance in multi-core systems without imposing synchronization requirements.
And of course you are still able to use the full wealth of libraries that are available for Java.
Because then you'll know a new language, which means a broader skill set and another way of looking at problems. But because Groovy and Scala run on the JVM and you know Java, you can integrate existing libraries and code if you want or need to.
from http://groovy.codehaus.org/:
"Groovy is like a super version of Java. It can leverage Java's enterprise capabilities but also has cool productivity features like closures, builders and dynamic typing. If you are a developer, tester or script guru, you have to love Groovy."
So in many cases it does make sense to use Groovy over Java; For instance in Java unit tests!
Scripting language advantages inside jvm. Seamless interaction with compiled java code.
I know with Groovy, you can load scripts (from files) at runtime from your Java application. This allows me to customize the behavior of application actions at a client site without requiring me to recompile code. It's rather lovely.
Sorry more questions then answers..
What would you spend your time on if you are not learning a new language?
Why are you limiting yourself to JVM languages?
Would it be of more benefit to learn test driven development?
What about learning standard design patents?
What are you trying to achieve with your investment of time?
Learning a language is always good, however if you don’t learn them very well and use them you don’t get a great benefit, as you need to be able to “think in a language” so as to broaden your mind.
Using more than one language in a company leads to lots of additional long term costs so you may gain more by learning how to programme better in your main language, only you can decide.

Is there a way to transfer/translate the code written in Java to other languages? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 7 years ago.
Improve this question
Theoretically this seems possible to me. So can Any one confirm this to me, if it's possible? and if there is such a software that does this?(like Java to C++ or C#)
And in general would it be possible to transfer languages like Java to server-side programing language like PHP?
Translating the syntactical elements of one language and producing another is not trivial but it's not impossible. A good parser can build syntax trees in one language and then emit another. The difficulty of porting code outside the context of simple "Hello World" type applications is twofold:
The libraries of one language will probably differ (e.g. WinForms vs Swing)
Some language features will have to be catered for: (lambda expressions, anonymous methods, different inheritance implementations etc).
It is possible, but the major problem is that Java has a very large runtime library which needs to be made available in the target language in order to be able to do a fully automatic conversion.
For the special case of Java -> .NET, you can use J# from Microsoft to compile it into a .NET assembly which can then be used. Also ikvm.net allows for running a JVM inside .NET.
For PHP I do not believe such a solution exist. You MAY be able to use gcj to create a native library which can be linked in, but I do not believe it is a feasible soultion.
What functionality do you need in PHP?
Visual Studio ships with a Java to C# translator, and even tough it does a pretty decent job, there's still a lot to clean up afterwards.
In my experience you really have to ask yourself if it makes sense to translate code from one language to another. What is the gain? Will the translated code be maintainable? If the answers to these questions point in the wrong direction, translating is probably not the right approach.
Google Web Toolkit does conversion from Java to JavaScript:
http://code.google.com/webtoolkit/overview.html
to answer your question, yup, theoretically this is indeed possible and practically such technology is used every day :)
The interesting thing, in my opinion, is that the Java converters typically convert by taking the bytecode, not the source code. Then it's, say, bytecode-to-ObjectiveC source code. For some converters (at least one opensource one) it's bytecode-to-XML then XML-to-target-language.
For example, the Uniwar application for the iPhone, which has been acclaimed by all and made its way to the appStore's top ten, as been written in Java (JME) and automatically converted from the Java bytecode. Reaching the top ten, even for a few days, means that this is deployed on a lot of machines ;)
In the Real-World [TM], Cobol-Java and, weirdly, Java-Cobol are not unheard of.
For all this to work that said you need a really good converter :)
Theoretically it is possible. But as others pointed out the main problem is to translate libraries.
Some time ago I made Java to Tcl(XOTcl) and Java to Python translators to evaluate the translation posibility. Search by java2tcl and yava2python.
They convert syntax but do not make relevant constructions translations (e.g. Java file operations to Python ones). That would require more development time.
In general my opinion is what such a translation may be possible. But only if your translator covers classes/libraries of the converted project.

C++ library with a Java-like API [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 5 years ago.
Improve this question
Hoping that anybody here knows about a good one: I'm looking for a (free to use) C++ library with a class hierarchy and methods resembling the Java API, with at least the I/O & networking part if it, specifically HTTP handling.
I work mainly with C & Java, but for this particular project C++ is recommended, so I thought of adopting a good set of C++ libraries without facing a steep learning curve.
Thanks in advance for any recommendation.
Qt is IMHO very java like. I.e. they prefer Java-Style Iterators over the STL ones. Qt includes networking (examples) and much other stuff (like scripting via javascript)
Have you looked at the Boost libraries?
Boost.IOStreams provides a framework for defining streams, stream buffers and i/o filters.
Asio - Portable networking, including sockets, timers, hostname resolution and socket iostreams.
Many others....
The Boost libraries provide similar capabilities as compared to the Java API, but they very much 'look and feel' - appropriately - like a C++ library.
There is also the option of using something like POCO, which is slightly simpler than using something like Boost, while still being cross platform.
While the only time I used HTTP in Java was a long time ago, the interface for the POCO library looks fairly simple to use. It gives a example of basic FTP usage a something like this:
Poco::Net::FTPStreamFactory::registerFactory();
std::ofstream localFile(inputFile, std::ios_base::out | std::ios_base::binary);
Poco::URI uri(inputURL);
std::auto_ptr<std::istream> ptrFtpStream(Poco::Net::URIStreamOpener::defaultOpener().open(uri));
Poco::StreamCopier::copyStream(*ptrFtpStream.get(), localFile);
A C++ library that looked like a Java one would be a bad library, IMHO. The two languages are so very different that what is good design for one will almost inevitably be bad design for the other.
You can take a look at Mindroid, which is primarily oriented to embeddded programming:
Mindroid is an application framework (with focus on messaging and concurrency) that lets you create applications using a set of reusable components - just like Android. The name Mindroid has two different meanings. On one hand Mindroid is a minimal set of core Android classes and on the other hand these classes also form Android's mind (at least in my opinion).

Categories

Resources