Author Topic: rate this code  (Read 8795 times)

0 Members and 1 Guest are viewing this topic.

Offline eliaou

  • Serf
  • *
  • Posts: 28
  • Cookies: 0
    • View Profile
rate this code
« on: December 08, 2012, 07:58:05 pm »
Hi i just started learning my first language, and i made an exercise function,  (i wont tell you what it does because i want you to juge the clarity of it)  could you rate it, and give me feedback please.

btw its in python.

Code: [Select]
def distance_from_zero(d):
    d = abs(d)
    if d != int and d != float:
        return "This isn't an integer or a float!"
    else:
        return distance_from_zero(d)
« Last Edit: December 08, 2012, 08:15:55 pm by eliaou »

Offline Snayler

  • Baron
  • ****
  • Posts: 812
  • Cookies: 135
    • View Profile
Re: rate this code
« Reply #1 on: December 08, 2012, 08:21:59 pm »
Code: [Select]
def distance_from_zero(d):
    d = abs(d)
    if d != int and d != float:
        return "This isn't an integer or a float!"
    else:
        return distance_from_zero(d)
Is it me or is this going to loop indefinitely? (In case "d" is a float or integer)
Shouldn't it be just "return d"? Otherwise the function will just call itself over and over again. Correct me if I'm wrong.

As for the clarity of it, it seems pretty clear to me. It's a function that checks the distance to zero of a given number "d", by converting it's value to the absolute value.
« Last Edit: December 08, 2012, 08:26:00 pm by Snayler »

Offline Kulverstukas

  • Administrator
  • Zeus
  • *
  • Posts: 6627
  • Cookies: 542
  • Fascist dictator
    • View Profile
    • My blog
Re: rate this code
« Reply #2 on: December 08, 2012, 08:22:28 pm »
1) By convention you don't use _ in method names - example of proper method name:
Code: [Select]
calculateDistanceFromZero()2) By convention you don't use 1 letter names for variable (unless it's a one-time use variable and not a script-wise variable, in that case it's allowed (can't remember correctly)) - example of proper variable naming:
Code: [Select]
distance_from_zero(length):3) Don't know if this is in the convention, but grouping compares in IF is a good way to clear the code, so example:
Code: [Select]
if (d != int) or (d != float):4) You must have only 1 return statement at the END of your code block. The way you did is a bad way.
5) When returning something from a method it should be of the same type for every case, not like it is now (a string if it's like this and some number if it's like that)
6) Why dafuq are you calling the method from itself? I dunno, it might be locking up in an infinite loop. If not then it sure is a bad way to return something.

So anyway, this block is trash and not acceptable. Sorry.

Proper way to do it would be like this:

Code: [Select]
def calculateDistanceFromZero(length):
    newLen = abs(length)
    if (newLen != int) or (newLen != float):
        newLen = "This isn't an integer or a float!"  # yeah yeah...
    return newLen

Offline eliaou

  • Serf
  • *
  • Posts: 28
  • Cookies: 0
    • View Profile
Re: rate this code
« Reply #3 on: December 08, 2012, 08:41:37 pm »
btw sorry for the ignorance but i didnt get why there was an infinite loop

Offline Snayler

  • Baron
  • ****
  • Posts: 812
  • Cookies: 135
    • View Profile
Re: rate this code
« Reply #4 on: December 08, 2012, 08:43:34 pm »
There are some cases in which a function is allowed to call itself: Recursive Algorithms.
Example:
You want to multiply a times b without using the * operator.
Code: [Select]
def RecursiveMultiplication(a, b):
    if b==1:
        return a
    else:
        return a + RecursiveMultiplication(a, b-1)
I know it may be hard to understand at a first glance, but try some random values in a and b and run the algorithm in your head. You'll understand what it's doing.

EDIT: Please try not to double-post. Use the Modify button instead, like I did just now.
« Last Edit: December 08, 2012, 08:46:20 pm by Snayler »

Offline eliaou

  • Serf
  • *
  • Posts: 28
  • Cookies: 0
    • View Profile
Re: rate this code
« Reply #5 on: December 08, 2012, 09:14:55 pm »
ok i got the idea but not the code.

its like doing a+a+a+....


AHHH ok i got it so the loop continiues till it reaches 1
« Last Edit: December 08, 2012, 11:03:25 pm by eliaou »

Offline Kulverstukas

  • Administrator
  • Zeus
  • *
  • Posts: 6627
  • Cookies: 542
  • Fascist dictator
    • View Profile
    • My blog
Re: rate this code
« Reply #6 on: December 09, 2012, 12:13:19 am »
AHHH ok i got it so the loop continiues till it reaches 1
More information needed to help. I understand - nothing.

Offline Snayler

  • Baron
  • ****
  • Posts: 812
  • Cookies: 135
    • View Profile
Re: rate this code
« Reply #7 on: December 09, 2012, 02:13:13 am »
More information needed to help. I understand - nothing.
He's talking about the code I posted.
btw sorry for the ignorance but i didnt get why there was an infinite loop
Let me put it this way:
Code: [Select]
def distance_from_zero(d):You're defining a function named "distance_from_zero" which will work with a variable "d". By using "return distance_from_zero(d)", you're telling your function you want it to call itself. So the function will run again, which will create the infinite loop - the function keeps calling itself over and over again.
In your case, what you want to return, is the variable "d", which is what your function manipulates and therefore, must return as a result.
« Last Edit: December 09, 2012, 02:27:16 am by Snayler »

Offline techb

  • Soy Sauce Feeler
  • Global Moderator
  • King
  • *
  • Posts: 2350
  • Cookies: 345
  • Aliens do in fact wear hats.
    • View Profile
    • github
Re: rate this code
« Reply #8 on: December 09, 2012, 03:07:43 am »
Yo dawg, I heard you like recursion.
>>>import this
-----------------------------

Offline Kulverstukas

  • Administrator
  • Zeus
  • *
  • Posts: 6627
  • Cookies: 542
  • Fascist dictator
    • View Profile
    • My blog
Re: rate this code
« Reply #9 on: December 09, 2012, 08:17:42 am »
Yo dawg, I heard you like recursion.
So I put a method in yo method so you can call while you call!

Offline Lykos

  • Serf
  • *
  • Posts: 26
  • Cookies: 0
    • View Profile
Re: rate this code
« Reply #10 on: December 09, 2012, 08:29:20 am »
This took me a little bit of looking at it to figure out why it would be looping infinitely... then I saw it.


Then my mind broke. 


Definitely should just be "return d", as these other fine people have said.

Offline s3my0n

  • Knight
  • **
  • Posts: 276
  • Cookies: 58
    • View Profile
    • ::1
Re: rate this code
« Reply #11 on: December 09, 2012, 09:19:32 am »
This is the correct code:
Code: (python) [Select]
def distance_from_zero(d):
    if (type(d) != int) and (type(d) != float):
        return "This isn't an integer or a float!"
    else:
        return abs(d)
« Last Edit: December 10, 2012, 09:54:35 am by s3my0n »
Easter egg in all *nix systems: E(){ E|E& };E

Offline Kulverstukas

  • Administrator
  • Zeus
  • *
  • Posts: 6627
  • Cookies: 542
  • Fascist dictator
    • View Profile
    • My blog
Re: rate this code
« Reply #12 on: December 09, 2012, 01:00:39 pm »
This is the correct code:
Indeed, thank you for fixing it. OP clearly didn't even run his original code :/

Offline Snayler

  • Baron
  • ****
  • Posts: 812
  • Cookies: 135
    • View Profile
Re: rate this code
« Reply #13 on: December 09, 2012, 05:24:22 pm »
This is the correct code:
Code: [Select]
def distance_from_zero(d):
    if (type(d) != int) and (type(d) != float):
        return "This isn't an integer or a float!"
    else:
        return abs(d)
Nicely written! +1
I actually forgot about type().

Offline techb

  • Soy Sauce Feeler
  • Global Moderator
  • King
  • *
  • Posts: 2350
  • Cookies: 345
  • Aliens do in fact wear hats.
    • View Profile
    • github
Re: rate this code
« Reply #14 on: December 10, 2012, 05:33:09 am »
For one, there is always more ways than one way to skin a cat. OP's code could be re-written many many times over. The point is that op learns what has been pointed out.


Here is another way to skin the bitch:
Code: [Select]
import sys


def distFrmLeZero(foo):
    bar = ""
    try:
        #PHP wud need le triple = for diz nigga
        if (type(foo) == float) or (type(foo) == int):
            bar += "Yo dawg, yo numberz will equal somethim liek diz %d" % abs(foo)
        else:
            bar += "Yo dawg, pick a diff number yo.\n"
    except:
        print "Sompin evil happens yo."
        sys.exit()
    return bar
« Last Edit: December 10, 2012, 05:33:28 am by techb »
>>>import this
-----------------------------