Showing posts with label Programming. Show all posts
Showing posts with label Programming. Show all posts

Friday, August 8, 2008

Class Immutability: Never thought of them before

"Classes should be immutable unless there's a very good reason to
make them mutable....If a class cannot be made immutable, limit its mutability as much as possible."

- Josh Bloch

I never thought of class immutability and its importance until I read Josh Bloch's statement quoted on a site about Java practices. There must be something about it that Bloch would quote such a tough statement. And it's true! A programmer must defend his class's instance variables from being changed from outside itself (except for instantiating the class, in which case you'll use a constructor or a static factory), unless there is a very good reason for it. Yeah, that is coolness to the uttermost level. Another point learned. :]

Continually learning,
Jep

Tuesday, July 29, 2008

Encapsulation in OOP

Level: Basic

Two of the main features of the Object-Oriented Programming (OOP) paradigm is flexibility and maintainability. It is so desirable for one's source code to be flexible in spite of changes in its internal implementations. Programmers don't have to worry about their codes getting broken due to changes in the implementation of the Application Programming Interfaces (API) that they are using. All they have to know is that public methods of a given API behave the way they know it should regardless of its implementation details. Through this sense of flexibility in one's code, users of such API are guaranteed that their own code will behave accordingly for a very long time. This advantage is offered by OOP through encapsulation. Encapsulation hides the implementation details of an API to accomodate maintainability and flexibility in its use. It serves as a protection for a class's attributes by forcing client users of the class to access its protected attributes through publicly accessible methods or behaviors.
An example implementation of encapsulation in class would be the following (using the Java programming language):

public class Person {
private String name;
private String age;

public void setName(String name){
this.name = name;
}
public String getName(){
return this.name;
}

public void setAge(int age){
this.age = age;
}
public int getAge(){
return this.age;
}
}


Notice that class Person's attributes are declared as private to protect them from being accessed directly outside Person. Instead, they are accessed through what we call setters (setName,setAge) and getters (getName, getAge). Clients of this class will have to use setters to set the value of its attributes and getters to determine the value of its attributes. By doing so, we will not be able to see others codes accessing Person's name and age as follows:

public class Tester {
void testPerson(){
Person person = new Person();
person.name = "Juan dela Cruz";
person.age = 20;
}
}


Instead, Tester will class will use Person class as follows:

public class Tester {

void testPerson(){

Person person = new Person();

person.setName"Juan dela Cruz");

person.setAge(20);

}

}


Also, since Tester class accesses Person's age through Person's setAge setter, we could be sure that Person's age will not have 0 or any negative value by adding a checker into Person's setAge method like the following:

//preceeding code removed
public void setAge(int age) throws
IllegalAgeValueException{
if (age > 0) {
this.age = age;
} else {
throw new IllegalAgeValueException(); //in which
IllegalAgeValueException is a //custom exception
}
}


Encapsulation in OOP leverages an API's maintainability and flexibility if used properly. Of course it doesn't mean that since OOP offers encapsulation the programmer does not have to do anything in order to take advantage of such feature. In order to do so, one has to write his code in an encapsulated manner. :]

Continually learning,
Jep

Monday, July 28, 2008

Back to the Basics

It has been a few weeks now since I started reviewing my Java basics. There are so many trivial matters about programming that I seem not to pay attention to just because they are, as I said, trivial. One example would be optimization. Due to busyness and all the rush, I seldom check my code if it runs the optimized way. If it works, then it works. But an excellent programmer should not treat his code as so. A programmer's code is his essence. Show me your code and I'll tell you what kind of programmer you are. I am just so thankful to God that He made me see this weakness of mine at this early stage of my software development career. Optimization is just one of those programming weaknesses of mine. There are still lots of them and I am hoping to realize them as I go back to my Java basics and then see what action plans I could come up with to improve on those areas. :]

Thursday, July 17, 2008

NASA Mars Lander's Robotic Arm shuts down to save its self

Yesterday I read an article from PC World about NASA Mars Lander's robotic arm shutting down its self to avoid getting hurt which would make its situation worse.
The Mars Lander found its self on a very crucial situation during execution of its mission. After receiving commands from NASA's command center, the robotic arm decided to shutdown its self instead of compromising and getting its wrist into a bad shape. Further details of the story can be found in this link: http://www.pcworld.idg.com.au/index.php/id;593684490 .
I just get so excited whenever I get to read news such as this. It has always been a dream of mine to be a robotics engineer. I get so amazed by how a robot could execute the commands given to it by its developer. So far I don't have yet a comprehensive plan on how I am going to pursue this dream of mine. Still, I believe there is always a time for everything under the sun. (Ecclesiastes 3:1) :]
If there is anyone out there who could help me on this, please do kindly lend me a hand by sharing your knowledge. I would greatly appreciate it :]

Continually learning,
Jep


Thursday, July 10, 2008

Learning Java Mobile Edition

This afternoon one of my college batch mates asked me if I want to learn Java Mobile Edition and I immediately said yes. For the past three years of working I have been much involved with web application development, like information systems. Now I am working on a considerably big scale information system with three years allotted for development and implementation. So I have so little idea on what I'll be learning about JME. I am excited about it and now thinking on how we will start our online class. We still have no curriculum and reading materials for the class and we are looking for people who want to join us. For those want to join the group, please send me your email so I could send you an invitation. We hope to have more people on the group before we start the class.


Continually learning,
Jep