This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
sjele's profile picture

[help] Formatting

Started by sjele, 02 August 2012 - 05:04 PM
sjele #1
Posted 02 August 2012 - 07:04 PM
So i have noticed that when i code i have a realy hard time understanding witch line works together. So what is like the rules to formatting?
How should i write the code?
Were to put spaces betwwen lines?
How much of a indent on the side?
Thanks
BigSHinyToys #2
Posted 02 August 2012 - 07:19 PM
This does vary a bit between languages and individual coders Best way to learn formatting is to look at lots of code. see how they do it. adopt parts of there method you like and look at more code. eventual you will have a method that works for you.

That is how i did it (not saying it is the correct way)
Rsstn #3
Posted 02 August 2012 - 07:40 PM
Yeah, you should decide yourself what you want your code to look like. Chances are, you are the only person who will look at your code, so as long as you can read it again in the future, it's ok.
Personally, I type my code as neat as possible. See HERE for example. That's because I'm a perfectionist, and because I know that if I look back at my programs in a couple of months time, I'm not going to want to spend ages working out what I meant at line 34!
A good rule to follow, is to leave a blank line between sections of the code, so that you can easily see what parts of the code work together.

In summary, as long as you put spaces in the places where you NEED spaces, then you can spread the code out as much as you like!
Kolpa #4
Posted 02 August 2012 - 07:43 PM
Yeah, you should decide yourself what you want your code to look like. Chances are, you are the only person who will look at your code, so as long as you can read it again in the future, it's ok.
Personally, I type my code as neat as possible. See HERE for example. That's because I'm a perfectionist, and because I know that if I look back at my programs in a couple of months time, I'm not going to want to spend ages working out what I meant at line 34!
In summary, as long as you put spaces in the places where you NEED spaces, then you can spread the code out as much as you like!
that's by far not as much formatting as i am doing comming from java/eclipse
Rsstn #5
Posted 02 August 2012 - 07:54 PM
Lua was my first programming language, so any code I write now (C++) follows the rules that I made up when using Lua (where possible!).
BigSHinyToys #6
Posted 02 August 2012 - 08:06 PM
Lua was my first programming language, so any code I write now (C++) follows the rules that I made up when using Lua (where possible!).
as someone who's first language is also lua how hard was the transition to C++ ? I am concidering trying it my self any tips ?
MysticT #7
Posted 02 August 2012 - 08:24 PM
Like said before, it really depends on how you like it. Normally, you add an indentation level each time you enter a new block of code (like a function, a loop, an if, etc.), and subtract one when it ends. How much space you use for each level depends on you.
Example:

function someFunction()
  doSomethingHere()
  if some_condition then
    doSomethingElse()
  end
end

while true do
  local evt, id, msg = os.pullEvent()
  if evt == "rednet_mesage" then
    if id == someID then
	  print("Message: ", msg)
    end
  else
    print("Not a rednet message :)/>/>")
  end
end

Lua was my first programming language, so any code I write now (C++) follows the rules that I made up when using Lua (where possible!).
as someone who's first language is also lua how hard was the transition to C++ ? I am concidering trying it my self any tips ?
I can't really say that, since I started with C/C++, then Java and now Lua (with some other languages in the middle), but I can say that they are completly different things. To start, Lua is an interpreted language, while C/C++ is compiled, so you'll have to learn something about compiling and that (if you use an ide, it does all the work for you, but it's good to know anyway). Also, C++ is object oriented, wich adds objects, classes, etc. And so much more, but this is kinda off-topic, so I better stop :ph34r:/>/>.
Rsstn #8
Posted 02 August 2012 - 08:28 PM
as someone who's first language is also lua how hard was the transition to C++ ? I am concidering trying it my self any tips ?

Well, the nice thing about Lua is that you can just start programming straight away. Take the 'Hello World' program for instance.
However, in C++, there are a few things you need to declare before you can begin writing your program. Although this was an irritation at first, after a couple of weeks of on/off self-learning, you get used to it, and can experiment more.
It is not a particularly hard transition to make, as the formatting is fairly similar, and the function use is just as easy as Lua.
I've been reading through THIS website, and find it very useful. If you want to get an idea of what the language is like to use, you should look at '1.1: Structure of a program'.
Possibly the biggest difference to Lua is that you compile your program before you can run it, but this means that after the program has been compiled (into an exe file) you can run it without the aid of another program.

Lua is a nice starter language that can be learnt in-game (like in CC+Minecraft or Roblox), but C++ frees you from the limitations.

Sorry, I ended up waffling a lot and didn't answer your question very clearly, so I'll summarise:
Yes, it's a reasonably easy transition to make, but you need to remember to define nearly every variable/function you use. It takes a while longer to get into the fun coding, but it's worth it in my opinion!

EDIT: MysticT thinks that it's quite different, but when you use an IDE, it's very simple as far as I can see. I'm not that far into the language, so I guess I haven't encountered much stuff that I find hard to use as a Lua programmer.
Rsstn #9
Posted 02 August 2012 - 08:36 PM
Mystic, I'd be interested to know what you think of that website I talked about above.
How did you learn C++? Are websites like that a reliable source?
MysticT #10
Posted 02 August 2012 - 08:53 PM
Well, I never found tutorials that good, it's really well explained.
I learned C at the university, in the programming class, and then started learning C++ with tutorials and just trying by myself. If you like programming, it's easy to learn any language, and when you already know one, it's even easier, since they are all basically the same, just with a different syntax.
Lyqyd #11
Posted 03 August 2012 - 07:51 AM
Well, I never found tutorials that good, it's really well explained.
I learned C at the university, in the programming class, and then started learning C++ with tutorials and just trying by myself. If you like programming, it's easy to learn any language, and when you already know one, it's even easier, since they are all basically the same, just with a different syntax.

Heh. I'd say that most imperative languages are fairly similar and use similar concepts for various tasks. There are, of course, things like functional languages (Haskell, etc.) and some even more unusual ones (Prolog), so I'm not sure I'd say that all of the languages are the same but for syntax.
Rsstn #12
Posted 03 August 2012 - 08:49 AM
When I get bored of life I'll move to this: http://en.wikipedia.org/wiki/LOLCODE
KaoS #13
Posted 03 August 2012 - 09:35 AM
When I get bored of life I'll move to this: http://en.wikipedia.org/wiki/LOLCODE

THAT IS SO AWESOME LMAO
Kolpa #14
Posted 03 August 2012 - 01:01 PM
When I get bored of life I'll move to this: http://en.wikipedia.org/wiki/LOLCODE
wait what did i just, no this cant be, oh really i guess, WTF