forums.evilmana.com
Welcome, Guest. Please login or register.

Login with username, password and session length
September 10, 2010, 05:46:35 AM
.
News: 06/10/2009 - New Features For Forum.
Friendly URLs, twitter for profile, and video site URLs will auto-display the video.
Pages: [1]   Go Down
0 Members and 1 Guest are viewing this topic. Topic Tools  
Read October 10, 2006, 11:46:55 AM #0
nathan42100

The 5 Bits of Lua Style

Style is a crucial component of professionalism in software development. Clean code that follows stylistic conventions is easier to read, maintain, and share with colleagues. Programmers who code in good style are less likely to have silly bugs and will actually spend less time developing and debugging their code. Finally, good style in programs is a concern because for us, humans, style and aesthetics are a concern in everything we do.

Following stylistic conventions is easy, and, after a litter practice, becomes second nature. Occasionally, an especially independent-minded student resists all conventions, arguing, "But it works!" To this person, we can point out two things. First, a programmer's product is not an executable program, but its source code. In the current environment, the life expectancy of a "working" program is just a few months, sometimes weeks. On the other hand, source code, updated periodically, can live for years; to be of any use, it must be readable to others. Second, bad or unconventional style is uncool. It immediately gives away an amateur, a kind of social misfit in the software developers' culture. As nerdy as this culture might be, it has its own sense of aesthetic pride.

In programming, style has a deeper meaning: it is that elusive quality which makes on person's code elegant and easy to follow than another person's convoluted and obscure although in line with all the superficial stylistic conventions. The mystery is not as wide open in writing code as in creative writing: there are many firm design principals, and "a person who thinks clearly" usually is able to code clearly. Still, some mystery remains. Here we discuss only the superficial stylistic conventions, leaving the deeper meaning alone.

Bit 0.
Quote
At the top of each Lua file, put a comment that states the purpose of the file, its author, date of completion, and other pertinent info, such as a copyright message, special instructions on how to run the program, data files that your program reads or creates, and a history of revisions.

Bit 1.
Quote
Place all dofile() functions at the top of your Lua file. Start with standard libraries/files (such as netlib) followed by your own files.

Bit 2.
Quote
Separate different functions in a file with blank lines and  separator comment lines. Split your code into "paragraphs" that represent meaningful steps or actions in your program by inserting blank lines, and, if necessary, comment lines

Bit 3.
Quote
Place each statement on a separate line. Indent

Lua code is usually indented by a tab or 3 spaces in the body of the function or under if, else, switch, and for and while loops

Bit 4.
Quote
Use spaces liberally. Do not cram things together.

There are no clear-cut rules on where to add spaces -- different people have different tasted. We use spaces on both sides of the assignment operator and other binary operators: Arithmetic, logical, relational. For example:
Code:
taxAmount=saleAmount*taxRate
less readable then
Code:
taxAmount = saleAmount * taxRate

Adapted from Java Methods Textbook


Administrator of PSP-Programming.comNo PMs for help please, that is why these forums are here!
ⓅⓈⓅ-Ⓟⓡⓞⓖⓡⓐⓜⓜⓘⓝⓖ.ⓒⓞⓜ Ⓐⓓⓜⓘⓝ
Offline  
Read October 10, 2006, 11:53:43 AM #1
Charlie

Re: The 5 Bits of Lua Style

good stuff nate


Offline  
Read October 10, 2006, 11:54:46 AM #2
nathan42100

Re: The 5 Bits of Lua Style

Thanks. Maybe a sticky is in order?


Administrator of PSP-Programming.comNo PMs for help please, that is why these forums are here!
ⓅⓈⓅ-Ⓟⓡⓞⓖⓡⓐⓜⓜⓘⓝⓖ.ⓒⓞⓜ Ⓐⓓⓜⓘⓝ
Offline  
Read October 11, 2006, 12:12:30 PM #3
yaustar

Re: The 5 Bits of Lua Style

I disagree with Bit 2. dofile executes the script unlike #include in C which is done before the compile and execution stages so putting it at the top of the file standard doesn't apply.
Offline  
Read November 16, 2006, 06:29:46 AM #4
tacticalpenguin

Re: The 5 Bits of Lua Style

However for scripts that are defining variables and functions, you should do them at the top so that you dont accidentally try using a function before defining it. Makes it neater too.


tactical makes everything better
Offline  
Read January 06, 2007, 12:30:00 PM #5
Glynnder

Re: The 5 Bits of Lua Style

I agree with all but 2 (same reason as yaustar) anyways, good work  Thumbs Up
Offline  
Pages: [1]   Go Up
Jump to:  

Theme Update by Runic Warrior Originally created by m3talc0re