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.
Related
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.
Is it better to use constructors with parameters to initialize the members of a class or do that indirectly using setter functions? I did not get a clear answer on this question so far. Any insights would be appreciated.
It is almost certainly a matter of design choice or sometimes style. I would consider a number of things:
Would constructing an object without setting the member variables leave the class in an invalid state? If so, you'll need your constructor to take valid values for those members. Otherwise, you should be able to provide useful defaults.
Would setting individual members of variables violate a class invariant? If so, they should not have their own setters.
Is the user of this class likely to want to modify individual members? If so, then setters may be appropriate.
Does it make conceptual sense to be able to change individual members of an object? I would argue that it makes no sense to be able to change a Person's date of birth. However, you could argue that changing a Person's name does make sense. It depends. Do you consider a Person whose name has changed to be a different Person in your system?
Can you group setters together to be more useful? Instead of Rectangle::setX(int) and Rectangle::setY(int), does Rectangle::setPosition(int,int) make more sense? Perhaps even Rectangle::setPosition(Point) is better.
In any case, a class whose full set of members are exposed through individual setters and getters is usually a code smell.
The one thing you want to avoid is having an object that isn't complete. If you can supply sensible defaults for every parameter then it's OK to skip setting them in the constructor, otherwise you really should put them there.
Naturally there's no reason you can't do both.
It depends. Certainly use constructor parameters where the object doesn't make sense without a valid class member - for example, scoped smart pointers, or an ofstream's filename.
explicit ofstream ( const char * filename, ios_base::openmode mode = ios_base::out );
Use additional parameters sparingly, and provide default values wherever possible.
However, don't add so many constructor parameters that the order becomes impossible to remember, or if there's a chance of dangerous confusion, such as this.
Person(std::string firstname, std::string lastname, std::string title, std::string occupation,
std::string address, std::string telephone, int age, std::string creditcard, time_t birthday)
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.
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 am about to start a simulation/modelling project.
Let's say I have a component of type A, characterised by a set of data (a parameter like temperature or pressure,a PDE and some boundary conditions,etc.) and a component of type B, characterised by a different set of data(different or same parameter, different PDE and boundary conditions). Let's also assume that the functions/methods that are going to be applied on each component are the same (a Galerkin method for example).
If I were to use an FP approach, each component would be broken down to data parts and the functions that would act upon the data in order to get the solution for the PDE. This approach seems simpler to me assuming that the parameters are constant. What if the parameters are not constant(for example, temperature increases suddenly and therefore cannot be immutable)?
What would be the best approach to tackle the problem of the mutability of parameters?
I come from a C++/Fortran background, plus I'm not a professional programmer, so correct me on anything that I've got wrong.
Just because something can change doesn't mean it can't be modelled with immutable data.
In OOish style, lets say you have something like the following:
a = some_obj.calculationA(some, arguments);
b = some_obj.calculationB(more, args);
return combine(a, b)
Obviously calculationA and calculationB depend on some_obj, and you're even manually threading some_obj through as inputs to both calculations. You're just not used to seeing that that's what you're doing because you think in terms of invoking a method on an object.
Translating halfway to Haskell in the most obvious way possible gives you something like:
let a = calculationA some_obj some arguments
b = calculationB some_obj more args
in combine a b
It's really not that much trouble to manually pass some_obj as an extra parameter to all the functions, since that's what you're doing in OO style anyway.
The big thing that's missing is that in OO style calculationA and calculationB might change some_obj, which might be used after this context returns as well. That's pretty obvious to address in functional style too:
let (a, next_obj) = calculationA some_obj some arguments
(b, last_obj) = calculationB next_obj more args
in (combine a b, last_obj)
The way I'm used to thinking of things, this is "really" what's going on in the OOP version anyway, from a theoretical point of view. Every mutable object accessible to a given piece of imperative code is "really" an extra input and an extra output, passed secretly and implicitly. If you think the functional style makes your programs too complicated because there are dozens of extra inputs and outputs all over the place, ask yourself if the program is really any less complicated when all that data flow is still there but obscured?
But this is where higher abstractions (such as monads, but they're not the only one) come to the rescue. It's best not to think of monads as somehow magically giving you mutable state. Instead, think of them as encapsulating patterns, so you don't have to manually write code like the above. When you use the State monad to get "stateful programming", all this threading of states through the inputs and outputs of functions is still going on, but it's done in a strictly regimented way, and functions where this is going on are labelled by the monadic type, so you know it's happening.
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.
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).