On code clarity and functionality [closed] - objective-c

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.
The following 2 methods are identical in terms of what they do. I wonder which of the following is preferred and why?
This looks cleaner from readability stand point, however i don't like the decks.playerDeck construct
-(void) playerMovedWithCard:(Card*) card {
[decks removeCard:card fromDeck:decks.playerDeck];
[decks addCard:card toDeck:decks.inplayDeck];
}
Definitely simpler, however the idea that card is removed or added seems to be lost (thinking of reader of the code)
-(void) playerMovedWithCard:(Card*) card {
[decks.playerDeck removeObject:card];
[decks.inplayDeck addObject:card];
}
I am leaning towards the first implementation, as in future the task of removeCard may be more involved then simply removing an object.
What do you think?

The first way is slightly easier to read, because your decks serves as a more meaningful object. However, passing decks.playerDeck and decks.inplayDeck is not ideal: removeFromInPlayDeck: and addToPlayerDeck: would be slightly better.
There is a definite advantage to the first way of doing it, though: you could add a moveCardFromDeck:toDeck: method to the class of your deck object, avoiding the need to pass the same card twice to two different methods.

It is better to even isolate the functionality of the decks object and remove any reference of decks.playerDeck and decks.inplayDeck from playerMovedWithCard. This way, you can change the class of decks without having to change any code in playerMovedWithCard.
For example:
- (void)playerMovedWithCard:(Card*)card
{
[decks moveToPlayerDeckTheCard:card];
// OR [decks moveToInPlayDeckTheCard:card];
}
This way, playerMovedWithCard is unaware of how cards are stored or even what happens when a card is moved.
You can change that according to what your app actually does, but the idea is to minimize any coupling between classes.

Related

Is Scala mixin really better than multiple C++ inheritance? [closed]

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 9 years ago.
People say C++ inheritance is evil, so Java 'fixed' this problem with interface.
But Scala introduced traits, they're... interface with partial implementation? Doesn't this brought multiple inheritance back?
Does it mean Scala guys think multiple inheritance is good? Or They have some critical differences I haven't noticed?
The worst part of multiple inheritance is diamond inheritance, where a subclass has two or more paths to the same parent somewhere up the chain. This creates ambiguity if implementations differ along the two paths (i.e. are overridden from the original implementation). In C++ the solution is particularly ugly: you embed both incompatible parent classes and have to specify when you call which implementation you want. This is confusing, creates extra work at every call site (or, more likely, forces you to override explicitly and state the one you want; this manual work is tedious and introduces a chance for error), and can lead to objects being larger than they ought to be.
Scala solves some but not all of the problems by limiting multiple inheritance to traits. Because traits have no constructors, the final class can linearize the inheritance tree, which is to say that even though two parents on a path back to a common super-parent nominally are both parents, one is the "correct" one, namely the one listed last. This scheme would leave broken half-initialized classes around if you could have (completely generic) constructors, but as it is, you don't have to embed the class twice, and at the use site you can ignore how much inheritance if any has happened. It does not, however, make it that much easier to reason about what will happen when you layer many traits on top of each other, and if you inherit from both B and C, you can't choose to take some of B's implementations and the some of C's.
So it's better in that it addresses some of the most serious criticisms of the C++ model. Whether it is better enough is a matter of taste; plenty of people even like the taste of C++'s multiple inheritance well enough to use it.

How do I name an object in the Business Layer that only does work? [closed]

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.
I've looked around and haven't found anything specific to my question but that's partially because I'm unsure how to phrase it.
I manage around 20 different C# .NET applications that all do relatively similar things.
I am working on consolidating the common code into a data, business logic, and presentation.
My question is related to the Business Logic layer.
I gather that a business/domain object is one that holds state and sometimes may perform related actions (if you take that approach).
But what would you call an object that is only working through a routine?
For example:
In the presentation layer a button event is fired.
The presentation layer points to this class and calls the "RunJob()"
method.
RunJob() does all the work it needs to do and then finishes. For
example, it may read a table and output it into a CSV (a lot of
these apps are data pushers). It may or may not use internal
fields/properties. These properties may be used to display data in
the interface or to create output.
Is there a name for this or is it just a bad pattern/bad OO in practice? I don't think this qualifies as a business object or helper. I've seen some other topics that hint it might be a "Service" object.
Thanks!
call it WorkerThread for now and see uncle bob's article on naming" http://www.objectmentor.com/resources/articles/Naming.pdf. then change the name to something reasonable.
your class is not necessarily a bad class. most entities usually do not have much behaviour, otoh some helper classes do not have much state.
The name of your objects depend on specifically what work they do. TableImporter and CsvExporter are good names for the tasks you described. The methods should also be appropriately named. It may be the case that you want to abstract an interface Runner and have a generic RunJob method to decouple your presentation and model layers, but it could be more clear and decoupled if you use a controller instead.

Are static methods really necessary for a programming language? [closed]

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.
Programming languages like C# or Java feature static methods, despite being heavily object oriented.
I'm aware that there are many cases where static methods are used for performance or convenience reasons, but i can't stop wondering if there exist actual coding problems which could not be solved without the use of static methods.
I think that some of the common cases which would be named here could be just "normal" methods, instead of being static, like:
main: The purpose of the main-method is the creation of the very first running thread of the program and starting it. So this might as well just an object derived from a Thread class
Loggers: Logger implementations often use static methods. I don't see the point in that as i might want to exchange a logger for another on with an identical interface
Math: Math functions really seem to be a perfect candidate for static methods at first sight, but there might be cases where you might want to exchange your math library transparently for another one (i.e. if you need more performance on the sin() function you might want to use an implementation with a faster, less precise algorithm if precision is not critical for your application)
Singletons: Are considered bad practice by many. If only one instance is necessary you might think about actually creating only one instance.
So, what might be cases where static methods are really absolutely needed?
IMO, Static methods are needed while defining factories to create objects of different sub types of a given type where the choice of sub type is dependent on the inputs to this static factory method and is hidden from the client.
Your Logger example actually falls under this category where the actual logger is decided based on the package/class it is needed (ofcourse the other factory methods on Logger take other parameters to decide on the appropriate Logger instance to be returned).

Isn't objective-c function parameter syntax weird? [closed]

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 11 years ago.
Consider the following method:
+(void) myMethod:(int)arg1 **argument2**(int)arg2 **argument3**(int) arg3;
See how the first argument, unlike the 2nd and 3rd, doesn't have a description, giving it an impression of bad symmetry. Also you would expect the extra typing will provide named argument as you pass it in, but you still have to pass them in the correct order.
Can anyone help me make sense of this?
You're missing : after argument2 and argument3
Also, first argument is named myMethod. By Apple's naming recommendation guide, you'd see the method should be named in the manner that identifies semantics of first argument.
EDIT:
check out this document Coding Guidelines - Naming Methods
Hopefully the response to this other question will help you make sense of what you see.
The logic behind this exists though hard to get used to.
regarding your first note, about the naming of the first param,
Apple encourage you to name your methods as follows:
+(void)myMethodWithArg1:(int)arg1 Arg2:(int)arg2 Arg3:(int)arg3;
thus making the name readable like a sentence in english
(my method had Arg1 of type int, Arg2 of type int, etc)
regarding the named params and the inability to change the order, that makes no sence to me either
and the comment above me is correct, you are missing those annoying : after the params
In addition, the syntax of ObjC has strong relation to that of Smalltalk (http://www.smalltalk.org/main/)
I'd encourage you to read on that and the relation between the two languages
hope this helps
The method name is supposed to described the first argument.
Like:
+ (void)updateUserWithId:id andAge:age
So that the whole expression gives sort of a natural sentence.

Is downcasting (i.e. casting to derived type) ALWAYS wrong? [closed]

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.
What is your perspective on downcasting? Is it ALWAYS wrong, or are there cases where it is acceptable, or even preferable or desired?
Is there some good measure/guideline we can give that tells us when downcasting is "evil", and when it's "ok"/"good"?
(I know a similar question exists, but that question spins out from a concrete case. I'd like to have it answered from a general design perspective.)
No, it's definitely not always wrong.
For example, suppose in C# you have an event handler - that gets given a sender parameter, representing the originator of the event. Now you might hook up that event handler to several buttons, but you know they're always buttons. It's reasonable to cast sender to Button within that code.
That's just one example - there are plenty of others. Sometimes it's just a way around a slightly awkward API, other times it comes out of not being able to express the type within the normal type system cleanly. For example, you might have a Dictionary<Type, object> appropriate encapsulated, with generic methods to add and retrieve values - where the value of an entry is of the type of the key. A cast is entirely natural here - you can see that it will always work, and it's giving more type safety to the rest of the system.
It's never an ideal solution and should be avoided wherever possible - unless the alternative would be worse. Sometimes, it cannot be avoided, e.g. pre-Generics Java's Standard API library had lots of classes (most prominently the collections) that required downcasting to be useful. And sometimes, changing the design to avoid the downcast would complicate it significantly, so that the downcast is the better solution.
An example for "legal" downcasting is Java pre 5.0 where you had to downcast container elements to their concrete type when accessing them. It was unavoidable in that context. This also shows the other side of the question though: if you need to downcast a lot in a given situation, it starts to be evil, so it is better to find another solution without downcasting. That resulted in the introduction of generics in Java 5.
John Vlissides analyzes this issue (aka "Type Laundering") a lot in his excellent book Pattern Hatching (practically a sequel to Design Patterns).