Archive for the 'programming' Category
Smalltalk passion
Sunday, May 13th, 2007I’m very rational programmer, with a academic bias. I know C, C++, Java, Perl, PHP, VBScript, Object Pascal and Prolog languages. But my language of choice is beyond the rational. Smalltalk.
Smalltalk is much more than a language, is a concept, a vision. Much of what we use today was inspired in the Smalltalk ideas and environment. Steve Jobs only saw the mouse and the window system, if he saw the Smalltalk language too, our lifes would be better now.
“But if Smalltalk is so good, why isn’t a mainstream language?” you can ask me. I guess it was a matter of investments, a big company behind, the resistance against the paradigm shift… Java is the main object oriented language today, and cleverly the language designer choose a syntax similar with C++. Smalltalk always was and is today a big lab beyond the common place.
If you don’t know Smalltalk, I invite you to take some time to know a free implementation, Squeak. I will try to present some features here, soon.
Software Architecture Pearls
Friday, February 2nd, 2007I found some software architecture pearls while reading the Art of Unix Programming by Eric Steven Raymond.
Although it was writed to the Unix community, these simple “rules” are so universal that I put here to always remember and apply in my projects. Here they are:
- Rule of Modularity: Write simple parts connected by clean interfaces.
- Rule of Clarity: Clarity is better than cleverness.
- Rule of Composition: Design programs to be connected to other programs.
- Rule of Separation: Separate policy from mechanism; separate interfaces from engines.
- Rule of Simplicity: Design for simplicity; add complexity only where you must.
- Rule of Parsimony: Write a big program only when it is clear by demonstration that nothing else will do.
- Rule of Transparency: Design for visibility to make inspection and debugging easier.
- Rule of Robustness: Robustness is the child of transparency and simplicity.
- Rule of Representation: Fold knowledge into data so program logic can be stupid and robust.
- Rule of Least Surprise: In interface design, always do the least surprising thing.
- Rule of Silence: When a program has nothing surprising to say, it should say nothing.
- Rule of Repair: When you must fail, fail noisily and as soon as possible.
- Rule of Economy: Programmer time is expensive; conserve it in preference to machine time.
- Rule of Generation: Avoid hand-hacking; write programs to write programs when you can.
- Rule of Optimization: Prototype before polishing. Get it working before you optimize it.
- Rule of Diversity: Distrust all claims for “one true way”.
- Rule of Extensibility: Design for the future, because it will be here sooner than you think.
My “Wow!” moment learning Ruby
Friday, January 12th, 2007I´m a absolutely beginner in Ruby, but recently, after I changed to my new web host, I was tempted to make a try.
My background is mainly in Java language, with some SmallTalk just because since I left my graduation, I never saw a so beatifull syntax. And until now, I write sometimes somethings in SmallTalk, when I need to think about the problem in hand without worry about the environment or language little tricks and details. After all, I think compiled and typed languages are doomed to become history.
My new web host gave me a lot of space and bandwidth, but only 2 practical choices of programming languages for dynamic web sites: PHP or Ruby. For personal reasons I hate PHP (please, I don´t want to start a flame war about what is the best language, this is just my personal opinion). My first job was about programming in PHP, and I don´t have good memories…
I´m studying right now, and for the people which come here looking for Java tips or tricks, I want to say I had a ‘Wow!’ moment right now. Look for this little piece of code:
1.class Product < ActiveRecord::Base 2. 3. validates_presence_of :title, :description, :image_url 4. validates_numericality_of :price, :only_integer => true 5. 6. protected 7. def validate 8. errors.add(:price, “should be positive”) if price.nil? || price <= 0 9. end 10.end
Don´t worry about what this code do. But the syntax is very cool, isn´t it? For me, some constructions doesn´t seem so strange, because they look like Smalltalk, but for a JavaMan… You can see the ‘if’ block? Or the method call in a null reference?
I see you in my next “Wow!” moment…
A SCJP question about Java enumerations
Thursday, January 11th, 2007Given the following:
1.public enum Wallpaper {
2. BROWN, BLUE, YELLOW;
3.}
Which of the following are legal?
-
enum PatternedWallpaper extends Wallpaper { STRIPES, DOTS, PLAIN; } -
Wallpaper wp = Wallpaper.BLUE;
-
Wallpaper wp = new Wallpaper(Wallpaper.BLUE);
-
void aMethod(Wallpaper wp) { System.out.println(wp); } -
int hcode = Wallpaper.BLUE.hashCode();
Answer: only items 2, 4 and 6 are corrects. We can´t extend or instantiate an enumeration. The item 2 is the correct way to get a enumeration reference, the item 4 shows a method receiving a enumeration reference, and finally item 6 shows a call for the hashCode() method that all enums inherit from Object.
Approximate strings joins in a database - Part 1
Wednesday, January 10th, 2007Strings are ubiquitous and ambiguous. When a communication channel is established between two people, inevitable noise and misunderstanding can introduce many errors when transferring textual data.
In any enterprise system, there are many places where this type of error can occur. Client names, addresses, company names, etc. This kind of error can become impracticable the exact match against a query for a registry.
Think about the poor life of the mega-action star Arnold Schwarzenegger, how many times he needs to repeat his name to the operator?
To solve this type of problem, a number of phonetic algorithms was developed. A phonetic algorithm use rules to transform substrings into phonemes, trying to unify two strings written as spoken. Some algorithms:
But all these algorithms suffer from the same problems. The rules are written specifically for one target language, and the most common target language is English. This isn’t a matter if your native spoken language is English, or if you don’t need to do internationalized applications…
There are another solutions? Sure. Searching for alternatives, I found the approximate string search algorithms, mainly the Levenshtein distance (or Edit-Distance) algorithm. More robust and reliable, can be used without changes. With a little, but very important, advantage: we can use to sort the result candidates by your distance to the searched string. This little advantage can be the difference between a poor result, with pages and pages of useless results, or a well ordered list, with the most relevant results first.
I expect to show how to use this great algorithm to construct a full text search engine on databases, soon.
References:
- Wikipedia
- Phonetic algorithms implementations: Apache Commons Codec
- Levenshtein distance algorithm implementation: Apache Commons Lang
Java Certification Path - Class Access - Public Access
Sunday, November 19th, 2006A class with the public modifier gives to all classes in all packages access to it. In other words, all classes can instantiate, extend or return a class of that type. Example:
package humanity;
public class Person {}
This class is visible to all classes in all packages. But if this class is in a different package, we need to import it yet.
Some exceptions exists, mainly when we use the nonaccess final modifier. I will explain soon.
Java Certification Path - Reserved Words
Saturday, November 18th, 2006Ok, my most boring post until now. Don’t forget these Java keywords and reserved words:
| abstract | class | extends | implements | null | strictfp | true |
| assert | const | false | import | package | super | try |
| boolean | continue | final | instanceof | private | switch | void |
| break | default | finally | int | protected | synchronized | volatile |
| byte | do | float | interface | public | this | while |
| case | double | for | long | return | throw | |
| catch | else | goto | native | short | throws | |
| char | enum | if | new | static | transient |
Java Certification Path - Class Access - Default Access
Saturday, November 18th, 2006A class with default access has no modifier preceding it in the declaration. The default access is a package-level access, because a class with default access can be seen only by classes within the same package. If a class has default access, a class in another package won’t be able to create a instance of that class, or even declare a variable or return type. The compiler will complain. For example:
package humanity;
class Person {}
package family;
import humanity.Person;
class Child extends Person {}
Try to compile this 2 sources. As you can see, they are in different packages, and the compilation will fail.
Java Certification Path
Saturday, November 18th, 2006After thinking a lot about, I take a decision: I will take some certification exams in the Java Language. My object is to take the entry lavel exam Sun Certified Java Programmer (SCJP), and after follow the path until the Sun Certified Enterprise Architecture (SCEA).
I will try to put some notes here, about what I learn while chasing this objectives.
The book Sun Certified Programmer & Developer for Java 2 Study Guide (Exam 310-035 & 310-027) will be my partner for the next months. I will try to get my Java Black Belt, while I too do some mock exams. Cross your fingers!