NSDictionary vs NsArray [closed] - objective-c

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 9 years ago.
Improve this question
I was asked in an interview what structure I would use to store 100 names: an NSDictionary or NSArray and why.
What is the best answer?
He said he wants to figure out whether a name exists.

This is an open ended question, and the interviewer is probably more interested in your thought process and the questions you ask. The short answer IMO is NSArray if you just need to enumerate objects. NSDictionary if you need to lookup objects by a key. And NSSet when you just need to check membership. Of course this all varies depending on the amount of items, and how they are being used. With 100 items, it is probably 6 one, 1/2 dozen the other. And more a matter of readability and understandability in the code.
Read this old, but not outdated article for an excellent look at the performance considerations of each collection. http://www.cocoawithlove.com/2008/08/nsarray-or-nsset-nsdictionary-or.html

I think this has some good explanations of the two even though it's more focused on the performance aspect https://stackoverflow.com/a/10545362/1415348
It's really ends up being more open ended b/c it depends on what you might want to do with the data.
And now that it's edited with more info about how they want to use it I'd agree with Hot Licks, NSSet would be best for that. It has the method containsObject to determine existence in the set. NSSet Class Reference

The best answer is to ask more questions to clarify the requirements.
What kind of names?
What languages and locales?
How will they be used?
Will they change or be static?
Is there a storage or performance concern?
And so on.
In all likelihood the goal was to find out how you think about design.
Even if they didn't answer further questions they probably expected at least some reasoning provided for using various data structures where you illustrate how they can be used and why they make sense.
I seriously doubt they expected a one word response

Related

Which documentation package more actively maintained: NaturalDocs or RoboDoc? [closed]

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 4 years ago.
Improve this question
I am documenting a small itcl project. Due to shortcomings in itcl support in doxygen, and the fact that Ruff! does not support itcl, I am left with NaturalDocs and RoboDoc as the leading candidates. However, I don't want to pick an unsupported system, and was wondering which is going to be there in the long term?
What will be there in the long term? Who knows! It depends on how much people use it, really, as with all open source code systems. It should be noted that both the tools you refer to are really slow developing at this point: they do what they do and need little significant change to keep on doing it.
As far as I can see, ROBODoc requires that you do pretty much all the annotation work yourself, whereas NaturalDocs will derive a bit more for you. Not very much though; in particular, you will have to write plenty of annotations on things whichever route you use. (I've no particular experience with either though; I tend to prefer to maintain documentation in a separate file with something like doctools but that's a very different approach. I've also done nasty custom things in the past; you really don't want to use them.)

If I'm the only developer on a project, do I still need to use encapsulation? [closed]

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 8 years ago.
Improve this question
I always hear that we need to encapsulate whenever we write object-oriented code. If I'm the only developer on a project, do I still need to use encapsulation?
One way to put an answer: Encapsulation, conceptually, exists for writing better, safer, less error-prone code. It doesn't exist, primarily, to facilitate teams working together on code (that might be a side effect, but that's not the purpose).
So the goods that encapsulation seeks to foster scale from one coder to many coders, and they are goods that do not really have to do with the number of coders, although those goods may find stronger expression the larger the project and teams are.
Encapsulation is there for a reason.
Someone has to maintain and manage your code after you are done, right? What if the project gets bigger and you get team members?
So, the answer is "yes", it is always best to use encapsulation whenever possible.
The fact you are asking this question makes me wonder you actually did not get the actual value of encapsulation as a means to reduce and thus deal with complexity.
My theoretical computer science professor used to tell me that in the end, if you think at the whole binary representation of a program, any program is just a number. Very big indeed but, only a number. And that is true, any other construct we use but 0 and 1 (i.e. C++, Java, Python, functional programming, object oriented programming, aspect oriented programming, etc..) is just because of the fact we need more abstract means to get the one number we need.

Ideal number of properties/methods in a class [closed]

Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 11 years ago.
Improve this question
What is the ideal number of properties and methods in a class? What considerations must be made in determining this?
There is no "ideal number of properties and methods" but there are the SOLID principles to which you should adhere if you want to have a good OO design.
But if you try to implement the Universe following the God Object anti-pattern the number is close to infinity.
The answer is, 42. It can be arbitrarily split between properties and methods.
Make some "private" because it's more intriguing when objects have something to hide.
This shouldn't be a question of numbers. A class should encapsulate a logical unit of code. You'll get a number of funny answers (one just popped in as I'm writing this ;) because it's kind of beside the point. If you have a concrete case however, you might want to put it into your question; there could be cases where a strange task may end up giving you a class with too many methods; that would probably be a sign of a design problem somewhere else.
There are really no ideal numbers. If a class is supposed to have hundreds of methods, and they logical behave to its domain, then use those methods.
Since good oop practices tends to maximize code reuse, then it's quite probable that a class can't reach a very large number of methods or properties without encounting the need to be splitted.
If you follow SOLID principles you are most likely to end up with the most appropriate number. The number of members of a type will differ a lot depending on the purpose of this type. There is no magic number available that will fit all the cases.
As few as possible, but no less, unless strictly necessary.

How many kinds of lock are there which can be used to design? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 6 years ago.
Improve this question
In a program, I can design lock to threads,lock to object or files.
Are there difference between these locks?
If yes,
how many kinds of lock are there which can be used to design?
Are there some tutorials to design locks in object-oriented programming?
As someone dss538 said, the "answer" really depends on the context of the question.
What exactly is it that you need to protect access to?
An piece of data or single object, items in a container of some sort, a piece of code, a system resource like a shared file?
Another really meaningful question is how frequently are you going to be accessing this "thing" and what are you going to do with it?
Different locking techniques tend to have different overheads and while a critical section or mutex may be perfectly fine for something that is rarely accesseed and rarely updated, if you're rarely updating it and frequently accessing it you might want to look at a reader writer lock. Another follow up question here is "should I be using spin-locks" here, and again the answer is it depends? What else is going on in the system and are you going to trade one kind of bottleneck (a coarse grained lock) for another?
A final question I like to ask is what are the alternatives to "locking" something, i.e. if I can "safely" take a copy of my protected thing and work with that copy "safely" (ideally without modifying it), it's certainly a lot easier than worrying about every place I might modify said "thing."
I apologize for the vague answer but a more specific question would help here.
I would encourage you to read up on concurrency prinicples and understand the traditional "Dining Philosopers" "Sleeping Barber" and perhaps even "Santa Clause" problems to understand more context of how these things work.
A lot of the content on Wikipedia is good, particularly 'Concurrency Control' is a useful starting reference point if you don't have a good OS / Concurrency book on your shelf.
The answer depends on the context of your question.
Are you obtaining locks from some API? Check the API documentation.
In general, though, I would say no. A lock is a lock. The fact that you are using it to protect a file or an object does not matter to the API. How you use it is up to you. You certainly could abstract away and have FileLocks and ObjectLocks if you wish, but that is up to you.

Best way to improve the database performance [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I was recently in an interview and i have been asked a question that was :
After one year of publishing your application the data in the database became massive , so what is the best way to optimize the DB performance in the DB side not in the coding side whether database is Oracle or SQL server ... i just want to know what is the best answer for this question ?
I can give you an answer, but can't guarantee that an interviewer would like it.
The best way to optimise the performance is to understand what your application does, and the data structures that the system provides. You must understand the business so that you can understand the data, and when you do that you'll know whether the SQL submitted to the system is "asking the correct question", and doing so in a way that makes sense for the data and it's distribution.
Furthermore, you should measure and document what the normal behaviour of the system is, and the cycles it might go through on a daily, weekly, monthly, quarterly and annual basis. You should be prepared to be able to quantify any deviation from normal performance.
You must understand the database technology itself. The concepts, the memory structures and processing, REDO, UNDO, index and table types, and maybe partitioning, parallelism, and RAC. The upsides and the downsides.
You must know SQL extremely well, and be completely up to date on its capabilities in your DB version, and any new ones now available. You must be able to read a raw execution plan straight from DBMS_XPlan(). Tracing query execution must be within your skill set.
You must understand query transformation and optimisation, the use of bind variables, and statistics.
If I had to choose only one of the above, it would be that you must have measured and documented historical performance, and be able to quantify deviations from it, because without that you will never know where to start.
I'm pretty sure the point of the question was to see how you deal with vague, overly broad questions. One thing you did that was pretty positive, was to seek out authoritative answers on SO. Don't know if that's going to help you now that the interview is done.
So - how do you respond to such a question? An "I have no way of knowing" is probably not the approach to take - even if it the correct answer.
Maybe something like, "I'm not entirely sure what you're asking - so let me try to understand with a couple of questions. Are we talking about query performance or update performance? Are there indexes to support the workload? What makes you feel optimization is necessary?"
I think it is as much about your approach to problem solving as any particular tech.
But, then on the other hand, maybe I'm wrong. Maybe the first answer is always "Index the hell out of it!" :-D
Interviewing is a nightmare, isn't it?