Is Smart Code Smart?

Learn how to write smart code.

Improving code

When you’re a beginner programmer, you’re happy that your programs work at all, and you won’t pay much attention to what your code looks like or how it performs. The important thing is that you get the result you want on the screen.

But as you get more experienced and learn more, you’ll start to embrace what you might consider smart solutions. A smart solution, for you, might be that you can rewrite 10 lines of code so that it now is done in three.

The question you always should ask yourself is if the changes that are made to working code improve it in any way. Only if they do will the new code be considered smarter than it was before.

How to code in a smart way?

Imagine that you wrote a little game in Python. It has a loop that runs 10 times, and in each iteration, it will ask the user for a number, either 0 or 1. It will also randomly pick either a 0 or a 1. If the user guessed the same number that the computer picked, the user wins; otherwise, the user loses. The code might look like the one below. Enter 10 input values on separate lines respectively and then press “Run”:

Get hands-on with 1200+ tech skills courses.