Question:
Beginner self taught programmer tips (any language, but c++ specifically)?
anonymous
2010-02-20 20:34:45 UTC
I've been programming in C++ for little over a year now, and I have taught myself everything up to now, and my I generally code to "get the job done".

Recently I was told that my code was, well, quite frankly "ugly". I believe the person was referring to my methods of solving problems, because I know I use correct formatting in my code, like braces on their own line, always assigning a value to a variable after initialization, unless for some odd reason not to, spacing different logical sections of code, etc.

I guess I would like to know what I can read or study to learn how to write efficient functions, and algorithms in TRUE c++ ( I come across too many books that say "c++ programming" but are somewhere in between C/C++, and sometimes confuse my ideas about c++).

I realise to do things like this, higher mathematics comes into play, such as calculus, but I don't think that will be too much of a problem. I'm 17- any help will be greatly appreciated :).
Seven answers:
Linkdead
2010-02-20 21:02:55 UTC
I know what you mean man I've been the same way in the past. I picked up a book by Microsoft (of all places) called "Code Complete". unlike all of the other programming books I've looked at up to that point this one isn't focused on any particular language, it just talks about algorithms and ways to improve them

also I'm going to college now and getting some official training which is helping, although my current instructor isn't that bright.

I also found this video series to be helpful

http://www.youtube.com/user/unswelearning#p/c/E621E25B3BF8B9D1



I'd say just spend some time looking at other people's code.

Some people try to get the most speed out of their programs but personally I think making the code look clean and understandable is the most important thing because bugs lurk in complex code so simplicity is preferable in most cases.
Pete S
2010-02-20 23:29:10 UTC
"Get the job done" usually leads to bad coding practices :).



You should have descriptive variable names, I shouldn't have to do a lot of reading to understand what a variable stores or what a function does. Comments are a good addition, but don't go overboard with them.



Functions\Methods should be short and only do 1 thing. If you can't explain what that function does quickly, then you probably need to break the code up into smaller pieces for better readability.



One phrase that really stuck with me from the Pragmatic Programmer's book (which is a great book about writing software) is DRY (Don't repeat yourself). If you need to do something similiar to a piece of code you already have, then generalize the common parts out, don't just copy and paste the code.



Favor composition over inheritance, another words, make objects that contain or use other objects as necessary instead of creating a new subclass every time you want to do something different. This will make it easier to reuse and extend your code later.



If you're not using objects, then consider what types of data your programs use that could be objects, the idea being that you separate a particular task from the rest of the program (say logging or calculating sales tax) so that you might be able to reuse it or change it easily.
A.T.
2010-02-20 20:57:03 UTC
- Pick up a book on algorithms. That would allow you to learn how to do things more efficiently. My favorite one is by Cormen, Leiserson, Rivest, Stein "Introduction to Algorithms"

- See if you can read something non-language specific about principles of optimization and avoiding redundant/useless code.

- If you have a constant that you use in several places then make sure to declare it somewhere as a #define variable so that you only have ONE place to change it if needed thus insuring that you won't accidentally forget to update it on some line of code.

- Do not just start coding and see what comes out - formulate the program in English or pseudo-code first. It is usually best to formulate in either pseudo-code or semi-math format and specify what are pre/post conditions for each function call and then when coding make sure to preserve those (read up on pre/post conditions if you don't know what those are).

- Try to keep your code succinct and modular - the larger is the system that you are coding up the more modular it should be. I.e.: use inheritance and separate objects & their functionality in such a way that it makes sense when defined in English AND when you think about updating or extending your code in the future.

- Modulating includes placing each class in it's own file etc.

- Avoid 'break' and 'go to' constructs - you want to have a SINGLE return point from your function that makes debugging easier and code cleaner.





Hope this helps :)
Ratchetr
2010-02-20 20:57:01 UTC
in general, when programmer A looks at programmer B's code, his first reaction is usually that it's ugly. A would do it different. Better? Maybe, maybe not.



Look at some of the code you wrote 6 or 9 months ago. Can you understand it? Is it ugly? If it's ugly, what can you do to improve it?



There is a subtle flaw in this statement:

"...I would like to know what I can read or study to learn how to write efficient functions, and algorithms in TRUE c++"



Good, efficient algorithms are language neutral. The language is just an implementation detail. So learn good algorithms. Learn what a design pattern is, how to know when to use one, and how to implement one.



Once you learn that stuff, it won't matter if you are working in C++ or Java or C# or FidgBits.



Some things to look at:
Anne
2010-02-20 21:10:48 UTC
As a former adjunct faculty member and long-time programmer, my experience is that the difference between an adequate or good programmer and a great programmer is one who understands the theory behind software design. The mechanics are important. But you need to understand how to write efficient code as well. I'd suggest starting out with a book on design patterns. Two I would recommend are "Applying UML and Patterns" by Craig Larman (expensive by worth it!) and Design Patterns: Elements of Reusable Object-Oriented Software by Erich Gamma, et.al.



Second thing I would recommend: learn to comment your code! No matter how clear it looks, not everyone thinks the same way you do. Not only that, but you may find yourself trying to revisit your own code and not remembering what your thought processes were that helped you arrive at the solutions you used.



Third, are you writing unit tests for all your code? If not, start doing so! Better yet, write the test first. It'll help you to better understand your problem.
anonymous
2016-04-12 13:05:47 UTC
Computer science in college means some basic programming languages, data structures, sorts and other algorithms. You learn to write code, yes. but you also learn how to figure out what code to rest. Most of the job interviews I have gone to have actually tested that. I've had to work in three or four compact languages which I only knew from the handout I was given at the job interview when they tested my knowledge -- sometimes the same handout from different employers but still they were testing my skills at applying algorithms I was supposed to know. You CAN get a job without a degree, but it is not easy. And it may not be easy to keep.
anonymous
2010-02-20 20:53:18 UTC
You're probably writing things like if(!a!=b). Start reading http://www-old.oberon.ethz.ch/WirthPubl/AD.pdf



Most people who teach themselves a language never actually learn programming.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...