Learn functional programming the easy way

If you’re getting into functional programming, trust me on this. As your very first step, buy and read this book:

https://mitpress.mit.edu/books/little-schemer

It starts with “is this an atom”, but by the time you’re halfway through, you’ve been through a bunch of different types of recursion, and you understand every step along the way, because they don’t skip any steps.

If you’ve never done it, functional programming is strange, and this book will walk you through the strange parts until you’re comfortable enough to stand on your own.

Advertisements

Decompiler for Eclipse

(Please note that I am not involved in any way with the development of this decompiler. I’m just a satisfied customer)

The other day I was working on porting some legacy code to a new app server. One of the legacy classes was doing something odd, so I needed to look at the source code. Unfortunately, I only had the .class file. Eclipse will display the bytecode in a reasonably user-friendly format, but it’s kind of a pain to read unless you spend a lot of time reading disassembled bytecode (I don’t).

So, I searched the Eclipse marketplace for a Java decompiler, and found this:

https://marketplace.eclipse.org/content/eclipse-class-decompiler

I installed it, and it exceeds all my expectations. It works seamlessly with Eclipse; instead of displaying bytecode, Eclipse decompiles the .class back to Java and displays it. The only things missing are the comments.

I don’t use a decompiler very often, but when I need it, it’s typically in this kind of situation; I’ve inherited an oddball piece of legacy code, it’s behaving oddly, and I don’t have the source code. In this circumstance, a decompiler can make the difference between success and failure. It’s a very handy tool to have in the toolbox

By Request: repost of Native Method example using JNI

Native Method example using JNI

Please note: this is a straight repost of an article I wrote about 10 years ago. I haven’t done anything with JNI lately, and can’t guarantee that it’s still correct, but I’d be surprised if things have changed much.

  1. Declare your native methods in an otherwise normal Java class.
    public class nativetest
    {
    public native String sayHello(String s);
    public static void main(String[] argv)
    {
    String retval = null;
    nativetest nt = new nativetest();
    retval = nt.sayHello(“Beavis”);
    System.out.println(“Invocation returned ” + retval);
    }
    }
  2. javac nativetest.java
    Standard Java compile
  3. javah -jni nativetest
    Generates .h files. Here is the code

    /* DO NOT EDIT THIS FILE - it is machine generated */
    #include 
    /* Header for class nativetest */
    
    #ifndef _Included_nativetest
    #define _Included_nativetest
    #ifdef __cplusplus
    extern "C" {
    #endif
    /*
     * Class:     nativetest
     * Method:    sayHello
     * Signature: (Ljava/lang/String;)Ljava/lang/String;
     */
    JNIEXPORT jstring JNICALL Java_nativetest_sayHello
      (JNIEnv *, jobject, jstring);
    
    #ifdef __cplusplus
    }
    #endif
    #endif
  4. Write your native code, using the .h file generated above Copy the function prototype from the .h file and paste it in.
    #include "nativetest.h"	/*double quotes tells it to search current directory*/
    
    JNIEXPORT jstring JNICALL Java_nativetest_sayHello
      (JNIEnv *env, jobject thisobject, jstring js)
    
    {
    	return js;
    }
  5. Compile
    cl -c /Ic:\jdk1.1.6\include /Ic:\jdk1.1.6\include\win32 nativetest.c 
    link /libpath=c:\jdk1.1.6\lib nativetest.obj /dll
    

    The above uses Microsoft Visual C++ command-line tools.

  6. Modify your java class to load the library.
    1. public class nativetest
      {
      	static {
      		System.loadLibrary("nativetest");
      	}
      	public native String sayHello(String s);
      	public static void main(String[] argv)
      	{
      		String retval = null;
      		nativetest nt = new nativetest();
      		retval = nt.sayHello("Beavis");
      		System.out.println("Invocation returned " + retval);
      	}
      }
      
  7.  run the example
    1. C:\jni\hello>java nativetest
      Invocation returned Beavis
      

RIP http://patriot.net/~tvalesky

My old website, http://patriot.net/~tvalesky is going offline soon, as the ISP is going out of business at the end of the calendar year. I first created it after I finished grad school in 1997, and it’s been up ever since.

It’s been a while since I updated it regularly, but I had a lot of simple “hello world” type examples for Java, as well as the “Free Corba Page”.

If there’s anything that you’re looking for, let me know and I’ll repost it here.

And pour one out for good old http://patriot.net/~tvalesky

My crystal ball’s broken — the lack of a component marketplace in Java

Tags

, ,

A hundred years or so ago, I used to write a column about JavaBeans. Later on, I wrote a book about Enterprise JavaBeans. Back then, I was still doing a lot of work in the Windows world. In that world, there was a strong marketplace for third-party components in the form of VBXs and, later, OCXs.

If you’re not from the Windows world, these were binary compiled objects that you could treat as components in your application. They were in a standard format understood by the development environments, so you could drag-and-drop them onto your user interface, manipulate their properties in the development environment, etc. This was the usual way of making use of third-party code in the late 90s and early 00s. It worked very well, chiefly because you didn’t need to understand the internals of the objects at all. You simply interacted with the interface, got the behavior you needed, and got on with development.

So, when I first encountered JavaBeans, I figured that this model of reuse would become common in Java. Further, when I encountered Enterprise JavaBeans, I figured that we would see this sort of reuse at an enterprise level — that is, you’d buy a library of enterprise widgets for, say, case management, drag them around in a visual app builder, and code enough to glue them together.

None of this happened. Well, I’m sure there were some products created, but then never really caught on, and Java code reuse seems to focus on libraries and frameworks instead of components. Why is this? Who knows, but I can make some guesses:

1) A lot of the early-2000s Java developers started working in Java as their first language. They hadn’t worked with components before, so they didn’t see a compelling need for them.

2) Economics. Open source became a huge factor in the Java world in the early 2000s, as people became disenchanted with Java Enterprise Edition and went off to invent their own enterprise frameworks. The net result was that the community became used to using free open source frameworks to build apps. In that environment, a company wishing to make money selling a widget library had to convince developers that it was worth paying for a library when one was available for free.

3) Drag-and-drop visual development never caught on in Java. We still build our user interfaces in code. Components become more obviously useful in a visual development environment. (There were some innovative visual development environments early in Java, but they’ve mostly fallen by the wayside).

4) On the positive side, the core Java libraries deliver a pretty solid set of functionality. This means that there are fewer gaps that can be filled by third-party developers.

5) The “patterns” movement also became a huge influence in Java in the early 2000s. Library developers focused on implementing GoF patterns.

6) It’s a bit more work to produce a JavaBean, rather than a Plain Old Java Object. Not necessarily a lot more work, but some, and you have to follow the same conventions across all the components you wish to treat as Beans.

Those are just some educated guesses. But for whatever reason, components didn’t catch on in the Java software-provider ecosystem, so they’re not typically used as core elements of development. I think this is a missed opportunity.

Advice to beginning programmers: learn to design first

Tags

, ,

During high school and college, I took a number of programming courses. I did OK, but never really felt like a “programmer.” That is, I could implement the assignments using the tools provided, but beyond that I was fairly clueless.

When I decided to take up programming for a living, I settled on C as my language of choice. It was in high demand at the time, and I really loved the terseness, speed, and lack of abstraction layers between me and the hardware. (Or rather, I really hated Pascal’s verbosity, so typing ‘{‘ instead of BEGIN really spoke to me).

At any rate, I decided to take a course in C at the local community college in order to get moving in my new career path. When I attempted to sign up, I found that they had a 100-level course in structured program design as a prerequisite. They were going to make me spend a semester drawing flowcharts with a template! I was not pleased, but rules are rules, so I took the course.

That course was the most important course I’ve ever taken.

Before that course, I’d never really been exposed to the idea of “program design.” You know how it goes — you get assigned to write a program to do, say, matrix multiplication. You write a little 100-line program, get it working, and turn it in. The end.

Well, that’s not how it works in the Real World. In the Real World, you’re working on systems with perhaps millions of lines of code, with a team of perhaps tens of programmers, over the course of many years. If I’d have tried to pursue Real Programming with my initial naive approach, I’d probably have walked away from the field in frustration.

Having a formal course in design as a starting point was critical for my growth as a programmer. It introduced me to Programming in the Large — thinking of the big picture, before getting into the nitty-gritty of banging out code. It introduced me to the idea of thinking of program design independent from the implementation language. Most critically, it showed me that, if I run into a problem that I don’t currently have an answer to, I can simply create a subroutine to perform the operation, defer the implementation to a future time when I’ve figured it out, and press on with a solution.

The question “What language should a beginner start with?” is a common one, and everyone has their own arguments and pet languages. But I’d suggest that the best language for a beginner is no language — rather, they should take a 100-level course in program design. Once you know how to design solutions, the choice of language is a lot less critical. Since those days, I’ve made money programming in C, C++, Visual Basic, C#, Java, JavaScript, umpteen dialects of SQL, various Pascal dialects, Perl, REXX, etc, etc. But the principles I learned in the 100-level design class have remained just as useful as the day I learned them.

If you don’t have access to a basic design course, here’s a course description of the current version of the course I took lo these many years ago:

http://www.nvcc.edu/academic/coursecont/summaries/ITP100.pdf

 

SQL Protip: indexes work

Tags

, ,

A reporting system I’m working on had a notably slow-running query. Run time was on order of 42 seconds. I applied indexes to the tables, and run time dropped to on order of 1 second. That’s a 4200% improvement, with no changes to logic required.

Indexes are trivially simple to implement in most DBMSs, and give a huge performance boost if used correctly.

There are 2 types of indexes: clustered and non-clustered. Clustered indexes physically reorder the rows so that the indexed columns are in order. This makes them especially helpful when you select ranges of values from a table. Non-clustered indexes simply create a lookup table of indexed values to physical locations.

Indexes can slow down inserts, so if you have a huge volume of transactions going into a table, you may want to leave it unindexed.

Indexes also degrade over time, so you should periodically update them. This is typically done by the database administrator, but on small projects there may not be a dedicated database administrator. It’s generally a straightforward operation. Just script it and run it once a day, once a week, etc, depending on need.

Wolfram — cautiously optimistic

I just finished watching the new promo video for the Wolfram language. 

I’ll reserve judgment on the language itself until I’ve had the opportunity to build something in it, but I’m enthusiastically in favor of the scope and approach of the language. I’m very tired of “hot new languages” that are just another 3gl with a bit of syntactic sugar applied. We don’t need that. We need BIG differences. We need, at minimum, a much higher level of abstraction. I talk to my customers, and they talk about “cases” and “reports”; I don’t want to have to translate that into “radio buttons” and “vectors of type String”. I want to operate at my customer’s level of abstraction, so that I can deliver solutions to them faster, there are fewer misunderstood requirements translations (because there are fewer translations required) etc. Today’s development tools are gigantic, taking gigabytes and gigabytes of disk space and megabytes and megabytes of runtime memory, and yet we’re still twiddling around with the same abstractions that we were back in 1990. It’s time to raise the game.

How to create an MSI installer in Visual Studio 2010

Q) How do I create an MSI package from Visual Studio 2010?
A) It’s a multi-step process.

1) Start with your existing solution and application.

2) In “solution explorer,” right-click the overall solution, and do  “add”->”new project”
 For project type, select “other project types”->”Setup and deployment”->”Visual Studio Installer”
Then click “setup project”
This should take you to the “installer” view
Under the app properties, set the following:
– removePreviousVersion: true
– author, manufacturer as appropriate

3) Right-click the “application” folder, and click “add”->”project output”
– for “project,” select your project name
– select “primary output” (should already be selected).

4) on “User’s Programs Menu, right-click and do “add”->”folder”
– go back to the “application folder”
– right click the “primary output..” and select “create shortcut to … from …”
– drag the shortcut, and drop it into the menu folder created in step 3

5) In the solution explorer, right-click the setup project and do “build”
6) In the “Debug” folder of your setup project, you should find an MSI,
along with an installer .exe