Proper Code Layout???
GSLogic
Posts: 562
Here?s a subject that goes along with politics and religion? code layout.
As most of use probably think our way of code layout is the best.
I?ve done many system upgrades/fixes and seeing the way others layout code. Needless to say it drives me crazy!
Why do programmers layout code so differently.
Please keep in mind I mention all this in a little way. I'd like to now if it all steams from the first code you ever wrote or did someone say "this is the correct way"?
I understand there are times to use different styles such as the "save real estate programmer", but I think the first example is the easiest to read and understand in the quickest time. I'm changing my code layout from using all caps to small, more in line with Java coding. That's something I should have done from the beginning.
What do you think?
As most of use probably think our way of code layout is the best.
I?ve done many system upgrades/fixes and seeing the way others layout code. Needless to say it drives me crazy!
Why do programmers layout code so differently.
AMX programmers 1: IF(1 == 1) { amxNumber 1 } AMX programmers 2: IF(1 == 1) { AMX_NUMBER 2 } AMX programmers 3: if (1 == 1) { AMX_NUMBER 3 } Java programmers: if(1 == 1){ Java } Save real estate programmers: IF(1 == 1){bad childhood}
Please keep in mind I mention all this in a little way. I'd like to now if it all steams from the first code you ever wrote or did someone say "this is the correct way"?
I understand there are times to use different styles such as the "save real estate programmer", but I think the first example is the easiest to read and understand in the quickest time. I'm changing my code layout from using all caps to small, more in line with Java coding. That's something I should have done from the beginning.
What do you think?
0
Comments
I vote for 1. This method provides the most reability and ease to copy and paste.
In addition, the hash marks in Studio line up nicely with the braces (curly brackets.)
Although I have been using mostly upper case, lately I have been converting to a combination, again for better readability.
Keywords should always be in upper case.
And everyone please start using lower case prefixes to indicate the data type.
I have added the underscore between the prefix and the data type to differentiate arrays from non-arrays.
See AMX Tech Note 407 for more information on recommended NetLinx programming conventions.
http://www.amx.com/techsupport/Netlinx/NetLinx%20Programming%20Conventions.pdf
The data type prefix recommendations can be found on page 8.
I tend towards all caps for keywords, and mixed case for variables, using a pseudo prefix system to tell at a glance what type of variable it is. I say pseudo because I do not strictly adhere to standards on this, partly from habit, and partly because I find some of the standards ridiculous for my use. For example, I'll name a string sStringName instead of strStringName; I'll use nNumber for any kind of data type unless it's real important to know the exact type, etc.
Finally, because I am feeling ambitious, I will ask a question of perspective:
In Brian's example:
The first defination could also be defined as:
and obviously the second definition could be:
So, I wonder, when does a char array become a string? Could it be said that the string exists only in our minds and hence everything is really just a char array? Or does one believe that because we think there is a string, the string must exist?
/me wanders off in a caffeine stupor
Jeff
Jeff,
Consider this regarding your question.
Use a lower case s representing a string as the prefix for defined constants.
Use a lower case c representing a string as the prefix for defined variables.
a string is always an array of characters so yes, it just lives in your head.
ontopic:
the problem with using tabs (i use them always) is that when someone uses another tab lineout, and you open his code, it's all messed up.
We have been recommending in our training classes for the programmer to write a comment for these settings somewhere in the beginning of the file.
Yuri, does that make you a 2 or am I still the only one? Although 1's also have to indent, they just do it wrong.
I initially used 4 in NS2 which was probably the default but I've since changed to 8 which I like. I use a widesceen which I'm sure every one uses now and I find that spreading the code out gives it a better visual flow. This is why I always use braces when I don't have to like a single statement after an "if" or wait.
Y
Going back to the DOS editor where columns were limited to 80 characters I have pretty much tried to keep that format. This allows you to vertically tile two editing windows of the same source code file on a wide screen display. To me a value of 3 seems the correct offset for each nested section.
I hope everyone else is taking advantage of the window tile feature.
One of my pet peeves is viewing/editing a file where scrolling horizontally is necessary. It really bothers me when a line of code is hiding out of complete view.
I agree, but like wise my variable and structure names are way to desciptive making them too long as well.
I too like example number one. However I like the commentating also. That is a problem that I have of tons of code that I have had to redo. NO comments at all.
And very very long variable names. A variable name should be specific but not 50 letters long.
I like the underscore for variable, such as i_tvinfo, i_dvdinfo etc.
Want I don't like is when code is all over the place, nothing lines up at all. Has anyone else found this is old code?
But that's where I hide all of the "fun" code...
;-)
I'm a #1.
--D
When comments don't fit to the right of a line of code there is nothing wrong with putting it just above the line.
Sloppy code is like a poorly wired equipment rack where cables look like spaghetti. You need just the right amount of organization so it can be easily serviced or revised and the right amount of comments so there is no need to reference the design drawings.
Of course you can also over do it. Anyone seen a rack with too many cable ties? I have and is amost as bad the spaghetti mess. In code I relate that to too many constants.
For example: So why do you need... Bottom line, avoid unnecessary constants
3 is just wrong.
I'll do the real estate thing if I only have one - >maybe< two - statements inside the braces
Re: Java & real estate, I can't really *****, because I have nearly forever done this just to save a line:
I find the "signify the variable type by using lower case characters in front of the name" to be distracting & annoying - at best. I got a couple of years under my belt of Axcess, when there were only two types of variables, and it wasn't hard to tell the difference. Now the only extension are structures and more numerical types, and well - if you can't tell a structure is being used by the fact that an element is being accessed... I also haven't run into any situations where an indicator of what numerical type a variable is would be required/helpful, so I just haven't bothered.
I still make keywords upper-case.
This is all IMHO - your mileage is more than likely to vary.
- Chip
i'm a 1
however i started out doing it the "java" way
Going back to Jeff's reply and pointing out the "typical" way of listing things in an array, note the placement of the commas.
I've started to do this after seeing it once:
To me it's easier to read, and also, if I want to remove the last number (5), I can delete the line immediately without having to think about getting a syntax error.
Does anyone else do it this way and what are your thoughts?
I add different header symbols for each different EVENT,
BUTTON_EVENTS get //########
DATA_EVENTS get //%%%%%%%
It makes reading a little easier on the eyes to see the different EVENTs
I also start all my button array names with prefix "bt".
All variables are small letters with Caps for the new words (nVarNameName) ala:Javaish.
All comment over a line of code get (* comment *) Next to a line of code get //comment
I still use "n" for all numbers and cString for all Strings/Chars.
I use caps for all keywords.
That's my 3 cents!
In the AMX world (for the moment) a string/char are the same but when/if you ever code in Java, you'll have to know the difference because they will not act the same. You'll have to declare a String or a Char as the variable.
Say on Brother!
I am a #1 with the following naming conventions.
CONSTANTS
INTEGER SOME_INTEGER = 1
CHAR SOME_CHAR = 'a'
VARIABLES
INTEGER nTPButtons[] //Touch panel channel arrays always have 'Buttons' at the end
INTEGER nIntegerArray[] // Somehow I just remember that this is an Array
INTEGER nSomeInteger //Simple Integer
CHAR cSomeCharacter //Simple Char
CHAR sSormeChaString[] //Use 's' to denote string
All KEYWORDS and CONSTANTS are capitalized.(Most of the time, some 'atoi's slip by)
And of course, color coding to differentiate Variables, Constants, Numbers, ASCII Text, Keywords, etc..
Comma's before the number just looks silly to me...But, to Each his own!!
I am a #1, I guess that is what was taught in class and sometimes I spend an unjustafiable time "re-indenting" someone's code before I make changes just because I can't read it. I have a hard time reading anything if it isn't "just so", a personal weakness of mine I think.
Does anyone else use the colors for different variable types to organize? I have my studio set to a black background (I guess a leftover preference from CAD) so I can use many more shades of orange and blue to reflect data types. This way even if the previous programmer of a system didn't follow the same naming conventions as I do I can still read it faster.
I also use many multiple DEFINE_XXX sections with a comment line after it so I can use the code folding to organize groups of operations.
Disclamer - None of these are my own ideas, but I figured I would pass on to anyone who hadn't seen them before.
So you're saying you can do things like "Define_Event" with a comment afterwards to specify a certain group of events, and then right after that do Define_Event again, wiht a different comment and have it nest separately?
That makes an awful lot of sense, I guess I'd just never realized I could have multiple Define_XXX sections of the same thing before . . . .
J
- DEFINE_EVENT// DATA EVENTS
+ DATA_EVENT[dvLCD]
+ DATA_EVENT[dvEXTRON]
+ DATA_EVENT[dvCLEAR_ONE]
+ DATA_EVENT[dvDVD]
+ DATA_EVENT[dvCD_A]
+ DATA_EVENT[dvTP_1]
+ DEFINE_EVENT// CHANNEL EVENTS, TIMELINE EVENTS
+ DEFINE_EVENT// SOURCE SELECTS, PAGE FLIPS
- DEFINE_EVENT// SOURCE CONTROL
+ BUTTON_EVENT[dvTP_1,nDVD_BUTTONS] ///// DVD CONTROL /////
+ BUTTON_EVENT[dvTP_1,56]
+ BUTTON_EVENT[dvTP_1,57]
+ BUTTON_EVENT[dvTP_1,nDSS_BUTTONS]
+ BUTTON_EVENT[dvTP_1,nCD_BUTTONS]
and so forth.
You can have multiple DEFINE_CONSTANT, DEFINE_VARIABLE, DEFINE_DEVICE, DEFINE_EVENT, I am sure others as well but those are the ones I use
He also showed an example of real estate programming and basically said "this will work but its confusing, dont do it"
Anyway, thats the official word of whats right, but his statement pretty much said "do whatever works as long as it isn't stupid"
Jeff