I'm writing a short module in Fortran 90/2003 that provides a simple and user friendly interface for counting time between different parts of the execution of a program. Inspired by the tic, tac commands in Matlab, the idea is that the user uses the module in a program as follows:
program test
use Timer
call Tic("timername")
! some heavy stuff
call Tac("timername")
end program test
Now, I know how I can achieve that result with Fortran intrinsics. My question is how I should do it. I'm doing this to learn good design practices, rather than about Fortran syntax.
I have defined a user defined variable called Timer, which is the main object that I use to implement the functionality. However there are (at least) two different ways to use this object to let the user using several timers:
a) I can make the user defined variable Timer public, and then force the user to create timers object by hand. The user has to create as many timers as he needs, and then use methods to handle them.
b) I can hide this type by making it private. Then, to store different timers, I create an array of Timer objects in the module as a global variable, although private for the module, and every time the user calls the subroutine Tic, a new timer is defined in this array. In the example above, the user is using a module implemented following the latter approach (note that the program does not use keyword type at all).
Although the two options work technically (I have already implemented both) each one has advantages and caveats, and the rules I know about software design somehow collide. I wonder which is the best approach from a "orthodox" point of view.
Option a) has the advantage of following better the OOP: the user creates explicitly and object and operates with it. It does not use any global variable.
Option b) has the advantage of being more strongly "encapsulated". By this I mean that the user does not even need to know what a Timer is, and even about the existence of it. Further, the interface provided to interact with Timer objects is just a simple string, making the whole module more opaque for the user, which does not need to define Timer variables on purpose. He/she just uses two subroutines provided by the module that accept a string as input. That's all. The problem is that I have the feeling that this design, based on an array defined for the whole module, goes against the rule of avoiding global variables. It's not a real global variable, since it is private, but still.
So having this two options, which should I go for to produce the most orthodox approach?
PS: maybe there is a third option, which allows the user to create objects indirectly without having access the user defined type (i.e. not just define elements in an existing array, as done in the solution b). I don't know if this is possible at all to create variables at run time. Any idea in this direction is also very welcome.
Usually, a good design is to hide the details of the implementation to the user. This is encapsulation.
This means you have some "object", you don't expose the details about its internal state, but only some ways how to use such object.
1. Module as object
In Fortran 90/95 the OOP was somewhat limited. One approach was to have a module which was this "object". The module variables was the internal state and the module procedures implemented the functionality and used the module's internal state. In this design you do not expose the module variables if not necessary. The problem is that you can always have just one instance of the object - the module.
This would be:
use Timers, only: Tic, Tac
call Tic()
! some heavy stuff
call Tac()
2. Derived type as object
Another classical way is to have a derived type, which contains the state in its components. In that case you can have multiple instances of the "object" - multiple variables with the type of the object. When you do any operation with the object, you call a module procedure from the module which defines the object and you always pass the object instance as an argument - often as the first one.
use Timers, only: Timer, Tic, Tac
type(Timer) :: t
call Tic(t)
! some heavy stuff
call Tac(t)
Your question’s code
use Timers, only: Tic, Tac
call Tic("timername")
! some heavy stuff
call Tac("timername")
or some variation like
use Timers, only: Tic, Tac
call Tic(1)
! some heavy stuff
call Tac(1)
is functionally similar, but strange. Why should the module which implements the functionality store also the state? Can't there be a clash when using this module from more places? I would definitely let the user to create the instances himself.
3. Fortran 2003
In this very simple example Fortran 2003 does not change that much, if you already expose the type. Again, the state is in the components of the derived type. But you can bind the procedures, which work with the type, to this type directly and you don't have to import them separately. You just use the type and every functionality, overloaded operators and similar, comes with it:
use Timers, only: Timer
type(Timer) :: t
call t%tic()
! some heavy stuff
call t%tac()
You can see that the most modern approach definitely exposes the Timer type to the user.
When you do expose the type, you can make the components private and use only the constructor and other associated procedures (optionally type-bound) to manipulate them (getters/setters and others).
Though I don't know much about OOP, I guess that there is nothing like "the most orthodox approach" (because Fortran allows OOP but does not enforce it). The choice also seems to depend on the need for creating multiple instances of Timer with the same character string (e.g., in parallel run?). In this case the option (a) may be more conveinent, while the option (b) seems more handy otherwise. Merging the two approaches is also possible by allowing the explicit creation of Timer objects by the user, while providing handy tic()/toc() routines that automatically create/manipulate necessary objects within a module.
Yes, data encapsulation and hiding are considered to be good practices in software design. We can achive that in Fortran by creating a derived type such that an instance of the type (the object) is opaque. Consider the following module
module SomeModule
implicit none
private
public :: SomeType
type SomeType
private
integer :: n
real :: x
end type SomeType
end module SomeModule
Note that SomeType is declared as public whereas the contents of the type are private. Now, when I can create an object of type SomeType
use SomeModule, only: SomeType
type(SomeType) :: st
the object st is opaque - I can create it, pass it around, but can not access its contents. The only way I can edit the contents of st is through a routine contain-ed in the module SomeModule.
I have a more concrete example here.
Related
I am currently working on improving the design of an insurance quote engine. There is no design documentation but it involves passing in a 'QuoteData' class object into almost every sub, and because of that design choice the object has to have a number of its variables 'reset' so that it doesn't accidentally carry over into other scheme-specific code blocks.
After processing its input data it calls out to each scheme engine block, for example:
PcEngine:
Call Engine201611Pc71(pobjQuote)
Engine201611Pc71:
Call Scheme1(pobjQuote)
Scheme1:
Call AreaRating(pobjQuote)
Call VehicleGroupRating(pobjQuote)
Call CheckOutEachDriver(pobjQuote)
It should be noted there are several scheme subs, each with over a dozen other sub calls related to its scheme rules.
Ideally I would want to have a QuoteData instance for each scheme quote but we're reusing the same one for everything; that's thousands of scheme quotes with just one instance of QuoteData.
How would I go about refactoring? Originally it was a struct being passed ByRef everywhere (with 1000+ members)!
I understand what public/protected/private accessors mean in Java or PHP for instance. However, when would you choose whether to make a method private?
Imagine I have a class that handles configuration strings - they must conform to a particular regular expression, and if so, further logic is performed to make sure the strings are valid.
I currently have this code in a private method in a Configuration class. This class accepts configuration strings and then returns values to client code after validating the strings.
However, I want to unit test the validation code, so perhaps it should be in another class. I typically don't do this though unless I know that the code will be reused. If it will only be used by a single class as in this case, I normally just make the method private.
So, my question is - what design rules should inform a programmer that a particular method should be private compared to being moved into its own class?
The Single Responsibility Principle is what I usually have in mind. Also, consider if you really need the validation in this class or if it does not have anything to do with it (perhaps the validation should not be handled in the domain logic but another layer above it).
Private methods, as you probably already know should not be tested in unit tests so if you really need to test this kind of functionality perhaps you should put it in its own validation class, responsible for validation only and then test it.
Then if function i use only local in object and i don't want show to other objects her because i can use her in future and make mistake and this will do some mess in my code i don't must thinking a lot to think what function i must use and what i can't use.
I using private method everywhere and doing some simple and short public methods to get/set data to my objects then i don't have mess in my code.
You should use private method when you are refactoring your code in class.
for example if you have some peace of code that repeats more than one in code.
you should to make extract method refactoring.
look at more refactoring methods
refactor
Implement you validation logic as a strategy as in the strategy pattern. This way you can not only unit test them separately but also replace the validation logic later on easily if and when required.
So make a separate Validator class which implements IValidator interface. Then compose your Configuration class with the appropriate Validator by injecting it as a dependency in Configuration's constructor.
Leave the private validation methods in the same class, and make the unit test class a friend of that class (in C++, at least--in Java, put the unit test in the same package).
Read more about SOLID design principles
SOLID
If no special requirement, keep members as private. I think its main purpose is to make better "encapsulation" and maintenance.
For example, you define a Car.class with different steer mode. Car(.class) has a member maxSpeed, which is setted by a setter: Car.maxSpeedSet(int mode). then the user cannot directly know or change the value for maxSpeed, except by changing its mode through the method.
In this way, users don't need to care or write a function about how maxSpeed is got from the mode. And when you need to change the function: maxSpeed = f(mode), you don't have to change it everywhere the Car is used. you just change the method maxSpeedSett(). perfect for encapsulation and maintenance, isn't it?
if a member is only: x= a, 'public' seems good enough but make sure you will not change the method of assigning in the future, especially when there are too many dependance on the class.
I've always been taught that if you are doing something to an object, that should be an external thing, so one would Save(Class) rather than having the object save itself: Class.Save().
I've noticed that in the .Net libraries, it is common to have a class modify itself as with String.Format() or sort itself as with List.Sort().
My question is, in strict OOP is it appropriate to have a class which performs functions on itself when called to do so, or should such functions be external and called on an object of the class' type?
Great question. I have just recently reflected on a very similar issue and was eventually going to ask much the same thing here on SO.
In OOP textbooks, you sometimes see examples such as Dog.Bark(), or Person.SayHello(). I have come to the conclusion that those are bad examples. When you call those methods, you make a dog bark, or a person say hello. However, in the real world, you couldn't do this; a dog decides himself when it's going to bark. A person decides itself when it will say hello to someone. Therefore, these methods would more appropriately be modelled as events (where supported by the programming language).
You would e.g. have a function Attack(Dog), PlayWith(Dog), or Greet(Person) which would trigger the appropriate events.
Attack(dog) // triggers the Dog.Bark event
Greet(johnDoe) // triggers the Person.SaysHello event
As soon as you have more than one parameter, it won't be so easy deciding how to best write the code. Let's say I want to store a new item, say an integer, into a collection. There's many ways to formulate this; for example:
StoreInto(1, collection) // the "classic" procedural approach
1.StoreInto(collection) // possible in .NET with extension methods
Store(1).Into(collection) // possible by using state-keeping temporary objects
According to the thinking laid out above, the last variant would be the preferred one, because it doesn't force an object (the 1) to do something to itself. However, if you follow that programming style, it will soon become clear that this fluent interface-like code is quite verbose, and while it's easy to read, it can be tiring to write or even hard to remember the exact syntax.
P.S.: Concerning global functions: In the case of .NET (which you mentioned in your question), you don't have much choice, since the .NET languages do not provide for global functions. I think these would be technically possible with the CLI, but the languages disallow that feature. F# has global functions, but they can only be used from C# or VB.NET when they are packed into a module. I believe Java also doesn't have global functions.
I have come across scenarios where this lack is a pity (e.g. with fluent interface implementations). But generally, we're probably better off without global functions, as some developers might always fall back into old habits, and leave a procedural codebase for an OOP developer to maintain. Yikes.
Btw., in VB.NET, however, you can mimick global functions by using modules. Example:
Globals.vb:
Module Globals
Public Sub Save(ByVal obj As SomeClass)
...
End Sub
End Module
Demo.vb:
Imports Globals
...
Dim obj As SomeClass = ...
Save(obj)
I guess the answer is "It Depends"... for Persistence of an object I would side with having that behavior defined within a separate repository object. So with your Save() example I might have this:
repository.Save(class)
However with an Airplane object you may want the class to know how to fly with a method like so:
airplane.Fly()
This is one of the examples I've seen from Fowler about an aenemic data model. I don't think in this case you would want to have a separate service like this:
new airplaneService().Fly(airplane)
With static methods and extension methods it makes a ton of sense like in your List.Sort() example. So it depends on your usage pattens. You wouldn't want to have to new up an instance of a ListSorter class just to be able to sort a list like this:
new listSorter().Sort(list)
In strict OOP (Smalltalk or Ruby), all methods belong to an instance object or a class object. In "real" OOP (like C++ or C#), you will have static methods that essentially stand completely on their own.
Going back to strict OOP, I'm more familiar with Ruby, and Ruby has several "pairs" of methods that either return a modified copy or return the object in place -- a method ending with a ! indicates that the message modifies its receiver. For instance:
>> s = 'hello'
=> "hello"
>> s.reverse
=> "olleh"
>> s
=> "hello"
>> s.reverse!
=> "olleh"
>> s
=> "olleh"
The key is to find some middle ground between pure OOP and pure procedural that works for what you need to do. A Class should do only one thing (and do it well). Most of the time, that won't include saving itself to disk, but that doesn't mean Class shouldn't know how to serialize itself to a stream, for instance.
I'm not sure what distinction you seem to be drawing when you say "doing something to an object". In many if not most cases, the class itself is the best place to define its operations, as under "strict OOP" it is the only code that has access to internal state on which those operations depend (information hiding, encapsulation, ...).
That said, if you have an operation which applies to several otherwise unrelated types, then it might make sense for each type to expose an interface which lets the operation do most of the work in a more or less standard way. To tie it in to your example, several classes might implement an interface ISaveable which exposes a Save method on each. Individual Save methods take advantage of their access to internal class state, but given a collection of ISaveable instances, some external code could define an operation for saving them to a custom store of some kind without having to know the messy details.
It depends on what information is needed to do the work. If the work is unrelated to the class (mostly equivalently, can be made to work on virtually any class with a common interface), for example, std::sort, then make it a free function. If it must know the internals, make it a member function.
Edit: Another important consideration is performance. In-place sorting, for example, can be miles faster than returning a new, sorted, copy. This is why quicksort is faster than merge sort in the vast majority of cases, even though merge sort is theoretically faster, which is because quicksort can be performed in-place, whereas I've never heard of an in-place merge-sort. Just because it's technically possible to perform an operation within the class's public interface, doesn't mean that you actually should.
In my design I am using objects that evaluate a data record. The constructor is called with the data record and type of evaluation as parameters and then the constructor calls all of the object's code necessary to evaluate the record. This includes using the type of evaluation to find additional parameter-like data in a text file.
There are in the neighborhood of 250 unique evaluation types that use the same or similar code and unique parameters coming from the text file.
Some of these evaluations use different code so I benefit a lot from this model because I can use inheritance and polymorphism.
Once the object is created there isn't any need to execute additional code on the object (at least for now) and it is used more like a struct; its kept on a list and 3 properties are used later.
I think this design is the easiest to understand, code, and read.
A logical alternative I guess would be using functions that return score structs, but you can't inherit from methods so it would make it kind of sloppy imo.
I am using vb.net and these classes will be used in an asp.net web app as well as in a distributed app.
thanks for your input
Executing code in a constructor is OK; but having only properties with no methods might be a violation of the tell don't ask principle: perhaps instead those properties should be private, and the code which uses ("asks") those properties should become methods of the class (which you can invoke or "tell").
In general, putting code that does anything significant in the constructor a not such a good idea, because you'll eventually get hamstrung on the rigid constructor execution order when you subclass.
Constructors are best used for getting your object to a consistent state. "Real" work is best handled in instance methods. With the work implemented as a method, you gain:
separation of what you want to evaluate from when you want to evaluate it.
polymorphism (if using virtual methods)
the option to split up the work into logical pieces, implementing each piece as a concrete template method. These template methods can be overridden in subclasses, which provides for "do it mostly like my superclass, but do this bit differently".
In short, I'd use methods to implement the main computation. If you're concerned that an object will be created without it's evaluation method being called, you can use a factory to create the objects, which calls the evaluate method after construction. You get the safety of constructors, with the execution order flexibility of methods.
How do you decide between passing arguments to a method versus simply declaring them as object instance variables that are visible to all of the object's methods?
I prefer keeping instance variables in a list at the end of the Class, but this list gets longer as my program grows. I figure if a variable is passed often enough it should just be visible to all methods that need it, but then I wonder, "if everything is public there will be no need for passing anything at all!"
Since you're referring to instance variables, I'm assuming that you're working in an object-oriented language. To some degree, when to use instance variables, how to define their scope, and when to use local variables is subjective, but there are a couple of rules of thumb you can follow whenever creating your classes.
Instance variables are typically considered to be attributes of a class. Think of these as adjectives of the object that will be created from your class. If your instance data can be used to help describe the object, then it's probably safe to bet it's a good choice for instance data.
Local variables are used within the scope of methods to help them complete their work. Usually, a method should have a purpose of getting some data, returning some data, and/or proccessing/running an algorithm on some data. Sometimes, it helps to think of local variables as ways of helping a method get from beginning to end.
Instance variable scope is not just for security, but for encapsulation, as well. Don't assume that the "goal should be to keep all variables private." In cases of inheritance, making variables as protected is usually a good alternative. Rather than marking all instance data public, you create getters/setters for those that need to be accessed to the outside world. Don't make them all available - only the ones you need. This will come throughout the development lifecycle - it's hard to guess from the get go.
When it comes to passing data around a class, it's difficult to say what you're doing is good practice without seeing some code . Sometimes, operating directly on the instance data is fine; other times, it's not. In my opinion, this is something that comes with experience - you'll develop some intuition as your object-oriented thinking skills improve.
Mainly this depends on the lifetime of the data you store in the variable. If the data is only used during a computation, pass it as a parameter.
If the data is bound to the lifetime of the object use an instance variable.
When your list of variables gets too long, maybe it's a good point to think about refactoring some parts of the class into a new class.
In my opinion, instance variables are only necessary when the data will be used across calls.
Here's an example:
myCircle = myDrawing.drawCircle(center, radius);
Now lets imaging the myDrawing class uses 15 helper functions to create the myCircle object and each of those functions will need the center and the radius. They should still not be set as instance variables of the myDrawing class. Because they will never be needed again.
On the other hand, the myCircle class will need to store both the center and radius as instance variables.
myCircle.move(newCenter);
myCircle.resize(newRadius);
In order for the myCircle object to know what it's radius and center are when these new calls are made, they need to be stored as instance variables, not just passed to the functions that need them.
So basically, instance variables are a way to save the "state" of an object. If a variable is not necessary to know the state of an object, then it shouldn't be an instance variable.
And as for making everything public. It might make your life easier in the moment. But it will come back to haunt you. Pease don't.
IMHO:
If the variable forms part of the state of the instance, then it should be an instance variable - classinstance HAS-A instancevariable.
If I found myself passing something repeatedly into an instance's methods, or I found that I had a large number of instance variables I'd probably try and look at my design in case I'd missed something or made a bad abstraction somewhere.
Hope it helps
Of course it is easy to keep one big list of public variables in the class. But even intuitively, you can tell that this is not the way to go.
Define each variable right before you are going to use it. If a variable supports the function of a specific method, use it only in the scope of the method.
Also think about security, a public class variable is susceptible to unwanted changes from "outside" code. Your main goal should be to keep all variables private, and any variable which is not, should have a very good reason to be so.
About passing parameters all they way up the stack, this can get ugly very fast. A rule of thumb is to keep your method signatures clean and elegant. If you see many methods using the same data, decide either if it's important enough to be a class member, and if it's not, refactor your code to have it make more sense.
It boils down to common sense. Think exactly where and why you are declaring each new variable, what it's function should be, and from there make a decision regarding which scope it should live in.