EvilZone

Programming and Scripting => Projects and Discussion => : eliaou December 08, 2012, 07:58:05 PM

: rate this code
: eliaou 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.

:
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)
: Re: rate this code
: Snayler December 08, 2012, 08:21:59 PM
:
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.
: Re: rate this code
: Kulverstukas December 08, 2012, 08:22:28 PM
1) By convention you don't use _ in method names - example of proper method name:
:
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:
:
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:
:
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:

:
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
: Re: rate this code
: eliaou December 08, 2012, 08:41:37 PM
btw sorry for the ignorance but i didnt get why there was an infinite loop
: Re: rate this code
: Snayler 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.
:
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.
: Re: rate this code
: eliaou 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
: Re: rate this code
: Kulverstukas 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.
: Re: rate this code
: Snayler 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:
:
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.
: Re: rate this code
: techb December 09, 2012, 03:07:43 AM
Yo dawg, I heard you like recursion.
: Re: rate this code
: Kulverstukas 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!
: Re: rate this code
: Lykos 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.
: Re: rate this code
: s3my0n December 09, 2012, 09:19:32 AM
This is the correct code:
: (python)
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)
: Re: rate this code
: Kulverstukas 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 :/
: Re: rate this code
: Snayler December 09, 2012, 05:24:22 PM
This is the correct code:
:
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().
: Re: rate this code
: techb 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:
:
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
: Re: rate this code
: Live Wire December 10, 2012, 08:34:55 AM
:
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

lol. i have a guy on my robotics team that codes like this.
: Re: rate this code
: xzid December 10, 2012, 09:47:17 AM
use the fucking highlight, all of you.
: Re: rate this code
: s3my0n December 10, 2012, 09:53:46 AM
use the fucking highlight, all of you.

(http://nyulocal.com/wp-content/uploads/2011/10/okay-face.jpg)
(http://www.youtube.com/watch?v=BWerj8FcprM)
: Re: rate this code
: lordarnoud December 10, 2012, 12:45:00 PM
Well not a note on the code itself, but maybe add some comments in your code? It's a good habit to develop :)
: Re: rate this code
: Deque December 10, 2012, 04:25:32 PM
Well not a note on the code itself, but maybe add some comments in your code? It's a good habit to develop :)

Best habit is to make the code that readable that it doesn't need any comments to be understood.
: Re: rate this code
: bluechill December 10, 2012, 09:18:03 PM
Best habit is to make the code that readable that it doesn't need any comments to be understood.

That's not possible to make it perfectly readable to the point of no comments needed :P (except for simple code).
: Re: rate this code
: Deque December 11, 2012, 09:08:28 AM
That's not possible to make it perfectly readable to the point of no comments needed :P (except for simple code).

Depends on the language. It is pretty hard to make ASM readable.  :P

However, what I am trying to say is: If you need a lot of comments to understand your code, think about a way to make it more readable instead. Comments are considered a code smell for a reason.
I.e. people tend to do something like that within a single method/function/procedure:

:
//read file
code
code
code
//compute something
code
code
code
//write file
code
code

When they should do this (seperation into procedures):

:
readFile()
compute()
writeFile()

The procedures get the name of the comments here, so these comments are not needed anymore.

Another example are magic numbers:

:
modus = 1 //sets modus to "encrypt"
[...]
if (modus == 3) //if modus is "decrypt"

What you should do instead is using enums or (if the language has no enums) constants.

:
modus = ENCRYPT
[...]
if (modus == DECRYPT)

I don't say you shouldn't write any documentation. That is very important too. But I say, look if you can change the code in a way, so that most of the comments are not necessary anymore.
: Re: rate this code
: lordarnoud December 11, 2012, 09:54:39 AM
Best habit is to make the code that readable that it doesn't need any comments to be understood.
Well I agree on naming the methods/parameters/objects/classes etc... in a way that shows what it does or what it represents. But it still is a good habit of explaining complex or unusual code. And in my opinion every method and class should be briefly explained. But that's just my opinion :). I would understand if someone decided not to add any comments to his code, it just would take other programmers a bit longer to find out how the code works/is structured, and for the programmer himself after he looks back on it after a couple of weeks.

But now this is not related to the topic anymore :P So back on topic. xD
: Re: rate this code
: Deque December 11, 2012, 10:11:32 AM
And in my opinion every method and class should be briefly explained.

That is documentation and should be done for every method/function that is public, therefore might be used by others. (I don't do it everytime, I have to admit)
Imho, documentation is enough in most cases.
: Re: rate this code
: lordarnoud December 11, 2012, 10:50:43 AM
Well yeah I know that is how you document the code but some people still overlook it when they are learning any form of code by themselves :) And I often only do the documentation as well unless I feel like doing extra work, or if I know that other programmers will see my code. Or just if I feel like I might need it for myself if I'm to look back at it later. It's just that now I'm making projects for college/internship and there is being hammered on the fact of structures like adaptability and documentation :) (I document private methods as well).