I just want to start off by saying that I do NOT KNOW CLOJURE. I have only experimented a little bit with it and do not have any professional opinions about it. These are my thoughts as a newcomer to the language coming from a Java / lisp background.
A bit about Clojure
Clojure is a Lisp dialect that compiles directly to bytecode for the Java Virtual Machine (JVM) like Java itself does. This gives it a property called Java Interopibility. This is one of Clojure's selling points. Java and Clojure are both completely compatible with one another in a compiled format. This means that you can use Java libraries in your Clojure project and vice versa. Even Swing / Awt graphical user interfaces can be integrated with your clojure project just as they would be in Java.
The Good
To start off Clojure IS a Lisp, and Lisp's syntax is terse and beautiful </opinion>. Clojure has fixed some of Lisp's problems, especially the awful way for handling iteration. In Clojure there is a macro called loop just like in Lisp, but the syntax is easier to follow. Here is an example using loop.
(loop [x 5] (print (concat "looping " (str x) " more times")) (if (= 0 x) nil (recur (- x 1))))
This is obviously written in terrible form since it is pretty procedural, but this is supposed to showcase how loop works. It is a let form that can be returned to with recur. The magic here is that this does not create a stack overhead. No more stack overflows! Of course, infinite recursion can become a problem. But now recursing can be much lighter on the resources and you no longer need to declare a local function to recurse.
The best thing about Clojure is the Java interop, though. Java is one of the most widely used programming languages out there, running on billions of devices and being responsible for Android, desktop applications, and embedded systems in some cases. When you use Clojure you do not lose this massive support, or any Java libraries you have coded up in the past.
Clojure is an open-source project and you can view the source code for all of the base Clojure functions right on the github page.
The not-so-good
Clojure has a confusing means of editing data directly since just about everything but variables (declared with (def)) are immutable; they cannot be changed. This pretty much forces the functional paradigm down the programmer's throat and can actually halt good practice in some cases.
The language is young and not very well known. Its popularity is on the incline into the mainstream, but for now it is still an obscure language. Very few practical programs are written in Clojure and the documentation leaves a bit to be desired on the official Clojure website.
Also due to its lack of popularity as of right now there are few Clojure libraries to play with. However, Java Interop makes this less of a problem than it would with other languages (like Common Lisp, for instance).
The tools used to write Clojure are not as good as Common Lisp's yet, in my opinion at least. Emacs does not have a Clojure-mode, but one can be added through a third-party repository (although I could not get it working for the life of me on Emacs 24). Eclipse also has a plugin called counterclockwise that hooks up to the Lein REPL and provides tools for saving and compiling Clojure.
Clojure Resources
#clojure on Freenode IRC seems like _THE_ meeting place to discuss clojure and ask for help, or just to brag about something you wrote in Clojure. Even Clojure Programming by Emerick Carper & Grand , the de-facto Clojure book for beginners and intermediate Clojure programmers mentions #clojure.
Showing posts with label java programming. Show all posts
Showing posts with label java programming. Show all posts
Sunday, September 9, 2012
Sunday, July 1, 2012
Access UI in Android Application Development
This summer I took a class in Android application development with Java. Coming into the experience I had an extensive knowledge with C++ and the Qt framework as well as Swing and Awt which are Java-specific. Qtcreator, a popular Qt IDE, used to make it very simple to access and change the ui using (ui->objname). With Android no such object was generated with the Eclipse IDE, which is the most popular tool for Android application development. I will assume that you are using Eclipse and ADT since that is the most popular choice for developing apps.
The Example
We have a grid layout with a Button called "Button1" in the middle of it. We are going to change the text from the Activity from which it is made.
The Implementation
Down to the business. We need to first access the button object using something called "findViewById()".
In Eclipse there is a variable named "R" that relates to the generated Dalvik Java. To alter the UI, we need to go through this "R" variable to get the ID of a object. The ID is the name given to the object in the layout xml file.
First off, make a Button that will be assigned to the button in the application. (Button b = new Button()). You need to import android.widget.Button to do this. Then, assign it to the button in the layout like this (inside onCreate is a good place to do this. After setContentView) (b= (Button)findViewById(R.id.Button1). This assigns b to the button with that ID from the XML. It is neandroid.wiget.buttoncessary to cast it to a Button since it is just finding the view by ID. Now you have access to all of the methods for that button. To change the text just use the "setText()" method. (b.setText("Button Text")).
Here is the condensed version of all of this as a note:
The Example
We have a grid layout with a Button called "Button1" in the middle of it. We are going to change the text from the Activity from which it is made.
The Implementation
Down to the business. We need to first access the button object using something called "findViewById()".
In Eclipse there is a variable named "R" that relates to the generated Dalvik Java. To alter the UI, we need to go through this "R" variable to get the ID of a object. The ID is the name given to the object in the layout xml file.
First off, make a Button that will be assigned to the button in the application. (Button b = new Button()). You need to import android.widget.Button to do this. Then, assign it to the button in the layout like this (inside onCreate is a good place to do this. After setContentView) (b= (Button)findViewById(R.id.Button1). This assigns b to the button with that ID from the XML. It is neandroid.wiget.buttoncessary to cast it to a Button since it is just finding the view by ID. Now you have access to all of the methods for that button. To change the text just use the "setText()" method. (b.setText("Button Text")).
Here is the condensed version of all of this as a note:
Sunday, April 8, 2012
Learning C++ after Java - My experience
As I have stated in previous posts and on the other pages of this blog, I attended a course at my community college to learn the Java programming language. Java is object oriented, allowing for inheritance, polymorphism, and encapsulation. However, after a while of programming in Java, I decided that the inefficient way that it executed would be too much of a burden when I started to write larger programs (and I was starting; one reached about 4000 lines). I began to learn another language: python. Python is a scripting language that has some of the same disadvantages of Java, so after I learned some of the key principles, I began to teach myself my first compiled language: C++.
C++ is object oriented as well. In fact, when it was first created it was called "C with classes". So, using online documentation I began to write a few simple programs in C++.
The C++ Hello World
As a reader of this article, you are probably a Java programmer interested in knowing how hard switching to C++ is. Java is designed to be similar to C++, so some may think that it is an easy transition; and it is.
Similarities
The syntax between the two languages is pretty similar. if, switch, int, double, float, short, void, etc. are all the same. This makes porting Java programs into C++ a pretty easy task. Most things that java can do without an import that are not in C++ can also be added with the #include directive. Cstdlib and boost are two other libraries that will provide enough functions to cover pretty much any usage that you will need for common tasks that you may have used in Java.
Differences
In Java, functions (methods) are called in such a fashion as class.func(arg); In C++, functions can be called that way from classes, structures, and unions, but they are commonly called by themselves, like this: func(arg). For functions following this format, it may be difficult to organize functions, which is why C++ offers class support. Also, C++ allows for pass-by-reference with pointers, and the syntax of pointers tend to really confuse newcomers (it confused be a lot. The video below really helps with them.
Also, Java includes its own graphical toolkit. In C++ a third-party library like GTK+ or Qt must be used.
It is also my opinion that no C++ IDE compares to the usefulness of a Java IDE. Eclipse and Netbeans, for example, are huge projects that make development a fun and easy task. With C++ I use Code::Blocks but many stick to vim and their compiler.
My personal experience
At first, it seemed like smooth sailing. However, when I started to do some of the more complicated things (changing parameter values in functions, for instance) I ran into some issues. Pointers were a really hard thing for me to grasp, and I could not see why they were needed. Aside from pointers and the god awful mess that seemed to be the C++ library, it has been a surprisingly easy transition from Java to C++ and anybody looking to attempt it should have no trouble. To travel from C++ to Java, you will probably have no issues whatsoever, other than finding the equivalents to functions in the C++ library.
C++ is object oriented as well. In fact, when it was first created it was called "C with classes". So, using online documentation I began to write a few simple programs in C++.
The C++ Hello World
#include <iostream>
using namespace std;
int main(){
cout << "Hello, world!"<<endl;
return 0;
}As a reader of this article, you are probably a Java programmer interested in knowing how hard switching to C++ is. Java is designed to be similar to C++, so some may think that it is an easy transition; and it is.
Similarities
The syntax between the two languages is pretty similar. if, switch, int, double, float, short, void, etc. are all the same. This makes porting Java programs into C++ a pretty easy task. Most things that java can do without an import that are not in C++ can also be added with the #include directive. Cstdlib and boost are two other libraries that will provide enough functions to cover pretty much any usage that you will need for common tasks that you may have used in Java.
Differences
In Java, functions (methods) are called in such a fashion as class.func(arg); In C++, functions can be called that way from classes, structures, and unions, but they are commonly called by themselves, like this: func(arg). For functions following this format, it may be difficult to organize functions, which is why C++ offers class support. Also, C++ allows for pass-by-reference with pointers, and the syntax of pointers tend to really confuse newcomers (it confused be a lot. The video below really helps with them.
Also, Java includes its own graphical toolkit. In C++ a third-party library like GTK+ or Qt must be used.
It is also my opinion that no C++ IDE compares to the usefulness of a Java IDE. Eclipse and Netbeans, for example, are huge projects that make development a fun and easy task. With C++ I use Code::Blocks but many stick to vim and their compiler.
My personal experience
At first, it seemed like smooth sailing. However, when I started to do some of the more complicated things (changing parameter values in functions, for instance) I ran into some issues. Pointers were a really hard thing for me to grasp, and I could not see why they were needed. Aside from pointers and the god awful mess that seemed to be the C++ library, it has been a surprisingly easy transition from Java to C++ and anybody looking to attempt it should have no trouble. To travel from C++ to Java, you will probably have no issues whatsoever, other than finding the equivalents to functions in the C++ library.
Saturday, February 25, 2012
Explaining Obfuscation
Obfuscation is the act of making computer programs more efficient by running through less processes. In this article I will explain how to obfuscate your computer programs. My code snippets are written in the Java programming language, but a lot of the functions are language agnostic.
1. Avoid variables
Variables are used to store information in RAM. RAM is very limited in how much it can hold, so making a lot of variables to store information is a bad idea. Below are two examples of avoiding variables.
boolean stopLooping=false;
bad
Your Computer Science professor will tell you that loops are controlled by the state of a boolean, so you might be tempted to do something like the above example. Instead, do something like this:
good
The above example still uses a boolean, but doesn't waste a variable on it. You could also use and if statement with a break, but since for contains functionality to do so it would waste processor resources.
2. Use centralized variables
Imagine that you are looking at somebody's code and then have 5 variables.
int x, y, a, b, c;
now, you come across this:
bad
You will be deciphering this for a while, so you should use a centralized variable. A centralized variables provides one place to store information, and then it is overwritten after it has been used.
good
In the above example, I could have just used System.out.println() with the number I wanted to display, but the point remains. It's better than initializing a bunch of variables to hold the information.
3) if-then-else cleanup
The last thing I will talk about is the reservation of using if-then statements. Let's say that you want to return true if x==4, or else return false.
bad
This is actually a very common approach, but here is how it should be done:
good
return x==4;
This approach overall shortens up the code and makes it ever so slightly more efficient. You could also use a conditional operator, but this would be a much better way of doing it.
1. Avoid variables
Variables are used to store information in RAM. RAM is very limited in how much it can hold, so making a lot of variables to store information is a bad idea. Below are two examples of avoiding variables.
boolean stopLooping=false;
bad
for(index=0; stopLooping==true; index++)
{
System.out.println(index);
if(index==5)
stopLooping=true;
}Your Computer Science professor will tell you that loops are controlled by the state of a boolean, so you might be tempted to do something like the above example. Instead, do something like this:
good
for(int index=0; index<5; index++)
System.out.println(index);The above example still uses a boolean, but doesn't waste a variable on it. You could also use and if statement with a break, but since for contains functionality to do so it would waste processor resources.
2. Use centralized variables
Imagine that you are looking at somebody's code and then have 5 variables.
int x, y, a, b, c;
now, you come across this:
bad
x=4;
System.out.println(x);
y=4;
System.out.println(y);
a=5;
System.out,println(a);
b=9 ;
System.out.println(b);
c=2;
System.out.println(c);You will be deciphering this for a while, so you should use a centralized variable. A centralized variables provides one place to store information, and then it is overwritten after it has been used.
good
int temp;
temp=4;
System.out.println(temp);
System.out.println(temp);
temp=5;
System.out.println(5);
temp=9;
System.out.println(9);
temp=2;
System.out.println(temp);In the above example, I could have just used System.out.println() with the number I wanted to display, but the point remains. It's better than initializing a bunch of variables to hold the information.
3) if-then-else cleanup
The last thing I will talk about is the reservation of using if-then statements. Let's say that you want to return true if x==4, or else return false.
bad
if(x==4)
return true;
else
return false;This is actually a very common approach, but here is how it should be done:
good
return x==4;
This approach overall shortens up the code and makes it ever so slightly more efficient. You could also use a conditional operator, but this would be a much better way of doing it.
Subscribe to:
Posts (Atom)