7 things I want you to keep so that it doesn't become a fucking code

Preface

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 !!!] **

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.

Inheritance is is-a relationship

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.

"Are all the properties and methods used in the parent class available?"

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.

Clarify method inputs and outputs

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.

If there is a start, there is also a stop

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.

Narrow, strong and gentle

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.

Callbacks are not tied to logic.

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?

Cross-reference is absolutely useless

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

Don't try to write comments neatly. Put your thoughts into it! !! !!

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

Don't leave a comment that asks "why?" !!

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

7 things I want you to keep so that it doesn't become a fucking code
A collection of patterns that you want to be aware of so as not to complicate the code
A flowing interface? -I want to give you an opportunity to write good code. 3 [C # refactoring sample]
I want to use swipeback on a screen that uses XLPagerTabStrip
The story of Collectors.groupingBy that I want to keep for posterity
Is it possible to separate function calls and conditionals? --I want to give you a chance to write good code. 9 [C # refactoring sample]
Java had a QR code creation library called QRGen that wrapped ZXing nicely, so I tried it.
Glassfish tuning list that I want to keep for the time being
I want to summarize Apache Wicket 8 because it is a good idea
[Swift] If you want to use a URL that includes Japanese, use addingPercentEncoding.
I want to develop a web application!
I want to write a nice build.gradle
I want to write a unit test!
What to do when you want to delete a migration file that is "NO FILE"
[Android] I want to create a ViewPager that can be used for tutorials
I want to simply write a repeating string
I want to design a structured exception handling
[Ruby] I want to make a program that displays today's day of the week!
[MyBatis] I want to map a query query to a table that has a one-to-many relationship to a nested bean
I want to use PowerMock in a class that combines parameterized tests and ordinary tests
I want to implement it additionally while using kotlin on a site running Java
If hash [: a] [: b] [: c] = 0 in Ruby, I want you to extend it recursively even if the key does not exist.
I want to call a method of another class
I want to use NetBeans on Mac → I can use it!
Things to check when it doesn't work with proguard
I want to use a little icon in Rails
I want to monitor a specific file with WatchService
I want to define a function in Rails Console
firewalld went wrong, so I managed to fix it.
I want to add a reference type column later
I want to click a GoogleMap pin in RSpec
I want to connect to Heroku MySQL from a client
I want to create a generic annotation for a type
I want to add a delete function to the comment function
I want to write a loop that references an index with Java 8's Stream API
Easy Null Check-I want to give you a chance to write good code. 6 [C # refactoring sample]
A little worrisome code collection-I want to give you a chance to write good code. 2 [C # refactoring sample]
Complicated conditional branching-I want to give you a chance to write good code. 1 [C # refactoring sample]
Bool type handling-I want to give you a chance to write good code. 7 [C # refactoring sample]
[Docker] Is it good enough to call it a multi-stage build? → The story that became so good
I was inspired by the article for newcomers, so make a note of it so that you don't forget your original intentions.