These concepts grew with programming experience in the community. Back in the 70s there was not much thinking about new concepts like code smells, SOLID, KISS, architecture and so on. Wrapping your head around stuff like this is awesome for learning!īut it is really bad for team owned code. K&R use extreme complex stuff in the middle statement of for-loops in this chapter. And I just have taken a look at chapter 2.1 and have not seen K&R using the third statement for another variable than the one in the first statement. Hi Steve, personally I do not think K&R is a good reference for production code. If you contribute somewhere or work on a team, the code and all naming has to be contradiction-free and fully literal in usage, this is high priority.Īn additional Note for Steve in the comments (if this might be allowed one time, because it fit's the topic, but not the comment space): What you wanted to communicate to a reader was maybe: signed long rc = 0 Do not use standard expressions in a tricky way without reason. Is a tricky use of for, but it is not what a reader expects from a for-loop (the first statement should initiialize something that has to do with the loop conditions) and this may lead to errors if someone refactors or debugs the code. And the code where it is used becomes less and less literal if naming like this is the overall practice. Messed up function names like this cost time, because everybody has to read up how to use it. If you ask me, a function called xxx2int should return an integer. I have only taken a quick look at binstr2int. Here you can see that the tests for a short input string and for Number and test for zero once it has done it: char* int2binstr(unsigned long num, char *s, size_t size) This is a case where aĭo while loop makes sense as it can handle the zero case as any other That it treats 0 separately from all other numbers. String - this is what all other C string functions do (or at least I'm In int2binstr I think the characters should be moved to the beginning of the Parameter and have the function return the status (0/-1): int binstr2int(const char *s, unsigned long &num) Make binstr2int produce an unsigned long of full range returned through a Them in turn to swap between integer and string representations. It makes sense for the two functions to be symmetrical - ie that you can call Int binstr2ul(const char *s, unsigned long *num)Ĭhar *ul2binstr(unsigned long num, char *s, size_t len) Num - Memory address to store the result in S - String with a maximum of log2(ULONG_MAX) binary characters The len parameter was changed to size_t type.Also the string parameter was in binstr2ul was made const.The functions now handle unsigned long ints.The names of the functions have been revised so they are not misleading.The for loop was unusual and the do while consumed one of the conditionals making the code easier to read. I did retain filling the buffer from end to beginning because it seems like a logical flow to me and requires one less variable to deal with. Len - Size in bytes of the region pointed to by sĬhar *int2binstr(unsigned long num, char *s, int len) S - Pointer to a memory region to return the string to Num - The number to convert to a binary string Decimal value of the binary string pointed to by s S - String with a maximum of 63 binary digits Also, I am attempting to follow this coding style CodingStyle.txt. I realize this is reinvetning the wheel I am just doing this for fun. I am working through K&R for review and I thought that I would write some of my own routines for conversion of binary strings to int and int to binary string.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |