I intend to write this time focusing on things that even engineers with some experience tend to fall into, so I have not written anything super-natural such as [Do not write duplicate code].
**2017/03/16 It seems that it is often seen recently, so add one [Don't inherit it in the first place !!!] **
Inherit only if nothing can be done. Before that, consider other methods such as the strategy pattern and the composite pattern, but only if it makes sense to inherit. Basically, it is more scalable and test code is easier to apply if you do not inherit it.
Don't skip it with "Ah, inheritance. Yes, yes." No, really !!! Almost every engineer knows what [is-a] is. Because it's a concept that appears in all object thinking books.
However, in my experience, too many people neglect this concept.
[One frame at the site] Mr. A: "This class and the method of this class are exactly the same." Mr. B: "Ah, then if you make a parent class and put it together, you can reduce duplicate code." Me: "It's ridiculous !!!"
That's right.
No, I think it's a good starting point. However, if [is-a], which is the most important concept in inheritance, is omitted, it is absolutely useless. [is-a] is neither a decoration nor a beginner's word, it is a concept that should be used in the implementation. You can make a class that you don't understand because you neglect this.
But to be clear, [is-a] is too abstract and it is difficult to drop it at the implementation level, or there are individual differences ...
So I will introduce one specific method of judgment, so I hope you can refer to it.
Don't get me wrong, it's not using it, it's ready to use. Of course, you may not use it, so if you want to use it, please judge whether it is ready for use. If this is done, it can be said that it is an [is-a] relationship. So first, if you want to create a child class, see if it is in this state.
Protect this anyway! If you can't keep it, write a test! (Because I have to protect it) There are too many methods with ambiguous in and out. Moreover, it is too painful to repair this, I really want you to protect it. I can't even write a test. Seriously spicy Ah.
I don't have to write anything else, but please make it a method that can be answered when asked "What are the inputs and outputs of this method?"
Also, a little more concretely, it means that the following processing should not be done with one method. [Calculated something → Save the calculation result] It means to return the calculation result and save it at the caller.
This is also quite common. Why doesn't it stop even though it started?
It's not just start and stop, but there are methods that should be paired. Isn't there a class that starts from the constructor of the instance and stops from the outside?
Make your code consistent anyway.
The disclosure range is narrow, the restrictions are strong, and I want to be kind Make all Kaku methods private. After that, please publish only what you need. It's really hard to see if you publish something that is only used in the class. The property is readOnly first, and if it still doesn't work, write it.
All variables are const, and method arguments are also const. Limit everything that can be restricted.
Don't call the callback in a method that has some logic in it.
This is when you return the callback to the caller. It means that the part returned from the child to the parent is not entwined with the logic.
(Tweet) What was the name of the code that didn't write the logic and just passed the value? I had a name, but I forgot it, so please write it in the comments if you understand it.
**2017/03/16 I asked Hatebu to write something like "I don't know", so when I reviewed it, I thought "That's right", so I added it below. I was so angry at that time that I wrote a lot www **
For example Suppose you have calculation 1, calculation 2, and calculation 3. Suppose you want to return "Calculation 1 + Calculation 2 + Calculation 3" in the callback. Do it in one method
//Since it is a callback, it is returned asynchronously.
All calculation results{
return [Calculation 1+Calculation 2+Calculation 3]
}
not
Calculation 1 result{
return calculation 1
}
Calculation 2 result{
return calculation 2
}
Calculation 3 result{
return calculation 3
}
//Since it is a callback, it is returned asynchronously.
All calculation results{
return [Calculation 1 result+Calculation 2 result+Calculation 3 result]
}
It means to write.
Why you ask? Is it all about which one is easier to write test code? Callbacks are asynchronous, so it's hard to test if you put logic in it, right?
I'm exhausted, as it is written. In the code I saw, the parent instance in the [has-a] relationship was a singleton, and the child instance was plunging itself into the properties of the parent instance it got in the singleton.
You don't have to have a property that directly references the parent. This is a cross reference
There was something like this in the comment I saw the other day.
//Run on main thread
Seeing this, I muttered, "Why?" In a word
about it.
Here's a good example of this comment.
//Called in the main thread because there is drawing processing at the call destination.
The following is an example of a comment with thought
//I'm not sure, but when I called it in the main thread, it no longer crashes.
Looking at this comment, you can feel the level of the engineer and understand that it is okay to correct it, so anyone who knows the cause can correct it.
Why don't you see the following comments again?
//Run on main thread
You can't do anything unless you know why. If this happens, the comments will increase the amount of code that you don't understand.
Postscript August 16, 2016 I received a comment I wrote from a person at the previous site who saw this post, so I will introduce it here w
# koitaro 2015/09/14
# Thread.current[:request]Put the url in and see if it contains a step
#The judgment is divided, but since this is a so-called global variable, it is not good to write it like this.
#So I tried to refactor it somehow, but it's deeply nested and used.
#Therefore, I gave up because I couldn't improve it easily.
#Wait for the day when the hero appears
Recommended Posts