ugly/messy vs clean code

General discussion about LÖVE, Lua, game development, puns, and unicorns.
kingslovelua
Citizen
Posts: 71
Joined: Mon Sep 26, 2011 7:16 pm

ugly/messy vs clean code

Post by kingslovelua »

So around the forum I see people saying ugly code so what I want to know is ugly code just a way of saying the code is unreadable. because the code still run's.

this is something I made today. I want to know if this is considered ugly or clean

Code: Select all

io.write("Hello how are you beautiful I'm lovebot whats your name:\n ");

 g_Name = io.read(); --females name
io.write( g_Name," oh ",g_Name," sounds nice, do you mind if I buy you a drink yes or no: ");


local drink1 = io.read(); -- reply1 should only equal yes or no




if  drink1=="no"  then
 io.write("Oh good what would you like to drink\n  rum $7\n  wine $10\n gray goose $12 \n beer $2");
 elseif drink1=="yes" then
 io.write("come on girl you know you want to have a good time to night, so what would you like to drink\n");
 drink1 = "no", "yes";
end

io.write("rum $7\n wine $10\n gray goose $12 \n beer $2\n pick your drink:");

local drink2 = io.read();

while drink2 ~= "beer" do
 io.write(" oh my the pri.. I mean do you think there might be nother drink\n please pick another drink:");
 drink2 = io.read();
end

io.write("oh nice your a ", drink2, "type of woman like that.\n I see in the naer future me and you having a lot of fun,\n how about you ", g_Name," enter yes or no:");
local input_YesOrNo =io.read();

if input_YesOrNo=="yes" then
 io.write("I had a feeling you would say that you lets go ack to my place and sex it up");
 elseif input_YesOrNo=="no" then
 io.write("bitch fuck you give me back my drink bitches always trying to get free shit");
 input_YesOrNo = "yes", "no";
end
So far thats where my skills are this is me just playing around and making learning fun
User avatar
Kadoba
Party member
Posts: 399
Joined: Mon Jan 10, 2011 8:25 am
Location: Oklahoma

Re: ugly/messy vs clean code

Post by Kadoba »

Yes, when people say "clean code" they mean it in terms of readability.

I'd say your code is on the messy side. Here are some tips:
  • Keep your spacing consistent.
  • Use indention.
  • If a line becomes long then break it up into multiple lines.
  • Use either camelCase or under_scores to indicate spaces in variable names. Try not to use both and be consistent. (camelCase is more common)
  • I really recommend sticking all of your globals into a single table. There are multiple benefits to doing this.
  • Put reusable code into functions. Functions are cheap and easy to define in lua so take advantage of that.
  • Comments should add understanding to the code. The ones you had were pretty obvious so I removed them.
Also keep in mind while there are a lot of common agreements on what is clean code and what isn't, there are some things people don't agree on. The absolute most important thing is that you must be consistent. If you do things a certain way then try and do them that way all the time, although that isn't always possible.

Here's your code the way I would write it (I haven't tested it):

Code: Select all

    io.write("Hello. How are you, beautiful? I'm Lovebot. What's your name?:\n ")

    global = {}
    global.name = io.read()

    io.write( string.format("%s. Oh, %s sounds nice. Do you mind if I buy you a drink? Yes or no: ", 
                               global.name, global.name))

    local response = io.read()

    if  response=="no"  then
         io.write("Oh good! What would you like to drink?\n")
    elseif response=="yes" then
         io.write("Come on girl. You know you want to have a good time tonight " ..
                  "so what would you like to drink?\n")
    end

    local function listDrinks()
         io.write("Rum: $7\n Wine: $10\n Gray Goose: $12 \n Beer: $2\n")
         io.write("Pick a drink: ")
         return io.read()
    end

    local drink = listDrinks()

    while drink ~= "beer" do
         io.write("Oh my, look at that pri.. I mean do you think there might be another drink?\n" ..
                  "Please pick another drink.\n")
         drink = listDrinks()
    end

    io.write(string.format("Oh nice! You're a %s type of woman. I like that.\n I see you and me in the near future " ..
                         "having a lot of fun.\n How about you, %s? Enter yes or no: ", drink, global.name)

    response = io.read();

    if response=="yes" then
         io.write("I had a feeling you would say that. Let's go back to my place and sex it up.");
    elseif response=="no" then
         io.write("Bitch, fuck you! Give me back my drink. Bitches always trying to get free shit.");
    end
User avatar
miko
Party member
Posts: 410
Joined: Fri Nov 26, 2010 2:25 pm
Location: PL

Re: ugly/messy vs clean code

Post by miko »

kingslovelua wrote:I want to know if this is considered ugly or clean
It depends on those factors:
- do you use any coding style at all
- do you strictly follow choosen coding style
- is that coding style easily understandable by others

Read more at:
http://en.wikipedia.org/wiki/Programming_style
http://www.koonsolo.com/news/dewitters-tao-of-coding/
http://www.maultech.com/chrislott/resources/cstyle/
http://framework.zend.com/manual/en/cod ... style.html
http://drupal.org/coding-standards

Above examples are not for lua, but you can see how it works. So, choose your style (maybe looking at some examples first, and then deciding which you like the best), and then stick with it.
My lovely code lives at GitHub: http://github.com/miko/Love2d-samples
User avatar
kikito
Inner party member
Posts: 3153
Joined: Sat Oct 03, 2009 5:22 pm
Location: Madrid, Spain
Contact:

Re: ugly/messy vs clean code

Post by kikito »

kingslovelua wrote:So around the forum I see people saying ugly code so what I want to know is ugly code just a way of saying the code is unreadable. because the code still run's.
It's a matter of priorities.

The one thing you must have to understand is this: nowadays when one writes code, the main objective is not that the machine understands it (or, as if you say, that "it runs"). The main objective is that other people understand it.

"But I'm going to be the only one using this code" you may say. Well, take this into account: your future self is also other people.

"But this is a small piece of code, I'm going to use it once and then never use it again": First, you never know for sure. Second, if it's just a small piece of code, then it's a great opportunity to practice clean code! It's such a small code! It's perfect!

So, first you make it "understandable for humans". Then you fix the errors, adapt the syntax, add secondary functions, etc until the machine also understands it.

A little bit of history: This has not always been this way. In the ancient times, when machines had less than 1KB of memory, and had to be programmed with direct 1s and 0s, and they were expensive as hell, the most important thing was making code run, and then making it understandable. But in those days, machines were expensive and people were proportionally cheap (I'm taking about time and money, not just money - for example, compiling a program could take one night). But nowadays machines are much, much cheaper than people. That's, basically, why this change happened.
Last edited by kikito on Fri Nov 04, 2011 10:30 am, edited 1 time in total.
When I write def I mean function.
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: ugly/messy vs clean code

Post by Robin »

What the others have said, plus:
In Lua, terminating semi-colons are not necessary. I'd suggest not using them in general, it only adds line noise.
The comments for the first two io.read()s are information for the user, not for the programmer, so it'd be better to print them if you want the user to see that.

I'm not sure what you want with:

Code: Select all

input_YesOrNo = "yes", "no"
It assigns "yes" to input_YesOrNo, and then the program ends.
Help us help you: attach a .love.
kingslovelua
Citizen
Posts: 71
Joined: Mon Sep 26, 2011 7:16 pm

Re: ugly/messy vs clean code

Post by kingslovelua »

Kadoba

I'd say your code is on the messy side. Here are some tips:

Keep your spacing consistent.
Use indention.
If a line becomes long then break it up into multiple lines.
Use either camelCase or under_scores to indicate spaces in variable names. Try not to use both and be consistent. (camelCase is more common)
I really recommend sticking all of your globals into a single table. There are multiple benefits to doing this.
Put reusable code into functions. Functions are cheap and easy to define in lua so take advantage of that.
Comments should add understanding to the code. The ones you had were pretty obvious so I removed them.


Also keep in mind while there are a lot of common agreements on what is clean code and what isn't, there are some things people don't agree on. The absolute most important thing is that you must be consistent. If you do things a certain way then try and do them that way all the time, although that isn't always possible.

Here's your code the way I would write it (I haven't tested it):
well when I was trying to learn C# Visual Studio did all of the spacing and indenting for you, is there a setting In Scite that will help with spacing and indention. Are you saying it's not clean or good to us both cameICase and under_Scores in the same program at all.
Will with the Functions and globals I haven't really got to that point or full understand how to use them and how did you make a function with out declaring it.

my skills are limited right now im just getting the grasp of the basics. i was planing on for today to be functions and understating for loops more then a little bit of table because i really want to start seeing oop programing.
miko
It depends on those factors:
- do you use any coding style at all
- do you strictly follow choosen coding style
- is that coding style easily understandable by others

well for me as of now the answers would be no, no, and I don't know I'm going to look at the link to see what is what.
kikito
It's a matter of priorities.

The one thing you must have to understand is this: nowadays when one writes code, the main objective is not that the machine understands it (or, as if you say, that "it runs"). The main objective is that other people understand it.

"But I'm going to be the only one using this code" you may say. Well, take this into account: your future self is also other people.

"But this is a small piece of code, I'm going to use it once and then never use it again": First, you never know for sure. Second, if it's just a small piece of code, then it's a great opportunity to practice clean code! It's such a small code! It's perfect!

So, first you make it "understandable for humans". Then you fix the errors, adapt the syntax, add secondary functions, etc until the machine also understands it.

ok ok i'll try that method out to see if it works for me, humans over machines.
Robin
What the others have said, plus:
In Lua, terminating semi-colons are not necessary. I'd suggest not using them in general, it only adds line noise.
The comments for the first two io.read()s are information for the user, not for the programmer, so it'd be better to print them if you want the user to see that.

I'm not sure what you want with:

Code: Select all
input_YesOrNo = "yes", "no"


It assigns "yes" to input_YesOrNo, and then the program ends.

semi-colons is something i picked up from C# i saw that it work and so i kept on using them but I'll stop.
ok i guess i should write better comment because that was for the programer. can you give me an example of what you would write for comments in my little code.
and yes one of my friends point that out to me I guess i should do input_YesOrNo ="yes", input_YesOrNo = "no"
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: ugly/messy vs clean code

Post by Robin »

kingslovelua wrote:Are you saying it's not clean or good to us both cameICase and under_Scores in the same program at all.
If you are inconsistent with someVariable, some_variable, some_Variable, SOME_VARIABLE, etc., at some point you will get confused about what variable is spelled what way. Better just avoid it right away.
kingslovelua wrote:can you give me an example of what you would write for comments in my little code.
None, in your code at least. I myself usually only comment in relatively complex code, and explain why: what the purpose of the code is, what it should do.
kingslovelua wrote:and yes one of my friends point that out to me I guess i should do input_YesOrNo ="yes", input_YesOrNo = "no"
Except not. It is still pointless. Why do you think you need it?
Help us help you: attach a .love.
kingslovelua
Citizen
Posts: 71
Joined: Mon Sep 26, 2011 7:16 pm

Re: ugly/messy vs clean code

Post by kingslovelua »

Robin wrote:
kingslovelua wrote:Are you saying it's not clean or good to us both cameICase and under_Scores in the same program at all.
If you are inconsistent with someVariable, some_variable, some_Variable, SOME_VARIABLE, etc., at some point you will get confused about what variable is spelled what way. Better just avoid it right away.
kingslovelua wrote:can you give me an example of what you would write for comments in my little code.
None, in your code at least. I myself usually only comment in relatively complex code, and explain why: what the purpose of the code is, what it should do.
kingslovelua wrote:and yes one of my friends point that out to me I guess i should do input_YesOrNo ="yes", input_YesOrNo = "no"
Except not. It is still pointless. Why do you think you need it?

ok I'll just stick to one and if cameIcase is the most common I guess that the one I'll use seeing that the pil I thing it was said to try not to use _var because some words are token already.



now for the nput_YesOrNo ="yes","no" I did that because I did that in the first if statement I made, so I did it for the second if statement and if I had made other if statement I would of done it again.

don't I have to declare yes and no as two variables that can be a input.

With the first input I was playing around with it and couldn't find out why the code wouldn't work then I learned you need to declare your variables with "" so I did it still didn't work so I put the the var= yes under the if statement and it work then I remember in pil it said you could do var= 1, 2, 3 so I did just that

maybe me showing what I did would be better.

ex1
var = io.read()
var = yes;

if var == yes then
io.write("blah blah blah")
elseif var == no then
io.write("blah")
end
that didn't work


ex2
var = io.read()
var = "yes"

if var == yes then
io.write("blah blah blah")
elseif var == no then
io.write("blah")
end

didn't work

ex3
var = io.read()
var = "yes"

if var == "yes" then
io.write("blah blah blah")
elseif var == no then
io.write("blah")
end

still no go


ex4
var = io.read()

if var == "yes" then
io.write("blah blah blah")
elseif var == "no" then
io.write("blah")
var = "yes"
end

that worked but no didn't work

var = io.read()


ex5
if var == "yes" then
io.write("blah blah blah")
elseif var == "no" then
io.write("blah")
var = "yes", "no"
end

now everything worked they way I wanted it to
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: ugly/messy vs clean code

Post by Robin »

kingslovelua wrote:don't I have to declare yes and no as two variables that can be a input.
Nope. You don't have to do that. In fact, it doesn't do anything useful.
Help us help you: attach a .love.
kingslovelua
Citizen
Posts: 71
Joined: Mon Sep 26, 2011 7:16 pm

Re: ugly/messy vs clean code

Post by kingslovelua »

Robin wrote:
kingslovelua wrote:don't I have to declare yes and no as two variables that can be a input.
Nope. You don't have to do that. In fact, it doesn't do anything useful.
then why in the first if statement it started to work when i did that ??
Post Reply

Who is online

Users browsing this forum: ivxLL and 6 guests