Do certain habits matter down the line,im guessing yes

Page 1 of 1

5 Replies - 443 Views - Last Post: 05 October 2018 - 10:03 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=412998&amp;s=2f8766ae3eaba512d0e8242344470860&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

#1 warrenb17

Reputation: 2
• Posts: 33
• Joined: 10-July 17

Do certain habits matter down the line,im guessing yes

Posted 05 October 2018 - 07:25 AM

just did an excercise

this was my code

```user_name = input("What is your name?")
user_age = int(input("What is your age?"))
turning_100 = 2018-user_age+100
print("You will turn 100 in the year", turning_100)
```

this was the solution

```name = input("What is your name: ")
age = int(input("How old are you: "))
year = str((2018 - age)+100)
print(name + " will be 100 years old in the year " + year)
```

are you better off casting or printing out variables the way i have? or does it really not matter?
also why is year = str((2018 - age)+100) written like that and my line here turning_100 = 2018-user_age+100
works exactly the same.

Is This A Good Question/Topic? 2

Replies To: Do certain habits matter down the line,im guessing yes

#2 DK3250

• Pythonian

Reputation: 462
• Posts: 1,467
• Joined: 27-December 13

Re: Do certain habits matter down the line,im guessing yes

Posted 05 October 2018 - 07:42 AM

There is almost always more than one way to write a program.
In your two examples it is more a matter of taste, - not syntax or performance.

Only, you don't use the 'user_name' for anything in the first code.

#3 baavgai

• Dreaming Coder

Reputation: 7288
• Posts: 15,169
• Joined: 16-October 07

Re: Do certain habits matter down the line,im guessing yes

Posted 05 October 2018 - 09:19 AM

Those are mostly stylistic differences. I wouldn't worry, either way. Your programming style will evolve over time as you figure out what works best for you.

I'd classify bad habits as structural, like globals or just too many bloody variables. These sort of habits are self correcting if you program long enough: you get better or you give up. A necessary, and rarely noted, quality in a programmer is a high tolerance for frustration. Anyone can write a program, it's debugging that is the real test.

There are as many ways to craft code as there are programmers. For the sake of argument, you could write the above as:
```name = input("What is your name: ")
print("{0} will be 100 years old in the year {1}".format(name, (2018 - int(input("How old are you: ")))+100))

```

I don't think I like it, but it does save a couple of variable declarations.

I actually don't like that the magic number 100 appears twice and would be tempted to go with:
```def show_future(target_age, name, age):
year = (2018 - age) + target_age
print("{0} will be {1} years old in the year {2}".format(name, target_age, year))

show_future(100, input("What is your name: "), int(input("How old are you: ")))

```

You could also parse out a calc_future... you get the idea.

#4 astonecipher

• Senior Systems Engineer

Reputation: 2680
• Posts: 10,715
• Joined: 03-December 12

Re: Do certain habits matter down the line,im guessing yes

Posted 05 October 2018 - 09:33 AM

```year = str((2018 - age)+100)
```

This is just good practice to enforce mathmatic rules and since it will be a string, might as well make it one.

If you took the same thing, but needed to change the formula,
```age = 12
year = (2018 - age)*100
print(year)
year = 2018 - age * 100
print(year)
```

Now we are way off.

#5 jon.kiparsky

• Beginner

Reputation: 11265
• Posts: 19,301
• Joined: 19-March 11

Re: Do certain habits matter down the line,im guessing yes

Posted 05 October 2018 - 10:00 AM

It's a good habit to be concerned about your habits If you want to expose yourself to some good habits, there are some well known books that you could take a look at. Clean Code by Robert Martin is one of them, Refactoring by Martin Fowler will expose you to a lot of good habits, and The Elements of Programming Style by (iirc) Kernighan and Plaugher is a classic.

In general, the guidelines for good habits are based in the observation that code is meant to communicate your intent. Things that communicate your intent, either to future-you or to other programmers who will read this, are good style.

The only specific style issue that I see here would be this:

```print(name + " will be 100 years old in the year " + year)

```

which would be more idiomatically done with string formatting, like this:

```print("%s will be 100 years old in the year %d" %(name, year))
```

or like this:

```print("{} will be 100 years old in the year {}".format(name, year))

```

#6 astonecipher

• Senior Systems Engineer

Reputation: 2680
• Posts: 10,715
• Joined: 03-December 12

Re: Do certain habits matter down the line,im guessing yes

Posted 05 October 2018 - 10:03 AM

+ for showing both the old, and new formatting styles.