Tampilkan postingan dengan label Programming. Tampilkan semua postingan
Tampilkan postingan dengan label Programming. Tampilkan semua postingan

Kamis, 05 Oktober 2017

Use of Private Constructor in OOPS

Use of Private Constructor in OOPS

If we set access specifier of a constructor to private, then that constructor can only be accessed inside the class. A private constructor is mainly used when you want to prevent the class instance from being created outside the class. 

This is mainly in the case of singleton class. Singleton classes are employed extensively in concepts like Networking and Database Connectivity. Using private constructor we can ensure that no more than one object can be created at a time. 

Example of Private Constructor in a Singleton Class

public class SingletonClass
{
    public static SingletonClass singletonClass;

    private SingletonClass() 
    {
    }

    public static SingletonClass getInstance() 
    {
        if(singletonClass == null) 
        {
            singletonClass = new SingletonClass();
        }
        return singletonClass;
    }
}

A class with private constructor cannot be inherited.

If we don’t want a class to be inherited, then we make the class constructor private. So, if we try to derive another class from this class then compiler will flash an error. Why compiler will flash an error? 

We know the order of execution of constructor in inheritance that when we create an object of a derived class then first constructor of the base call will be called and then constructor of derived class. Since base class constructor is private, hence, derived class will fail to access base class constructor.

We can also use sealed class to stop a class to be inherited. Sealed class provide more flexible and readable way to stop inheritance.

Diamond Problem in Multiple Inheritance in OOPS

The diamond problem occurs when two super classes of a class have a common base class. 

Suppose there are four classes A, B, C and D. Class B and C inherit class A. Now class B and C contains one copy of all the functions and data members of class A. Class D is derived from class B and C. Now class D contains two copies of all the functions and data members of class A. One copy comes from class B and another copy comes from class C.

Let’s say class A has a function with name display(). So class D have two display() functions as I have explained above. If we call display() function using class D object then ambiguity occurs because compiler gets confused that whether it should call display() that came from class B or from class C. If you will compile above program then it will show error.

This kind of problem is called diamond problem as a diamond structure is formed (see the image).

That is why major programming languages like C#, Java and Delphi don't have multiple inheritance because it can lead to diamond problem and rather than providing some complex way to solve it, there are better ways through which we can achieve the same result as multiple inheritance. We can use interfaces to resolve this problem.

C++ supports multiple inheritance.

Notice that the above problem with multiple class inheritance can also come with only three classes where all of them has at least one common method.

Because of this problem we can not extend two classes for implementing multiple inheritance and to resolve this problem of multiple inheritance in object oriented programming we use interfaces for implementing the functionality of multiple inheritance. 

As we know we do not define a function but only declare that function in an interface. So if we use interfaces we can extend one class and one or more interfaces or we can implement more than one interfaces at a time to use the functionality of multiple inheritance and we can escape from diamond problem.

Selasa, 26 September 2017

What is the difference between Singleton Class and Static Class? Which one to use and when?

What is the difference between Singleton Class and Static Class? Which one to use and when?

Almost both singleton class and static class serve the same purpose which creates confusion among developers and architects about which one to use. When to use singleton class and when to use static class. In this article I have tried to differentiate between the two. Following is the list of differences between a singleton class and a static class:

1. Singleton classes are more inclined to the object-oriented concepts as compared to the static classes. With Singleton classes, you can use inheritance and polymorphism to extend a base class, implement an interface and capable of providing different implementations. While a static class cannot inherit their instance members. So, Singleton classes are more flexible than static classes.

2. Singleton classes can be passed as an object to other methods while a static class allows only static methods and you cannot pass static class as parameter.

3. If your requirement is to maintain the state, then singleton class is the better choice than static class, because maintaining the state with static classes is very cumbersome and leads to subtle bugs.

4. Singleton classes can be lazy loaded if it’s a heavy object, but static class doesn't have such advantages and always eagerly loaded. Static class is generally initialized when it is first loaded and it will lead to potential class loader issues.

5. Static classes are hard to mock and consequently hard to test than singletons, which are easy to mock and thus easy to test. It’s easier to write unit tests for singleton than static classes, because you can pass mock object whenever singleton is expected, e.g. into constructor or as method arguments.

6. Singleton classes provide more flexibility. Consider the possibility that your application has a global shared object and tomorrow you decide that you must make more than one instance of the class. If you use a singleton class then all you should do is make the constructor public. This is not so simple with static classes.

7. Many Dependency Injection framework manages singleton quite well e.g. Spring, which makes using them very easy.

8. Singleton objects are stored in Heap, but static objects are stored in Stack.

9. Static class provides better performance than singleton class, because static methods are bonded on compile time.

10. Singleton classes maintain single object across the application life cycle, so the scope is at application level. The static does not have any object pointer, so the scope is at App Domain level. Moreover, both should be implemented to be thread-safe.

Rabu, 19 Februari 2014

Can Software Developers enjoy their personal life?

Can Software Developers enjoy their personal life?

Can Software Developers enjoy their personal life?

Normally in any job around the world, you have to work for 40-45 hours a week. But is this really true for a software developer? As a software developer, if you have to grow in your professional life, you have to sacrifice your personal life. This statement is intimidating, but true to some extent. Mainly, the initial years of a software developer are very crucial and you have to work extensively to thrive in your career. Only the lucky software developers get flexible working hours in the office otherwise most of the software developers have to compromise their personal life.

In a software industry, you will have to face:

1. Tight Deadlines
2. Excessive Work Load
3. Staying Late Nights
4. Working on Weekends
5. Code quality and code delivery at same time
6. Bug fixing

If you are a software developer, you have to try hard to maintain work-life balance. Software development is full of uncertainties. Any time any thing can happen in your project and then you have to sit to solve that issue. You always have to think twice before planning your evenings, weekends and holidays as these uncertain and untimely issues can destroy them easily. If you are planning to go for a long holiday, you will have to plan that according to the situation in the project. If you are running through a lot of issues, forget that you will get leaves. You have to inform your manager months ago for your holiday plans otherwise just forget it.

Single vs Married vs Parent Developers

If you are still single, that's fine. You can stay late evenings, you can come on weekends etc. You can also take excessive work load and pressure because at this stage of life you don't have to care about other troubles of your life (except your girlfriend, if you have :) ) as your personal life is smooth till now. But once you get married, you get extra personal life responsibilities on your shoulder. Now you also have to care about your life partner. You also have to give sufficient amount of your time to her/him. You cannot come stay late in office every evening, cannot come to office every weekend etc. etc. The situation becomes worst after kids. At this stage of life, you really have to struggle hard to maintain work life balance otherwise you will not be able to see your kids growing.

Advice to software developers:

1. It depends on how you handle the situation and your skill. However, to survive in the any kind of stream, you should invest your time for atleast 2 to 3 years in that domain and understand clearly. Then you will go into a better position in that stream and you can do well with balanced life. I mean, after you understand clearly about what you do, then only you can prioritize the work properly and can live balanced life with work and family. There are lot of companies which expects only 40 hours of work. Even there, if you fail to deliver, you should put more time and complete the work.

2. Companies take advantage of eager graduates and workaholic tendencies. Just refuse to fall into the pressure to extend hours. If companies can't deal with that, then your better off looking for a new employer. When interviewing at new jobs, it would be good to find out what their overtime policies are. If they have real overtime policies that involve giving you respectable amounts of money for working late, they're probably not as likely to ask for 60-hour work weeks (though I would try to talk to other employees there to get an idea of what actually goes on).

3. Product companies have better working hours compared to Service Oriented companies. So try to get into product companies. But product companies are limited as compared to software oriented companies. 

Senin, 10 Februari 2014

5 Advantages and Disadvantages of Software Developer Job

5 Advantages and Disadvantages of Software Developer Job

5 Advantages and Disadvantages of Software Developer Job 

No job is perfect. Every job has some advantages and disadvantages. If you are enjoying your job, that job is best for you. You should carefully choose a right career for you as it can make your life heaven or hell. If you have chosen for becoming a software developer, you must know the pros and cons of this career. I would like to share the advantages and disadvantages of software developer's job.

Advantages of Software Programming Job

1. Good Pay: As compared to other jobs and professions, software programming job's pay is high.

2. Portable Skills: You learn a lot of things like programming language, software tools etc. in one company and can apply that knowledge in another company. When you switch from one company to another, you also get a good salary hike.

3. Work Anywhere: You can work from anywhere. All you need is one laptop and the internet connection. Work From Home is a very good facility in this field.

4. Comfortable Working Environment: You work in a very comfortable environment. You always have access to fast internet connection. You always have air condition office, nice comfortable chair. You don't have to worry about the harsh weather conditions outside. Heavy rainfall, hailstorm, cold snow, hot or cold weather cannot affect your work.

5. Continuous Learning Curve in all directions: You always keep on learning new things in your office, in your project. Your knowledge continuosly increases with your experience. You always remain updated with the new technologies and inventions.

Disadvantages of Software Programming Job

1. Global Competition and Outsourcing: We live in a very well connected world. Every body in any corner of the world is well connected with each other through emails, messengers etc. There is great IT workforce in India which can do the same job in less money as compared to the developers in USA and Europe. Almost every IT company outsources its lot of work to India to reap benefits by utilising the talent of India and saves a lot of money. So, the competition in this field is global and you have to be very perfect to cope this.

2. You have to upgrade your skills continuously: Technology is changing every day. Everyday, there are new enhancements, everyday there is new invention. You have to keep yourself very active and updated otherwise you will be left behind in the race. You have to learn new thing everyday. You should learn to adapt with new things. Standing rigid with the old things will make you lose the competition. Keep on learning new programming languages and always add new thing to your resume.

3. Sitting Job: You have to sit tight to your desk and stare at your computer continuously. If this is not fine for you, you should not come to this profession. But today, other jobs are also becoming like this. So you will find this kind of scenario in each and every job. It can make you unhealthy very easily.

4. Age Discrimination: There is a lot of age discrimination in the software programming field. Many people think that a person above 40 would not be able to perform well. Preference is given to the young developers because it is assumed that they have fresh mind and would be able to solve the programming puzzles and tricks easily as compared to the old brains. Also young developers will cost less to the companies.

5. Long hours, Tight deadlines and no personal and social life: In IT field, you are bound to the tight deadlines and have to work for long hours and on weekends to meet the deadlines. Most of the Software developers have to sacrifice their personal and social life to make the deliveries on time. 

Jumat, 07 Februari 2014

Should Interviewers ask Puzzles to Software Developers in an Interview to evaluate their skills?

Should Interviewers ask Puzzles to Software Developers in an Interview to evaluate their skills?

Should Interviewers ask Puzzles to Software Developers in an Interview to evaluate their skills?

Programming is not about writing lines of code, it is about solving problems for the people. Some interviewers ask puzzles in an attempt to gauge software developer's ability and approach to solving problems. Puzzles can be useful in assessing problem solving skills, which is of course one of the key aspects of programming. Interviewer's expectations in that case are to hear you articulate your approach to the problem. What other data would you try to gather? How would you test your hypothesizes, etc. The point of asking the puzzles is to watch how developers work toward the solution, not necessarily if they get the right answer. You can spot good programmers pretty quickly just by watching this process. The interviewer must have been referring to problem solving and logic skills, which is part of the everyday work of a programmer. When given a problem, you need to be able to analyze it, subdivide it, and write a solution for it by using the most optimal approach.

Personally, I don't think that such puzzles provide an accurate indicator. I cannot understand the connection between such puzzles and programming. In the "real world", you have more than five minutes to figure out if your dealing with a bin packing vs a back pack problem, for instance. Initially, it's sometimes easy to misunderstand the problem at hand until you're in the middle of applying the wrong solution. That happens to people with 1, 5, 10 or even 20 years of experience.

The best interview 'puzzles' are the ones where you sit down at a computer to solve a problem in the domain in which you claim expertise. I also dislike the "Well, a programmer should be able to ..." thinking because it doesn't take into consideration that people get anxious when hit with something unexpected in a setting that is already stressful. Sure, you could solve that if you had time to think about it.. and perhaps you could solve it faster if you realized that your life would be over if you didn't. Do you want to work somewhere where your life will be over if you can't solve problems in five minutes? Will you get fired if you can't?

Should all great programmers also be champion sudoku solvers? I'm sure that plenty are, but it's not like some kind of prerequisite for competency.

I'm not saying that you should not be tested on how you approach problems, but the tests should be fun and invite the 'best' that the applicant has to give, given their area of expertise. Proving that you are as smart as a character that Bruce Willis portrays seems kind of pointless, considering that producers spent a pretty sum to get that scene just right.

These sorts of puzzles are generally used by interviewers who neither know what they are doing nor how to look for developers? These are just voodoo hiring practices. Some interviewers ask these questions so they feel like they are supposed to. They know that not answering the question is "bad" and answering is "good", but they can't tell you why beyond non-answers like "a developer needs these skills". They are a waste of time and an indicator that the interviewer is not a competent interviewer. In other words, if you detect that you're being interviewed by someone who has little comprehension over what you'll actually be doing, excuse yourself to go to the restroom and never return.

Suggestion to Interviewers

It is far better to ask coding questions to the software developers. In other words, you need to ask questions that allow you to explore the skills and behaviour of the software developer as they try and tackle the problem. Asking silly puzzles instead of coding philosophies will not give you any indication about how good that person is for the job.

Always try to make the applicant feel as comfortable as possible, so that you can actually try to find out how good that person will be for the job. The best programmers are those who understand the dynamics of software development, even if they don't have the highest IQs.

Sabtu, 25 Januari 2014

Devexpress vs Telerik vs Infragistics: Which one to choose and why?

Devexpress vs Telerik vs Infragistics: Which one to choose and why?

Devexpress vs Telerik vs Infragistics: Which one to choose and why?

Devexpress, Telerik and Infragistics are the great third party tools for you for both your windows and web application. These third party tools mainly support .NET technologies like WinForms, Silverlight, ASP.NET and WPF. Devexpress also supports windows application built in Delphi programming language and I am currenty working on it. Also I have heard some other third party tools like ComponentOne, SyncFusion and Januscontrols but never worked upon them. Which one is good, which one to choose and why? Although this is difficult and time consuming task, but let's try to solve the confusion.

Why do we require these third party tools for our application?

These all thrid party tools are mainly used for improving the user interface which standard controls cannot provide. Devexpress, Telerik and Infragistics third party tools provide a lot of exciting features and components which you require like attractive dashboards, advanced reporting tools, sorting, searching, grouping, paging, filtering, databinding, drag and drop features, intelligent editors, smart control panels, importing and exporting features, attractive ribbon bars, complex tree views and graphs, cool transition controls, rich textboxes, navigation contorls, charting controls, complex themes and much more...

Devexpress vs Telerik vs Infragistics: Which one to choose and why?

Which tool is best? This is very hard to say. This question seems philosophical to me. There's no one answer.

Devexpress, Telerik and Infragistics have their own strong and weak points. Each and every tool has some learning curve to understand its way of doing the things. These all tools provide excellent performance and great convenience to the user. 

It is upto you and your needs to choose one of these. Following are some humble suggestions which you can consider before choosing one of these tools for your windows or web application.

1. What is your requirement?

What is the nature of your application and what functionalities do you want to put in? This should be the first questions striking in your mind before choosing any thrid party tool for your UI. Once you finalized your requirement, then move to these contorls and read their documentation, features list, online demos, try trial versions to implement your functionality. If the tool is giving you your desired functionality, consider it.

2. Your familiarity with tool

A good choice, nevertheless is to choose one you know. I would start by expressing my opinion that the best tool set is the one with which you gain your greatest familiarity. I think the market demonstrates that there is a sufficient percentage of buyers to support all these three tools. I would also conjecture that the longer you work in any one, the more you will like it. 

My observation is that each library comes with its own set of presuppositions: about structure, nomenclature, interface and class design and inheritance; about consistency and your mental model of the controls and events; about many things. 

Some of these presuppositions blend easily with the way some people think. Others blend better with other brain architecture. It doesn't mean one is better or worse. It means one is different than the other. You have to find the one that works for the way you think and program. Otherwise you will be twisting on a right-hand nut with a left-hand wrench.

3. Which tool your co-workers and other teams of your company are using?

You should also investigate with other teams in your company if they are using any of these tools. If yes, learn from their experience. 

4. Support and Documentation

Evaluate each tool in terms of their support and documentation. Go with tool which provide solid support, documentation, sample examples, tutorials, example codes and demonstrations. All the component libraries should be well-represented and documented. When you will use a third party software or tool, you will need much help from those people. The tool vendor should have good forum.

5. Cross browser compliant

If you are looking for Devexpress, Telerik and Infragistics for you web application, must ensure that all the features which you need from these third party tools run same on all the browsers. 

6. Cost and Licensing 

Cost should be reasonable and licensing should be simple. 

Conclusion

To be fair, all grid components will do great if you're on the standard track. As soon as you'll have something to implement that is not a general purpose usage it will get harder. This is true for all third party controls.

Compare demos and trial versions based on your needs. I would encourage you to evaluate the controls on your own to see which control will best meet your needs. That's the best suggestion I can suggest.