25d86
![]() |
|
|
|
|
|||||||
| Userland Programming & Scripting C, C++, Python, Perl, Shell, etc. |
![]() |
|
|
Thread Tools | Display Modes |
|
#1
|
||||
|
||||
|
Hi guys,
Introduction (to make it clear why I am asking this question). Exactly one year ago, I thought to throw OS Windows and switch to UNIX OS. A whole summer of 2011 I studied the various UNIX like operating systems, and stopped on FreeBSD OS. Even last fall, I do not know how to create / read / watch, even plain text files. Through this forum, I somehow mastered this OS, and now, after seven months, I can freely work on FreeBSD. When I worked on the Windows OS, I was fond of programming, but I used C++ and IDE Borland Builder - bad Windows way! Now I want to continue to study programming, but only ANSI C (c89, c99, c11). Now I do not use the IDE. My tools: devel/geany or editors/vim, graphics/dia, devel/doxygen and BSD make. That's all what I learned! In order to begin a detailed study of ANSI C, I decided to write a small program coding of text data. She has a very simple algorithm, but it's enough to make that first step. My question. Here's the source code of my program, could you, as the more experienced programmers look at it and give me what some suggestions / comments / criticism / cry. I am very interested to know: I'm moving in the right direction? I am interested in your criticism! Details for the compilation: Directory structure: Code:
~/yep/
|__ bin/
|__ doc/
|__ src/
|__ ..
|__ .
C code, my program. ~/yep/src/yep.c (View here!) Doxyfile ~/yep/src/Doxyfile (View here!) Makefile ~/yep/src/Makefile Code:
# Project: yep # Date created: April 22, 2012 # Whom: valsorym <valsorym.e@gamil.com> # binary EXECUTABLE = $(.CURDIR)/../bin/yep # compiler CC = cc CFLAGS = -Wall -ansi -std=c99 LDFLAGS = -O2 # targets all: $(EXECUTABLE) $(EXECUTABLE): yep.o $(CC) $(CFLAGS) -o $@ $> $(LDFLAGS) yep.o: yep.c $(CC) $(CFLAGS) -c $> .PHONY: all clean run install clean: rm -f *.out *.o *.core run: $(EXECUTABLE) install: cp $(EXECUTABLE) ./bin/ Last edited by valsorym; May 21st, 2012 at 23:31. |
|
#2
|
|||
|
|||
|
Quote:
Well I've looked very quickly the C code. First, I'm happy to see that you use spaces. It looks like spaces are very expensive when I look the code made by some beginners at work (but may be I'm too old and I need new glasses...). Be consistent with spaces, I don't see why "int i=0" has no space but "i < 0" has some? There are few things I dislike: In main(), the "if else if else if else if" where you test options is quite hard to read and understand, this is a typical case where you can use "continue / return / goto" to make the code more readable. There are few useless cast (avoid cast!) : Code:
hookword = (hookword ? (char*)hookword : (char*)DEFAULT_HOOKWORD); Also the construction above is not very readable, why you want to say is "if hookword is null, put DEFAULT_HOOKWORD". I don't see the point to use the "?" operator here. I prefer Code:
if (hookword == null)
hookword = DEFAULT_HOOKWORD;
To finish, I've noticed some "if (i++ < XXX)", I'm not sure if this is and undefined behavior or not (when the post incrementation is done?). I'm not a good C programmer and I'm too lazy to check this, so I would avoid this (doing C is like carrying some TNT, if you are not cautious it will explode IMO). VoilĂ , this is highly subjective of course. Perfect Code is a bit like the Holly Grail. you will find some advise for C style in the "man style" manual page of FreeBSD and all the FreeBSD's source code to see how it is made. It's good C and there are a lot of things to learn from. Regards. |
| The Following User Says Thank You to plamaiziere For This Useful Post: | ||
valsorym (May 6th, 2012) | ||
|
#3
|
|||||||||
|
|||||||||
|
Quote:
Quote:
Quote:
When I do the initialization I do not use spaces: Code:
int a=21; int b=10, *c=(int)&a; Code:
int a, b, *c; a = 21; b = 10; c = (int)&a; Code:
if (a > b)
*c = b;
Code:
for (int i=0, /* Initialize - there is no space */
i < 100, /* Boolean expression - there is space */
i++) {... ... ...}
Quote:
![]() If you reduce the number of "if - else" I came up with just such a solution! What do you think about this? Quote:
Quote:
But I agree with you! You're right - in this situation is no sages. In this case, I had to write: Code:
if (!hookword)
hookword = DEFAULT_HOOKWORD;
Quote:
Suffix and prefix increment / decrement somewhere described in the Standard c89 - it should always work predictably. Quote:
Quote:
Thank you for your time! You helped me to find some problems, and think about the style! Last edited by valsorym; May 6th, 2012 at 01:37. |
|
#4
|
||||
|
||||
|
Quote:
![]() I usually prefer astyle. But both have their strengths and weaknesses. Last edited by graudeejs; May 6th, 2012 at 09:04. |
| The Following User Says Thank You to graudeejs For This Useful Post: | ||
valsorym (May 6th, 2012) | ||
|
#5
|
||||
|
||||
|
Quote:
But honestly, did not see the point in this program. Code rules are quite simple, and use programs lint(1) and/or devel/astyle it seems to me superfluous. In terms of design code to me more like the language Python. There is code written by different people always looks the same. It is a pity that such a rule is not entered in the C language. But in any case, thank you! Me need to think about the code style. Quote:
Yes, I have something to think about it. Thank you! |
|
#6
|
||||
|
||||
|
I looked at the style code FreeBSD sources, listened to your advice and looked like running astyle and made the final version of the source code.
P.S. Guys, I know you are bored to look at it. But I'm really interested in your opinion - any recommendations / suggestions / tips .... Last edited by valsorym; May 21st, 2012 at 23:33. |
|
#7
|
|||
|
|||
|
Personally (this is not BSD style or whatever):
On spaces vs tabs.... if you want 4 space tabs, set your editor to display tabs as 4 spaces. Other people working on your code may prefer more/less visual indentation. If you use tabs, they can choose without having to reformat all of your code. All that is stored in the file is a TAB character, how that is expanded out when viewed is totally up to the user viewing the code.
__________________
I use: FreeBSD, Mac OS X, Windows, Netapp, Cisco UCS, Cisco CUCM, Cisco IOS, Cisco ASA, vSphere 5.1, Cisco ISE, Orion NPM |
| The Following User Says Thank You to throAU For This Useful Post: | ||
valsorym (May 7th, 2012) | ||
|
#8
|
|||
|
|||
|
I appreciate the level of comments in your code, that is not usual and is very useful.
Besides what have been already commented about the general coding, I would recommend you to use some kind of versioning, like github or bitbucket or alike, this will make reviewing your code easier and will not require pastebin. |
| The Following User Says Thank You to fluca1978 For This Useful Post: | ||
valsorym (May 7th, 2012) | ||
|
#9
|
|||
|
|||
|
Some of these points may just be my preferences, but:
I hope some of those help. I only commented on style, rather than examining any algorithms, or techniques especially. |
| The Following User Says Thank You to anon12b For This Useful Post: | ||
valsorym (May 7th, 2012) | ||
|
#10
|
|||||
|
|||||
|
Quote:
Quote:
Quote:
Code:
$ fgrep -R 'goto ' /sys/* | wc -l 22047 Quote:
Quote:
Regards. Last edited by DutchDaemon; May 8th, 2012 at 00:00. |
| The Following User Says Thank You to plamaiziere For This Useful Post: | ||
valsorym (May 8th, 2012) | ||
|
#11
|
||||
|
||||
|
Yes, thank you. Your comments are very helpful for me.
|
|
#12
|
||||
|
||||
|
Thank you all, I saw a lot of things out of your criticism. I have something to think about - thank you all very much!
P.S. The final result. Last edited by valsorym; May 9th, 2012 at 23:27. |
|
#13
|
||||
|
||||
|
I've noticed you don't use curly brackets to set block for if/while/for when only one statement follows it. Though this is OK it does make code less readable. Brackets set block and it's easier to understand which block belongs where.
But know there are more styles you can use - there's not one good standard (at least indent wise). As you asked on FreeBSD forums, you can check developers handbook. I think K&R C Programming language book has some style recommendations too.
__________________
..when you do things right, people won't be sure you've done anything at all.. Last edited by matoatlantis; May 12th, 2012 at 20:32. Reason: minor typos |
| The Following User Says Thank You to matoatlantis For This Useful Post: | ||
valsorym (May 12th, 2012) | ||
|
#14
|
||||
|
||||
|
Quote:
|
|
#15
|
||||
|
||||
|
Google's C++ style guide is another style resource. It's a superset of what you need since it's for C++ and not C.
|
| The Following User Says Thank You to jrm For This Useful Post: | ||
valsorym (May 12th, 2012) | ||
|
#16
|
||||
|
||||
|
Quote:
|
|
#17
|
|||
|
|||
|
Code:
$(EXECUTABLE): yep.o $(CC) $(CFLAGS) -o $@ $> $(LDFLAGS) yep.o: yep.c $(CC) $(CFLAGS) -c $> You don't even need a makefile to work in a simple manner: Code:
$ echo 'int main(void){return 0; }' > 1.c
$ make 1
cc -O2 -pipe 1.c -o 1
$ export CFLAGS='-g -Wall'
$ rm 1
$ make 1
cc -g -Wall 1.c -o 1
|
| The Following User Says Thank You to bigearsbilly For This Useful Post: | ||
valsorym (May 21st, 2012) | ||
|
#18
|
||||
|
||||
|
Quote:
Your argument parsing is kinda messy chain of ifs and else ifs, should rewrite this for tsarg() to return argument code and then just switch() it, that would prevent unnecessary function calls (function call is waste of CPU cycles). There is a buffer overflow possibility in errexit() since you use sprintf instead of snprintf. Similar error in terminal(). Code:
if (terminal("rm -f %s", tempfile))
There are to many strlen() calls and you are failing to use "const" declarations on a lot of variables that you use as constants. Thats what I've noticed with quick look over the code. |
| The Following User Says Thank You to expl For This Useful Post: | ||
valsorym (May 21st, 2012) | ||
|
#19
|
||||
|
||||
|
Quote:
It is constructive, your comments useful to me. |
![]() |
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| {{{ }}} for code blocks | techtonik | Feedback | 5 | November 14th, 2011 09:21 |
| [Solved] RT and perl programmer's help wanted :D | romeor | Userland Programming & Scripting | 1 | August 24th, 2011 12:57 |
| Ideas for programs. Amateur programmer looking for projects to dabble in | lockfile | Userland Programming & Scripting | 6 | October 8th, 2010 12:33 |
| Let's code! | CodeBlock | Off-Topic | 19 | December 7th, 2009 02:12 |
| [Solved] new bb code red, plz | graudeejs | Feedback | 4 | December 27th, 2008 03:16 |