Author Topic: rate this code  (Read 8797 times)

0 Members and 1 Guest are viewing this topic.

Offline Live Wire

  • Knight
  • **
  • Posts: 189
  • Cookies: 4
  • Up on your Net
    • View Profile
Re: rate this code
« Reply #15 on: December 10, 2012, 08:34:55 am »
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

lol. i have a guy on my robotics team that codes like this.
"There is no right or wrong, there is only fun and boring."

Offline xzid

  • Knight
  • **
  • Posts: 329
  • Cookies: 41
    • View Profile
Re: rate this code
« Reply #16 on: December 10, 2012, 09:47:17 am »
use the fucking highlight, all of you.

Offline s3my0n

  • Knight
  • **
  • Posts: 276
  • Cookies: 58
    • View Profile
    • ::1
Re: rate this code
« Reply #17 on: December 10, 2012, 09:53:46 am »
use the fucking highlight, all of you.


« Last Edit: December 10, 2012, 09:54:10 am by s3my0n »
Easter egg in all *nix systems: E(){ E|E& };E

Offline lordarnoud

  • Peasant
  • *
  • Posts: 112
  • Cookies: 6
    • View Profile
Re: rate this code
« Reply #18 on: 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 :)
« Last Edit: December 10, 2012, 12:45:21 pm by lordarnoud »

Offline Deque

  • P.I.N.N.
  • Global Moderator
  • Overlord
  • *
  • Posts: 1203
  • Cookies: 518
  • Programmer, Malware Analyst
    • View Profile
Re: rate this code
« Reply #19 on: 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.

Offline bluechill

  • Cybermancer
  • Royal Highness
  • ****
  • Posts: 682
  • Cookies: 344
  • I am the existence in these walls
    • View Profile
Re: rate this code
« Reply #20 on: 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).
I have dreamed a dream, but now that dream has gone from me.  In its place now exists my own reality, a reality which I have created for myself by myself.

Offline Deque

  • P.I.N.N.
  • Global Moderator
  • Overlord
  • *
  • Posts: 1203
  • Cookies: 518
  • Programmer, Malware Analyst
    • View Profile
Re: rate this code
« Reply #21 on: 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:

Code: [Select]
//read file
code
code
code
//compute something
code
code
code
//write file
code
code

When they should do this (seperation into procedures):

Code: [Select]
readFile()
compute()
writeFile()

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

Another example are magic numbers:

Code: [Select]
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.

Code: [Select]
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.
« Last Edit: December 11, 2012, 09:20:46 am by Deque »

Offline lordarnoud

  • Peasant
  • *
  • Posts: 112
  • Cookies: 6
    • View Profile
Re: rate this code
« Reply #22 on: 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

Offline Deque

  • P.I.N.N.
  • Global Moderator
  • Overlord
  • *
  • Posts: 1203
  • Cookies: 518
  • Programmer, Malware Analyst
    • View Profile
Re: rate this code
« Reply #23 on: 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.

Offline lordarnoud

  • Peasant
  • *
  • Posts: 112
  • Cookies: 6
    • View Profile
Re: rate this code
« Reply #24 on: 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).