开源日报 每天推荐一个 GitHub 优质开源项目和一篇精选英文科技或编程文章原文,坚持阅读《开源日报》,保持每日学习的好习惯。
今日推荐开源项目:《NTB nodetube》
今日推荐英文原文:《Tips to Improve Your Code Today》

今日推荐开源项目:《NTB nodetube》传送门:GitHub链接
推荐理由:如果有一天 youtube 开源了……那现在当然不可能,最起码现在只有梦里才会这样。既然没有那就造一个吧,这个项目就是开源的 youtube 替代品,因为借助了 Node.js 的力量,顺手也把名字合起来,就成为了 nodetube。作为开源替代自然提供了不少方便的功能——比如说更方便的下载等等,可以期待它今后的发展。

今日推荐英文原文:《Tips to Improve Your Code Today》作者:Dhananjay Trivedi
原文链接:https://medium.com/better-programming/tips-to-improve-your-code-today-2c70b6af7a76
推荐理由:新的一年,新的 tips

Tips to Improve Your Code Today

Next steps for beginners looking to become a pro in 2020

Coding is a skill that needs refinement, you are not born a programmer. It’s just people who put in those thousands of hours to improve upon that as a skill.

You just can’t expect to become a great programmer if you only code during your 9-to-5 job. If you are a guitarist and the only time you play is during the concerts, you won’t be able to find new ways of adding value to your band as a guitarist.

Similarly, you, as a programmer, always need to find ways of doing things better, I am sure that is why you are here at Better Programming, so let’s get to it.

Pair Programming

This one is really cool.

According to Wikipedia:

Pair programming is an agile software development technique in which two programmers work together at one workstation. One, the driver, writes code while the other, the observer or navigator, reviews each line of code as it is typed in. The two programmers switch roles frequently.

Your peer and you both can learn from each other, you might know a way to optimize things while your peer might now how to write cleaner code, this way, you both compliment each other’s skills, get your work done, and learn from each other. Isn’t that cool?

It might be awkward at first but it’s fun, but find someone you want to pair with, you don’t have to do it with everyone.

Build Projects

This is fun.

Build an app for everything!

Build Twitter, Instagram, Tinder, or even Medium, that's totally up to you.

This way, you will get to learn how to implement certain complex features and get a better understanding of how things are being done in real-world apps that we all love.

Try Out Different Programming Tools

Your programming language is a tool too.

Be open to trying out the hottest programming languages of your time.

Build the same app with the new programming language you are trying out, whether it be Ruby, Go, Python, or Clojure. This way, you experience the pros and cons of various languages and advocate for them when needed.

That will be your biggest strength as a good programmer and one you can develop over time.

Strive for Code Readability

As a beginner, this is something you should definitely aim for. It doesn’t require many skills and gives good returns.

We, as beginners, try to jump into writing the highest performing code even without knowing all the tools that will help us achieve the same.

It’s good that you intend to write the most efficient programs but writing cleaner code will help you boost your efficiency as a programmer and your team’s and it doesn’t require much effort.

Don’t abbreviate variable, function, or class names. Use long, descriptive names that clearly explain what you want out of the function.

The following are some tips that you can implement right now to improve your code.

Be Careful With Comments

int b = 5;
int c = 4;// I am sure you are reading about comments after a long time
// the more comments I write here,
// the longer it will take for you 
// to read this codeblock and make sense of it.
// BTW, it's just a simple addition.int a = b + c;
We tend to comment-out code at times, thinking: “I may come back and use it later” and, most of the time, you don’t come back. That code sits there to haunt its victims.

When somebody else is reading your code, that commented-out code comes out of nowhere and slows down the reader. The reader/programmer can’t delete it thinking that it's important which is why it's there.

If comments are misleading, that is even worse! To know more about this, here is an interesting story about comments which is worth your time.

Do Just One Thing Right

Here, read this function.
int addTwoNumbers(int a, int b) {
    displayToUser("Adding two numbers");
    setFontColorTo(Colors.RED);
    return a + b;
}
The function name is very innocent, addTwoNumbers, and when you read the name, what comes to mind?

It should just add two numbers, but when you see its implementation, it is doing a lot more than just adding two numbers which are hidden from the user at first.

This is a very common mistake we tend to make when writing our code. Functional programming teaches atomic functions which do one, and only one, thing.

This way, you are very clear with your code and don’t leave any room for bugs or surprises. Learn about SRP (Single-Responsibility Principle), it is the heart of cleaner code.

When you are striving for single responsibility, your functions will tend to be short which is exactly how long they should be!

Function Arguments

You know how long the function should be but what about the arguments?

The fewer the numbers, the better. Zero is ideal.

The higher number of arguments/parameters make your code inefficient in multiple ways:
  • Understanding the function takes longer.
  • Becomes harder to test.
  • More chances of messing things up.
  • Higher chances of violating SRP.
Sad but true.

Simplify Conditional Statements

You tell me, which one is easier to read?
if(person.age > 60 && person.salary > 5000 && person.score > 300 && person.score < 600 && person.retiredYear > 2014) {
    enrollForRetirementPension();
}
Vs.
if(isPersonEligibleForPension(person)) {
    enrollForRetirementPension();
}boolean isPersonEligibleForPension(Person person) {
    return (person.age > 60 && person.salary > 5000 && person.score > 300 && person.score < 600 && person.retiredYear > 2014);
}
You don’t really have to waste your time reading through the implementation if the function name is clear to you, hence, improving your efficiency and making your code very clean.

Hence, refactor your complex conditions into a function and give it a descriptive name. Clean and simple.

Declaration of Variables

Declare them as close to where they are being used.

Some of us (including ex-me) may have a practice of declaring all variables names at the top of our class/file, even though it might be used only in one place.

If you declare variables like that, then all the methods of your class have access to them and can modify the value, hence increasing the number of probable suspects for some bug.

Hence, just try to declare the variable exactly where they are being used.

That’s all folks!

From one of my favorites — Jim Kwik.
“Knowledge is not power, it’s potential power”

下载开源日报APP:https://openingsource.org/2579/
加入我们:https://openingsource.org/about/join/
关注我们:https://openingsource.org/about/love/