views:

887

answers:

15

Hello,

I am a new to professional development. I mean I have only 5 months of professional development experience. Before that I have studied it by myself or at university. So I was looking over questions and found here a question about code quality. And I got a question related to it myself. How do I increase my code understanding/reading skills? Also will it improve the code quality I will write? Is there better code notation than Hungarian one? And is there any really good books for C++ design patterns(or the language doesn't matter?)? Thank you in advance answering these questions and helping me improving :)

P.S. - Also I have forgot to tell you that I am developing with C++ and C# languages.

+17  A: 

There is only way I've found to get better at reading other peoples code and that is read other peoples code, when you find a method or language construct you don't understand look it up and play with it until you understand what is going on.

Hungarian notation is terrible, very few people use it today, it's more of an in-joke among programmers.

In fact the name hungarian notation is a joke itself as:

"The term Hungarian notation is memorable for many people because the strings of unpronounceable consonants vaguely resemble the consonant-rich orthography of some Eastern European languages."

From How To Write Unmaintainable Code

"Hungarian Notation is the tactical nuclear weapon of source code obfuscation techniques; use it! Due to the sheer volume of source code contaminated by this idiom nothing can kill a maintenance engineer faster than a well planned Hungarian Notation attack."

And the ever popular linus has a few words to say on the matter.

"Encoding the type of a function into the name (so-called Hungarian notation) is brain damaged—the compiler knows the types anyway and can check those, and it only confuses the programmer."

- Linus Torvalds

EDIT:

Taken from a comment by Tobias Langner.

"For the differences between Apss Hungarian and Systems Hungarian see Joel on Software".

Joel on Software has tips on how to read other people code called Reading Code is Like Reading the Talmud.

e5
Note that the guy who invented Hungarian Notation was Hungarian, which helped in naming it.
Aric TenEyck
There is indeed no substitute for practice. After some time, one transitions from the state of just plain "this code hurts my eyes" to "this code hurts my eyes because..." This will be true of one's own code as much as that of others.
Steve Gilham
There is a difference between Apps Hungarian (can be good) and Systems Hungarian (the devil)
Brian Ramsay
for the differences between Apss Hungarian and Systems Hungarian see Joel on Software: http://www.joelonsoftware.com/articles/Wrong.html
Tobias Langner
Joel Spolsky wrote an article on variable naming standards which also briefly discussed the Hungarian notation's origin. While I will agree Hungarian Notation is ridiculous as it was applied, the intention was that the prefixes should have more to do with the PURPOSE of a variable, and not the CLASS of the variable. Unencrypted data might have a "u" in front of the variable name and encrypted would have an "e". THIS actually makes sense, and it still followed in certain ways -- there is an understood coding practice in my company that private variables have an underscore in front of them.
Christopher W. Allen-Poole
It seems Tobias beet my previous comment to the punch... Oh well.
Christopher W. Allen-Poole
Bear in mind that, when using the Dark Side of the Hungarian, zero-terminated strings (which are all over in C code and lots of C++) begin with "sz". That's a fairly common digraph for the Hungarian languages, and not to common anywhere else that I know.
David Thornley
@Christopher W. Allen-Poole unencrypted vs encrypted prefixed for variables is a glorious idea, although a mistake in the notation could be extremely dangerous. Are there Java annotations which could tag encrypted vs unencrypted data?
e5
+1  A: 

Reading and understanding skills are a question of time. You will improve them as you get more experienced. Also depends in the quality of the code you are reading.

Keep in mind that sometimes it's not the best idea to learn directly from what you see at work. Books will teach you the best practices and you will be able to adapt them to yourself with the experience.

yeyeyerman
+5  A: 

Ask other people to read your code! Try and see if you get a fellow coworker or similar to have a code review with you. Having someone else comb through your code and ask you questions about your code will provide new insights and critiques to your style and techniques. Learn from your mistakes and others.

Bryan Denny
+2  A: 

Reading code is a lot like reading literature in that you need to have some insight into the author sometimes to understand what you're looking at and what to expect. They only way to improve your comprehension skills is by reading as much code and possible and trying to follow along.

I think a lot of what is mentioned here is applicable to coding...

CptSkippy
+2  A: 

The biggest improvement in readability of my code came about when I started liberally using white space.

DanDan
whywouldyoubothertowastevaluablebytesbyusingwhitespace?
Kevin Panko
I agree. I like to put 25 newlines between methods, so you don't get confused by seeing more than one method at a time.
Michael Myers
A: 

Check out this list of books for good pointers on (mostly language agnostic) best practices books

Ruben Steins
Diomidis Spinellis wrote a book (mostly in the key of C) aimed at answering some of what the OP asks, called Code Reading (http://www.spinellis.gr/codereading/).
TSomKes
That seems like an awesome book! I might even buy it myself :)
Ruben Steins
+6  A: 

Just to give you a bit of encouragement, I've been a professional programmer for 30 years now, and I still find reading other people's code very difficult. The major reason for this, unfortunately, is that the quality of the code follows Sturgeons Law - 90% of it is crap. So don't think it's your fault if you find it hard going!

anon
*Everything* follows Sturgeon's Law....
RolandTumble
Or its corollary - "90% of programmers shouldn't."
anon
+1, I've been a professional developer for 9 years, and I was beginning to think it was just me who found reading other people's code difficult.
Dan
+3  A: 

I found this article on Joel on Software to be very relevant to the Hungarian notation debate.

It seems that the original intent of the notation was to encode type information that wasn't immediately obvious- not whether a variable is an int (iFoo), but what kind of int it is- such as a distance in centimeters (cmFoo). That way, if you see "cmFoo = mBar" you can tell that it's wrong because even though both are ints, one is meters and the other is centimeters, and thus the logic of the statement is incorrect, even though the syntax is fine. (Of course, I personally prefer to use classes such that that statement wouldn't even compile, or would do the conversion for you).

The problem was at some point people started using it without understanding it, and the world was cursed with lots of dwFoos and bBars.

Everyone needs to read that article- Making Wrong Code Look Wrong

arolson101
+10  A: 

How do I increase my code understanding/reading skills?

Read read read. Learn from your mistakes. Review answers on SO and elsewhere. When you can think back on a piece of code you wrote and go "aha! I should've done xyz instead!" then you're learning. Read a good book for your language of choice, get beyond the basics and understand more advanced concepts.

Then, apart from reading: write write write! Coding is like math: you won't fully grock it without actually solving problems. Glancing at a math problem's solution is different than getting out a blank piece of paper and solving it yourself.

If you can, do some pair programming too to see how others code and bounce ideas around.

Also will it improve the code quality I will write?

See above. As you progress you should get more efficient. It won't happen by reading a book on design patterns. It will happen by solving real world problems and understanding why what you read works.

Is there better code notation than Hungarian one?

It depends. Generally I avoid them and use descriptive names. The one exception where I might use Hungarian type of notations is for UI elements such as Windows Forms or ASP.NET controls, for example: using btn as a prefix for a Submit button (btnSubmit), txt for a TextBox (txtFirstName), and so on but it differs from project to project depending on approach and patterns utilized.

With regards to UI elements, some people like to keep things alphabetical and may append the control type at the end, so the previous examples become submitButton and firstNameTextBox, respectively. In Windows Forms many people name forms as frmMain, which is Hungarian, while others prefer naming it based on the application name or form purpose, such as MainForm, ReportForm, etc.

EDIT: be sure to check out the difference between Apps Hungarian and Systems Hungarian as mentioned by @Tobias Langner in a comment to an earlier response.

Pascal Case is generally used for method names, classes, and properties, where the first letter of each word is capitalized. For local variables Camel Case is typically used, where the first letter of the first word is lowercase and subsequent words have their first letters capitalized.

You can check out the naming conventions and more from the .NET Framework Design Guidelines. There is a book and some of it is on MSDN.

And is there any really good books for C++ design patterns(or the language doesn't matter?)?

Design patterns should be applicable to any language. Once you understand the concept and the reasoning behind that pattern's usefulness you should be able to apply it in your language of choice. Of course, don't approach everything with a "written in stone" attitude; the pattern is the goal, the implementation might differ slightly between languages depending on language features available to you. Take the Decorator pattern for example, and see how C# extension methods allow it to be implemented differently than without it.

Design Pattern books:

Head First Design Patterns - good beginner intro using Java but code is available for C++ and C# as a download (see "book code and downloads" section on the book's site)

Design Patterns: Elements of Reusable Object-Oriented Software - classic gang of four (GOF)

Patterns of Enterprise Application Architecture - Martin Fowler

If you're looking for best practices for quality coding in C++ and C# then look for the "Effective C++" and "More Effective C++" books (by Scott Meyers) and "Effective C#" and "More Effective C#" books (by Bill Wagner). They won't hold your hand along the way though, so you should have an understanding of the language in general. There are other books in the "Effective" series so make sure you see what's available for your languages.

I'm sure you can do a search here for other recommended reading so I'll stop here.

EDIT: added more details under the Hungarian Notation question.

Ahmad Mageed
Whats is your opinion on the Gang of Four book, I've heard mostly bad and good things, mostly from people that haven't read it (I am a member of the set as well)?
e5
@e5: I haven't read it but have constantly heard great praise for it. The reviews on Amazon are pretty high too. I've glanced at it though and I can see why people may not like it, mainly because of it being a little too dry which makes it difficult to appreciate w/o a good foundation in development. The Head First book is usually mentioned as a good starting point (before GOF) with its friendly jargon and drawings. I own that one and it's a large book that's very easy to read. I've read parts of the Fowler book (office copy) and it's great but probably not for beginners w/o field experience.
Ahmad Mageed
I thought it was hard to read. I liked Head First Design Patterns a lot more when starting to learn pattern.
Alex Baranosky
+8  A: 

I can't speak for everyone else, but in my experience I've found that the best thing I learned about making readable and/or in general better code was reading (and ultimately cleaning) a lot of other people's code. Some people may disagree with me but I think it's invaluable. Here's my reasoning:

  • When you start programming, its difficult to determine what is crap vs. not crap vs. good. Being logical, rational and extremely intelligent help in making good code, but even those factors don't always contribute. By reading others works and doing the dirty work, you'll have gone through the experience of what works and what doesn't. Eventually, you'll be able to mentally navigate those minefields that others had to cross and you'll be prepared to avoid those identical minefields.

  • By reading other's works, you gain insight into their mind and how they tackle a problem. Just from an architecture or technique aspect, this can be very useful to you whether their tactics were good or bad. By reading other peoples successful or unsuccessful implementation, you've gained that knowledge without putting in the actual time it took them to learn it.

  • Design patterns are extremely useful. Only time and experience with them will help you in knowing what the appropriate pattern for whichever problem. Again, read other peoples' code for this if they've successfully built some pattern that may be useful for you.

  • When dealing with extreme problems where people's work falls short, you learn to research and dive into the internals of whatever system/language/platform/framework you're working with. This research ability on your own is very useful when all else fails. But you'll never know when to start looking or for what until you get through the crud of other people's work. Good code or bad, it's all valuable in some form or fashion.

  • All these notations and formats and nomenclature are helpful, but can be learned or implemented rather quickly and their payoff is fairly substantial. By reading code from other people, you'll develop your own style of logic. As you encounter other peoples work and the tremendous amount of effort it takes to read through, you'll learn what logical pitfalls to avoid and what to implement the next time for yourself or even how to fix bad code even faster.

I've never felt as if I was a great programmer. Not to say I'm a bad one either, but I feel confident in my abilities as my experience has taught me so much and my ability to adapt to every situation is what makes me a solid programmer. Learning from other people and their code has helped me. Whether their work was good or bad, there's always something you can take from them and their experience, add it to your memories, knowledge, etc.etc.

osij2is
+1  A: 

I am reading the Head First Design Patterns at present and it is very useful. It presents the information in a novel way that is easy to understand. Nice to see they have a C# version for download.

pave
+1  A: 

How do I increase my code understanding/reading skills?

Reading code is like dancing by yourself. You need a partner, and I suggest a debugger.

Stepping through code with a debugger is a true, lively dance. I recommend getting a quality, open-source project in the language of your choice, and then step through with the debugger. Concepts will come alive if you ask "why did that happen?", "what should happen next?".

One should ultimately be able to reason about code without a debugger; don't let it become a crutch. But that said, it is a very valuable tool.

Michael Easter
A: 

There will always be struggles with reading code unless you are Jon Skeet. This isn't to say that it is a big problem, rather that unless you can eat, sleep, breathe in that programming language, it will always take a little time to digest code. Looking at other people's code is certainly a good suggestion for helping in some ways but remember that there are many different coding conventions out there and some may be more enforced than others, e.g. interface names start with an I to give a simple example. So, I guess I'm saying that even with Visual Studio and Resharper, there is still a little work to understand a few lines of code since I can't quite write out sentences in C# yet.

JB King
A: 

1) Educate yourself. Read relevant literature. 2) Write code 3) Read code 4) Read relevant blogs. Visit http://hanselminutes.com . He is a programmer from microsoft. Even though you don't program on microsoft stack, it's good to read through. There is a podcast in there that answers this question.

Christy John
A: 

Another suggestion is to make sure you have the appropriate tools for the job before you start digging into a piece of code. Trying to understand a code-base without the ability to search across the entire set of files is extremely difficult.

Granted, we very rarely have the entire set of files, especially in large projects, but whatever boundaries you draw, you should have good visibility and searchability across those files. Whatever lies outside those boundaries can be considered 'black box' and perhaps lies outside the scope of your grokking.

There are many good open source editors including Eclipse and the CDT. Spending some time learning how to effectively create projects, search across projects, and enable any IDE-specific tooltips/helpers can make a world of difference.

dls