Related
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 6 years ago.
Improve this question
I know that it is more time consuming to find an object in the heap than in stack. And I have also seen that people advising to create more classes for different tasks. May be the code can be more understandable and clean if there are more classes. But,what about the time? can anyone please explain that to me.
Object-oriented programming means more separation of concerns and a good segregation of concepts.
It tries to address problems closer to how our mind works. Real world has uncountable objects and things. Each one has a definition and properties, and also behaviors.
Clearly object-oriented programming may use more memory and even CPU power, but more than 2 decades ago everyone thought that, once computer raw power and memory were increased every year, optimization was still important but productivity became the priority.
There're still some corner cases where object-oriented programming isn't the answer, but almost any business or home-use application in the world is developed using object-oriented programming because companies want to ease maintanability and be able to respond to change in a meaningful and productive way (to do more and reduce costs).
Your code, more often than not, will be a solution to a problem. The number of classes isn't something that you predetermine. It depends on the complexity of the problem to be solved and the complexity of its solution. If the raison d'etre of your code is just to find the nth fibonacci number and nothing else then the code may reside in just a single class(or none at all) but if your code is, say, an enterprise software designed to tackle a complex problem like banking then there are many factors that decide your design and thereby the number of classes. Two of the biggest factors are maintainability and extensibility of your code.
You should have a look at the SOLID principles for Object Oriented design, first named by Robert C. Martin.
Once you go through them and more on OO design I hope "more classes vs less classes" won't be a question for you.
I don't think it's a decision being made in order to boost performance, being the difference irrelevant at runtime IMHO. Neither I think the developing time is going to differ a lot. I think what it's important to understand here is the Single Responsibility Principle. So I think the criteria isn't "create more classes" but "do not use the same class for two different things", (what in the end makes you create more classes, but isn't the same).
What I've realized on the time I've been working is that, while the system grows up, the most important thing is how it does it. If you create less classes you will end up with 30k lines classes that are completely impossible to debug efficently. The same applies with 400 lines methods doing 20 different things. This means a lot of extra time wasted as adding new functionalities and fixing existing ones become more and more complex. In the end you need to apply a certain criteria to separate your code and avoid ending up on tha situation and the Single Responsibility Principle seems a good one.
I am a senior developer, so this appears to me a stupid question. My answer should be NO, or WHAT? NO!!!
But I was in a meeting yesterday, and I was explaining some PMD results. When we get to the "too long method name" issue, I started to explain and the customer said: well, and remember a long method name has an impact on performance, the program run slower.
I said: no, you are wrong, is only a clean code rule, and is important to get a good code, but has nothing to do with performance, the bytecode is similar with different names.
But the client, and there were some people in the meeting arguing in this, was sure about this. They had some projects in that long method names were the cause of poor performance.
The only idea I have is that some introspection or reflection thing has is related to this, but apart from this, I am sure, or I thought I was Sure, the method name length has not any performance impact.
Any idea or suggestion about this?
Arguably it will take more space in memory and storage - so a jar file containing classes with enormous method names will be larger than one with short class names, for example.
However, any difference in performance is incredibly unlikely to be noticeable. I think it's almost certain that the projects where they were blaming long method names for poor performance were actually misdiagnosed. It's not like it would be the first time that's happened.
Of course, the best way to take the heat out of this situation is to provide evidence - if performance is important, you should have tests for performance. Run those tests with long method names, then refactor them to short method names and rerun the tests. I'd be incredibly surprised if there were a significant difference.
Method names are not just relevant with reflection but also during class loading and of course in both cases a long method names means that at some level there is more for the CPU to do. However, with method name length that are even remotely practical (i.e. not thousands of characters long), I am absolutely certain that it's impossible for this to be significant compared to other things that have to be done during reflection or class loading.
But the client, and there were some people in the meeting arguing in
this, was sure about this. They had some projects in that long method
names were the cause of poor performance.
It sounds like a total guess being treated as fact.
This is just a case of some people's general nuttiness about performance.
Even if they happen to be right, it's a total guess.
Every program has room for performance improvement by changing certain things.
Guessing does not inform you what those things are.
If two programs that do the same thing have different performance, it only means they've been optimized to different degrees.
Your challenge is to explain this.
Startup times will be affected positively if class names and member names are shortened. To that end one can use a bytecode shrinker.
For example, yguard (LGPL) can shrink code. It also allows you to deobfuscate stack traces for debugging purposes.
Manually assigning short class and member names for performance reasons is of course a horrible idea.
I can't why it can possibly impact performance significantly unless you are pulling method names out yourself through reflection and then render them on an UI. That is obviously not the case. So I'm just confused. Are you sure your client isn't confusing method name with file name or is he thinking about the cases where some really old programming languages do not support super long method names? Depending on how old that person is, their judgement is definitely absurd to a computer scientist. If they can prove their point with fact, they may as well submit it to ACM, Oracle/Sun or MIT to verify their findings.
I think the length of function name impact to performance as followed:
compile time from bytecode to binary code (with java, .net, ..). The byte code still contains file name, class name, package name.
if we use *.lib, *.dll, *.so it may impact to performance (in android for example when you use native code)
when we use native code to call to java function (in java, android)
when a black box (lib file,app) connect to other black boxes (lib file,app) it use function name in header file as the indetification. So I think length of name will impact to performance.
As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.
Closed 10 years ago.
So do I really need to learn about them ? Isn't there an interesting way to learn about stacks, linked lists, heaps, etc ? I found it a boring subject.
**While posting this question it showed some warning. Am I not allowed to post such a question ? Admins please clarify and I will delete it :/
Warning :: The question you're asking appears subjective and is likely to be closed.
okay..I get it
So what is THE best way to learn them ? What book do I refer ? What website ?
It's compulsory to learn about data structures if you want to be a programmer. Data structures are your bread-and-butter - if you don't understand things like the behavior, uses, and run-time complexity ('big-O') of at least the basic structures (arrays, linked lists, stacks, queues, trees (binary / n-ary, self-balancing varietes), hash-tables, heaps, graphs) and the algorithms that run on them (insert / locate / delete), you won't know which is appropriate to use under what circumstances.
Every trade has its tools; these are ours. Data structures are the most basic underpinnings of almost any algorithm that you're going to learn. Unless you want to be a cargo cult programmer, you need to understand how they work.
Whether or not there are interesting ways to learn about them is a separate question entirely... :)
I would go even so far as to say that most of programming revolves around manipulating data structures, it is the foundation of computing after all: you get some data, you process it, you possibly give output. All the data usually reside in data structures and choosing inappropriate structures will have the bigger impact the bigger the project.
As you get more experience you will find that algorithms and datastructures are invaluable to your day-to-day development, and actually pretty interesting.
By learning about them now you will learn:
Which data structure is appropriate for which context, i.e. when to use a single-linked list, when to use a stack, when to use a queue, when to use a tree
Which algorithms are appropriate for which purpose, such as tree depth-first search or breadth-first search.
Space and time complexity of algorithms, for example why is a quicksort sometimes the best solution, and sometimes heapsort.
Overall it'll teach you the beginnings and fundamentals of computer science, even if you never have to implement a stack again you'll know the kind of thought and consideration that goes into it. If you then ever have to implement your OWN data structure (and chances are you will, quite often), you will know what to do and what not to do.
If you want to be a successful programmer, data structure is a must. How will you program if you don't know data structures and algorithms?
Whether you like it or not, all programming is built around data structures. You may never have to write one, but you will have to choose which one to use many times. It is not really a requirement to programming in general, but if you want to excel in the field, understanding of the basics is a must.
Anyone can build a shed without knowledge of materials or construction techniques. You can even work in a house placing bricks and mortar under the orders of someone else, but if you want to build a house yourself, you do need to understand materials and techniques.
Data structures are the programming materials. Algorithms are techniques. Will you use data structures? You will use the simplest ones in a daily basis, each so often you will need to solve a problem where an specific data structure is required, and while you may manage not to build your own bricks, you will need to understand whether you need bricks or a concrete wall for your purposes.
If you want some evidence for the importance of data structures, take a look at the Google hiring process. Whatever you think about Google as a company, there is no denying that they have some very good people working for them. Their interview process is setup to determine the candidates knowledge of data structures and algorithms. Because when it comes down to it, that is what is at the core of programming, no matter what language you are working in or what domain you are programming for.
If you are planning a career as a professional programmer, you need to know the fundamentals, not just how to crank out code that "works". Otherwise, your just playing.
Is it compulsory to learn about arithmetic to be an engineer?
If you take the attitude of "is it compulsory" with regards to any of the building blocks of programming languages, you're probably not cut out to be a coder. Regardless of "compulsory" or not, you should always be looking for new concepts to learn and seeing if it'll improve your coding style/standard.
But in answer to your question: yes.
I'd say it's compulsory at some point in your development to have a firm grasp. I'm not necessarily sure the standard Data Structures course is the best way to learn. Sometimes, the best way to learn them is "I have problem X. For some reason, it's taking my algorithm a long time to solve X. How can I make this faster?"
One book I'd highly recommend is Programming Pearls. It has some really good analyses, backed by a lot of examples of where the real-world motivations for the solutions came from. It presents the problems in an interesting manner, and never teaches by giving you a laundry list of data structures.
Yes 99% of books on Data Structures are boring and exercises contrived. They feel like they are just making up problems that server no practical purpose :(
This book is the one exception to the rule I've come across. You will have a naive but working RPG game by the end of the book:
Data Structures for Game Programmers
Read the above book and you will solve your chicken and egg problem and see you really can't do much without data structures after all.
Well, this may sound a little awkward but I wouldn't say it is COMPULSORY to learn Data Structures to be a - regular- developer. Seriously! Of courser if you study then hard it will give you a lot of insights and knowledge on several programming aspects and that's always good. But compulsory ... well, I think it is just too much. VERY GOOD would be enough.
Let me explain why. It is not that often, for today, to write Data Structures code because - let's face it - it would be RE-writing, RE-inventing what we already know for so many years! What I would say it is COMPULSORY is to study just the general theory of them and the APIs/libraries that are already commonly in use (and tested and optimized) like the Collections API in Java. You must know by heart the differences between a List and a Set (in Java for instance) and their capabilities and proper usage but you don't need to know exactly HOW they are implemented - checking every private method and attribute - to deal with most common, day to day, coding problems. You will do just fine without all the "guts" of Data Structures for the general stuff. We face different challenges now.
But don't get me wrong - neither think I am crazy or naive! Off course there are situations that you will need to implement yourself some sort of custom data structure (maybe your own BalancedBinaryTreeMap!). You got to be prepared for everything.
I am just arguing about being compulsory or not. Again, I don't think is compulsory but it is indeed very good.
Cheers.
I suppose you could learn programming without learning a whole lot about data structures or algorithms. To make an equivalent example, think of it like if a carpenter knew how to build things, but didn't know about measurements and such. Would he be able to get a career in carpentry? Possibly, but let's say he needed to know the exact material he would need to complete a project. He'd probably get fired because he doesn't know what sort of material or measurements to use.
So with data structures and algorithms, you can say it's the ability to give exact measurements of an application and knowing what sort of performance you'll get out it.
Like a musician learning scales, data structures are part of the tools of the software trade. Sure you can work as a programmer without the knowledge but you're handicapping yourself. If I'm interviewing two people for a position and one of them understands and uses structures and the other can't even explain what a stack is, my choice is pretty clear.
If you want to be judged to be a competent, employable programmer, you need to learn your craft.
Is it compulsory to learn them?
No, you can program without them, just as it's not compulsory to break your code into functions.
That being said, if you want to be an effective programmer that can write at least decent code without getting your car egged by your coworkers, you want to at least be able to make a decent selection of library classes.
Every programmer should understand the tradeoff between a LinkedList and an Array, or why binary searches and binary trees are useful for sorted data. This isn't just about performance - it's about correctness too since you can't just put anything into a tree set.
Does it mean that you need to know how to implement your own AVL tree, build super-smart data structures, etc.? Not necessarily. It's a matter of how much you want to know what's going on "beneath the hood" and whether your tasks necessitate it.
I'm not a big fan of deep data structure and algorithms questions in interviews because the vast majority of developers don't need to implement these things, just to use library stuff. I prefer to ask job related questions in interviews. However, accept that if you don't learn those things you would face a tougher battle to get other jobs.
It should be, yes...
No body should force you to learn anything you don't want to learn.
If you're the type of person that is compelled to be the best that he/she can be at what he/she does, and you love what you do for a living, you'll learn everything there is to know on your own accord.
#happysoul: You should ask yourself WHY learning data structures bore you. Also, it would help if you also identify what DOESN'T bore you.
If you at least love to learn about algorithms, I'm sure we can all suggest a perfect marriage of the two that would be exciting to learn!
My recommendation for best algorithm/data structure combo for the most fun learning experience: graphs.
As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.
Closed 10 years ago.
Ok so this isn't 100% programming question, for which I'm really sorry, but still this is the best place I know to ask it.
In ~2weeks time I'll have a job interview, first they'll make me take a test from Java and I was wondering what's the best way to prepare for it? I bet it will be something similar to SCJP (but probably much easier), are there any (short) books, tutorials that are worth reading? I did order the SCJP book by Sierra/Bates but I won't be able to read it before the test.
Don't get me wrong I do have experience with Java etc. but I really want to get the job so I want to go there well prepared.
Also marry Xmas to everyone, no matter what religion you are.
Don't get me wrong I do have
experience with Java etc. but I really
want to get the job so I want to go
there well prepared.
If you have enough experience don't worry about the test. Don't try to impress your next boss with an awesome test that does not reflect your current knowledge. Read Joel's posts about interviewing:
The second worst kind of interviewer
is the Quiz Show Interviewer. This is
the kind of person who thinks that
smart means “knows a lot of facts.”
They just ask a bunch of trivia
questions about programming and give
points for correct answers. Just for
fun, here is the worst interview
question on Earth: “What’s the
difference between varchar and
varchar2 in Oracle 8i?” This is a
terrible question. There is no
possible, imaginable correlation
between people that know that
particular piece of trivia and people
that you want to hire. Who cares what
the difference is? You can find out
online in about fifteen seconds!
Remember, smart does not mean “knows
the answer to trivia questions.”
Anyway, software teams want to hire
people with aptitude, not a particular
skill set. Any skill set that people
can bring to the job will be
technologically obsolete in a couple
of years, anyway, so it’s better to
hire people that are going to be able
to learn any new technology rather
than people who happen to know how to
make JDBC talk to a MySQL database
right this minute.
quoted from "The Guerrilla Guide to Interviewing (version 3.0)" by Joel Spolsky
If you need to impress someone to get the job, may be that's not a good place to work...
I recommend you pick up Effective Java, 2nd Edition by Joshua Bloch and read it cover to cover. It details several "best practices" for Java and will have you walking into the interview knowing for example things like:
when to use Static Factory Methods
when to use the Builder Pattern
what a Singleton is and how to enforce it
how to prevent instantiation of a class
why to avoid finalizers
the general contract for overriding equals and hashcode
to use accessor methods instead of public fields
to prefer lists to arrays
to favor generic types
to use enums instead of int constants
to return empty collections or arrays instead of null
to prefer for-each loops over traditional for loops
to avoid float and double if exact values are needed
beware of performance loss when doing string concatenation
refer to objects by their interfaces
Not to mention the other 40 or so topics that I can't remember off the top of my head. If you're able to talk about these topics and demonstrate an understanding, then there's no reason you shouldn't be in the running for the position since learning this material will already put you in the top 10 percentile of Java developers.
I have a little book with "Exam Cram" in its title. The emphasis is on "little;" you can read through it in a single evening. It describes in prose all the important features of Java, it contains tables of quick facts that will be essential, and it covers, briefly, all of the SCJP exam.
For the less than eidetic, it gives hints on how to write yourself a crib sheet from memory once the test starts (meaning you can carry a bit of what you need to know in short-term memory). There are also some tips on pitfalls and such. Finally, and perhaps most importantly, there is at least one practice exam in it (haven't looked at it in a while) and a bunch of drill questions.
Alas, from what I see at Amazon, the book I recommended only covers Java 2: http://www.amazon.com/Java-Exam-Cram-310-025/dp/1576102912 Still, there are bound to be similar, more timely books.
This one: http://www.amazon.com/SCJP-Certified-Programmer-Java-310-065/dp/0071591060/ref=sr_1_1?ie=UTF8&s=books&qid=1261601183&sr=1-1 by Kathy Sierra is similar and about Java 6.
Some people would (and in fact did) recommend buying a general purpose Java book. Ideally, all you should have to do is learn "all of Java" and then you'd be ready for the exam. But this is not a winning strategy!
related anecdote
The first time I took my driver's test with the DMV, I had a long wait and quickly skimmed through a little info booklet they provided. I easily scored 100.
I later had to take the test again. Knowing the test was laughably easy, I spurned the booklet and failed the theoretical test. The booklet didn't contain anything I didn't already know: But being by the same people who administer the test, it contained the same wording, it had similar situations from the test, it emphasized similar topics and situations. In a few minutes, it could easily make the difference between passing and failing.
Conclusion
Same approach with these "prepare for test XXX" books. It's a racket, but the special-purpose prep books work better at preparing you for the test than general knowledge (or general knowledge books) do.
May be you should read Eckel's Thinking in Java. In this book many specific java features are considered.
I would rather prepare my self to explain what I already know about java and how much I would like to work for that company.
While the programming knowledge is essential, I don't think you can improve much further in two weeks.
Better would be for you to prepare to answer, why do you want to work there, why should they hire you. etc.
My recommendation is HeadFirst Java to brush up on anything you may have some issues on. The SCJP is something that they may make you take upon hiring within 6 months for example, however I have never heard of a company making someone take a proctored test at an interview. Hope this helps.
Write something small but useful in Java, in whatever environment they're going to be using (applets, JSP, desktop...). That way you won't freeze trying to remember how to do e.g. console output.
If possible, show them some Java code you've obviously written and that you're proud of.
Brush up or learn the following :
Simple concepts e.g. static keyword, importance of equals and hashCode, etc.
How generics in java work?
How threading works, deadlock, race conditions, etc.
How java deals with memory. Can garbage collection be enforced?
Basics of OOP.
Basics of String, StringBuffer, StringBuilder, RuntimeException, etc.
Design patterns, e.g. singleton, visitor, etc.
If you want to know the language well then The Really Big Index will be invaluable. It's a huge list and you'd struggle to read it straight though but for a reference it's fantastic. Read it along with a set book like Thinking in Java or the SCJP book you've already ordered and you'll be absolutely fine.
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 6 years ago.
Improve this question
A friend of mine is talking about these design techniques regarding state transitions of an object (he's a Java guru, btw), performed without having a boolean myState member, but rather declaring the myState member as an object that implements the same interface of the "owner" one.
Ok, I've been too much cryptic, so you can find the discussion here, with code samples.
Personally, I'm excited with the approach, as my friend explained me the philosophy behind it; I also think it is pretty coherent, from a design perspective. My concerns, by the way, are mainly about performance and memory usage, as maybe compile-time and run-time optimizations enter the game. Since I don't know JIT compiler and JVM internals, I'm curious to have a wider opinion.
What do you think about?
I have to disagree - useful design patterns aside, this particular example is ridiculous overkill:
a 15-line class with an easily-understandable process
became a 50-line class with an obfuscated purpose
I fail to see how this is an improvement - it violates YAGNI1 and ASAP2, bloats the code, and reduces efficiency (multiple objects instantiated to do the job when not required).
As an intellectual exercise, mildly interesting. As a programming habit, terrifying! ;-)
1 YANGI = You Ain't Gonna Need It
2 ASAP = As Simple As Possible
It sounds like you're asking whether there's a "concretely useful" benefit to using the state design pattern, to which I'd say definitely yes, particularly if your app actually relies heavily on the "state" of its objects. A popular canonical example is that of the video player, which is always in one state, and can only transition into different states depending on the state it's currently in (e.g., it can't stop if it's already stopped, but it can play, and it can rewind, and so on).
While that particular example can be managed relatively easily with a handful of if/else/switch-type conditions (if (isStopped()), play(), etc.) because there aren't that many states to deal with, when states or their transition rules start to become more numerous or complicated, the state pattern absolutely becomes quite valuable, as without it, your code tends to accumulate elseifs like crazy, and things become less and less readable and manageable over time.
So yes, in general, if you find your objects' behaviors varying according to their states (if isStopped() play() / elseif isPlaying() stop() / elseif (isBroken() fix()), etc.), then yes, do consider using the State pattern. It's a bit more work up front, but generally well worth the effort, and done right I doubt you'd notice any significant overhead simply for the using of it.
Head First Design Patterns offers a great description of its hows and whys, too -- I highly recommend picking up that book to pretty much anyone writing object-oriented code.
The state/strategy patterns are tried and true. They aren't just an intellectual exercise. If you're thinking about performance and memory usage now...you are probably shooting yourself in the foot. Get code...then profile.
Its a very useful technique.
You don't propagate if(myState) statements around your class.
You can extend the technique to many states and only have to change what you assign to the myState member.
As for performance and memory usage, wait until you've got something to measure - maybe you need to try both ways and measure them both when they're working.
Yes, this works, and is cool ... up to a point (the point where it becomes silly). You can even use anonymous inner classes, if you must, even enums.