Why create new classes in R? - r

I know that you can create new classes in R, but why would you want to? I've thought of two reasons:
You can use the is. function to test whether an object belongs to a particular class (classifications of objects)
To only allow certain classes of entries into slots of an object (e.g., only a string for the surnmane and only a number for a zip code in the person class).
I haven't thought of situations where these benefits couldn't be achieved fairly easily by other means or when they'd really be useful.
I hope that this isn't too open ended and more concrete examples how one might use defining classes would be great. Thanks for any thoughts.

Its called Object-Oriented programming. Look it up, but in short:
Objects encapsulate behaviour - eg the behaviour of the 'print' method for a class is specific to that class. You can then keep the code for that method on that class separate from other code. You then only have to tell your users to "print" the thing - which is something they already do - and they get a nicely custom printed version of your thing, without having to use a special print function, like "printMyThing(thing)".
Objects inherit behaviour from their parent classes - eg the 'formula' method for the glm class falls back to the formula method for the lm class (not sure if this is true, but its just for illustration.
In short, its a Good Thing.

Related

How to calculate or at least constraint the value of tags defined in a stereotype?

To prioritize the engineering effort around Requirements in a defined way, I came to the idea to use the information already added for the FMEA.
For this I have created a Stereotype named fmeaRelevant (will be named better later), which has several Tags.
My target now is to first calculate the averages of all of these Tags and then from these the average RPN, which is again a Tag.
For this, I have already created a ConstrainedProperty, which has the respective ConstrainedParameters and Constraints.
BindingConnectors are created between the ConstrainedParameters of the ConstraintProperty and the Tags of the Stereotype.
The Stereotype is assigned to my Requirements.
However, Open ConstraintView ... does not show me anything.
Is there anything I am missing?
P.S.: I am not sure how to provide something like a source code snippet for model content. If one makes a proposal in a comment, I will add this.
You used binding connectors between stereotype properties and constraint parameters? That should not be possible, since they are on different levels. The parameters are on the model level and the stereotype properties are on the language level.
Of course, Rhapsody sometimes allows strange things, so it might well be, that there is a way to get it running.
The RPN relates to an actual hazordous situation. Your model describes this situation and all values relating to the situation should therefore be value properties of the FMEAitem. This element would be defined in a library. By the way, this is how it's done in the new RAAML-specification, which might be a useful reference.

OO design: should collections always be stored?

I've wondered this in many different situations, so here I come, looking for the experts knowledge.
Let's say I have to model something that requires a collection. A simple example: an application that stores famous quotes along with their author and a set of tags or keywords. The user should be able to enter a tag or keyword and get matching quotes for it.
My question is: do I really need a class that contains my collection of quotes? Something like this:
Or would this also be correct?
I'm asking this in the more abstract way possible (after all, UML should never depend on the implementation).
I've always thought the second example (just 1 class) was incorrect, but now I'm thinking that maybe the user can press a button on some interface and that button executes some code that gets a quote stored somewhere, and the second example would also be correct?
Basically, should I always have a collection stored somewhere, even if the storing class does nothing else but just store the collection (and provide the methods to access it)?
I definitelly prefer only one class, if there is no strong reason to have another container class (especially on abstract conceptual level). Then I add the collection methods as static functions. A separate container class would only bring more complexity, more dependencies and doubts like yours. :) Doubts often indicate the lack of a real need. When you really need something, you know it.
Here an example with some explanations. I find it simple, clear, elegant and abstract, meaning non-restrictive, easy to transform to any implementation you like:
When it comes to relationships of this class to other class, then you actially have your collection, without introducing new class. This diagram shows two examples. "Other class" actually sees a collection "quotes" which is ordered, like Vector. "One more class" also has a collection of Quotes with different characteristics.
Later on implementation level you can implement it directly like this or eventually adding a Factory or Container class, according to concrete, implemention resctrictions and special reqs.

Identify the ObjectFactory method used to create the object

Given an object o which was or could have been created with a JAXB ObjectFactory, what's the best way to find the method in that ObjectFactory which would be used to create the object?
My objective is to be able to generate Java code sufficient to recreate that object (ie one or more createXYZ statements).
Does the answer change if I commit to a specific JAXB implementation, say MOXy, for example?
Suppose I just know the object is from some JAXBContext (so one of several ObjectFactory classes could have been used to create it). Does this change the answer at all?
Where the object is a JAXBElement, #XmlElementDecl comes in to play. #XmlElementDecl can have a scope. My JAXB objects know their parent, so hopefully this matches scope.
I've written some proof of concept code which uses getGenericReturnType and getAnnotation(XmlElementDecl.class), to find the method, but I'm guessing there is likely to be stuff in one of the JAXB implementations, which could be re-used to do this more effectively/elegantly.

Best way to list files in a QFileSystemModel()?

I'm beginning Qt/pySide programming and am trying to implement a simple QListView with QFileSystemModel as the model. I have this working and in addition have defined a name filter on the model. I'd like to get a list of all files in the QListView (or rather the underlying model).
The following code appears to do this, but is incredibly ugly and cannot possibly be the correct way. Help!
model = myQListView.model()
idx = model.index(model.rootPath())
for i in range(0, model.rowCount(idx)):
child = idx.child(i, idx.column())
print model.fileName(child)
That is the correct way of working. The whole idea of the QAbstractItemModel abstraction is to provide a unified API for accessing arbitrary and possibly dynamic data which happen to fit into a list, table or tree presentations. Because this API has to accomodate everything from a simple dummy list of a few strings to the contents of an address book, including the rich contact details, it is inherently complex. Depending on what you want to achieve, using a one-purpose tool might be better in your specific situation.
By the way, the QFileSystemModel is very dynamic in nature (the directory enumeration happens on a separate thread). You won't get meaningful data until the directoryLoaded signal is emited, you have to wait for it. If you are simply looking for a list of files to use in your code, using Python's native facilities might be easier.

What's best practice in this situation?

I was just writing a small asp.net web page to display a collection of objects by binding to a repeater, when this came to mind.
Basically the class I've created, let's call it 'Test', has a price property that's an integer data type (ignore the limitations of using this type, I'm just using it as an example). However I want to format this property so it displays a currency and the correct decimal places etc.
Is it best practice to have a function within the class that returns the formatted string for the object, or would it be better to have a function in the back end of my web form that operations on the object and returns the formatted string?
I've heard before that a class should contain all it's relative functions but I've also heard that presentation should be kept in the 'presentation layer' in my N-tier app.
What would be the best approach in my situation? (and apologies if I haven't explained this clearly enough!)
Thanks!
In my opinion, both options are valid from an OO point of view.
Since the value is a price (that just happens to have the wrong data type), it makes sense to put the formatting into the data class. It's not something that's specific to the web interface, and, if you develop a different kind of user interface, you are very likely to require this formatting again.
On the other hand, it's a presentation issue, so it also makes sense to put it into the presentation layer.
For general OOP stuff, the object should not be exposing implementation details. I choose to interpret this as "avoid setters and getters when possible".
In the context of your question, I suggest that you have a getPriceDisplay() method that returns a string containing the formatted price.
The actual implementation of the formatting is hidden in the implementation details. You could provide a generic function for formatting, use some backend call, or something else. Those details should make no difference to the consumer of the 'Test' object.
Though it's not an OOP approach, in my opinion, this is a good time for an extension method. Call it .ToCurrency() which has the format of the currency...this could be taken from the Web.Config file if you wanted.
Edit
To elaborate, I would simply call .ToString("your-format") (of course this could be as simple as .ToString("C") for your specific question) in the extension method. This allows you change the format throughout the UI in one place. I have found this to be very useful when dealing with DateTime formats in web applications.
Wouldn't .ToString("C"); do the job? This would be in the presentation layer I would imagine.

Resources