linked list traversal example is classic C
if you don’t know what for(ss = s→ss; ss; ss = ss→ss); does then you shouldn’t be writing C code!
if you don’t know what for(ss = s→ss; ss; ss = ss→ss); does then you shouldn’t be writing C code!
Welcome! OmniNerd's content is generated by nerds like you. Learn more.
RE: linked list traversal example is classic C by Anonymous
I can buy that a C-programmer should know it’s traversing a linked list. But how exactly do you know what “s” stands for? Or why the linked list is being traversed in the first place?
RE: linked list traversal example is classic C by Anonymous
Agreed. That line quite clearly loops through s’s ss list.
I find it telling that he presented that line as an example of “clever” code, but only deigned to complain about its variable names (something that is already covered in the following section). A summary of much of the article seems to be “I don’t like C”.
Not saying that the code isn’t as bad as he paints it, but he certainly doesn’t show it.
RE: linked list traversal example is classic C by Anonymous
Seconded.
RE: linked list traversal example is classic C by Anonymous
I agree with the poster who said
if you don’t know what for (ss = s→ss; ss; ss = ss→ss); does then you shouldn’t be writing C code!
It’s not fair to quote a single line of code, point at it and laugh. That loop may make sense in context. It could have appeared in a function like…
(It’s been years since I wrote any C, so there are probably many things wrong with my example.)
The point is, programming is a conversation between programmers. It is useful in conversations to use very short abbreviations as long as everyone knows what they are. If we spelled out Network Interface Card, Automatic Teller Machine, Hyper Text Markup Language and all the rest every time we spoke of these things it would get tedious fast. Calling Strings “s” and SubStrings “ss” makes perfect sense. Calling indexes into arrays “i” and “j” also makes sense because it’s usually easy to tell what they mean from context. Indexing into something spatial with “x”, “y”, “z” and sometimes “w” also makes sense because we all did it in our math classes in school.
Another thing people do in verbal and written communication is to use idioms. They make no sense if you’re new to the language, but they convey lots of precise information for those who know them. The
for (pointer = struct->head; pointer; pointer = pointer->next)idiom is so common in C that anyone who’s seen much good linked list code will recognize it immediately. That the variables are “s” and “ss” is the only thing about that which is unusual, and in the context of strings and sub-strings, those names sound appropriate.It may be that the code you’re modifying was written by someone who’s not good at writing to be read. Programming is not often taught side-by-side with writing and speaking classes or as an exercise in conveying meaning to both the compiler and future programmers. That is a tragedy. But the practices you are complaining about are used in clear code, and the practices you espouse are used in opaque code.
Communicating is easy. Communicating well is hard. There are no shortcuts or rules which always produce code which is easy to understand.
RE: linked list traversal example is classic C by Jackson
if you don’t know what for(ss = s→ss; ss; ss = ss→ss); does then you shouldn’t be writing C code!
Luckily, I am allowed to write C++ instead. So, no worries there. If only I could magically have all the C code I maintain not be poorly written classic C. Also, knowing that a line of code traverses a linked list doesn’t tell you anything useful about what the code intends to accomplish. Maybe I was unclear. You know what the statement does, but not what the program does, and the statement does not help you understand the overall goal of the program. Lines in code, when possible, should help the maintainer understand what the code’s goal is.
I say that if you are writing code like
for(ss = s→ss; ss; ss = ss→ss)then you shouldn’t be writing code in the new millennium. That crap is 80s and 90s style coding. Things change. Classic C is not maintainable C, so let us revere those who had to live with classic C, but let’s not imitate their practices. I hear some computers have more than 64kb of memory these days.RE: linked list traversal example is classic C by Anonymous
Yeah, I’m an experienced C programmer and I got this right away. Just a pointer to a struct that recursively contains a pointer to the same type of struct. This is easily identifiable as being a linked list.
RE: linked list traversal example is classic C by Anonymous
“if you don’t know what for(ss = s→ss; ss; ss = ss→ss); does then you shouldn’t be writing C code!”
As much as I dislike speaking in absolutes, if you don’t comment a line like this then you are not programming at a professional level.
In academia or on your personal computer writing code without comments is fine. But professional software development IS the science of naming variables and writing comments.
RE: linked list traversal example is classic C by Anonymous
@ the original author:
If you really have any problems grasping what this code does, you shouldn’t be programming C++, Java, Pascal or any other procedural programming language either. It’s probably best if you get out of programming altogether, since in the real world this is about as clear-cut as it will get.
And another point: while I agree with you on using descriptive names, one can actually overdo it and thereby reduce legibility. E. g. :
for (traversalPointerOfSpaceItemsList = spaceItemsContainer→spaceItemsList; traversalPointerOfSpaceItemsList != NULL; traversalIndexOfSpaceItemsList = traversalPointerOfSpaceItemsList→nextSpaceItem) {…}
Do you REALLY think that this is more legible? And if so, what kind of screens are you usually working with? 60" ?
On the other hand, most of the article is quite nice, and I actually agree with you on most points.
BTW: I’m probably to be counted as your “old guard”, since I’ve been commercially developing software for 26 years. Nonetheless I wouldn’t let anyone near any code I’m responsible for who had any problem with the example above.
RE: linked list traversal example is classic C by Anonymous
OR, its the reason that crappy, obscure linked-list implementations are part of exactly what the author is talking about. Just because an experienced C programmer may be able to tell its a linked list, doesn’t make the code:
1) Good
2) Easily maintainable.
I have had the nightmare of having to traverse miles of “clever” linked-list spaghetti that 10 time harder to maintain that it should have been. It became so out of control that it became part of our best practices not to use linked lists. and guess what? there are other ways of doing good dynamic data structures anyway.
Let the religious war resume.