|
They're primitives; like having bones; not really something you can obsolete.
It was only in wine that he laid down no limit for himself, but he did not allow himself to be confused by it.
― Confucian Analects: Rules of Confucius about his food
|
|
|
|
|
I guess the writer is correct in that specific scenario, but he has not addressed all the others.
|
|
|
|
|
Poorly considered, but it fits well within the current software engineering zeitgeist, so it makes sense that it would get written. That's the fancy way to say: "it's a fad, don't worry about it". And stop reading Uncle Bob.
- "it's not flexible", not if you consider adding an extra case to be a Big Deal, but the alternative is adding an entire new class.
- Even if you are of the opinion that creating new classes is somehow easier than creating new cases, then spend a little time thinking about what would happen when the function signature of that virtual function is changed, or if something about the API used by the subclasses to implement themselves changes. This has the annoying property that the fix you're about to write is highly non-local, you'd have to "hunt down" all the places that need to change - in the best case they correspond to compile errors.
- If a requirement is broken into N cases, but the code is spread out non-locally over N classes, your code does not look like the requirement which makes it harder to check whether it matches.
- "it's not SOLID", maybe, but SOLID is subjective and overrated.
- "horrific", let's not even talk about it.
- The presented "better way" presupposes that we have a convenient instance of the "class that represents a particular reason". How did we get that? Chances are that there's a switch hiding in a factory pattern or something. Moving the problem. And if there is some pattern such as
new AddressChanged().Update() , how about we don't wrap it in a class and just call a method that does that. - Article forgets to point out that in the first place the only domain that was considered is the business logic domain. Even if it makes sense there (which I don't agree with either), it won't make sense anywhere else. There's no way
Math.Min would have its if replaced by polymorphism.
|
|
|
|
|
swampwiz wrote: This article purports
First of course writers have different goals than programmers. Always keep that in mind when reading postings on the web.
From the link
"New requirements come along. Who would have thought? You were so sure nothing would happen"
Wrong, wrong, wrong.
Attempting to write code in case something might happen is a sure way to lead to code that is difficult to maintain.
If you already have requirements, even if it is just from a whiteboard, then your design and implementation should, of course, take that into account. But if you are claiming that you are writing your code to make it easy to add requirements that are unknown then you are at least ignorant of the challenges of maintaining legacy code. Especially if your claim is for something that might happen far in the future.
What actually ends up happening with that hubris is that code must be maintained for years or decades despite the fact that it serves no purpose. The complexity actually makes it harder, not easier, to add new features because it is more complex.
The best you can do now for such future possibilities is to write code that is easy to understand. And to make it clear how the code that you wrote now meets the requirements that you have now. Then that programmer 10 years from now who must add a feature that is actually needed then, will at least be able to understand what your code actually is doing and very likely needs to continue to do even with the new feature in place.
|
|
|
|
|
A keyword won't replace another.
Because of amount of 'features' available as new langages arrive.
Think from the core : Assembly ( or C ),
you can renew an adress , or the value, or call an adress, or a value,
so goto: leads to another 'adress' , It's go there/ do that ....
If statements are 'condition tests' area ,
you want to upgrade your style with a relevant idea,
throwing out the 'overload of logical' and the point is here : renew the keywords you use.
Like you'll have already your idea about the 'How To' :
one essential thing to get clues for relevance : measure / benchmarck / tests / and finally Compare your tries.
There are lots of 'equality' ,
if ( Evt 1 ){ ........... }{ .... }
so it's like :
while( Evt 1 ){
..............
}
while( ! Evt 1 ){
..............
}
will succeed same .. and commit the following .
It's about 'How many laps in loop' 'How many conditions to evaluate',
( or maybe a " loop/if handler " is required )
A code could use if() .. or other manner..
But in a very short and relevant time and process consumming ?
or for one hour over required work time ?
//////////
It's really 'one case' won't solve this questions',
benchmark will do !
benchmark will prove your choice as the best one.
There will never have deprecated keywords.
modified 23-Aug-21 21:01pm.
|
|
|
|
|
I can only suppose that your comment was intended for the original poster and not to me.
Member 14109043 wrote: There will never have deprecated keywords.
That might be specifically true for 'if' but in general that is not true.
|
|
|
|
|
I wouldn't label if statement as obsolete but I found dynamic polymorphism (hidden by the facade of SOLID mnemonic) pretty convenient thing in order for all OOD keywords to find their place. And indeed before entering the thread my guess was that author will make the case that if.. else.. violates the open-closed principle. And my guess was right. Although if I were the author I'd avoid such words as terrible, horrific etc
|
|
|
|
|
For example,
public void DoSomething(A a, B b, C c)
in which, many of properties of Object B b will be modified in DoSomething()
and the caller of DoSomething() will also use the changed Object B b
I would like to ask which of the following approaches is appropriate:
1) just use the B b after calling DoSomething()
public void DoSomething(A a, B b, C c)
2) Return the Object B to emphasize that B b being changed in DoSomething()
public B DoSomething(A a, B b, C c)
3) Or there is a different design of DoSomething() , that means the above method signature won't happan at all in OO world
|
|
|
|
|
I would go for the first choice. As long as the rest of your design is correct then other parts of the application will most likely rely on the b object being changed at this point.
|
|
|
|
|
You could make the method name more specific in this case: DoSomethingToB
It was only in wine that he laid down no limit for himself, but he did not allow himself to be confused by it.
― Confucian Analects: Rules of Confucius about his food
|
|
|
|
|
-sniff- -sniff-
I smell a homework or test question.
|
|
|
|
|
Not illegal.
Bastard Programmer from Hell
"If you just follow the bacon Eddy, wherever it leads you, then you won't have to think about politics." -- Some Bell.
|
|
|
|
|
Thanks for all your replys,
but I cannot change the method name as DoSomethingToB
for example, the following method will calculate total price and update it to repository,
but also update the Customer c , such as
--Customer.Points
--Customer.Role
depended on the amount he bought:
public void SetTotalPrice(ShoppingCart s, Customer c, List<Product> lp)
and the caller of the method will use Customer.Points and Customer.Role to do other calculation.
Therefore, I cannot change the method name because its purpose is about total price,
the updated of Customer c is a side effect
I think, in OO design, the above SetTotalPrice() method is not appropriate, isn't it?
|
|
|
|
|
codecs1 wrote: the following method will calculate total price and update it to repository, To be strictly OO a method should only have one job. In this case to set the total price. Updating the repository should be a different method.
|
|
|
|
|
Yes, In OO, one method should only have one job,
Assume SetTotalPrice() has just one job: set the total price.
However, at the same time, it also produces some values of Customer .
I think there is no reason to calculate the Customer one more time after SetTotalPrice() called.
And the caller of SetTotalPrice() just uses the updated Customer logically
Therefore, I hope to know how to handle such circumstance in OO practice, Thanks!!
modified 3-Apr-21 23:35pm.
|
|
|
|
|
codecs1 wrote: I think there is no reason to calculate the Customer one more time
Err...yes that actually is a problem.
You are suggesting that you should program to convenience rather than to design. This it same ideology that leads programmers to start making base classes for everything for functional reasons and ignoring the "is a" design principle (and leading to highly coupled software as well.)
There are other ways to handle redundant functionality versus attempting to insert it in methods for convenience.
|
|
|
|
|
This still seems a strange approach. Generally, it is good to make methods as functional as possible - in the sense that we should generally avoid updating parameters within methods.
In this case, the process being performed by 'SetTotalPrice' doesn't meet the requirement of being an expected result. The method describes setting the price, but not the fact that the operation affects the value of the customer.
Consider moving some of the behaviour into another method or class, representing the process as a whole.
If the whole thing is to process an Order for example, so 'SetTotalPrice' calculates the total price, then the part to deal with Points and Roles should reside elsewhere, and the process of updating them should be part of, for example, the ProcessOrder method that in turn calls SetTotalPrice.
"If you don't fail at least 90 percent of the time, you're not aiming high enough."
Alan Kay.
|
|
|
|
|
Quote: in which, many of properties of Object B b will be modified in DoSomething()
To be honest situations like this are the reason why people start bragging that OOP is a problem and you should stick to FP. I mean it's hard to reason about what the B will after it gets processed by DoSomething . The same way what A and C will become.
This is the reason why I'm a proponent of the second option but to return the copy of the input without changing the input itself. Although it seems more functional at least it tells "hey here's new B for you but in case you'll need original values you may use them as well".
Alternatively, you may go with the first option. IMO void return type tells "be prepared that input parameters may be changed unpredictably"
|
|
|
|
|
"IMO void return type tells "be prepared that input parameters may be changed unpredictably"
For methods, I'd normally expect a method returning nothing to update the object itself, not the other parameters.
"If you don't fail at least 90 percent of the time, you're not aiming high enough."
Alan Kay.
|
|
|
|
|
How about two objects acting as input parameters? Which of them do you expect to be updated?
|
|
|
|
|
Assume there is a method which return the query result List<Student>
List<Student> FindStudents(StudentCriteria sc); in which the StudentCriteria is a model for passing the search information:
public class StudentCriteria
{
public string Name { set; get; }
public int? Age { set; get; }
public DateTime? BeginRegisterDate { set; get; }
public DateTime? EndRegisterDate { set; get; }
}
Moveover, we should validate the input parameter StudentCriteria , so we call the following method in FindStudents(...)
ValidationResult Validate(StudentCriteria sc); in which ValidationResult is an object representing the validation result, something like that:
public class ValidationResult
{
public bool Success { set; get; }
public List<String> ErrorInfo { set; get; }
}
All 'Error' information will be contained in ValidationResult and return to FindStudents(...) if Validate(...) finds any problem in StudentCriteria sc :
such as Age is negative int, EndRegisterDate is before BegionRegisterDate...
But, how can FindStudents(...) return the ValidationResult to the caller?
-----Someone may consider that FindStudents(...) throws ArgumentException to the caller if StudentCriteria sc is invalid....
However, I think Exceptions are for exceptional circumstances
|
|
|
|
|
I see no point in "validation" when you're in the "data access / model layer"; all data should have been validated before that.
You return a list; with items or empty (if nothing found / returned). That's it.
(Database server issues are handled in their own layer.)
It was only in wine that he laid down no limit for himself, but he did not allow himself to be confused by it.
― Confucian Analects: Rules of Confucius about his food
|
|
|
|
|
For a start you are not validating the result, you are validating the criteria!
So
ValidateCriteria can return a string (empty if valid else the error info) OR a bool and the method itself informs the user of the error info.
Then if the Criteria is valid pass it to the database for search and if it returns an empty list then there is no such student.
Never underestimate the power of human stupidity -
RAH
I'm old. I know stuff - JSOP
|
|
|
|
|
public class StudentCriteria
{
public string Name { set; get; }
public int? Age { set; get; }
public DateTime? BeginRegisterDate { set; get; }
public DateTime? EndRegisterDate { set; get; }
public ValidationResult Validate ()
{
ValidationResult result = new ValidationResult();
if (Age > 0)
result.ErrorInfo.Add("Nope, too young.");
return result;
}
}
public class ValidationResult
{
public bool Success { get { return Convert.ToString(ErrorInfo).Length > 0; } }
public List<String> ErrorInfo { set; get; }
}
codecs1 wrote: All 'Error' information will be contained in ValidationResult and return to FindStudents No.
You may validate you search-criteria, but "FindStudents" should not be linked to that. Reading from a db has nothing to do with search-criteria. Decouple those. Before calling "FindStudents", confirm and validate your input, before passing to a database.
Bastard Programmer from Hell
"If you just follow the bacon Eddy, wherever it leads you, then you won't have to think about politics." -- Some Bell.
|
|
|
|
|
Thanks for all your replies!! I have gotten the idea that the caller must validate the data first.
But if it is a DLL static method for providing services to the upper presentation layer such as Winform, Webform, Asp.net Mvc, WCF or WPF:
public static List<Student> FindStudents(StudentCriteria sc); The presentation layer has no any idea or logic on the Student and also StudentCriteria .
It just calls the static service using the information such as age or Name to get List<Student>
If you are the developer of such service layer, how do you design the signature of the stateless
method FindStudents(...) ?
(i.e. For the service consumer, All he needs to know is just to call one service and get the data)
modified 14-Mar-21 21:23pm.
|
|
|
|