5 great programming techniques every developer should know

Posted Dreamer who

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了5 great programming techniques every developer should know相关的知识,希望对你有一定的参考价值。

    5 great programming techniques every developer should know

1 – Encapsulate ifs inside their methods, not outside them:

Bad example:

?

1

2

3

4

5

6

7

8

9

If (value > 500)

 

     applyDiscountForGreaterThan500();

 

else if (value > 1000)

 

     applyDiscountForGreaterThan1000();

 

Good example:

?

1

2

3

4

5

// encapsulated ifs in the methods

 

applyDiscountIfValueIsGreaterThan500();

 

applyDiscountIfValueIsGreaterThan1000();

2 – Stop a moment to think about the name of the package/class/method/variable

YES, names are EXTREMELY important.

With a self-describing name, it is very easy to find out what your code is doing.

For this reason, do not use the first name that pops into your head. Think about the best possible name that describes exactly for what your package, class, method, variable is responsible.

Avoid ambiguous names! If a bug occurs in this functionality, it will be very difficult to find it.

Examples of ambiguous names:

?

1

2

3

4

5

6

Double clientValue; // Value of the client, value of what?

 

void hide() // What does that hide?

 

class Historic // Historic of what? Historic for everything?

 //Where is the cohesion?

Specific names help A LOT!

3 – Get to know the shortcuts of your IDE

I am going to use Eclipse’s example. It’s very important to use the shortcuts so you can code two or three times faster.

Control + 3 = “Eclipse’s Google” – write “new class” for example.

Control + shift + F = format your code.

Control + 1 = do various actions. For example, invoke a method that does not exist yet and use the shortcut.

Alt + up = moves a line of code to the above line.

Control + shift + R = search any archive you want on your project.

Control + H = search any word you want on your project.

Alt + Shift + R = rename any package, class, method, variable and update all the references.

Configure Eclipse’s format. Change it from 80 characters per line to 120. Configure save actions too so you can have actions when you save your code on Eclipse. For example, we can use the command “organize imports” after each code save.

There are many other shortcuts, but remember, you need to use them until they get automatic for you. If you forgot to use the shortcut, undo what you did without using the shortcut and then, do the same thing using the shortcut, so you’ll never forget it. For example, if you are going to delete a code line and you forget to use the shortcut, just undo it and use the shortcut control + D.

More sources:

Eclipse Shortcuts

Netbeans Shortcuts

IntelliJ Shortcuts

4 – Encapsulate methods for what you need

Have you noticed a lot of code in your application that does the same thing?

Why not encapsulate a method for this?

For example:

?

1

2

3

4

5

6

7

Calendar c = Calendar.getInstance();

 

c.setTime(new Date());

 

c.add(Calendar.DATE, +1);

 

Date userDate = c.getTime();

It would be a lot easier and much clearer to encapsulate all this code in a separated class and method:

?

1

Date nextDay = DateUtils.getNextDay();

Consider if need to keep state of your object, if so, instantiate it!

Remember, the most important thing here, is to encapsulate your solution!

5 – Create the culture of Code Review in your company

Even if the company you work for doesn’t use a Code Review tool, ask your friend to take a look at your code.

Each developer has his own experiences. We can learn a lot by sharing them.

Many things that you may not have noticed in a simple Code Review could be uncovered in a peer Code Review. You will see your mistakes and learn from them!

There is more! You can get this FREE E-Book too!
No Bugs, No Stress – Create a Life-Changing Software Without Destroying Your Life

Keep up improving yourself!

 

 

https://nobugsproject.com/2017/04/07/5-great-programming-techniques-every-developer-should-know/

以上是关于5 great programming techniques every developer should know的主要内容,如果未能解决你的问题,请参考以下文章

Calculated padded input size per channel: (3 x 3). Kernel size: (5 x 5). Kernel size can‘t be greate

P2986 Great Cow Gathering G

A. Great Sequence哈希

History of the Great Game

iOS-Core-Animation-Advanced-Techniques

iOS-Core-Animation-Advanced-Techniques