Code Complete

Steve McConnell

Mentioned 154

Features the best practices in the art and science of constructing software--topics include design, applying good techniques to construction, eliminating errors, planning, managing construction activities, and relating personal character to superior software. Original. (Intermediate)

More on

Mentioned in questions and answers.

Why prefer composition over inheritance? What trade-offs are there for each approach? When should you choose inheritance over composition?

Think of containment as a has a relationship. A car "has an" engine, a person "has a" name, etc.

Think of inheritance as an is a relationship. A car "is a" vehicle, a person "is a" mammal, etc.

I take no credit for this approach. I took it straight from the Second Edition of Code Complete by Steve McConnell, Section 6.3.

It wasn't that long ago that I was a beginning coder, trying to find good books/tutorials on languages I wanted to learn. Even still, there are times I need to pick up a language relatively quickly for a new project I am working on. The point of this post is to document some of the best tutorials and books for these languages. I will start the list with the best I can find, but hope you guys out there can help with better suggestions/new languages. Here is what I found:

Since this is now wiki editable, I am giving control up to the community. If you have a suggestion, please put it in this section. I decided to also add a section for general be a better programmer books and online references as well. Once again, all recommendations are welcome.

General Programming

Online Tutorials
Foundations of Programming By Karl Seguin - From Codebetter, its C# based but the ideas ring true across the board, can't believe no-one's posted this yet actually.
How to Write Unmaintainable Code - An anti manual that teaches you how to write code in the most unmaintable way possible. It would be funny if a lot of these suggestions didn't ring so true.
The Programming Section of Wiki Books - suggested by Jim Robert as having a large amount of books/tutorials on multiple languages in various stages of completion
Just the Basics To get a feel for a language.

Code Complete - This book goes without saying, it is truely brilliant in too many ways to mention.
The Pragmatic Programmer - The next best thing to working with a master coder, teaching you everything they know.
Mastering Regular Expressions - Regular Expressions are an essential tool in every programmer's toolbox. This book, recommended by Patrick Lozzi is a great way to learn what they are capable of.
Algorithms in C, C++, and Java - A great way to learn all the classic algorithms if you find Knuth's books a bit too in depth.


Online Tutorials
This tutorial seems to pretty consise and thourough, looked over the material and seems to be pretty good. Not sure how friendly it would be to new programmers though.
K&R C - a classic for sure. It might be argued that all programmers should read it.
C Primer Plus - Suggested by Imran as being the ultimate C book for beginning programmers.
C: A Reference Manual - A great reference recommended by Patrick Lozzi.


Online Tutorials
The tutorial on seems to be the most complete. I found another tutorial here but it doesn't include topics like polymorphism, which I believe is essential. If you are coming from C, this tutorial might be the best for you.

Another useful tutorial, C++ Annotation. In Ubuntu family you can get the ebook on multiple format(pdf, txt, Postscript, and LaTex) by installing c++-annotation package from Synaptic(installed package can be found in /usr/share/doc/c++-annotation/.

The C++ Programming Language - crucial for any C++ programmer.
C++ Primer Plus - Orginally added as a typo, but the amazon reviews are so good, I am going to keep it here until someone says it is a dud.
Effective C++ - Ways to improve your C++ programs.
More Effective C++ - Continuation of Effective C++.
Effective STL - Ways to improve your use of the STL.
Thinking in C++ - Great book, both volumes. Written by Bruce Eckel and Chuck Ellison.
Programming: Principles and Practice Using C++ - Stroustrup's introduction to C++.
Accelerated C++ - Andy Koenig and Barbara Moo - An excellent introduction to C++ that doesn't treat C++ as "C with extra bits bolted on", in fact you dive straight in and start using STL early on.


FORTH, a text and reference. Mahlon G. Kelly and Nicholas Spies. ISBN 0-13-326349-5 / ISBN 0-13-326331-2. 1986 Prentice-Hall. Leo Brodie's books are good but this book is even better. For instance it covers defining words and the interpreter in depth.


Online Tutorials
Sun's Java Tutorials - An official tutorial that seems thourough, but I am not a java expert. You guys know of any better ones?
Head First Java - Recommended as a great introductory text by Patrick Lozzi.
Effective Java - Recommended by pek as a great intermediate text.
Core Java Volume 1 and Core Java Volume 2 - Suggested by FreeMemory as some of the best java references available.
Java Concurrency in Practice - Recommended by MDC as great resource for concurrent programming in Java.

The Java Programing Language


Online Tutorials - The online documentation for this language is pretty good. If you know of any better let me know.
Dive Into Python - Suggested by Nickola. Seems to be a python book online.


Online Tutorials
perldoc perl - This is how I personally got started with the language, and I don't think you will be able to beat it.
Learning Perl - a great way to introduce yourself to the language.
Programming Perl - greatly referred to as the Perl Bible. Essential reference for any serious perl programmer.
Perl Cookbook - A great book that has solutions to many common problems.
Modern Perl Programming - newly released, contains the latest wisdom on modern techniques and tools, including Moose and DBIx::Class.


Online Tutorials
Adam Mika suggested Why's (Poignant) Guide to Ruby but after taking a look at it, I don't know if it is for everyone. Found this site which seems to offer several tutorials for Ruby on Rails.
Programming Ruby - suggested as a great reference for all things ruby.

Visual Basic

Online Tutorials
Found this site which seems to devote itself to visual basic tutorials. Not sure how good they are though.


Online Tutorials
The main PHP site - A simple tutorial that allows user comments for each page, which I really like. PHPFreaks Tutorials - Various tutorials of different difficulty lengths.
Quakenet/PHP tutorials - PHP tutorial that will guide you from ground up.


Online Tutorials
Found a decent tutorial here geared toward non-programmers. Found another more advanced one here. Nickolay suggested A reintroduction to javascript as a good read here.

Head first JavaScript
JavaScript: The Good Parts (with a Google Tech Talk video by the author)


Online Tutorials
C# Station Tutorial - Seems to be a decent tutorial that I dug up, but I am not a C# guy.
C# Language Specification - Suggested by tamberg. Not really a tutorial, but a great reference on all the elements of C#
C# to the point - suggested by tamberg as a short text that explains the language in amazing depth


nlucaroni suggested the following:
OCaml for Scientists Introduction to ocaml
Using Understand and unraveling ocaml: practice to theory and vice versa
Developing Applications using Ocaml - O'Reilly
The Objective Caml System - Official Manua


Online Tutorials
nlucaroni suggested the following:
Explore functional programming with Haskell
Real World Haskell
Total Functional Programming


wfarr suggested the following:
The Little Schemer - Introduction to Scheme and functional programming in general
The Seasoned Schemer - Followup to Little Schemer.
Structure and Interpretation of Computer Programs - The definitive book on Lisp (also available online).
Practical Common Lisp - A good introduction to Lisp with several examples of practical use.
On Lisp - Advanced Topics in Lisp
How to Design Programs - An Introduction to Computing and Programming
Paradigms of Artificial Intelligence Programming: Case Studies in Common Lisp - an approach to high quality Lisp programming

What about you guys? Am I totally off on some of there? Did I leave out your favorite language? I will take the best comments and modify the question with the suggestions.

Java: SCJP for Java 6. I still use it as a reference.


O'Reilly Book:

  1. Real World Haskell, a great tutorial-oriented book on Haskell, available online and in print.

My favorite general, less academic online tutorials:

  1. The Haskell wikibook which contains all of the excellent Yet Another Haskell Tutorial. (This tutorial helps with specifics of setting up a Haskell distro and running example programs, for example.)
  2. Learn you a Haskell for Great Good, in the spirit of Why's Poignant Guide to Ruby but more to the point.
  3. Write yourself a Scheme in 48 hours. Get your hands dirty learning Haskell with a real project.

Books on Functional Programming with Haskell:

  1. Lambda calculus, combinators, more theoretical, but in a very down to earth manner: Davie's Introduction to Functional Programming Systems Using Haskell
  2. Laziness and program correctness, thinking functionally: Bird's Introduction to Functional Programming Using Haskell

Some books on Java I'd recommend:

For Beginners: Head First Java is an excellent introduction to the language. And I must also mention Head First Design Patterns which is a great resource for learners to grasp what can be quite challenging concepts. The easy-going fun style of these books are ideal for ppl new to programming.

A really thorough, comprehensive book on Java SE is Bruce Eckel's Thinking In Java v4. (At just under 1500 pages it's good for weight-training as well!) For those of us not on fat bank-bonuses there are older versions available for free download.

Of course, as many ppl have already mentioned, Josh Bloch's Effective Java v2 is an essential part of any Java developer's library.

Let's not forget Head First Java, which could be considered the essential first step in this language or maybe the step after the online tutorials by Sun. It's great for the purpose of grasping the language concisely, while adding a bit of fun, serving as a stepping stone for the more in-depth books already mentioned.

Sedgewick offers great series on Algorithms which are a must-have if you find Knuth's books to be too in-depth. Knuth aside, Sedgewick brings a solid approach to the field and he offers his books in C, C++ and Java. The C++ books could be used backwardly on C since he doesn't make a very large distinction between the two languages in his presentation.

Whenever I'm working on C, C:A Reference Manual, by Harbison and Steele, goes with me everywhere. It's concise and efficient while being extremely thorough making it priceless(to me anyways).

Languages aside, and if this thread is to become a go-to for references in which I think it's heading that way due to the number of solid contributions, please include Mastering Regular Expressions, for reasons I think most of us are aware of... some would also say that regex can be considered a language in its own right. Further, its usefulness in a wide array of languages makes it invaluable.

C: “Programming in C”, Stephen G. Kochan, Developer's Library.

Organized, clear, elaborate, beautiful.


The first one is good for beginners and the second one requires more advanced level in C++.

I know this is a cross post from here... but, I think one of the best Java books is Java Concurrency in Practice by Brian Goetz. A rather advanced book - but, it will wear well on your concurrent code and Java development in general.


C# to the Point by Hanspeter Mössenböck. On a mere 200 pages he explains C# in astonishing depth, focusing on underlying concepts and concise examples rather than hand waving and Visual Studio screenshots.

For additional information on specific language features, check the C# language specification ECMA-334.

Framework Design Guidelines, a book by Krzysztof Cwalina and Brad Abrams from Microsoft, provides further insight into the main design decisions behind the .NET library.

For Lisp and Scheme (hell, functional programming in general), there are few things that provide a more solid foundation than The Little Schemer and The Seasoned Schemer. Both provide a very simple and intuitive introduction to both Scheme and functional programming that proves far simpler for new students or hobbyists than any of the typical volumes that rub off like a nonfiction rendition of War & Peace.

Once they've moved beyond the Schemer series, SICP and On Lisp are both fantastic choices.

For C++ I am a big fan of C++ Common Knowledge: Essential Intermediate Programming, I like that it is organized into small sections (usually less than 5 pages per topic) So it is easy for me to grab it and read up on concepts that I need to review.

It is a must read for me the night before and on the plane to a job interview.

C Primer Plus, 5th Edition - The C book to get if you're learning C without any prior programming experience. It's a personal favorite of mine as I learned to program from this book. It has all the qualities a beginner friendly book should have:

  • Doesn't assume any prior exposure to programming
  • Enjoyable to read (without becoming annoying like For Dummies /
  • Doesn't oversimplify

For Javascript:

For PHP:

For OO design & programming, patterns:

For Refactoring:


  • C - The C Programming Language - Obviously I had to reference K&R, one of the best programming books out there full stop.
  • C++ - Accelerated C++ - This clear, well written introduction to C++ goes straight to using the STL and gives nice, clear, practical examples. Lives up to its name.
  • C# - Pro C# 2008 and the .NET 3.5 Platform - Bit of a mouthful but wonderfully written and huge depth.
  • F# - Expert F# - Designed to take experienced programmers from zero to expert in F#. Very well written, one of the author's invented F# so you can't go far wrong!
  • Scheme - The Little Schemer - Really unique approach to teaching a programming language done really well.
  • Ruby - Programming Ruby - Affectionately known as the 'pick axe' book, this is THE defacto introduction to Ruby. Very well written, clear and detailed.

So I'm working on this class that's suppose to request help documentation from a vendor through web service. I try to name it DocumentRetriever, VendorDocRequester, DocGetter, but they just don't sound right. I ended up browsing through for half an hour trying to come up with an adequate word.

Start programming with bad names is like having a very bad hair day in the morning, the rest of the day goes down hill from there. Feel me?

This is one of the reasons to have a coding standard. Having a standard tends to assist coming up with names when required. It helps free up your mind to use for other more interesting things! (-:

I'd recommend reading the relevant chapter of Steve McConnell's Code Complete (Amazon link) which goes into several rules to assist readability and even maintainability.




I'm a recent AI graduate (circa 2 years) working for a modest operation. It has fallen to me (primarily as I'm the first 'adopter' in the department) to create a basic (read useful?) C# coding standards document.

I think I should explain that I'm probably the most junior software engineer going, but I'm looking forward to this task as hopefully I might actually be able to produce something half usable. I've done a pretty extensive search of the Internet and read articles on what a coding standards document should / should not contain. This seems like a good as place as any to ask for some suggestions.

I realise that I am potentially opening a door to a whole world of disagreement about 'the best way to do things'. I both understand and respect the undeniable fact that each programmer has a preferred method of solving each individual task, as a result I'm not looking to write anything so draconianly proscriptive as to stifle personal flair but to try and get a general methodology and agreed standards (e.g. naming conventions) to help make individuals code more readable.

So here goes .... any suggestions? Any at all?

IDesign has a C# coding standards document that is commonly used. Also see the Framework Design Guidelines 2nd Ed.

I would add Code Complete 2 to the list (I know Jeff is kind of a fan here)... If you are a junior developer, the book comes in handy to set up your mind in a way that sets the foundation for the best code writing practices and software building there are.

I have to say that I came to it a bit late in my career, but it rules a lot of the ways I think about coding and framework development in my professional life.

It's worth checking out ;)

I am a hobbyist programmer (started with VBA to make excel quicker) and have been working with VB.NET / C#.NET and am trying to learn ADO.NET. This is my first post and I apologise for the subjective nature of the question.

A facet of programming that has always frustrated me is what does 'good' look like? I am not a professional so have little to compare against. What makes a better programmer? Is it:

  • They have a better understanding of all the objects / classes / methods in a given language?
  • Their programs are more efficient?
  • The design of their programs are much better in terms of better documentation, good choice of names for functions etc.?

Put another way, if I were to look at the code of a professional programmer, what is the first thing that I would notice about their code relative to mine? For example, I read books like 'Professional ASP.NET' by Wrox press. Are the code examples in that book 'world class'? Is that the pinnacle? Would any top-gun programmer look at that code and think it was good code?

The list below is not comprehensive, but these are the things that I thought of in considering your question.

  • Good code is well-organized. Data and operations in classes fit together. There aren't extraneous dependencies between classes. It does not look like "spaghetti."

  • Good code comments explain why things are done not what is done. The code itself explains what is done. The need for comments should be minimal.

  • Good code uses meaningful naming conventions for all but the most transient of objects. the name of something is informative about when and how to use the object.

  • Good code is well-tested. Tests serve as an executable specification of the code and examples of its use.

  • Good code is not "clever". It does things in straightforward, obvious ways.

  • Good code is developed in small, easy to read units of computation. These units are reused throughout the code.

I haven't read it yet, but the book I'm planning to read on this topic is Clean Code by Robert C. Martin.

Read the book Code Complete. This explains a lot of ideas about how to structure code and the the reasons for doing so. Reading it should short-circuit your time to aquiring the experience necessary to tell good from bad.

i second the recommendation for uncle bob's "clean code". but you may wish to take a look at as i think this deals with your specific question a bit better. good code should leap off the page and tell you what it does/how it works.

Rather then repeat everyone else's great suggestions, I will instead suggest that you read the book Code Complete by Steve McConnell

Essentially it is a book packed full of programming best practices for both functionality and style.

I've been programming a long time, and the programs I see, when they run out of memory, attempt to clean up and exit, i.e. fail gracefully. I can't remember the last time I saw one actually attempt to recover and continue operating normally.

So much processing relies on being able to successfully allocate memory, especially in garbage collected languages, it seems that out of memory errors should be classified as non-recoverable. (Non-recoverable errors include things like stack overflows.)

What is the compelling argument for making it a recoverable error?

OOM should be recoverable because shutdown isn't the only strategy to recovering from OOM.

There is actually a pretty standard solution to the OOM problem at the application level. As part of you application design determine a safe minimum amount of memory required to recover from an out of memory condition. (Eg. the memory required to auto save documents, bring up warning dialogs, log shutdown data).

At the start of your application or at the start of a critical block, pre-allocate that amount of memory. If you detect an out of memory condition release your guard memory and perform recovery. The strategy can still fail but on the whole gives great bang for the buck.

Note that the application need not shut down. It can display a modal dialog until the OOM condition has been resolved.

I'm not 100% certain but I'm pretty sure 'Code Complete' (required reading for any respectable software engineer) covers this.

P.S. You can extend your application framework to help with this strategy but please don't implement such a policy in a library (good libraries do not make global decisions without an applications consent)

What is the difference between a heuristic and an algorithm?

One of the best explanations I have read comes from the great book Code Complete, which I now quote:

A heuristic is a technique that helps you look for an answer. Its results are subject to chance because a heuristic tells you only how to look, not what to find. It doesn’t tell you how to get directly from point A to point B; it might not even know where point A and point B are. In effect, a heuristic is an algorithm in a clown suit. It’s less predict- able, it’s more fun, and it comes without a 30-day, money-back guarantee.

Here is an algorithm for driving to someone’s house: Take Highway 167 south to Puy-allup. Take the South Hill Mall exit and drive 4.5 miles up the hill. Turn right at the light by the grocery store, and then take the first left. Turn into the driveway of the large tan house on the left, at 714 North Cedar.

Here’s a heuristic for getting to someone’s house: Find the last letter we mailed you. Drive to the town in the return address. When you get to town, ask someone where our house is. Everyone knows us—someone will be glad to help you. If you can’t find anyone, call us from a public phone, and we’ll come get you.

The difference between an algorithm and a heuristic is subtle, and the two terms over-lap somewhat. For the purposes of this book, the main difference between the two is the level of indirection from the solution. An algorithm gives you the instructions directly. A heuristic tells you how to discover the instructions for yourself, or at least where to look for them.

So basically I am looking for good templates for writing both technical and functional specs on a project or work request.

What do you use? How deep do you get while writing the specs? Any additional general tips you could provide would be appreciated.

My company needs these badly. I work for a contractor and right now we do not use these documents at all.

EDIT: I have read Joel's take about Painless Specification, I really liked it, but are there any other opinions :)

If you want to purchase a book, Software Requirements by Karl Wiegers has templates for a few documents as an appendix. Unfortunately, I'm at work and that particular book is at home. If someone has it handy, they might be able to confirm that.

You can buy templates from ieee and other places, but I have always ended up making my own.

For a technical spec, "Code Complete" by Steve McDonnell has a good checklist, you can draw some info from that. At my last job, I just made a template out of his section headers, and tweaked it from there.

As far as a functional spec, the important thing is to define all the interfaces:

  1. UI (screen mockups)
  2. Software interfaces (plugins, etc.)
  3. Hardware interfaces (if appropriate)
  4. Communications interfaces (Services, email, messaging, etc.)

There should also be a section for business rules, things that are important functionally that are not covered in any interface definition.

Has anyone seen a recent (and fairly balanced) study into the relative costs for software development using differing languages ? I would particular like to see the relative costs of Java Vs. C# Vs. Delphi.

Quantitative comparisons of this sort would be very hard to pin down, due to the number of complicating variables: developers' experience with the language, suitability of the language to the target domain, developers' overall quality (it's been argued that non-mainstream languages attract higher quality developers), tradeoffs with the resulting product (is a Ruby or Python app as fast as a well-written Delphi or C++ app?), etc.

In Code Complete, 2nd Ed., Steve McConnell lists several languages in terms of their expressive power (how many lines of equivalent C code can be expressed in a single statement of each language). It's been suggested that programmers' productivity in lines of code is relatively constant regardless of language; if this is true, then the expressive power of each language should give a rough estimate of the relative cost of development in each language. From Table 4.1, page 62:

C              1
C++            2.5
Fortran 95     2
Java           2.5
Perl           6
Python         6
Smalltalk      6
Visual Basic   4.5

He lists several sources for this table: Estimating Software Costs, Software Cost Estimation with Cocomo II, and "An Empirical Comparison of Seven Programming Languages" (by Prechelt, from IEEE Computer, October 2000).

The figures that McConnell cites are all several years old, but from what I understand, the Cocomo II model is ridiculously detailed, so current Cocomo II material may offer current numbers on Delphi and C#.

I'm trying to boil down the concepts of coupling and cohesion to a concise definition. Can someone give me a short and understandable explanation (shorter than the definitions on Wikipedia here and here)? How do they interact?


Anybody have a good, short example?

One of the best comprehensive discussions of software design concepts related to OO (including these ones) is Bertrand Meyer's Object Oriented Software Construction.

Regarding 'coupling', he gives his Weak Coupling / Small Interfaces rule as follows:

If two modules communicate, they should exchange as little information as possible.

Meyer's material related to cohesion isn't ever boiled down to a single pithy statement, but I think this sentence from Steve McConnell's Code Complete sums it up pretty well:

Cohesion refers to how closely all the routines in a class or all the code in a routine support a central purpose

I'm expanding my Ruby understanding by coding an equivalent of Kent Beck's xUnit in Ruby. Python (which Kent writes in) has an assert() method in the language which is used extensively. Ruby does not. I think it should be easy to add this but is Kernel the right place to put it?

BTW, I know of the existence of the various Unit frameworks in Ruby - this is an exercise to learn the Ruby idioms, rather than to "get something done".

I think it is totally valid to use asserts in Ruby. But you are mentioning two different things:

  • xUnit frameworks use assert methods for checking your tests expectations. They are intended to be used in your test code, not in your application code.
  • Some languages like C, Java or Python, include an assert construction intended to be used inside the code of your programs, to check assumptions you make about their integrity. These checks are built inside the code itself. They are not a test-time utility, but a development-time one.

I recently wrote solid_assert: a little Ruby library implementing a Ruby assertion utility and also a post in my blog explaining its motivation.. It let you write expressions in the form:

assert some_string != "some value"
assert clients.empty?, "Isn't the clients list empty?"

invariant "Lists with different sizes?" do
    one_variable = calculate_some_value
    other_variable = calculate_some_other_value
    one_variable > other_variable

And they can be deactivated so assert and invariant get evaluated as empty statements. This let you avoid any performance problem in production. But notice that The Pragmatic Programmers recommend against deactivating them. You should only deactivate them if they really affect to the performance.

Regarding to the answer saying that the idiomatic Ruby way is using a normal raise statement, I think it lacks of expressivity. One of the golden rules of assertive programming is not using assertions for normal exception handling. They are two completely different things. If you use the same syntax for the two of them, I think you code will be more obscure. And of course you lose the capability of deactivating them.

You can be convinced that using assertions is a good thing because two must-read classic books like The Pragmatic Programmer From Journeyman to Master and Code Complete dedicate whole sections to them and recommend their use. There is also a nice article titled Programming with assertions that illustrate very well what is assertive programming about and when to use it (it is based in Java, but concepts apply to any language).

We learn programming by writing programs and learning from other programs. Which open source code repositories/programs do you suggest for learning/improving programming?

While citing reference please also mention the thing you liked about it.

If anyone has a copy of Code Reading by Diomidis Spinellis, what open-source projects does he write about there?

@Avinash: If you want to learn more about programming in general, I would recommend both Code Reading and Code Quality by Spinellis. They have code samples from various projects, all FOSS, I believe, so you can not only read about them, but go and get the version discussed in the book and the latest version to read more code from them and learn.

I thoroughly recommend Code Complete 2nd Edition (ISBN: 0735619670) written by Steve McConnell. For the most part, it uses C++ as its lingua franca, however it has occasional mentions of Visual Basic code. In fact, this book was actually used throughout my college's computer science department for advocating good coding practices. And, to be frank, after reading this book, my coding skills and productivity improved by leaps and bounds.

Whilst browsing through answers on SO I came across something that is, in my view, one of the more frequent software development management misconceptions: "[software development] plan is a reasonably detailed description of all the activities you need to undertake".

Hence the question: what is good software development plan? Can it be boiled down just to a work breakdown structure; is WBS the single most important thing for a software development plan anyway?

A Software Development Plan is a specific type of project plan. While a WBS is important, it is only scratching the surface.

A comprehensive project plan should have:

  1. Scope plan (contains the WBS)
  2. Schedule plan
  3. Cost plan
  4. Quality plan
  5. Staffing plan
  6. Communication plan
  7. Risk plan
  8. Procurement plan.

More information on each of these plans can be found in

The project Management Body of Knowledge.

For more specific guidlines, see Code Complete 2.

It's something that's bugged me in every language I've used, I have an if statement but the conditional part has so many checks that I have to split it over multiple lines, use a nested if statement or just accept that it's ugly and move on with my life.

Are there any other methods that you've found that might be of use to me and anybody else that's hit the same problem?

Example, all on one line:

if (var1 = true && var2 = true && var2 = true && var3 = true && var4 = true && var5 = true && var6 = true)

Example, multi-line:

if (var1 = true && var2 = true && var2 = true
 && var3 = true && var4 = true && var5 = true
 && var6 = true)


if (var1 = true && var2 = true && var2 = true && var3 = true)
     if (var4 = true && var5 = true && var6 = true)

Check out Implementation Patterns by Kent Beck. There is a particular pattern I am thinking of that may help in this situation... it is called "Guards". Rather than having tons of conditions, you can break them out into a guard, which makes it clear which are the adverse conditions in a method.

So for example, if you have a method that does something, but there are certain conditions where it shouldn't do something, rather than:

public void doSomething() {
    if (condition1 && condition2 && condition3 && condition4) {
        // do something

You could change it to:

public void doSomething() {
    if (!condition1) {

    if (!condition2) {

    if (!condition3) {

    if (!condition4) {

    // do something

It's a bit more verbose, but a lot more readable, especially when you start having weird nesting, the guard can help (combined with extracting methods).

I HIGHLY recommend that book by the way.

Steve Mcconell's advice, from Code Complete: Use a multi-dimensional table. Each variable serves as an index to the table, and the if statement turns into a table lookup. For example if (size == 3 && weight > 70) translates into the table entry decision[size][weight_group]

In an attempt to be a better programmer, I am planning to read a lot of books and learn at least one new language (which I think is going to be python) during the 3-month long holiday that I am going to have.

The list of books that I am planning to read --


The list of things that I want to do --

  • Start using Linux (probably starting with Ubuntu).
  • Learning to use the bunch of tools mentioned here.
  • Setup a blog (hopefully).

I enjoy watching lectures as well, so, along with Introduction to Algorithms I am going to watch a bunch of Stanford Courses.

A little background: I am a 17 year old guy and I really enjoy programming and every aspect related to it. I have been programming in C and C++ for a while now. The former is more stronger than the latter. I was hoping to utilize the time that I have got on hand thoroughly. Any changes needed with the plan or any additions?

EDIT: Did not mention programming projects.

  1. Making a game using Allegro.
  2. Using QT4 to create a GUI-based database for my school.

Do not just passively read all that information, instead practice after every book chapter or lecture. Practice by writing those algorithms or regexpes for yourself, check your previous code in the light of what code complete has taught you and so on.

If that doesn't give you enough time to do it all, doesn't matter, you better learn properly instead of swallowing material as fast as you can. And all of that is probably too much to do it in only three months anyway, so prioritize by your interests. Take one book and a set of lectures and go at them until finished, then pickup the next, never forgetting to put in practice the concepts shown.

Along with the excellent books you and others listed here I recommend The Little Schemer which will give you a perspective of functional programming. Do not lock yourself into imperative languages (C, C++, C#, Pascal, Java,.. ) while you can dive into different paradigms easily. 17 is a great age :-) Enjoy your journey!

These probably won't fit within your three month plan, but should you wish to master C and Unix, long have I been glad that I spent the time to follow The Loginataka. After that, I found Lions' Commentary on UNIX 6th Edition to be deeply enlightening.

I would add the following two books if you haven't read them already:

Programming Pearls by Jon Bentley

Code by Charles Petzold

Even as it stands, you're going to have a very busy, hopefully productive break. Good luck!

Response to question edit: If you're interested in learning about databases, then I recommend Database in Depth by Chris Date. I hope by "create a GUI-based database" you mean implementing a front-end application for an existing database back end. There are plenty of database solutions out there, and it will be well worth it for your future career to learn a few of them.

I have used OO programming languages and techniques years ago (primarily on C++) but in the intervening time haven't done much with OO.

I'm starting to make a small utility in C#. I could simply program it all without using good OO practice, but it would be a good refresher for me to apply OO techniques.

Like database normalization levels, I'm looking for a checklist that will remind me of the various rules of thumb for a 'good' object oriented program - a concise yes/no list that I can read through occasionally during design and implementation to prevent me from thinking and working procedurally. Would be even more useful if it contained the proper OO terms and concepts so that any check item is easily searchable for further information.

What should be on a checklist that would help someone develop good OO software?

Conversely, what 'tests' could be applied that would show software is not OO?

Steve McConnell's Code Complete 2 contains a lot of ready to use checklists for good software construction.

Robert C. Martin's Agile Principles, Patterns, and Practices in C# contains a lot of principles for good OO desgin.

Both will give you a solid foundation to start with.

Why is theory useful? Do you ever use it in your day-to-day coding? For example, we learned about the halting problem, Turing machines, reductions, etc... a lot of classmates are saying it's abstract and useless and there's no real point to knowning any of it (meaning, you can forget it once the course is over and not lose anything).

If you work in a company that does groundbreaking work, it is important to be able to communicate to architects and developers what the benefits are. There is a lot of hype about all kinds of technologies and positioning yourself can be difficult. When you frame your innovation in scientific and theoretical terms you are definitely at an advantage and customers sense you are the real thing. I can tell folks: there is a new way to deal with state, encoding and nondeterminism (i.e. complexities) and you can definitely be more productive than you are today.

If you take the long view in your career learning about theory will give you depth, the depth you need to grow. The return on investment in learning your 5th or 6th programming language will be a lot less then learning your 2nd and 3rd. Exposure to theory will give you a sense for real engineering, about where the degrees of freedom are and how you can make the right trade-offs.

The important concepts 1) State, 2) Encoding, 3) Nondeterminism. If you don't know them they will not help you. What theory should provide you with is the big picture and a sense of how basic concepts fit together. It should help you hone your intuition.

Example: some of the answers above mention the halting problem and Turing machines. When I came across Turing's paper in college I did not feel enlightened at all. One day I came across Goedel's incompleteness theorem and Goedel numbering in particular. Things started to make a lot of sense. Years later I read about Georg Cantor at a bookstore. Now I really started to understand Turing machines and the halting problem. Try for yourself and look up "Cantor's Diagonal Argument" on Wikipedia. It is one of the most awesome things intellectually you will ever encounter.

Food for thought: A typical Turing machine is not the only way to design a state transition machine. A Turing machine with two rather than one tape would give you a lot more speed for a number of algorithms.

You can expose yourself to these insights more efficiently then I did by reading this book. Link at the bottom of this post. (At the very least, check out the table of contents on Amazon to get a taste of what this is all about):

I found the book by Rosenberg sensational. If you have only one book on theory IMHO this should be the one.

I found that all I need for daily bliss from the CS theoretical world is the utterance of the mantra "Low coupling and High Cohesion". Roger S. Pressman made it scholarly before Steve McConnell made it fashionable.

I think I've become quite good at the basics of programming (for a variety of languages). I can write a *good** line of code. I can write a good method. I can write a good class. I can write a good group of classes. I can write good small or medium application.

I do not however know how to build a good large application. Particularly in the case where multiple technologies are involved and more are likely to become involved with time. Say a project with a large web front-end, a large server back-end that connects to some other integration back-end and finally a large and complex database. Oh, I've been involved in a few of these applications and I could build one I'm sure. I'm not so sure however that it could qualify as "good".

My question is thus for a reference to a book or other good source of reading where I could learn how to distribute and organize code and data for general large projects. For example, would I want to layer things very strictly or would I want to encapsulate it independent units instead. Would I want to try to keep most of the logic in the same pool, or should it just be distributed as it seems most logical when adding whatever feature I'm adding.

I've seen lots of general principals on these issues (e.g. No spaghetti code, meatball code...) and read a few excellent articles that discuss the matter but I've never encountered a source which would lead me to concrete practical knowledge. I realize the difficultly of the question and so I'd be happy to just hear about the readings that others have found to help them in their quest for such knowledge.

As always, thank you for your replies.

****Given the debated nature of the definition of "good" code, the term "good" in this context won't be defined (it means whatever you think it ought to mean).

Here's a book that we have used to guide our coding standards and methods:

alt textLarge-Scale C++ Software Design

The program I'm working on has been in development for almost 10 years since it was first drawn up on the back of the proverbial napkin. And the project is still going strong today. It hasn't been perfect, and there are still problems with circular dependencies and some class interfaces not being very clean, but most classes aren't like that, the program works and our users are happy.

I would also recommend, as has been done before by many others, Code Complete and Software Estimation by Steve McConnell. I particularly like his metaphor of "growing" software rather than constructing or building. This way of viewing software lends itself better to something that will have a long life-cycle.

Personal preferences aside, is there an optimal tab size (2 spaces? 3 spaces? 8 spaces?) for code readability? In the different projects I've worked on, people seem to have vastly different standards. I can't seem to read 2 space indents, but companies like Google use it as a standard.

Can anyone point to documentation, studies, or well-reasoned arguments for the optimal size of a tab?

If we want to get specific, I work mostly in python. The goal of this question is to pick a standard for the team I work on.

I think I recall that there is a section about indentation in Code Complete, quoting some studies about which level of identation makes the code most readable, but I do not have a copy of it with me right now, so I can't check it.

I read Richard P. Gabriel's book "Patterns of Software" (pdf) which contains an essay called "Writing Broadside" in which he contends that programmers should develop their ability to write clearly. I have found that his suggestions have definitely improved my ability to write technical specifications and design documents.

One of his suggestions is to develop writing workshops at work. That this will help improve your ability to express yourself clearly when documenting your designs.

We already have a system where, once a week, a team member gives a Pecha Kucha talk on any topic to help improve our ability to give presentations.

So I'm thinking of suggesting the writing workshops as well.

Does anyone have writing workshops at their place of work?

"A man who has the knowledge but lacks the power clearly to express it is no better off than if he never had any ideas at all." - Thucydides

Edit: What I am talking about here is the ability to document your code in a clear manner, e.g. technical specifications and design documents. Not the writing of the source code itself.

Yes, programmers should document their own work; would you build a house from a one-eighth-scale model and trust that blueprints weren't necessary?

Worth reading: Steve McConnell's Code Complete, especially the chapters on naming functions and writing documentation. If you're naming and commenting your code appropriately, the docs write themselves.

I have been coding for a while now and am confortable with a few programming languages but now feel that I need to improve my problem solving or program design technique.

What skills do I need have to be able to design a program as a solution to a problem?

More: The question is about how to design a solution rather than how to code better.

For problem solving technique, look at how other people have solved problems. I recommend The Algorithm Design Manual and Algorithms in a Nutshell. Both of these books take you from a problem statement through several iterations of solutions to show you the thought process, rather than going directly to the final solution.

Talking about books, I suggest this great one, by Martin Fowler

Refactoring: Improving the Design of Existing Code

I've heard that it is a good technique to define your variables at the top of a function, so you don't end up with variable hoisting problems. This:

// Beginning of file

function something(){
    var a, b, c = 1, d, e;
    // Do something

// End of file

is a good example (excluding the bad variable names, of course).

My question is: Is this always the best approach? What if you are working with a lot of variables? Should they really all just be plopped on one line?

I'd highly suggest giving Code Complete 2 by Steve McConnell a read. His argument is that you should neither declare all of your variables in one line, nor should should declare them all at the top of a routine. So, don't do this:

function foo() {
    var a,

      * 20 lines that use a and b

      * 10 lines that use c and d

Instead, you should declare your variables close to where they are needed. In the above code, that might look like this:

function foo() {
    var a,

      * 20 lines that use a and b

     var c,

      * 10 lines that use c and d

The benefits are that you can understand at a quick glance what variables are used by a block of code by just looking at the declarations above it. You don't need to read the code to see what variables are set, just which are declared.

Don't write code for the compiler or for the computer. Write it for developers. Your code should be as easy to read as possible, and require as little effort as possible to understand a particular section of code.

We are building an ASP.NET application and would like to follow the best practices. Some of the best practices are:

Server side Code:

  • Use catch blocks to trap & log low level errors too.
  • Use Cache objects to populate dropdowns etc. where we won’t expect the changes in the underlying data/database.
  • In case of error logging framework, provide emailing alerts along with logging the errors.

HTML code: - Don’t write inline CSS. - Place the JavaScript code (If needed by the page) at the end of the page unless the page needs it for load time actions.

Now coming to the point, Would you please share these best practice points if you have a comprehensive list of them already?

I would recommend a couple of books if you are interested in pursuing a journey to become a better, more productive developer. These books are language agnostic and as you can see by the user reviews, very very helpful.

Code Complete 2

Pragmatic Programmer

If you are looking for a .NET specific book, you may appreciate the following book:

Microsoft Application Architecture Guide [available online for free outside of print format]

While the C# spec does include a pre-processor and basic directives (#define, #if, etc), the language does not have the same flexible pre-processor found in languages such as C/C++. I believe the lack of such a flexible pre-processor was a design decision made by Anders Hejlsberg (although, unfortunately, I can't find reference to this now). From experience, this is certainly a good decision, as there were some really terrible un-maintainable macros created back when I was doing a lot of C/C++.

That said, there are a number of scenarios where I could find a slightly more flexible pre-processor to be useful. Code such as the following could be improved by some simple pre-processor directives:

public string MyProperty
  get { return _myProperty; }
    if (value != _myProperty)
      _myProperty = value;
      // This line above could be improved by replacing the literal string with
      // a pre-processor directive like "#Property", which could be translated
      // to the string value "MyProperty" This new notify call would be as follows:
      // NotifyPropertyChanged(#Property);

Would it be a good idea to write a pre-processor to handle extremely simple cases like this? Steve McConnell wrote in Code Complete (p208):

Write your own preprocessor If a language doesn't include a preprocessor, it's fairly easy to write one...

I am torn. It was a design decision to leave such a flexible pre-processor out of C#. However, an author I highly respect mentions it may be ok in some circumstances.

Should I build a C# pre-processor? Is there one available that does the simple things I want to do?

Consider taking a look at an aspect-oriented solution like PostSharp, which injects code after the fact based on custom attributes. It's the opposite of a precompiler but can give you the sort of functionality you're looking for (PropertyChanged notifications etc).

This is related to a chapter from beautiful code. And in that chapter I read about the nested ifs.

The author was talking about deeply nested ifs as orginator of bugs and less readable. And he was talking about replacing nested ifs with case statements and decision tables.

Can anybody illustrate how to remove nested ifs with case (select case) and decision tables ?

Well, not directly an answer to your question since you specifically ask about switch/case statements, but here is a similar question.

Invert “if” statement to reduce nesting

This talks about replacing nested if's with guard-statements, that return early, instead of progressively checking more and more things before settling on a return value.

For decision tables, please see my answer to this question, or better still read chapter 18 in Code Complete 2.

I am looking for studies about applied code quality, comparing costs before and after. They should show a clear benefit in cost (or maybe a negative effect of too much cost). I need hard facts like (entirely fictional):

After we added static code analysis to our build, the number of defects reduced to half. So we saved approx. 10 developer days effort per iteration on bug fixing. Extra cost by buying and setting up the analysis was x$. Development was slowed down 0.1% by obeying the analysis results, increasing the total development effort by 5 days per iteration. During the first half year the initial cost was returned. etc. Now we save approx. y$ per iteration.

I only know one such story given in Code Complete 2nd Ed. It is talking about Boeing that defects decreased after adding reviews during the QA process (AFAIK). Unfortunately most shops would not compare with Boeing, so studies from Boeing do not count.

Do you know such studies or do you have any hard data from your shop?

There is a related question, but does not give any hard data.

The books Code Complete 2 and Rapid Development have lots of examples from real life case studies and experiments. Almost everything they argue is backed up with hard facts.

When can a design pattern make your software worse?

I have seen a program where they used the facade pattern between the GUI and logic. They considered that no objects may be transported over this, so only primitive types were used which made it difficult to code.

A design pattern can make your software worse if you misuse it, applying it in a wrong situation. Common sense should be the perfect companion for a design pattern.

The misuse of patterns sometimes is associated with a lack of understanding of the forces associated with the problem and the pattern proposed solution to that problem. Also the intent of the pattern could be misunderstood leading to misuses.

To a better comprehension of patterns you also need to learn about the Anti pattern concept and read a lot more than the GOF book. One suggestion is to read Pattern Hatching to complement GOF's concepts.

For decades programmers have often spent time making software worse by applying practices they don't understand, rather than learning why that practice is good. The use of certain tools/keywords/frameworkes can make a programmer feel sophisticated, when they're just screwing things up. Some common examples:

  • Throwing in lots of try/catch/finally blocks can make a programmer feel like they have an error handling scheme, when they in fact don't.
  • Replacing sql with stored procedures can make programmers feel like they have a data access layer, that magically gives them efficiency, reuseability, encapsulation, etc.
  • Using classes makes many programmers believe they're engaging in object oriented programming.

This list could go on forever. This tendency has always been around and always will be, because we humans always look for shortcuts. We see this a lot with patterns now because patterns are currently popular. The root problem is the same: developers not having a respect for how complicated software development is, and thinking a blindly implemented pattern or practice is a substitute for learning and humility.

The solution: difficult to say. You can't go to wrong though handing a junior developer Code Complete or some such, to help them understand this is all about applying principles to particular situations, and that great developers keep it simple.

For the last, hmm, 6 months I've been reading into Programming in C, I got myself K&Rv2, BEEJ's socket guide, Expert C programming, Linux Systems Programming, the ISO/IEC 9899:1999 specification (real, and not draft). After receiving them from Amazon, I got Linux installed, and got to it.

I'm done with K&R, about halfway through Expert C Programming, but still feel weak as a programmer, I'm sure it takes much more than 6 months of reading to become truly skilled, but my question is this:

I've done all the exercises in K&Rv2 (in chapter 1) and some in other chapters, most of which are generally really boring. How do I lift my skills, and become truly great? I've invested money, time and a general lifestyle for something I truly desire, but I'm not sure how exactly to achieve it.

Could someone explain to me, perhaps if I need to continuously code, what exactly I'm to code? I'm pretty sure, coding up hello world programs isn't going to teach me any more than I already know about anything.

A friend of mine said "read" (with emphasis on read) a man page a day, but reading is all I do, I want to do, but I'm not sure what! I'm interested in security, but I'm not sure as a novice what to code that would be considered enough.

Ah, I hope you don't delete this question :)


Consider reading more content on the "concepts" of programming than purely syntax:

Join a mailing list/newsletter/magazine/podcasts for programmers in your area of expertise:;jsessionid=XVZEO0SKOCRRBQE1GHPCKH4ATMY32JVN

Visit and try to solve issues periodically to give yourself a mental challenge and help others.

I code for a while now and learned several programming languages. I programmed a lot of little tools and such. I think I master the act of coding itself pretty good so I have e.g. no problems with syntax, I got a pretty good understanding of what is going on under the hood (reasonable assembler knowledge) and so on.

My big problem though is: I just can't design bigger/more complex applications. I learned the principles of OOP, design patterns, learned some basic lisp programming and all stuff I could find and thought it would help me with my issue.

But no matter what I try, how long I try: I just can't get it right. My designs always seem wrong to me somehow. Cause of that I never drawn through a bigger project, i'm kinda never satisfied with the structure of my program.

Have you had a similiar problem? How did you manage to solve it? Got you any hints for me on how to go on?

Design itself is an iterative activity. You first start with a certain design and then in phases you make it better.

Design is not about achiveing perfection, which can't even be achieved in larger application. Design is all about making good balances and tradeoffs that in the end provide a good, robust and maintainable application, that conforms to the requirements.

You will never get it 100% right on all sides.

Reading a few good books on design/architecture will not make you directly a rock star on the matter but it will certainly give you the tools that you can use to improve and perfect your skills.

Here are some examples of books:

Of course practical experience also counts.

I would be appreciative if someone could explain to me the difference between the following two pieces of code in terms of Visual Studio's Code Metrics rules. Why does the Maintainability Index increase slightly if I don't encapsulate everything within using ( )?

Sample 1 (MI score of 71)

public static String Sha1(String plainText)
    using (SHA1Managed sha1 = new SHA1Managed())
        Byte[] text = Encoding.Unicode.GetBytes(plainText);
        Byte[] hashBytes = sha1.ComputeHash(text);
        return Convert.ToBase64String(hashBytes);    

Sample 2 (MI score of 73)

public static String Sha1(String plainText)
    Byte[] text, hashBytes;
    using (SHA1Managed sha1 = new SHA1Managed())
        text = Encoding.Unicode.GetBytes(plainText);
        hashBytes = sha1.ComputeHash(text);
    return Convert.ToBase64String(hashBytes);   

I understand metrics are meaningless outside of a broader context and understanding, and programmers should exercise discretion. While I could boost the score up to 76 with return Convert.ToBase64String(sha1.ComputeHash(Encoding.Unicode.GetBytes(plainText))), I shouldn't. I would clearly be just playing with numbers and it isn't truly any more readable or maintainable at that point. I am curious though as to what the logic might be behind the increase in this case. It's obviously not line-count.

Because of the increased distance between the declaration of your variables and where they are used.

The rule is to reduce the variable span as much as possible, the span is the distance between the declaration of the variable and where it is used. As this distance increases, the risk increases that later code is introduced that affects the variable without the programmer realising the impact further down in the code.

Here is a link to a good book that covers this and many other topics on code quality.

Statistical analysis/programming, is writing code. Whether for descriptive or inferential, You write code to: import data, to clean it, to analyse it and to compile a report.

Analyzing the data can involve many twists and turns of statistical procedures, and angles from which you look at your data. At the end, you have many files, with many lines of code, performing tasks on your data. Some of which is reusable and you capsulate it as a "good to have" function.

This process of "Statistical analysis" feels to me like "programming" But I am not sure it feels the same to everyone.

From the Wikipedia article on Software development:

The term software development is often used to refer to the activity of computer programming, which is the process of writing and maintaining the source code, whereas the broader sense of the term includes all that is involved between the conception of the desired software through to the final manifestation of the software. Therefore, software development may include research, new development, modification, reuse, re-engineering, maintenance, or any other activities that result in software products. For larger software systems, usually developed by a team of people, some form of process is typically followed to guide the stages of production of the software.

According to this simplistic definition (and my humble opinion), this sounds very much like building a statistical analysis. But I imagine it is not that simple.

Which leads me to my question: what differences can you outline between the two activities?

It can be in terms of the technical aspects, the different strategies or work styles, and what ever else you think is relevant.

This question came to me from the following threads:

As I said in my response to your other question, what you're describing is programming. So the short answer is: there is no difference. The slightly longer answer is that statistical and scientific computing should require even more controls around development than other programming.

A certain percentage of statistical analysis can be done using Excel, or in a point-and-click approach using SPSS, SAS, Matlab, or S-Plus (for instance). A more sophisticated analysis done using one of those programs (or R) that involves programming is clearly a form of software development. And this kind of statistical computing can benefit immensely from following all the best practices from software development: source control, documentation, a project plan, scope document, bug tracking/change control, etc.

Moreover, there are different kinds of statistical analyses that can follow different approaches, as with any programming project:

  • Exploratory data analysis should follow an iterative methodology, like the Agile methodology. In this case, when you don't know explicity the steps involved up front, it's critical to use a development methodology that is adaptive and self-reflective.
  • A more routine kind of analysis (e.g. an government annual survey such as the Census) could follow a more traditional methodology such as the waterfall approach since it would be following a very clear set of steps that are mostly known in advance.

I would suggest that any statistician would benefit from reading a book like "Code Complete" (look at the other top books in this post): the more organized you are with your analysis, the greater the likelihood of success.

Statistical analysis in some sense requires even more good practices around version control and documentation than other programming. If your program is just serving some business need, then the algorithm or software used is really of secondary importance so long as the program functions the way the specifications require. On the other hand, with scientific and statistical computing, accuracy and reproducibility are paramount. This is one of John Chambers' (the creator of the S language) major emphases in "Software for Data Analysis". That is another reason to add literate programming (e.g. with Sweave) as an important tool in the statistician's toolkit.

The following have been proposed for an upcoming C++ project.

  • C++ Coding Standards, by Sutter and Alexandrescu
  • JSF Air Vehicle C++ coding standards
  • The Elements of C++ Style
  • Effective C++ 3rd Edition, by Scott Meyers

Are there other choices? Or is the list above what be should used on a C++ project?

Some related links

I've written a coding standard for a major British company and was very conscious of putting reasons why I selected certain things rather than just make it a bunch of "Thou shalt" pronouncements. (-:

As a quick way out, I'd suggest mandating:

  • Scott Meyers's Effective C++ 3rd Edition (Amazon link) - if you can find a copy of the 1st edition of this book then buy it for the overview of OO design which was removed from later editions. )-:
  • Scott Meyer's book Effective STL (Amazon link) - you must use STL to use C++ efficiently.
  • Steve McConnell's book Code Complete 2 (Amazon link) - not C++ specific but full of great insights.

C++ Coding Standards: 101 Rules, Guidelines, and Best Practices (C++ In-Depth Series) by Herb Sutter and, Andrei Alexandrescu.

Can any one suggest what is the best way to write good code that is understandable without a single line of comments?

Read Code Complete, 2nd Edition cover to cover. Perhaps twice.

To give some specifics:

  • Making code readable
  • Eliminating code repetition
  • Doing design/architecture before you write code

Clean Code by Robert C. Martin contains everything you need to write clean, understandable code.

I have been learning C++ for three months now and in that time created a number of applications for my company. I consider myself fairly comfortable with C++ / MFC and STL, however I don't just want to be an OK programmer, I want to be a good programmer. I have a few books on best practices but I was wondering if anyone could suggest reading materials that helped them and any disciplines which should be encouraged?


You can check out the Boost library and a number of the books written about it. While this may not have been what you had in mind, IMO, the Boost libraries are examples of well-designed modern C++ libraries that use the features of the language in pretty much the way they should be used to create among the most effective solutions for their problem domain. Granted of course, there are bizarre libraries like preprocessor and MPL which make you wonder if you'll ever have any use for them, but they're all round quite good. From my own experience, exploring the library and its literature has given me insight into how C++ can be used effectively.

Boost Beyond the C++ Standard Library: An Introduction to Boost

For C++, Scott Meyers books are very good, and will help take you to the next level.

If you don't already have it C++ by Bjarne Stroustrup, 3rd Edition

3 months into c++ and you're already comfortable with it? Sheesh, I've been learning c# for over a year and have taken numerous Microsoft courses and I'm nowhere near comfortable with it.

That being said, you'll hear Code Complete tossed about as a very good book. I'm in the process of reading it now.

I lot of folks can suggest more modern, up-to-date books. But I still recommend The Annotated C++ Reference Manual by Margaret A. Ellis & Bjarne Stroustrup.

The ARM was published back in '90. It's become somewhat outdated with respect to templates. STL is (obviously) absent. (Though the website at does a good job of covering STL!)

However, the ARM is dirt cheap (used). (Shipping will exceed the cost of the book.) Its signal-to-noise ratio remains off the scale. It's very good at digging into C++'s dirty areas, explaining what was done & why.

I still use it as a reference. I rank it up there with K&R.

Good blogs: Guru of the Week, and all the books by Herb Sutter. Those will give you quite a lot to chew already.

Modern C++ Design by Alexandrescu if you want to get a good feel for what you don't yet know, and probably don't want to know.

Code Kata's for practice!

As I see, nobody mentioned Bruce Eckel brilliant books "Thinking in C++". IMHO, it`s one of the best books to start your C++ development from. From my point of view, first volume is more helpful that the second, but both of them worth reading.

I work in the technical group at a large Architecture firm. While there are a number of people here that are very proficient at various programing and scripting languages, it's far from the environment of the development environment of a software company. Rarely are there ever more then one or two people working on a development project, most of which are just automation scripts. There's no source control, or version control setup or anything like that.

My academic background is engineering but not CS or CE. So I've taking a number of programing classes in school, and actually tinkered a lot with VB back when I was a little kid. Yet it wasn't until this past year at my current job have I really had the opportunity to do any real development outside of homework problems in school. In the past year I've transitions from making simple automation scripts to full blow plug-in applications in C#.NET.

I have family and friends that are CS or CE majors, and work for companies whose main product is software. While have no interest in actually working for a software company, but it seems like they do have a major advantage over me. At their work they have people with more experience that can look over their should and give them suggestions to improve their code or logic. Sites like Stack Overflow are great for solving bugs and getting advice, but only when you know you need the advice. I'm sure there are many times when I'm taking the completely wrong approach to something.

What are some of the best ways to improve these real life programing skills? Is there a way to find open source projects (preferably .Net based) that I can help work on, so I can gain some experience working with other (more experienced) programmers? I've also been looking into design patterns, but have a hard time recognizing when to use certain patterns.

I'm going to go out on a limb here and suggest that you write some code.

Read Code Complete. Look at other questions about this topic here at StackOverflow. Find an open source project and contribute to it. Start your own pet project that focuses on the skills you're interested in.

When to use Design patterns
How to get into C# Open Source projects

Possible Duplicate:
C and C++ : Partial initialization of automatic structure

While reading Code Complete, I came across an C++ array initialization example:

float studentGrades[ MAX_STUDENTS ] = { 0.0 };

I did not know C++ could initialize the entire array, so I've tested it:

#include <iostream>
using namespace std;

int main() {
    const int MAX_STUDENTS=4;
    float studentGrades[ MAX_STUDENTS ] = { 0.0 };
    for (int i=0; i<MAX_STUDENTS; i++) {
        cout << i << " " << studentGrades[i] << '\n';
    return 0;

The program gave the expected results:

0 0
1 0
2 0
3 0

But changing the initialization value from 0.0 to, say, 9.9:

float studentGrades[ MAX_STUDENTS ] = { 9.9 };

Gave the interesting result:

0 9.9
1 0
2 0
3 0

Does the initialization declaration set only the first element in the array?

If you use anything but empty braces then you only initialize the first N positions to that value and all others are initialized to 0. In this case, N is the number of arguments you passed to the initialization list, i.e.,

float arr1[10] = { };       // all elements are 0
float arr2[10] = { 0 };     // all elements are 0
float arr3[10] = { 1 };     // first element is 1, all others are 0
float arr4[10] = { 1, 2 };  // first element is 1, second is 2, all others are 0

Possible Duplicate:
Do you comment your code?

What are the different standards and practices of code documentation and which have worked best for you?

I ask because myself and friend are starting a company and will probably need to have some agreed upon standard for commenting things so that we can read each other's code more easily.

Simply put, comment on the why, not the how. I realize that this is redundant given the other answers, but it cannot be stated enough.

Write your code and comments so that it will be easy to read when you have to come back in 6 months and fix the bugs that will be there. Also, if the code is written such that the logic and intent are clear, beware of over-commenting. This can make the code hard to read. In other words, be nice to the person that has to maintain the code.

I highly recommend reading Code Complete and the section on commenting there.

The code in this question made me think

assert(value>0); //Precondition
if (value>0)

I never write the if-statement. Asserting is enough/all you can do. "Crash early, crash often"

CodeComplete states:

  • The assert-statement makes the application Correct
  • The if-test makes the application Robust

I don't think you've made an application more robust by correcting invalid input values, or skipping code:

assert(value >= 0 );  //Precondition
assert(value <= 90);  //Precondition
if(value < 0)         //Just in case
  value = 0;
if (value > 90)       //Just in case
  value = 90;

These corrections are based on assumptions you made about the outside world. Only the caller knows what "a valid input value" is for your function, and he must check its validity before he calls your function.

To paraphrase CodeComplete: "Real-world programs become too messy when we don't rely solely on assertions."

Question: Am I wrong, stuborn, stupid, too non-defensive...

First let me say, I am not a coder but I help manage a coding team. No one on the team has more than about 5 years experience, and most of them have only worked for this company.. So we are flying a bit blind, hence the question.

We are trying to make our software more stable and are looking to implement some "best practices" and coding standards. Recently we started taking this very seriously as we determined that much of the instability in our product could be linked back to the fact that we allowed Warnings to go through without fixing when compiling. We also never bothered to take memory leaks seriously enough.

In reading through this site we are now quickly fixing this problem with our team but it begs the question, what other practices can we implement team wide that will help us?

Edit: We do fairly complex 2D/3D Graphics Software that is cross-platform Mac/Windows in C++.

The first thing you need to consider when adding coding standards/best practices is the effect it will have on your team's morale and cohesiveness. Developers usually resent any practices that are imposed on them even if they are good ideas. The people issues have to be addressed for a big change to be successful.

You will need to involve your group in developing the standards and try to achieve consensus. That said, you will never get universal agreement on anything, so you will have to balance consensus and getting to standards. I've seen major fights over something as simple as tabs versus spaces in source.

The best book I've seen for C/C++ guidelines in complicated projects is Large Scale C++ Software Design. That book along with Code Complete (which is a must-read classic) are good starting points.

I recently forced myself to study C++ and I just finished reading the book C++: The Complete Reference, by Herbert Schildt. I liked the book and think I got more or less the big picture. I noticed though that when I try to check with other people the things I code using the material I learned, they are usually considered non-idiomatic and superseded by an STL way to do it that is safer and easier (well, the book doesn't cover STL and Boost libraries).

So I'd like to ask: what are good sources to learn the patterns of a good C++ program? Where can I learn basic patterns from the "C++ way" to do things and not just repeating C patterns in C++?

I'd be particularly interested in sources that included STL and Boost stuff.

I'd (also) recommend:

  • Effective C++, Effective STL by Steve Myers. They are easy to digest, yet very valuable - sometimes even illuminating.
  • Code Complete (The 1993 edition is available cheaply and not that much different). It's lengthy, but it walks across the entire field from what it means to be a programmer to how a for loop should look. (It would be better if it was shorter - the way it is it covers so much ground it's hard to place it). I hold it dear because it illustrate two points very well:
    • code is compromise
    • There are know facts, (but we still manage to get by by gut feel)
  • C++ FAQ Lite / C++ FAQ.
  • I'd throw in Facts and Fallacies by Robert Glass - it doesn't fit your request very well, but go read it.

It's natural that you are unhappy with other people's code. That's typical for programming - heck, even my own code of five years ago was written by a total n00b. That might be more articulated for C++, since it caters for different styles, and often puts freedom ("you can") over guildelines ("that's the way").

Still, mulling over existing code - yours or others - and considering how it can be improved. Also, figuring out why it is the way it is sometimes helps.

(I remember a few TheDailyWTF's where everyone would chime in how stupid and unreasonable this is - yet somewhere, buried among the me too's, was someone with domain experience explaining convincingly under what circumstances this was better than the obvious solution).

You might wnat to check out The Definitive C++ Book Guide and List

For your purposes I would especially recommend:

They are not in particular order, also you might want to read and code something in between them.

(Note: As noted by @John Dibling the Boost book might be a bit out of date, I do not have experience with that one myself)

I've been writing PHP for about six years now and have got to a point where I feel I should be doing more to write better code. I know that Object Oriented code is the way to go but I can't get my head around the concept.

Can anyone explain in terms that any idiot can understand, OO and how it works in PHP or point me to an idiots guide tutorial?

I recommend reading Code Complete. The few links on PHP OO are great, but this gets to the bread and butter of the idea.

Where can I find samples of C# code written by top programmers for learning?

I found the following book greatly useful when I started programming:

Code Complete: A Practical Handbook of Software Construction (Steve McConnell - Microsoft Press)

In addition to the many suggestions on source code, I would also recommend the book Framework Design Guidelines as it has numerous useful dos and don'ts, which I find very helpful. There's also a section on MSDN with some of the same topics.

How much percent of time do you spend on documentation per day as software developer and ideally how much time should given to documentation ?

Very little of my time is spent documenting code and most days the "percentage of time" is zero.

This is kind of an "old school" question. Time and experience have proven that code documentation is not the best use of your resources. Instead, the code should be written to be readable from the start - it should be self-documenting. Documentation, used sparingly, can be applied in just those places where the underlying logic cannot be made clear enough via the code itself.

If you want to learn more about the roots of this idea - and how to create self-documenting code - then the reference work is Robert C. Martin's "Clean Code". For a deeper analysis of the art as a whole (but less focus on coding standards and documentation) I'd also strongly suggest that you get the "Bible": McConnell's Code Complete 2.

Update: Here is an example of what I am talking about. Let's say that you want to have a client verify their demographic information (name, birthdate, gender, email) in a web app.

Old school developers might create a page "Verify.aspx" where the Page_Load pulls initial client demographics from the database and places the field data in UI controls. In the submit response, they might apply some business logic ("Ensure that a newly entered EMail doesn't conflict with another record's email", "check that firstname is not empty", etc.) and then populate database parameters, fill a SqlCommand object with a SQL insert statement and execute it. Yuck...but definitely how most code I've seen operates. You're likely to be tempted to explain each of these steps so people know when you've switched from DB logic to Business Logic to UI logic, etc. (e.g. "The session must have a CustomerID set").

Now, if your code is properly segmented - into database, business logic and UI layers - and you have named things in a clear and obvious way then you'll find the code itself to be easy to read. Here is (pseudocode) for how I handle this task in one of my projects:

protected void PageLoad()
  using (var customerLogic = new CustomerLogic(sessionData))
     Customer customer = customerLogic.Fill();
     if (customer.Status == fcError.mbInvalidCustomerID) 
       throw new Exception("Invalid customer ID in validation request...");

protected void SubmitHandler()
  var customer = new Customer();
  if (!CopyUIFieldsToCustomerDataObject(customer)) return;
  using (var customerLogic = new CustomerLogic(sessionData))
     if (customer.Status == fcError.mdDuplicateEmail)
       ErrorMessage.Text = "This email is already in use by the client having an ID of " + customer.SigninID;

protected bool CopyUIFieldsToCustomerDataObject(Customer customer)

protected void CopyCustomerDataToUIFields(Customer customer)

Truly, what is there to document? Are you really going to put in a comment like this:

 // Copy all of the customer data in the customer object to the UI fields

No! Because your naming conventions are well thought out and your code is all at the same level of abstraction, the documentation is completely redundant.

If the next developer wants to know more about the Business Logic, they'll open up the CustomerLogic class - which is an entirely separate and self-contained world. This kind of segmentation lets the developer work at a level of abstraction wherein the logic of their task is obvious. In the end, the need for documentation falls dramatically if you follow descriptive naming conventions and avoid mixing code that implements multiple levels of abstraction.

One last thing: segmentation also facilitates unit testing as well. This code is an almost straight paraphrase of code in a project I am working on now so this isn't just theory.

I always wondered: Are there any hard facts which would indicate that either shorter or longer identifiers are better?



opposed to


Short identifiers should be faster to read because there are fewer characters but longer identifiers often better resemble natural language and therefore also should be faster to read.

Are there other aspects which suggest either a short or a verbose style?

EDIT: Just to clarify: I didn't ask: "What would you do in this case?". I asked for reasons to prefer one over the other, i.e. this is not a poll question.

Please, if you can, add some reason on why one would prefer one style over the other.

Personally I like to try a follow the wisdom of Clean Code by Uncle Bob.

To me it suggests that Code should read like prose. By using descriptive names and ensuring that methods have a single responsibility we can write code that accurately describes the programmers intent obviating the need for comments (in most cases).

He goes on to make the observation that when we write code, we often spend 90% of the time reading the surrounding code and dependent code. Therefore by writing code that is inherently readable we can be far more productive in our writing of code.

Please go directly to the relevant chapter of Steve McConnell's "Code Complete" (sanitised Amazon link).

Specifically, chapter 11, "The Power of Variable Names".

I need a urgent crash course on software development. We are about to start a major project at work. The project is a huge web application and I have been chosen to lead the team. The problem now is, I am not familiar with the software development process. The way I used to go about my development is think it and do it, but now I won't be able to do that because I have to collaborate with others. I will very much appreciate if someone can help with a link to a crash course. Thanks.

You want to look at Construx. It's owned by Steve McConnell, the author of Code Complete.

You'll probably want to read Rapid Development.

Bug tracking, source control and code reviews are your friend. You might want to read Agile Software Development and adapt some of those processes to your needs.

It seems to be an article of faith that top-flight programmers are several times as productive as mediocre programmers. Can anyone point to studies or reports which support this with hard data?

It's hard to find hard data on anything related to programming, but I'll second the suggestion by Shiraz Bhaiji that you look at Code Complete by Steve McConnell. You might want to look at the next book he wrote, Rapid Development, as well. It also has some statistics.

As comments above pointed out, you need a good definition of productivity. Some people say that experienced and inexperienced programmers both write about the same number of lines of code per day but experienced programmers keep more lines of code per day. The inexperienced programmers will spin their wheels and rewrite most of today's code tomorrow.

There's some reference on this point in The Mythical Man-Month; I don't have my copy with me right now to see if the information is well-sourced, though.

I've always followed the logic: if assert fails, then there is a bug. Root cause could either be:

  • Assert itself is invalid (bug)
  • There is a programming error (bug)
  • (no other options)

I.E. Are there any other conclusions one could come to? Are there cases where an assert would fail and there is no bug?

Yes, there is a bug in the code.

Code Complete

Assertions check for conditions that should never occur. [...]

If an assertion is fired for an anomalous condition, the corrective action is not merely to handle an error gracefully- the corrective action is to change the program's source code, recompile, and release a new version of the software.

A good way to think of assertions is as executable documentation - you can't rely on them to make the code work, but they can document assumptions more actively than program-language comments can.

One of my personal programming demons has always been complex logic that needs to be controlled by if statements (or similiar). Not always necessarily that complex either, sometimes just a few states that needs to be accounted for.

Are there any tools or steps a developer can perform during design time to help see the 'states' and take measures to refactor the code down to simplify the resulting code? I'm thinking drawing up a matrix or something along those lines...?

If you haven't already, I'd highly suggest reading Code Complete. It has a lot of advice on topics such as this. I don't have my copy handy at the moment, otherwise I'd post a summary of this section in the book.

This is not my code. I took this code off this website:

I am using for reference material on how to build a linked list. I'm a little confused on what is going on. Can someone please explain to me what is going on. I'll mark what is confusing me with 1-5.


struct list_el {
   int val;
   struct list_el * next;

typedef struct list_el item;

void main() {
   item * curr, * head;
   int i;

   head = NULL;   //1

   for(i=1;i<=10;i++) {
      curr = (item *)malloc(sizeof(item));
      curr->val = i;
      curr->next  = head; //2
      head = curr; //3

   curr = head; // 4

   while(curr) {  //5
      printf("%d\n", curr->val);
      curr = curr->next ;
  1. head = NULL → why is head being set to NULL? I know that you are supposed to (I do it out of habit) but I don't really know why.

  2. curr->next = head → I never really understood this as well. Maybe I have my definition of "head" wrong but in a regular linked list, is it the starting node or the last node on the list? I've always assumed it was the starting node but in this line it looks like it's the last node.

  3. head = curr → Why are we setting it equal to curr?

  4. curr = head → and then setting curr = head after the loop is done.

  5. while(curr) → Just to make sure, this is traversing through the list and it is equivalent to while(curr != NULL) right?

#1: head = NULL

Initializing the pointer. It's generally recommended to initialize the pointer to NULL either (1) at declaration or (2) immediately after declaration. If programmers mistakenly dereference uninitialized pointers, garbage values are returned. Often times, this is extremely hard to debug if your static analyzer and compiler don't display warning or error messages for uninitialized pointers.

For more information, please refer to Steve McConnell's Code Complete: A Practical Handbook of Software Construction or Wikipedia page on Defensive Programming.

#2: curr->next = head

Building the linked list. The curr node is being "linked" to previously created node in the sequence.

#3: head = curr

Updating the head pointer. The head pointer is being updated to point to the most recently malloced node.

Below illustrations visualize Steps #2 and #3:

first second third forth and final

#4: curr = head

Re-initializing the pointer. This step is similar to step #2:curr->next = head. By setting curr node to head, curr gets "ready" for linked-list traversal in the while loop. Analogically speaking, it's like initializing the iterating variable to 0 in the beginning of the loop (i.e. i = 0). To visualize this step, please refer to the below illustrations showing before/after this statement is executed:



#5: while(curr)

Traversing the list. Given that curr is pointing to the first node (from step #4), this while loop traverses the list until curr->next returns NULL. In a less abstract form, we can rewrite this statement as while(curr != NULL).

Consider the two code segments below. Which one is better and Why? If you have any other idea, please do mention. Where can I find answers to coding practices like these? If you are aware of any book / article, please do share.

//Code 1

bool MyApplication::ReportGenerator::GenerateReport(){
    bool retval = false;
        if (!isAdmin()){
        if (!isConditionOne()){
        if (!isConditionTwo()){
        if (!isConditionThree()){

        retval = generateReport();
    } while(0);
    return retval;


bool MyApplication::ReportGenerator::GenerateReport(){
    if (!isAdmin()  || !isConditionOne() || !isConditionTwo() || !isConditionThree()){
        return false;
    return generateReport();

The clean code by Robert C. Martin is a nice book which deals with this. But, I guess that book is inclined towards Java.


  1. I have purposefully used do{ }while(0); loop as I did not wanted to use goto.

  2. I wanted to get rid of so many if and break statements, so I proposed Code 2.

What I can see from the replies is a mixed set of responses for both the Code1 and Code2. There are people who prefer goto as well compared to Code 1 (which I thought was better).

Personally I prefer sample 2. It groups those items that will not result in the report being generated together.

As far as general coding guidelines, the book Code Complete (Amazon) is a good reference for coding style issues.

At work I am responsible for writing specifications quite often and I am also the person who insisted on getting specifications in the first place. The problem is I am unsure how specifications should look and what they should contain. A lot of the time when my boss is writing the specifications (we are both inexperienced in it) they put in table names and things that I don't think belong there. So what is a good way to learn to write a good spec?

EDIT: Should a functional spec include things like assuming I am specifying a web application, the input types (a textbox, dropdown list, etc)?

There's a great chapter in Steve McConnell's Code Complete that runs through specification documents and what they should contain.

When I was tasked to build an Architecture and Business Analysis team at a company that had never had either, I used McConnell's spec chapter to create the outline for the Technical Specification document. It evolved over time, but by starting out with this framework I made sure we didn't miss anything and it turned out to be surprisingly usable.

When writing specs, a rule of thumb I follow is to try to have technical documents always start from the general and move to the specific -- always restate the business problem(s) or goal(s) that the technical solution is being developed to solve, so the person reading the spec doesn't need to go to other documents to put it in any sort of context.

I am preparing a presentation about unit testing to managers. My team has been writing unit tests for years now but other areas of the company seem to have a hard time adopting it. I can get developers excited about it, but if there is not a buy-in from management, it will not become a standard.

Do you guys have suggestions how to best approach management about this? What are some things you have tried in the past that worked? What are things that did not work?

I think statistics would make the best case as far as nontechnical managers are concerned. Code Complete, 2nd Ed. has a summary of a couple of studies showing that unit testing leads to an average of 30% defect detection rate (Table 20-2, p 470). I think it should be easy to take it from there and make the case that less bugs translate to more money saved.

I myself am one of these types of coders, who never learned coding in a formal setting, but am instead, self-taught by the use of copy and paste scripts, and pre-authored works available through GPL projects and premium software's(which is often the way commerical script companies show you how to change, or update code in their script ie. "Copy & paste this on line 234, etc..")... this are my beginnings.

I don't understand how what I do works, but I am getting good at what I do with code, though I feel because of my informal learning methods, that I lack in knowledge, some of the really simple, but necessary principles of web coding in general(the ins and outs of servers, browsers, etc). I feel as though I have started from the inside of understanding code, the practical side.. and lack the basic fundamentals, and theorized understanding of what I am doing.

I also know quite a few others who are the same, who started learning in the same communities as I, and are pretty much at the same point as me... Feeling as though something big is missing from the picture. I see the lacking in my discipline when I attempt to use PHP authoring tools, which I find very challenging to understand... and this is becoming a downfall on the time frames in which I complete my projects(having to hand code everything). I also find it difficult to ask for help at times, for lack of the specific language that comes with programming, and being able to use the right words to explain the intricacy of my problem.

I was a hairdresser by trade, I worked as a bartender, and hotel manager, I'm a business consultant by profession. I've been learning web development slowly now for 3 years, and I have taken on quite a few commercial projects over the past 2 years, but I'm obviously feeling some lacking in my skills & overall knowledge.

Basically, Id like to ask any experienced coders who've had similar beginnings to give me an idea on how I can adapt my coding disciplines to be more effective and productive. What would someone with such an informal understanding of programming want to seek to learn, and understand. What are the things I must know as a programmer... and what is a basic overview of the whole picture???? Feel free to direct me to articles

Any help is always appreciated.


Edit: Just to give a round up of my actual capabilities. I am a programmer, I do author my own code from scratch.. and sell my work. I can write PHP functions, and classes, use mysql, have a good working understanding of html & css, and am very good in the design aspects(web graphics), and am learning javascript/ajax/jquery, as I am using jquery in most of my projects. Thanks for all of the answers so far!

The easiest and fastest way to do this? Find a mentor, or a set of mentors. Not necessarily a real-life mentor, they can be offline, although a real-life mentor is more practical.

You can find these people when you participate in local software development communities, e.g., forums, or user group meetings. Frequent these places, both offline and online; the amount of knowledge that you will learn is immense, not to mention the benefit of being able to bounce ideas off of other people.

Code Complete and Pragmatic Programmer are likewise excellent book suggestions above. I suggest you go with Code Complete first, as it tackles on how to make your code sensible and elegant, which you certainly can use immediately.


In my most C++ project I heavily used ASSERTION statement as following:

int doWonderfulThings(const int* fantasticData)
        return -1;
    // ,,,
    return WOW_VALUE;

But TDD community seems like to enjoy doing something like this:

int doMoreWonderfulThings(const int* fantasticData)
        return ERROR_VALUE;
    // ...
    return AHA_VALUE;

    ASSERT_EQ(ERROR_VALUE, doMoreWonderfulThings(0L));
    ASSERT_EQ(AHA_VALUE, doMoreWonderfulThings("Foo"));

Just with my experiences first approaches let me remove so many subtle bugs. But TDD approaches are very smart idea to handle legacy codes.

"Google" - they compare "FIRST METHOD" to "Walk the shore with life-vest, swim ocean without any safe guard".

Which one is better? Which one makes software robust?

In my (limited) experience the first option is quite a bit safer. In a test-case you only test predefined input and compare the outcome, this works well as long as every possible edge-case has been checked. The first option just checks every input and thus tests the 'live' values, it filters out bugs real quickly, however it comes with a performance penalty.

In Code Complete Steve McConnell learns us the first method can be used successfully to filter out bugs in a debug build. In release build you can filter-out all assertions (for instance with a compiler flag) to get the extra performance.

In my opinion the best way is to use both methods:

Method 1 to catch illegal values

int doWonderfulThings(const int* fantasticData)
    ASSERTNOTEQUAL(0, fantasticData)

    return WOW_VALUE / fantasticData;

and method 2 to test edge-cases of an algorithm.

int doMoreWonderfulThings(const int fantasticNumber)
    int count = 100;
    for(int i = 0; i < fantasticNumber; ++i) {
        count += 10 * fantasticNumber;
    return count;

    // Test lower edge
    ASSERT_EQ(0, doMoreWonderfulThings(-1));
    ASSERT_EQ(0, doMoreWonderfulThings(0));
    ASSERT_EQ(110, doMoreWonderfulThings(1));

    //Test some random values
    ASSERT_EQ(350, doMoreWonderfulThings(5));
    ASSERT_EQ(2350, doMoreWonderfulThings(15));
    ASSERT_EQ(225100, doMoreWonderfulThings(150));

I've been reading Code Complete 2. As I am not native english speaker some statements take some time for me to understand. I would like you to describe the difference between these two statements the author made in his book:

  1. You should program into Your Language (programming language).
  2. You shouldn't program in Your Language.

Why in is bad and into is recommended?

Program into your language means that you use the language to construct the "missing" pieces - leverage it to do more than it currently does. Things like creating missing data structure, algorithms and ways of accomplishing tasks that are not native to the language.

Program in your language means just that - not trying to leverage it.

I thought the examples given in the book were quite good.

Back in college, only the use of pseudo code was evangelized more than OOP in my curriculum. Just like commenting (and other preached 'best practices'), I found that in crunch time psuedocode was often neglected. So my question is...who actually uses it a lot of the time? Or do you only use it when an algorithm is really hard to conceptualize entirely in your head? I'm interested in responses from everyone: wet-behind-the-ears junior developers to grizzled vets who were around back in the punch card days.

As for me personally, I mostly only use it for the difficult stuff.

Steve McConnel's Code Complete, in its chapter 9, "The Pseudocode Programming Process" proposes an interesting approach: when writing a function longer than a few lines, use simple pseudocode (in the form of comments) to outline what the function/procedure needs to do before writing the actual code that does it. The pseudocode comments can then become actual comments in the body of the function.

I tend to use this for any function that does more than what can be quickly understood by looking at a screenful (max) of code. It works specially well if you are already used to separate your function body in code "paragraphs" - units of semantically related code separated by a blank line. Then the "pseudocode comments" work like "headers" to these paragraphs.

PS: Some people may argue that "you shouldn't comment what, but why, and only when it's not trivial to understand for a reader who knows the language in question better then you". I generally agree with this, but I do make an exception for the PPP. The criteria for the presence and form of a comment shouldn't be set in stone, but ultimately governed by wise, well-thought application of common sense anyway. If you find yourself refusing to try out a slight bent to a subjective "rule" just for the sake of it, you might need to step back and realize if you're not facing it critically enough.

I m looking to write some quality C code. Can someone point me to some articles , websites..whatever I need something with examples. I have already seen and read K&R C book.

But times have changed, some one must have more to say on quality C Code. and another important thing is How do you ensure that you as programmer have written quality C code??

There are a lot of aspects for quality of code and tons of articles, books, blogs

but I can advice you this ones as beginning:

Code complete

Code secure


Moving from a .NET / SQL Developer role to a more of an architect one. Can anyone reccomend any good books for modern Software Architecture in enterprise?

Appreciate your help.



Domain Driven Design by Eric Evans.

I frequently write throwaway code (in a research environment) - for example to explore an algorithm or a model for a scientific property or process. Many of these "experiments" are one-off but sometimes I find that I need to use a few later. For example I have just unearthed code for string matching I wrote 7 years ago (stopped because of other priorities) but which is now valuable for a coworker's project. Having looked at it (did I really write such impenetrable code?) I realise there are some things I could have done then to help me when I restarted the "project" ("experiment" is still a better word). The earlier experiment "worked" but I know that at the time I would not have had time to refactor as my priorities lay elsewhere.

What approaches are cost-effective in enabling such work to be dug up and re-used?

EDIT: I have answered my own question (below) because there are issues beyond the actual source itself.

I disagree with all of the answers saying "write comments". That's being offered as a catch-all for the code itself not being understandable.

Get yourself a copy of Code Complete (Steve McConnell, 2nd edition). If you learn the techniques of writing maintainable code in the first place, it won't take you more time, and you will be able to return to your work later with less trouble.

Which would you prefer:

  • Cryptic code with comments?
  • Mostly OK code without?

I strongly prefer the latter, as the OK code is easier to understand in the situations where the cryptic code was uncommented, and comments are another place that the original developer can make mistakes. The code may be buggy, but it's never wrong.

Once you're comfortable with Code Complete, I'd recommend The Pragmatic Programmer, as it gives slightly higher-level software-development advice.

I'm trying to make my code easily understood by future readers.

I've always had issues with how to word my if statement comments to make it the most understandable.

Maybe it seems trivial, but it's something that's always bothered me

Here's an example:

if ( !$request ) {
    $request = $_SERVER['REQUEST_URI'];

Here are some way I can think of commenting it

// If request doesn't exist
if ( !$request ) {
    // Set request to current request_uri
    $request = $_SERVER['REQUEST_URI'];

// Check for a request
if ( !$request ) {
    $request = $_SERVER['REQUEST_URI'];

// Request doesn't exist
if ( !$request ) {
    // Set request
    $request = $_SERVER['REQUEST_URI'];

Not the best example, but the way I see it there are infinite ways to word it.

I've never really worked on a team so I don't have much experience on other people reading my code.

What are your experiences on the best way to word that to make it readable for future coders.

Read the book Clean Code by Robert Martin.

Comments should be only used to explain concepts that the code can't explain itself.

As said before, maybe your example is a bit too simple in order really have a relevant comment on it, but here are a couple of general suggestions:

  • usually is easier to read "positive" conditions rather than negations (not condition)

  • don't hesitate to extract methods that will have a detailed name that will communicate the intent and avoid thus the need of some of the comments. In your case, say you create:

    function getRequest( $req ) {
        if( isset( $req ) ) {
            return $req;
        } else {
            return $_SERVER['REQUEST_URI'];

    but again, we need a more appropriate example, in your case it might be overkill

  • must read :

in private and in companies it is over and over again a problem that I or we developers comment in fact our code but generally nobody knows exactly how the code of the whole project works together. When I write my own code and the project is getting bigger I sometimes have this problem too. Although I write tons of comments, after 3 months you don't know what the whole thing exactly does, that means how the different methods and classes work together.
How do you solve this in your company or in private (if there is just marginal project development and no requirements specification). Or do you have always such a good project development with contract document and requirements specification that you don't have to worry about that?

Good question. Part of what you are asking relates to code maintainability. In my view the two main things you can do to improve this are:-

  • Write some design documentation
  • Develop maintainable and clearly written code

From past experience the first item is very often neglected on software projects due to time constraints, but I think that if you can produce at least a class diagram of your system, then this is worth a lot in terms of understanding how objects interract when you revisit the code in a few months. Depending on the complexity, then sequence diagrams can also be useful. Producing this documentation will also be of benefit to new members of the team, in quickly having an overview of how the software is structured.

I can't stress enough the importance of writing clear and maintainable code. My eyes were recently opened when I read Clean Code by Robert Martin. You owe it to yourself and your fellow developers to read at least the first couple of chapters in this book. That alone will immediately improve the readability and maintainability of your code.

The idea is that the code should read almost like a narrative, where methods follow in a logical order, are short, appropriately named, and take few parameters. Doing this almost eliminates the need for code comments, and improves the code structure.

Code complete can explain the solution to your problem better than I ever could.

There seems to be two accepted variable declaration placements for Java variables, each with different raison d'être.

From the Sun's code conventions we can see:

Put declarations only at the beginning of blocks. (A block is any code surrounded by curly braces "{" and "}".) Don't wait to declare variables until their first use; it can confuse the unwary programmer and hamper code portability within the scope.

However in the highly praised "Code Complete" and some other authors advocate for reducing the scope of a variable to a minimum. That is basically waiting to declare variables until their first use.

These two approaches are clearly contradictory although I can see the point for both of them.

Which should I follow? Is there any consensus on the matter?

Variables should be declared as close to their use as possible, for two reasons:

  • Vertical locality
  • Tool hinting

Vertical locality makes it easier to reason about chunks of code. The less scanning a reader has to do the easier it is to understand what code does, and what side-effects it

Reducing variable scope also allows better tool hinting for automated tools, like refactoring. The closer together related things are the more obvious they are.

That said: if a method is long enough that the above points come in to play, it's likely the method is already too long, and should be refactored anyway.

We are a .NET shop, mostly coding using C# in VS. We created coding standards for our organization, and now we need to enforce them. We can use tools like FxCop & StyleCop.

What we want:

  1. On each developer's PC, have settings that will give them a warning or error when they compile their code.
  2. When they check in the code into version control, don't let them check in if they don't follow company's coding standards.

How can we achieve this? How does your team enforce coding standards?

I'm looking to learn how you do that, so we can improve our procedures.


What should coding guidelines do, and are there any good examples of guidelines?


Our setup:

IDE: VS 2008

Version control: Currently it's StarTeam but in a near feature it's going to be SVN.

In our build process we use CruiseControl.Net with NAnt scripts.


I've written C++ coding standards for major companies. One lesson I've learnt regarding adoption of coding standards is to spell out the advantages of adhering to the coding standards to those using the standards. In fact, my last set of C++ coding standards provided the reasoning behind each of the rules that were provided.

They weren't "carved in stone" either, so if someone had a good reason to change something the suggestion was listened to.

Steve McConnell's excellent book "Code Complete" (sanitised Amazon link) has several excellent sections that cover reasoning behind using coding standards.

Off the top of my head, I can think of several:

  1. enforcing naming standards frees people's heads to work on the more interesting aspects of the code. For example, the author can work on the algorithms to be used rather than spending ages working out what to call this class, or variable.
  2. contrary to some people's beliefs, it does improve maintainability of code and allows someone to pick up some new code that's been written to the same standard and understand it much faster than if they have to work out the author's naming conventions, architectural style, code layout techniques, etc. If you say that the IDE enforces this, then you are simply copping out by pushing the coding standard into that enforced by the IDE being used.
  3. enforcing semantics for constructs minimises problems with the designs. For example, having a guideline that "public inheritance shall only be used to implement an 'isa' relationship" means that the script kiddies who want to use inheritance because "it's a really cool way to bring in functionality under the covers" can't do that.



What's best practice for reuse of code versus copy/paste?

The problem with reuse can be that changing the reused code will affect many other pieces of functionality.

This is good & bad : good if the change is a bugfix or useful enhancement. Bad if other reusing code unexpectedly becomes broken because it relied on the old version (or the new version has a bug).

In some cases it would seem that copy/paste is better - each user of the pasted code has a private copy which it can customize without consequences.

Is there a best practice for this problem; does reuse require watertight unit tests?

Is there a best practice for this problem; does reuse require watertight unit tests?

Yes and sort of yes. Rewriting code you have already did right once is never a good idea. If you never reuse code and just rewrite it you are doubling you bug surface. As with many best practice type questions Code Complete changed the way I do my work. Yes unit test to the best of your ability, yes reuse code and get a copy of Code Complete and you will be all set.

When reviewing requirements specification (that includes functional, non-functional requirements, constraints etc) however small or large it is what are the "deadly sins" committed by authors to look out for?

Please list not more than 7 most essential things (in order of decreasing severity) that being done (or not done) in requirements specification have adverse effect on the quality of software product. Less than 7 is perfectly OK.

My recommendation and what I always do before a new project is double check the check list on Page 42,43 of Steve McConnell's Code Complete

When seeking advice on good programming practices the typical answers are a variation of agile software development, test driven development or design patterns. However, as far as I know, neither of these are proven using the scientific method (if I'm wrong on this then feel free to correct me).

I wonder, are there any good resources on the topic of evidence-based development practices?

The best comprehensive reference I know about software engineering scientific evidence is Facts and Falacies of Software Engineering. The book is concise, with refernces to the original sources (or it plainly says there are not), well-written, and not expensive.

The second best reference is Code Complete but it is much longer, and it is not so focused on the evidence itself. It is nevertheless a must read book.

Once you have read these two books it is also worth looking on the "voice of evidence" series of articles from the IEEE Software magazine.

I began reading the book "Code Complete" 2nd edition, but stopped reading when I noticed most of the solutions were easily solvable in Ruby with Ruby idioms. Is there a similar book for Ruby?

Here's the version that I started reading:

Pick that book back up and start where you left off. As someone who read the first edition and these days writes Ruby for a living, I can confidently say that the lessons of Code Complete are universal. The wisdom in that book about good code construction - quality naming, decoupling, how to structure a function, etc. - will stand any programmer in good stead. I still refer to my dog-eared first edition regularly.

As far as books on practices which pertain more specifically to Ruby: first, every programmer should have a copy of "The Ruby Way, 2nd Edition", by Hal Fulton, on his or her desk. Second, while I haven't read it yet, I have heard good things about "Ruby Best Practices".

For such an old programming language (well, maybe not "Lisp" old, but Ruby is older than Java, after all) and a community that fanatic about code quality, style and beauty, the Ruby book market is surprisingly light on "higher level" books.

There are several reasons for this: a lot of Rubyists are already seasoned Smalltalk or Lisp programmers when they pick up Ruby, so they don't need those books. And Ruby and Smalltalk are similar enough that you can just read a Smalltalk book instead. (For example, you can pretty much read Stéphane Ducasse's list of free Smalltalk books from top to bottom.) The same goes for Perl.

Until very recently, the best higher level Ruby book was basically Smalltalk Best Practice Patterns by Kent Beck. (There is a newer version of this book, called Implementation Patterns which uses Java instead of Smalltalk. However, since Kent's Java style is very Smalltalk-ish, it is essentially SBPP, 2nd Ed.) However, since then, several higher level books have been released or are in the works:

I'm learning java and one thing I've found that I don't like, is generally when I have code like this:

import java.util.*;

public class GraphProblem
    public static void main(String[] args)
        if (args.length < 2)
            System.out.println("Error: Please specify a graph file!");

        FileReader in = new FileReader(args[1]);
        Scanner input = new Scanner(in);

        int size = input.nextInt();
        WeightedGraph graph = new WeightedGraph(size);

        for(int i = 0; i < size; i++)
            graph.setLabel(i,Character.toString((char)('A' + i)));

        for(int i = 0; i < size; i++)
            for(int j = 0; j < size; j++)
                graph.addEdge(i, j, input.nextInt());

        // .. lots more code


I have an uncaught exception around my FileReader.

So, I have to wrap it in a try-catch to catch that specific exception. My question is does that try { } have to encompass everything after that in my method that wants to use either my FileReader (in) or my Scanner (input)?

If I don't wrap the whole remainder of the program in that try statement, then anything outside of it can't access the in/input because it may of not been initialized or has been initialized outside of its scope. So I can't isolate the try-catch to just say the portion that intializes the FileReader and close the try statement immediately after that.

So, is it the "best practice" to have the try statement wrapping all portions of the code that are going to access variables initialized in it?


Local variables declared within a block are scoped to that block.

So, is it the "best practice" to have the try statement wrapping all portions of the code that are going to access variables initialized in it?

In general, you should try to minimize the scope of your variables to make them as narrow as possible. This is covered in Effective Java and Code Complete, among others.

This sort of code is a recipe for NullPointerExceptions:

FileReader in = null;
try {
  in = new FileReader(filename);
} catch(IOException e) {
  //handle error, but now what?
// Code that needs "in", but what if it is null
// because of a FileNotFoundException?
// This code is junk.

Virtually all local variable declarations should include an assignment.

FileReader in = new FileReader(filename);

If you follow this rule of thumb, you'll end up with better code.

// .. lots more code

If you have giant try/catch statements, it sounds like your methods are too big. This is a code organization issue rather than something to do with try/catch specifically.

  public static void main(String[] args) {
    if (args.length < 1) {
      System.out.println("Error: Please specify a graph file!");
    try {
    } catch (IOException e) {
      // Deal with error

  private static void processGraphFile(String filename) throws IOException {
    FileReader in = new FileReader(filename);
    try {
      Scanner scanner = new Scanner(in);
      if (scanner.ioException() != null) {
        throw scanner.ioException();
    } finally {

  //other methods

As an aside:

  • Note that Java arrays start at index 0, not 1
  • The FileReader is convenient while learning, but should generally be avoided due to encoding issues; this is unlikely to be a problem until the code leaves your machine

In Code Complete 2 (page 601 and 602) there is a table of "Cost of Common Operations".

The baseline operation integer assignment is given a value 1 and then the relative time for common operations is listed for Java and C++. For example:

                                  C++        Java
Integer assignment                1             1
Integer division                  5             1.5
Floating point square root       15             4 

The question is has anyone got this data for C#? I know that these won't help me solve any problems specifically, I'm just curious.

Straight from the source, Know what things cost.

IIRC Rico Mariani had relative measures as the ones you asked for on his blog, I can't find it anymore, though (I know it's in one of thoe twohudnred "dev" bookmarks...)

I wrote the following method to determine the max file size:

    public static long GetMaxFileSize(string dirPath, long maxFileSize)
        DirectoryInfo [] dirInfos = new DirectoryInfo(dirPath).GetDirectories();
        foreach (DirectoryInfo dirInfo in dirInfos)
            DirectoryInfo [] subDirInfos = dirInfo.GetDirectories();
            foreach (DirectoryInfo subDirInfo in subDirInfos)
                maxFileSize = GetMaxFileSize(dirInfo.FullName, maxFileSize);

            FileInfo [] fileInfos = dirInfo.GetFiles();
            foreach (FileInfo fileInfo in fileInfos)
                if (maxFileSize < fileInfo.Length)
                    maxFileSize = fileInfo.Length;

        return maxFileSize;

Code Complete recommends to "use recursion selectively". That being the case, I was wondering if the community thought this was a valid use of recursion. If not, is there a better technique of doing this?

EDIT: I can't use LINQ because its not available in .NET 2.0, but I don't want to tag this as a .NET 2.0 question only to further discussion points like Jared's below.

EDIT: Cleaned up code based on an issue that was spotted in not getting the root directory's files.

   public static long GetMaxFileSize(DirectoryInfo dirInfo, long maxFileSize)
       DirectoryInfo [] subDirInfos = dirInfo.GetDirectories();
       foreach (DirectoryInfo subDirInfo in subDirInfos)
           maxFileSize = GetMaxFileSize(subDirInfo, maxFileSize);

       FileInfo [] fileInfos = dirInfo.GetFiles();
       foreach (FileInfo fileInfo in fileInfos)
           if (maxFileSize < fileInfo.Length)
               maxFileSize = fileInfo.Length;

       return maxFileSize;

I think a better way is to the File System API to do the searching for you via Directory.GetFiles. This method provides automatic searching of sub-directories. This eliminates the question of whether or not to recurse and instead leaves the decision of how to implement it on the designer of the API (who likely designed it for such a scenario).

This method combined with LINQ provides a very elegant solution

var max = Directory
  .GetFiles(path, "*", SearchOption.AllDirectories)
  .Select(x => new FileInfo(x))
  .Select(x => x.Length)

EDIT As Jimmy pointed out, for 4.0 and higher, it's better to use EnumerateFiles to avoid the overhead of creating a potentially large array

var max = Directory
  .EnumerateFiles(path, "*", SearchOption.AllDirectories)
  .Select(x => new FileInfo(x))
  .Select(x => x.Length)

What are Table-Driven methods as mentioned by Bill Gates on Vista commercial on

Table-driven methods are schemes that allow you to look up information in a table rather than using logic statements (i.e. case, if). In simple cases, it's quicker and easier to use logic statements, but as the logic chain becomes more complex, table-driven code is simpler than complicated logic, easier to modify and more efficient.

Reference: McConnell, Steve. Code Complete, Second Edition. Redmond (Washington): Microsoft, 2004. Print. Page 411, Paragraph 1.

How do you write code that is easily read by other people and who have had no hand in writing any part of it?

Buy & read Code Complete 2. There's loads of stuff in there about writing easy to read / maintain code.

I don't think it's a subjective question, but it's too broad! It's not just about commenting and giving good variables names. It deals with how humans comprehends code. So your system must be implemented in a way that the reader can easily construct a mental model of its design in two way:

  • Top-down: assuming the user knows the system domain, he tends to make assumptions on how it would be implemented, so he'll scan the system packages and classes looking for entities he can identify. Giving good names to your classes and properly modularizing it would help very much.

  • Bottom-up: once the user reaches a portion of code he'll start navigation from there, building chunks of knowledge. If your system has low cohesion and lots of implicit dependencies the user will be lost.

Kent Beck adopts three principles: Communication, Simplicity and Flexibility. Of course, sometimes you'll have to trade simplicity for flexibility, and vice-versa.

This could go on and on. The answer to this question fits in a large book. As @rmbarnes suggested, buy and read Code Complete 2. I also suggest Implementation Patterns by Kent Beck - its highly related to your question.

You may want to take a look at Clean Code by Robert C. Martin. It offers up a lot of useful practices for ensuring your code is readable.

Additionally, if your code is supported by a number of unit tests that thoroughly test your code, it offers a way for your user to understand the code by looking at what the tests are doing. You will also find that if you follow the Test Driven Development process, and you write tests for each bit of functionality, your functions tend to be small, do one thing only and do it well, and tend to flow more like a story than simply a large complex web of "stuff".

Tests tend to stay up-to-date more than comments. I often ignore comments anymore due to simple fact that they become obsolete very quickly.

Since everyone else said pretty much what I'm thinking when I read this question, I'll just share two books related to this subject that you might be interested in reading. These books use open source code examples to explain how to read and write high quality code. In addition to Code Complete, I think they are valuable resources when you want to write good code in any language.

I am currently writing an open source wrapper for a COM object. I have just installed StyleCop and run it against some of my code, and as I expected it threw up a load of warnings (some of which I should have been doing already eg adding "this." to all local method calls and variables)

How many of these warnings should I try and reduce? Which ones would be the best ones when writing an open source library?

Most large open source projects have their own style guidelines. For example, here's the Apache C Language Style Guide.

As a start, I suggest trawling the FAQ for the OS project you're interested in and take it from there.

Anyway, I'm sure there'll be plenty of people who'll quickly let you know if you're breaking any existing style rules in a big way! ;-)

Take heed of all the warnings, usually they are trying to tell you something useful. Don't finish up "programming by coincidence" (book excerpt) as the Pragmatic Programmers say!

Edit: I didn't realise that this was your own, new, OS project. My bad. Anyway, I'd have a sniff around some similar already existing OS projects and see if they have any style guides. Definitely, check out Steve McConnell's book Code Complete (Amazon link - sanitised for the script kiddie link nazis that roam these halls).

What I said about warnings and "programming by coincidence" still applies! (-:




I have an issue that I feel many programmers can relate to...

I have worked on many small scale projects. After my initial paper brain storm I tend to start coding. What I come up with is usually a rough working model of the actual application. I design in a disconnected fashion so I am talking about underlying code libraries, user interfaces are the last thing as the library usually dictates what is needed in the UI. As my projects get bigger I worry that so should my "spec" or design document.

The above paragraph, from my investigations, is echoed all across the internet in one fashion or another. When a UI is concerned there is a bit more information but it is UI specific and does not relate to code libraries. What I am beginning to realise is that maybe code is code is code. It seems from my extensive research that there is no 1:1 mapping between a design document and the code.

When I need to research a topic I dump information into OneNote and from there I prioritise features into versions and then into related chunks so that development runs in a fairly linear fashion, my tasks tend to look like so:

  1. Implement Binary File Reader
  2. Implement Binary File Writer
  3. Create Object to encapsulate Data for expression to the caller

Now any programmer worth his salt is aware that between those three to do items could be a potential wall of code that could expand out to multiple files. I have tried to map the complete code process for each task but I simply don't think it can be done effectively. By the time one mangles pseudo code it is essentially code anyway so the time investment is negated.

So my question is this:

Am I right in assuming that the best documentation is the code itself. We are all in agreement that a high level overview is needed. How high should this be? Do you design to statement, class or concept level? What works for you?

I'd HIGHLY recommend reading Code Complete 2 for some excellent insight into these kinds of questions.

I like reading open-source code to see how particular problems have been solved, or simply to study how to create nice and clean code.

I was wondering if there actually was any good book out there that goes through excerpts of different (preferably in Java) open-source projects to show how patterns have been used and implemented, some nifty tricks, etc.?

Update: I took design patterns as an example, but it’s not really what I am after. I read a lot of open-source code, and I often find interesting ways of doing things which I didn’t think of before – my question is whether there is a book that shows some “cool” open-source code, a bit like how Clean Code uses FitNesse code to illustrate refactoring and other points.

Well, you can read just book about open source project, and authors can show what and how. E.g. I read Pro Spring 2.5 and there is even chapter called "Spring Patterns" with explanation of why and how they call files, directories, design patterns they use and invite you to use etc. Probably take a look at the books about open source projects you investigate.

UPDATE (just that paragraph insert to extend answer:) : as I told previously, you can look through the books about open source projects and find there information how this project is developed, architectural issues etc. Once I looked for real good example of "Open/Closed Principle" using, theory is nice but I would not say that it is so clear how to use it. The only book which helped me was Expert Spring MVC and Web Flow (I already pointed you to google books with that chapter I read). I didn't read the whole book, but I am sure it contains a lot of such things (what and how was used to make Spring done). From book Clean Code: A Handbook of Agile Software Craftsmanship I remember that there is a chapter, when they take a code from one open source project and make it better (unfortunately open source does not mean that it is good enough); that book also contain reference to how some other projects where written (e.g. jUnit). I don't know any book, which is completely dedicated to any project, there are just books where is "theory", and books about project which can have some explanation of what is inside. I would recommend books from APress (Pro and Expert series, afaik "beginning" books does not contain such explanation), and Packt Publishing - their main topic is open source.

UPDATE 2 (just can't stop :) Just remembered new series of books from OReilly "Beautiful ...." - I never read it, but I looked through. It actually consists of different stories behind development, real stories from life. I see there is also Beautiful Code: Leading Programmers Explain How They Think - I should contain what you need. Other "beautiful" books can be worth of your time too.

About patterns in general there are a lot of good books, and it depends on what exactly are you looking for. E.g. Head First Design Patterns is quite good to explain common patterns in fun way. If you need more, you can take a look at Enterprise Integration Patterns: Designing, Building, and Deploying Messaging Solutions to see how different applications, systems etc. can be integrated (and how open source projects do it too). Etc. Just specify what patterns or tricks do you want to read about, community will make its suggestion ;)

Some web links out of my bookmarks:

  1. Anti Patterns Catalog - know what you are not going to do!
  2. Design Patterns and Martin Fowler's Catalog - that's shortly about what is good to use and know about
  3. SOA patterns - community site for SOA patterns (integration etc.)

And few more books to take a look at:

  1. Head First Object-Oriented Analysis and Design
  2. Agile Software Development, Principles, Patterns, and Practices
  3. Code Complete - this book is just a bible of how to write code...

people, who develop open source definitely know what are those books about ;)

Hope that helps

UPDATE (25-05-2011) Just found one book, which I definitely going to buy, and I think it perfectly fits your question:

The Architecture of Open Source Applications
enter image description here

Architects look at thousands of buildings during their training, and study critiques of those buildings written by masters. In contrast, most software developers only ever get to know a handful of large programs well — usually programs they wrote themselves — and never study the great programs of history. As a result, they repeat one another’s mistakes rather than building on one another’s successes. This book’s goal is to change that. In it, the authors of twenty-five open source applications explain how their software is structured, and why. What are each program's major components? How do they interact? And what did their builders learn during their development? In answering these questions, the contributors to this book provide unique insights into how they think. Electronic versions of The Architecture of Open Source Applications for e-readers will be available for download soon.

I'm reading a lot about good and bad practices in OOP design. It's nice to know your design is bad, or good. But how do you get from bad to good design? I've split the interface (xaml) and codebehind from the main businesslogic class. That last class is growing big. I've tried splitting it up into smaller classes, but I'm stuck now. Any ideas on how to split large classes? The main class has 1 list of data of different types. I'm doing calculations on the total, but also on the individual types. I've got methods to perform these calculations which are called from events handled in the codebehind. Any ideas where to go from here?

Additional Info:

We are already about 6 months into this project. I've worked with object oriented laguages for years (first c++, java and now c#), but never on a large project like this one. I believe we've made some wrong turns in the beginning and I think we need to correct these. I can't specify any details on this project at the moment. I'm going to order one or two books about design. If I separate all the classes, how do I stick them back together? Maybe it's even better to continue this way to the first release and rebuilt parts after that, for a second release?

I highly recommend picking up Code Complete. It's a great book that offers tons of good advice on questions like yours.

To give you a quick answer to your question about how to split large classes, here's a good rule of thumb: make your class responsible for one thing, and one thing only. When you start thinking like that, you quickly can identify code that doesn't belong. If something doesn't belong, factor it out into a new class, and use it from your original class.

Edit: Take that thinking down to the "method" level, too - make your methods responsible for one thing, and one thing only. Helps break large (>50 line) methods down very quickly into reusable chunks of code.

Refactoring by Martin Fowler is an excellent book about how to change the design of your software without breaking it.

Design Patterns works similarly to algorithims but tells you how to combine objects to perform various useful tasks.

Finally Martin Fowler has a variety of useful design pattern for applications. For example Passive View

Michael Feathers's "Working Effectively with Legacy Code" is supposed to be very good, but I'll confess that I haven't read it myself.

Same goes for "Refactoring to Patterns."

Is there any website where people share and discuss good examples of object-oriented design?

Ideally such website should be populated with posts of the following structure:

  • Concise description of the problem, including definitions, links, etc.
  • Several attempts of OO design, diagrams, pseudocode listings (voted up/down by users)
    • Comments (also voted by users)

Currently I'm looking for a source of inspiration.

Object Oriented Software Construction - Betrand Mayer

He's the man that originated the Eiffel language, the most complete analysis of OO software construction I've read.

Well I can't point you to web sites, but I can refer you to some great books :

Of course, those are C++ books, but a lot of concepts in them applies to much more than just C++

I was asked this question : I was not able to answer - Any answers here?

What can be said about a java class which has 1000 lines of code , with 1-n methods having 100 lines and n+1 to m methods having 200 lines of code?

I thought, Classes should be okay to have 1000 lines of code and methods are also okay to have 100-200 lines of code - So I didn't answer considering that the class is perfectly okay.

Are there known compile time performance related to the number of lines a java class has? or a java method has ? are there any standards - if so, how can one justify technically?


I think that the main problem here is not the compile-time performance. A 1000 lines class is difficult to read and understand.

Perhaps the class should be decomposed in other classes (or subclasses, if in your case inheritance is more valuable than composition), so every class has a well defined responsibility in the system.

But I can't say very much about this problem if I can't view the class' implementation.

There are a lot of good books that explain how to solve this and others design problems. Two well known books are Code Complete 2nd Edition by Steve McConnell and Refactoring by Martin Fowler et al.

This is a best practices question. I am making an array

type * x = malloc(size*sizeof(type));

AFAIK sizeof gives a return value of size_t. Does that mean that I should use a size_t to declare, or pass around size? Also when indexing the array should I also use a size_t for the index variable? What is the best practice for these these? This is not something that they taught in school, and now that I'm getting into serious c++ I want to know.

Also if anyone has references of where I can find best practices for this kind of stuff it would be helpful? Kind of an etiquette for programmers book.

EDIT: The malloc should be cudaHostAlloc, or cudaMalloc, since I am developing a class that stores an array simultaneously on the device and host, and updates both at the same time. So malloc here is just a place holder for what I'll actually be doing.

In reference to your follow-on question:

The best reference I have used for general high-level programming "current good practices" sort of thing is:

Code Complete by Steve McConnell (ISBN 0-7356-1967-0)

I reference it all the time. When my company formalized its coding standards, I wrote them based off of it. It doesn't go into design or architecture as much, but for actually banging out code, the book is appropriately named.

In general, I use whatever minimizes the number of implicit or explicit casts and warning errors. Generally there is a good reason why things are typed the way they are. size_t is a good choice for array index, since it's unsigned and you don't generally want to access myarray[-1], say.

btw since this is C++ you should get out of the habit of using malloc (free) which is part of CRT (C runtime library). Use new (delete), preferably with smart pointers to minimize manual memory handling.

Once you have mastered the basics, a good practices reference (language-specific) is Effective C++ by Scott Meyers. The logical next step is Effective STL.

We all walked out of school with the stars in our eyes and little experience in "real-world" programming. How have your opinions on programming as a craft changed since you've gained more experience away from academia?

I've become more and more about design a la McConnell : wide use of encapsulation, quality code that gives you warm fuzzy feelings when you read it, maintainability over execution performance, etc..., whereas many of my co-workers have followed a different path of fewer middlemen layers getting in the way, code that is right out in the open and easier to locate, even if harder to read, and performance-centric designs.

What have you learned about the craft of software design which has changed the way you approach coding since leaving the academic world?

Before college: the trick is to be smart enough to figure out the right way to create your program before you start coding. I was never smart enough, and realized my errors after 15 minutes of coding. :-(

After college: estimating time to create software is impossible to do with any accuracy, so there should be no schedules.

After a few years in industry: After consistently failing to get things right the first time, I decided it was impossible. Since I was working on the Visual C++ Debugger, I decided that the key to being a successful programmer is to be really good at debugging. You write whatever you can, then use the debugger to find the problems, then fix them.

After another few years: I switched from the Visual C++ Debugger to the Visual C# Editor. I decided that the debugger doesn't matter, the key to being a great developer is to be great at editing. I also read Fowler's Refactoring. Instead of trying to get it right the first time, or get it wrong the first time and live with it, I decided that to get it wrong the first time, and then improve the design with refactoring.

Further learnings: Simplicity and clarity of code are critical. Get those right, and you every other thing you could want (correctness, performance, ability to add new features) become accessible. Also, whenever you have a bug, don't just fix the bug, don't just fix similar bugs in other places, but change how you do things to guarantee that kind of mistake will never happen again.

As a manager, I led a team according to the values of simplicity, refactoring, clarity, testability, getting feedback quickly, striving for good design, coding practices that eliminate entire classes of mistakes, etc. We were fantastically successful. As an individual developer I continued to follow what I had learned as a manager, and had the greatest productivity of my career (although my former team was much better at it than I was.) My point is that these principles have been tested in the real world.

What is the point of putting asserts into our code ? What are the benefits of assertive programming ?

private void WriteMessage(string message)
    Debug.Assert(message != null, "message is null");

    File.WriteAllText(FILE_PATH, message);

For example we can check the message variable and throw an exception here. Why do I use assert here ? Or is this a wrong example to see benefits of asserts ?

For an excellent discussion of assertions (and many other topics related to code construction), check out Code Complete by Steve McConnel. He devotes an entire chapter to effective use of assertions.

What naming convention should I follow for class members? Right now, I’m prefixing all fields with an underscore, and using the regular name for the accessor method, e.g.: int _age and int age() { return _age; }

Any preferred approaches?

Personally I would recommend to start member variables with m_ and the accessor method with get and set prefixes, e.g.: m_age and int getAge() {}.

For a good rule set of naming conventions and best practices read these two books: (general programming style) (C++ specific)

Possible Duplicate:
How do you like your comments?


Edit 2: Where the above question asks for general thoughts on commenting code, I am more interested in ways to ensure that code and comments stay in step.

I've read both of Steve McConnell's excellent Code Complete books "Code Complete" and "Code Complete 2" and was wondering if people have any other suggestions for keeping comments and code in step.

Apart from an excellent technique for removing the necessity for writing comment's, this blog post by Jeff about commenting doesn't really cover keeping things in step.

My commenting mantra could be summed up by the basic idea of expressing "what the code below cannot say".

As you say, comments should express what isn't already obvious from the code (the reason for the code being as it is, the approaches that were tried and found to have flaws that led to the code being the way it is now, etc)

But in a more general sense, think of code as being logically grouped into related chunks. Namespaces, Classes, Methods, Blocks, Lines. These chunks form a hierarchical overview of the application. So by commenting the blocks and giving an overview of what they do, you can summarise the code and make it quick and easy for someone else to find the bit that is relevant to them, understand it, and then use or modify it effectively and with minimum risk of failure.

So explaining that File.Open opens a file is of no use.

But explaining that a block of 10 lines of code will locate, open, read, and store the preferences for your application, you save the reader having to actually read and decode the full text. In a few words they can understand the full purpose of the code, and know whether or not it is something they need to delve deeper into. Or if they wish to call the code, they will understand what it does and how to use it - again, without needing to actually delve into the finer details of how it achieves its behaviour.

So, a comment is required at the beginning of any important chunk of code (at any/all of the levels described above) where a summary will save the reader having to read the code to understand reasonably well what it does. And in any place where you need to annotate the code to explain how or why it does what it does.

Remember that your comment is describing code to someone who has never seen that code before. Or to yourself when you revisit it in 6 months. A few well chosen words can save hours of work trying to decode a full understanding of some code.

as you can probably tell from my previous posts i have horrific naming conventions. do you know of any tutorials dealing with how to name stuff?

Can you please tell me some tutorials/books about thinking a large application in c#. I have worked with 3 tier, but i need something more specific:

a way to design panels, so that for specific users specific panels will load and specific dlls a database management so that when a user modifies an entry, others can not modify that entry should i use entity framework or should i go old school with db and class creation

any links on the above will be greatly appreciated

thanks a lot!

Kind of a broad subject but two books I would recommend giving a read regardless of what language you are doing or the specifics of your project:

If you have specific questions about how to accomplish something I would post it on here or google it.

That's not gonna be covered by a single book or tutorial, trust me. You need to decide a UI framework, data access technology, patterns and the overall architecture. And of course you need an in-depth knowledge on .NET framework, & how the CLR works. It's nothing C# specific. Since you have UI customization (Panels) requirements, WPF is definitely a better candidate for UI framework.

That being said -

  • For C#: Pro C# 2010 and the .NET 4 Platform

  • .NET & CLR: CLR via C#

  • UI Framework - WPF: Pro WPF in C# 2010

  • Use MVVM with WPF. MVVM will keep you on track maintaining the Separation of Concern. You can follow Microsoft's Prism guideline. For large applications with sophisticated UI requirements Prism is a better choice. You might need to use Dependency Injection. MEF is there to go hand-to-hand with MVVM & Prism.

  • Yes, Entity Framework can handle concurrency issues, but that its available in almost all data access framework. Entity Framework provides lots of other facilities, but when using it you should have clear idea of what you're doing. Programming Entity Framework is definitely gonna help.

Microsoft .NET: Architecting Applications for the Enterprise - with a very good code example and there is a chapter on concurrency

Professional Enterprise .NET

ASP.NET 3.5 Website Programming: Problem - Design - Solution

I guess, there are not any online recources that could compete the complexity of these books.

Hallo everybody

Recently I've been reading the book:

"Agile software development, Principles, Patterns and Practices" by Bob Martin

The following (S.O.L.I.D) agile-design-principles are listed within the book:

  • Single Responsibility Principle
  • Open Closed Principle Principle
  • Liskov Substitution Principle
  • Interface Segregation Principle
  • Dependency Inversion Principle

Because of the fact that this book quite old(2003), I have a question:

  1. Are there any other newly developed principles besides the SOLID methods?? If yes, is there any book/site covering these new emerging principles with practical code examples that you could recommend to me??

Of course I can google for some of these.

However, in stackoverflow read and write many profis, so I would like to hear their opinion too :D

You may want to look at books such as Code Complete and Pragmatic Programmer as they also talk about some excellent development principles.

I highly like to comment my code for my own reference, and also since i keep (most) of my projects open-source.

I current comment as if I'm the narrator:

// Check if y is higher than x.

Should i comment as if im talking to a group of people?:

// Now we check if y is higher than x.

This isn't much of a question, i just want to get people's commenting preferences


As Mark Byers says in his comment to the question, explain why you are doing it.

Have a look at the relevant section in Steve McConnell's "Code Complete 2". Too many comments serve to detract, especially when the are of the type "now add one to the counter".

Comments should only be there if it is necessary to highlight something in the code or to explain why you are doing something. Not just "adding to the noise".



I'm reading Code Complete and there is the statement in it that warns against using a variable with a double purpose, for example:

1) If a variable is a number, it contains error code.
2) If a varibale is array, it contains data.

And that's exactly what I'm doing in my program with the variable $text in the code snippet below:

$text = $editor->getTextForLinking($data_array['idText']);
if (Arr::is_array($text)) {
} else {
    Log::instance()->add(Log::Error, $text);
    $this->response->body("Text can't be retrieved");

I have access to the method getTextForLinking() so it can be changed. How can it be changed to exclude the undesirable situation with double purpose?

I don't want to use exceptions like this:

$text = Array();
try {
    $text = $editor->getTextForLinking($data_array['idText']);
} catch(SomeException $e) {
    Log::instance()->add(Log::Error, $text);
    $this->response->body("Text can't be retrieved");

I think its clear that if anything returned by getTextForLinking(), that is not an array, should be considered an error (logged) - So I'm not entirely convinced that your example warrants such a change.

With that said it might be an improvement to keep the return signature of the function the same data type (array) regardless of what data you send it. This way it will be consistent (you loose the need for $text = Array();) and you wont have to make special cases depending on if its an error or not.

$results = $editor->getTextForLinking($data_array['idText']);
if (empty($results)) { 
  Log::instance()->add(Log::Error, $data_array['idText']);
} else {
  // Handle results array 


If you are setting an error message within the function, this violates the single responsibility principle - A function/method should only have one job. As far as the $editor->getTextForLinking() is concerned it will always return an array of text, not deal with the return of an error.

The error message should depend on the context (where the method is used). If at some point an empty array is invalid handle/set the error (message) outside of the function as I have shown above.

Doing it this way allows the $editor to be oblivious of the validity of the returned result and could allow you to reuse the function elsewhere where an empty array is not considered an error.

In Jeffrey Richter's "CLR via C#" (the .net 2.0 edtion page, 353) he says that as a self-discipline, he never makes anonymous functions longer than 3 lines of code in length. He cites mostly readability / understandability as his reasons. This suites me fine, because I already had a self-discipline of using no more than 5 lines for an anonymous method.

But how does that "coding standard" advice stack against lambda's? At face value, I'd treat them the same - keeping a lambda equally as short. But how do others feel about this? In particular, when lambda's are being used where (arguably) they shine brightest - when used in LINQ statements - is there genuine cause to abandon that self-discipline / coding standard?

I don't know if having a guideline for short lambda's and delegates is really useful. However, have a guideline for having short functions. The methods I write are on average 6 or 7 lines long. Functions should hardly ever be 20 lines long. You should create the most readable code and if you follow Robert Martin's or Steve McConnell's advice, they tell you to keep functions short and also keep the inner part of loops as short of possible, favorably just a single method call.

So you shouldn't write a for loop as follows:

for (int i = 0; i < 100; i++)
    // Potentially significant amounts of code

but simply with a single method call inside the loop:

for (int i = 0; i < 100; i++)

With this in mind, while I in general agree with Jon Skeet’s answer, I don't see any reason why you shouldn't want his example to be written as:

Parallel.For(0, 100, i =>


Parallel.For(0, 100, i => WellDescribedPartOfHeavyCalculation(i));

or even:

Parallel.For(0, 100, WellDescribedPartOfHeavyCalculation);

Always go for the most readable code, and many times this means: short anonymous methods, and short lambda's, but most of all short -but well described- methods.

I've been working in ASP.NET for several years now (since the 1.0 days!), but I've never been formally instructed. I'm fully capable of doing pretty much anything I want and I've built several production-level, data-driven sites, including one that does over a million in sales a year (according to the owner). But I'm starting to get the feeling that the holes in my knowlege are dragging my productivity down. I read a lot and try to learn wherever I can to try to stay up with all the new technologies, but sometimes I just don't get it, and I think it's because of my lack of formal training.

Does anyone have any ideas on the best way to fill in these gaps without having to rehash the fundamentals?


1) Read the Code of popular Open Source Projects. There are a few that have some really good practices in place.

I recommend checking out BlogEngine.NET. Also if you're more ambitious, I'd suggest looking at the code for ASP.NET MVC 1.0.

2) Sometimes you need to "get back to the basics" when you've been working with a particular framework since a much earlier version. In this case, it can be really useful to pick up a book that covers some of the newer features.

Here's a good book that shows of some of the new features in C# 3.0:

C# 3.0 Design Patterns

3) It may seem odd, but reading up on other languages/platforms (such as Ruby on Rails) will help you in the way you design your classes and code by taking tips of the good and bad of different platforms and combining them.

4) Read some books on general best practices and development methodologies.

Some of these books I recommend are:

The Pragmatic Programmer: From Journeyman to Master

Practices of an Agile Developer: Working in the Real World

Code Complete: A Practical Handbook of Software Construction

I've seen statements like this

    //do some stuff
    //do some more stuff

and am wondering if putting a function in an if statement is efficient, or if there are cases when it would be better to leave them separate, like this

bool AwesomeResult = SomeBoolReturningFunc();
    //do some other, more important stuff


I seem to recall that one statement per line was the recommendation of the book Code Complete, where it was argued that such code is easier to understand. Make each statement do one and only one thing so that it is very easy to see very quickly at a glance what is happening in the code.

I personally like having the return types in variables to make them easier to inspect (or even change) in the debugger.

One answer stated that a difference was observed in generated code. I sincerely doubt that with an optimizing compiler.

A disadvantage, prior to C++11, for multiple lines is that you have to know the type of the return for the variable declaration. For example, if the return is changed from bool to int then depending on the types involved you could have a truncated value in the local variable (which could make the if malfunction). If compiling with C++11 enabled, this can be dealt with by using the auto keyword, as in:

auto AwesomeResult = SomeBoolReturningFunc() ;
if ( AwesomeResult )
    //do some other, more important stuff

Stroustrup's C++ 4th edition recommends putting the variable declaration in the if statement itself, as in:

if ( auto AwesomeResult = SomeBoolReturningFunc() )
    //do some other, more important stuff

His argument is that this limits the scope of the variable to the greatest extent possible. Whether or not that is more readable (or debuggable) is a judgement call.

Not a book that talks about how to code but more high level organization of software.

Though perhaps it is not strictly architecture-related (although I believe you'll find that most prescriptions of architecture patterns are mere guidelines at best, and far from comprehensive), Steve McConnell's Code Complete is truly required reading. More importantly than teaching you design patterns, it will teach you to be a better programmer so that you can make these kinds of decisions for yourself.

General Responsibility Assignment Software Principles and Domain Driven Design are in my opinion are the next must have things you should get familiar with after learning to code. API Design is also a good read, especially when you are developing the software that will be used/extended by multiple people.

I am not in favor of learning patterns as it is EASIER to misuse them if the intention is not understood correctly. (Everything seems like a nail if you have a hammer kind of thing). I have nothing against patterns but I have seen it mostly misused by the junior developers thus creating hard to maintain products.

Could you give some good reasons for having the class name as part of the name of any variable? We use to have this policy, which I find quite useful. Some team member wants to revert the decision.

My arguments for the moment:

  • you can directly know what you're talking about:

    for (Student student: students) { ... }

is quite easy to understand (vs Student s or Student anyone)

  • it helps self-commenting the code
  • our ide provides direct support for that
  • you can directly see wheter you're using apples instead of pears (or bears ;-) )

Less confusion where subtle differences matter:

criteriaBuilder.equal(nameExpression, name);

The only argument I can see against this is that it makes the code longer (which I think isn't an issue with modern IDEs).

Is there public provisioning for such a recommendation? Anyone using the same rule? Any alternative?

Your bible on this question is Steve McConnel's book, Code Complete, which is the most comprehensive book on software construction practice like this. He has a whole chapter on variable naming and why it is important.

The key is to make the name a full description of what the variable does, so that it is easy to understand for the person reading it. If it achieves that, then it's good practice.

Student student looks like a simple to understand policy, but it has an immediate disadvantage - it contains no extra information about the variable. You already know its a student. If you know anything else about the object then add it to the variable name - studentUnderReview, graduatingStudent etc. "student" should only be used if you know absolutely nothing else, such as the variable is used to iterate over all Students. Now in a long method it's useful to know the type by just looking at the name, but if the variable has short scope then it's marginal whether its useful or not. There are some studies (see McConnel) which indicate that for variables with very short scope, such as for loop indices, short names are better.

As soon as you have two variables, this system breaks down. If the default is to call one variable "student" then the temptation is to call two variables "student1" and "student2", which is bad practice indeed (see McConnel for details). You need to make names that describe the object - goodStudent and badStudent; studentBeingSaved and studentBeingRead.

I always write if statements like this:

if (...) {
    /* do something */

When I create topics on stackoverflow, sometimes people change this code to:

if (...)
    /* do something */

So the first { goes to a new line.

Does it make sense, what's the true way?

I'd suggest reading Code Complete. He goes into quite depth on the subject (Devoting practically an entire chapter on it), and gives what I see as a convincing argument for #1.

He actually says not to use the second one explicitly since it fails the black box test:

if (blah) 

When black boxed turns into:


So the opening brace is seeming like the start of the block, wheras it's not (it's just the delimiter).

Instead, he recommends either the first option (opening brace on the same line), or:

if (blah)

Since it passes the black box test:


Note: I can't remember if the closing delimiter is supposed to be inside the block or not... I don't have my copy handy, so it might be:

if (blah) 

However, it all boils down to consistency. In order for your code to be readable, you need to be consistent in how you format it. Pick a method, and then stick to it. Don't switch from file to file or routine to routine. So long as you're consistent and actually format it, don't worry about it...

I had a, lets say, deficient program design/architecture class in undergrad and thus am really struggling on how to design programs given a problem*. I was wondering if there was a set of resources (books, websites, etc) or problems(kind of like** or path to help me work on this deficiency.

I'm looking for resources/problems/etc to better learn design/architecture patterns within programming.

A great example: For my job, I have implemented an MVC framework in PHP(custom, because I want to learn). I needed to bake User management into this framework and am just struggling with the implementation (I understand to use the $_SESSION var, I just want to user management to be modular and flexible like the rest). This post is not intended to solve my user management issue. I know fixing the design issue will fix this naturally.

I've looked around these here forums and haven't seen any great list of resources to draw off of. If anyone has anything or point me to a post that I missed, feel free to point me there. I really appreciate any insight/help/resource/etc that might be pointed out to me.

*I am familiar and can work with OOP/MVC frameworks, not completely proficient but I can do it **ProjectEuler, where I am tends to be more functional than anything else. I'm slowly working towards integrating OOP etc. But my architecture skills in programming are terrible.

tl;dr: I'm looking for resources/problems/etc to better learn design/architecture patterns within programming.

I'm not really sure what the question is, but if you are only looking for some resources, there is this a good book for architectures - Patterns of Enterprise Application Architecture by Martin Fowler, with some parts available online.

Than there are GoF patterns summed up by Gamma.

Software architecture in practice can be useful too.

Some basic intro to architectures is in this paper, it's kinda old, but still valid...

Basically, GoF patterns, Enterprise patterns and GRASP are patterns you should be aware of...

Was this helpful?

I would start with Head First Design Patterns and the Architecture sections of Code Complete by Steve McConnell. I think those 2 are the best introductions to the concepts and then move into the other stuff.

I have just started looking into .net MVC and I really like it. There are a few developers within our team who think the same. But before we are alowed to use it for any project we need to get the approval of management.

What would be the best way to convince management(which know little about programming) that this would worth while and it could have some cost benefit?

My belief is that it will force us to write better more accessible sites which are a lot more testable than web forms. Which in return would mean less bugs and higher customer satisfaction. But I have no evidence of this.


We have a team meeting in 4 days, this is where I will be making my pitch to the team and management.That gives me two weeks to learn more on MVC and do a presentation.

If you are looking at trying to bring MVC in and need to convince management of the value, your best approach is too show how it can reduce long term support costs. Having an application that has well developed unit tests with decent code coverage can ensure quality over the life cycle of the application because you can higher level of confidence that changes you introduce have not broken pieces of the application. This doesn't negate the need to test the application, but you will find breaking changes faster. An approach utilizing MVC over MVC is a much more flexible platform for this testing.

There are many studies you can utilize that show the long terms support/maintenance costs of an application are often greater than the initial development costs. Stephen McConnell's Code First has a good discussion of this.

Good luck!

I'm using Google protocol buffers in a messaging application. The protos are often nested several levels deep, and we have taken Google's advice and made them all optional. The protos can describe many different types of overlapping messages - i.e. a message of Type == X should contain the member my_X, Type == Y should contain my_Y. Further, certain combinations of fields impose other restrictions on which fields should be present and what values they should have. Changing the proto's structure is beyond the scope of what I can do.

Right now, all of this validation is in a mess of if..else statements. If the cases didn't overlap, that might be workable, but validation cases can impose different restrictions on a certain field, so it can get pretty ugly. Is there a better way? My goal is to make the code more maintainable. I've looking into functors/predicates a little, and it seems like that may simplify the problem, but it still seems like it will be a mess.

If the code starts to contain too many ifs and elses then sometimes a table-driven approach is the solution. Chapter 18 of Code Complete Edition 2 does a good job explaining the concept with plenty of examples. You can find a few examples in this article as well.

Hoping to help.

I'm wondering if this is the best way to tackle this issue. I am merging a Facebook users friends data, (from facebook - returns a multi array) with the votes from the users in that list that voted (from MySQL).

This is how I accomplished this. I'm a junior developer and looking for help on making my code as optimized as possible.

public function getFriendVotes(){
    global $facebook;

    // Get The users friends that use this app from facebook
    $friends = $facebook->api_client->fql_query(
      "SELECT uid, first_name, last_name
        FROM user
        WHERE uid IN (SELECT uid2 FROM friend WHERE uid1=$this->user)"

    // Create an array of just the ids
    foreach($friends as $friend){
      $userids[] = $friend['uid'];

    // Create a string of these ids
    $idstring = implode(",", $userids);

    // Get the votes from only the users in that list that voted
    $result = $this->db->query(
      "SELECT vote, userid FROM user_votes WHERE userid IN ($idstring)"

    // Create a new result set (multi array).  Include the data from the first
    // Facebook query, but include only those who voted and append their votes
    // to the data
    $row = $result->fetch_assoc();
    foreach($friends as $friend){
      if($row['userid'] == $friend['uid']){
        $return[$count] = $friend;
        $return[$count]['vote'] = $row['vote'];
        $row = $result->fetch_assoc();
    return $return;

I can't tell you how your code would perform without measuring and testing. I would look for other issues with your code, that would make it a bit more readable/maintanable. For example:

Create smaller methods.

Inside the main method , I see some chunks of code that are well commented. Why not create a method instead of making a huge comment in the main method?

For example:

// Get The users friends that use this app from facebook
$friends = $facebook->api_client->fql_query(
  "SELECT uid, first_name, last_name
    FROM user
    WHERE uid IN (SELECT uid2 FROM friend WHERE uid1=$this->user"
return $friends;

Would make an interesting

functin get_users_friends_from_facebook($facebook){
    // Get The users friends that use this app from facebook
    $friends = $facebook->api_client->fql_query(
      "SELECT uid, first_name, last_name
        FROM user
        WHERE uid IN (SELECT uid2 FROM friend WHERE uid1=$this->user"
    return $friends;

In the same manner,

// Get the votes from only the users in that list that voted
$result = $this->db->query(
  "SELECT vote, userid FROM user_votes WHERE userid IN ($idstring)"

Is a good candidate to

function get_votes_from_voters(){
    // Get the votes from only the users in that list that voted
    $votes = $this->db->query(
      "SELECT vote, userid FROM user_votes WHERE userid IN ($idstring)"

Give variables meaningful names to the context.

$return isn't a good name. Why don't you name it $users_votes for example?

Try to keep the naming convention of your plataform.

Check the apis you're using. Are they using camelCase? Are they using underscores? Try to keep with your libraries and plataform conventions. Check this topic for a good reference.

And welcome to SO. Your code is fine. Try to read some OO principles, you could even cut more lines of your code. All the simple advices I wrote here are avaiable in a great book named Code Complete.

In C#, when setting a boolean variable's value to false only when it is true, should I check if it is true before setting it or just set it?

Assuming the variable is true 50% of the time, checking it makes sense since a comparison is faster. If the variable is true most of the time, should I just skip the comparison?

Which is the better practice?

Method 1, checking first:

if (bVariable)
    bVariable = false;

or Method 2, Just setting it:

bVariable = false;

Under which conditions is method 2 preferred, if ever?

What makes you think that the comparison is faster? If the code you were writing was done in a C compiler, the IF statement would be split into at least two instructions -- a comparison/branching instruction and a "set" instruction on a single bit on a single word.

The "set" would compile to a single instruction. Your "optimization" could possibly make your program run slower, and would cause your program to be less readable. Just set the variable and don't try to overthink little things.

CPUs aren't like databases. You don't pay a high penalty for data modifications. You pay high penalties for making trips to main memory and for branching (if statements). Branches cost performance because pipelining CPUs actually start executing instructions after the branch before the branch instructions even make their decision! (I know, that statement is somewhat mind-blowing). But what that means is that the CPU has to spend resources "guessing" what the outcome of your IF statement is going to be. If it guesses wrong, it has to "throw away" the results of all instructions that it guessed would be executed after the branch and try again. It's bad. It's why branches are expensive.

The moral of this particular story is not that you should never optimize, but that you should never optimize without completely understanding the implications of your optimization. In this case, had you gone with Option 1, you could possibly end up with a slower app which is less readable to boot.

Actually, if you're really interested in this sort of thing, you should definitely pick up a copy of Code Complete. It's full of discussions about this sort of thing, and is brilliantly written.

In my free time, I've been trying to improve my perl abilities by working on a script that uses LWP::Simple to poll one specific website's product pages to check the prices of products (I'm somewhat of a perl noob). This script also keeps a very simple backlog of the last price seen for that item (since the prices change frequently).

I was wondering if there was any way I could further automate the script so that I don't have to explicitly add the page's URL to the initial hash (i.e. keep an array of key terms and do a search query amazon to find the page or price?). Is there anyway way I could do this that doesn't involve me just copying Amazon's search URL and parsing in my keywords? (I'm aware that processing HTML with regex is generally bad form, I just used it since I only need one small piece of data).

use strict;
use warnings;
use LWP::Simple;

my %oldPrice;
my %nameURL = (
    "Archer Season 1" => "",
    "Code Complete" => "",
    "Intermediate Perl" => "",
    "Inglorious Basterds (2-Disc)" => ""

if (-e "backlog.txt"){
    open (LOG, "backlog.txt");
        my @temp = split(/:\s/);
        $oldPrice{$temp[0]} = $temp[1];

print "\nChecking Daily Amazon Prices:\n";
open(LOG, ">backlog.txt");
foreach my $key (sort keys %nameURL){
    my $content = get $nameURL{$key} or die;
    $content =~  m{\s*\$(\d+.\d+)} || die;
    if (exists $oldPrice{$key} && $oldPrice{$key} != $1){
        print "$key: \$$1 (Was $oldPrice{$key})\n";
    print "\n$key: $1\n";
    print LOG "$key: $1\n";

I made simple script to demonstate Amazon search automation. Search url for all departments was changed with escaped search term. The rest of code is simple parsing with HTML::TreeBuilder. Structure of HTML in question can be easily examined with dump method (see commented-out line).

use strict; use warnings;

use LWP::Simple;
use URI::Escape;
use HTML::TreeBuilder;
use Try::Tiny;

my $look_for = "Archer Season 1";

my $contents
  = get ""
        . uri_escape($look_for);

my $html = HTML::TreeBuilder->new_from_content($contents);
for my $item ($html->look_down(id => qr/result_\d+/)) {
    # $item->dump;      # find out structure of HTML
    my $title = try { $item->look_down(class => 'productTitle')->as_trimmed_text };
    my $price = try { $item->look_down(class => 'newPrice')->find('span')->as_text };

    print "$title\n$price\n\n";

There are many different styles of variable names that I've come across over the years.

The current wikipedia entry on naming conventions is fairly light...

I'd love to see a concise catalog of variable naming-conventions, identifying it by a name/description, and some examples.

If a convention is particularly favored by a certain platform community, that would be worth noting, too.

I'm turning this into a community wiki, so please create an answer for each convention, and edit as needed.

The best naming convention set that I've seen is in the book "Code Complete" Steve McConnell has a great section in there about naming conventions and lots of examples. His examples run through a number of "best practices" for different languages, but ultimately leave it up to the developer, dev manager, or architect to decide the specific action.

A colleague of mine preferes to encapsulate all of the PHP functions behind classes. His justification seems to be that if its in a class its going to be easier to change if they ever update the code. My concern after looking at his code is I have no clue what might be happening, or why things might error out. Additionally if it were in class form I'd prefer a a static function to what he is doing as it seems to me would be better practice.

For example instead of:

$var = filter_var('0755', FILTER_VALIDATE_INT);

He would have:

class Filter {
     public function filterInt($var) {
           return filter_var($var, FILTER_VALIDATE_INT);

$filter = new Filter();
$var = $filter->filterInt('0755');

Which way would be better practice? better for long term performance? better for understanding?

Hiding functionality in methods is good practice, Code Complete explains quite well, why that's the case.

If you ever want to change the filterInt, e.g. add upper and lower bounds, you are way better off if you have encapsulated it.

  • less code lines to be changed
  • easier to test against
  • ..

Are there any official C++ recommendations that concern with the amount of information that should be disclosed in a method name? I am asking because I can find plenty of references in Internet but none that really explains this.

I'm working on a C++ class with a method called calculateIBANAndBICAndSaveRecordChainIfChanged, which pretty well explains what the method does. A shorter name would be easier to remember and would need no intellisense or copy & paste to type. It would be less descriptive, true, but functionality is supposed to be documented.

calculateIBANAndBICAndSaveRecordChainIfChanged considered to be a bad function name, it breaks the rule of one-function-does-one-thing.

Reduce complexity

The single most important reason to create a routine is to reduce a program's complexity. Create a routine to hide information so that you won't need to think about it. Sure, you'll need to think about it when you write the routine. But after it's written, you should be able to forget the details and use the routine without any knowledge of its internal workings. Other reasons to create routines—minimizing code size, improving maintainability, and improving correctness—are also good reasons, but without the abstractive power of routines, complex programs would be impossible to manage intellectually. You could simply break this function into below functions:


To name a procedure, use a strong verb followed by an object

A procedure with functional cohesion usually performs an operation on an object. The name should reflect what the procedure does, and an operation on an object implies a verb-plus-object name. PrintDocument(), CalcMonthlyRevenues(), CheckOrderlnfo(), and RepaginateDocument() are samples of good procedure names.

Describe everything the routine does

In the routine's name, describe all the outputs and side effects. If a routine computes report totals and opens an output file, ComputeReportTotals() is not an adequate name for the routine. ComputeReportTotalsAndOpen-OutputFile() is an adequate name but is too long and silly. If you have routines with side effects, you'll have many long, silly names. The cure is not to use less-descriptive routine names; the cure is to program so that you cause things to happen directly rather than with side effects.

Avoid meaningless, vague, or wishy-washy verbs

Some verbs are elastic, stretched to cover just about any meaning. Routine names like HandleCalculation(), PerformServices(), OutputUser(), ProcessInput(), and DealWithOutput() don't tell you what the routines do. At the most, these names tell you that the routines have something to do with calculations, services, users, input, and output. The exception would be when the verb "handle" was used in the specific technical sense of handling an event.

Most of above points are referred from Code complete II. Other good books are Clean Code, The Clean Coder from Robert C. Martin

Are there objective reasons for using spaces instead of tabs for indenting files as per PSR-2 standard, can someone provide:

  • facts,
  • references,
  • specific expertise

on which PSR-2 standard is based?

Authors of PSR-2 standard had in mind something more than "look and feel", something more than just opinion based thing, and lots of people have trouble understanding why spaces are better during teamwork.

Explanation on accepted answer:

According to Farsides' answer: repositories thing may be an exact case of why spaces are in PSR-2 explained as indentation tool. PSR-2 is standard developed to assist teamwork. Single accidental spaces at the beginning of line - when using tabs - may not be visible in the IDE and can sneak out to repository. If a couple of people work on same file it is highly possible to generate unnecessary conflicts. Using spaces instead of tabs makes it possible to easily catch such an accidental space on eyeball and this is probably a reason, why using them become a standard.


1. GIT and other version controls systems treat white-space differently

Based on my experience, we faced on our projects: GIT and other version controls systems treat invisible spaces + TABS differently, and it leads to changes in lines, which actually haven't been affected. It's easy not to notice, when there will accidentally added one space + TAB = indent looks the same in IDE, but GIT will make the difference when merging. It damages your ability to effectively compare revisions in source control, which is really scary. It never going to happen when you are having spaces only.

2. Neutralize difference in collaborator's environment (Editor, OS, preferences, etc.)

The tab width (in spaces) depends on your environment (text editor, OS, preferences, etc.), but the space width is the same everywhere. IDEs are smart enough to treat white spaces up to you personal taste, but the output generated for collaboration should be up to standards.

3. Numerous studies on coding style importance

If every collaborator on your project would keep the same standards on coding - it will be good in long run, collaboration is more efficient and professional, the same indent when you refactor or develop. Studies regarding that:

  1. For example, Ben Shneiderman confirmed this in Exploratory experiments in programmer behavior:

    when program statements were arranged in a sensible order, experts were able to remember them better than novices. When statements were shuffled, the experts' superiority was reduced.

  2. An old 1984 study by Soloway and Ehrlich cited in Code Complete, and supported studies from The Elements of Programming Style:

    Our empirical results put teeth into these rules: It is not merely a matter of aesthetics that programs should be written in a particular style. Rather there is a psychological basis for writing programs in a conventional manner: programmers have strong expectations that other programmers will follow these discourse rules. If the rules are violated, then the utility afforded by the expectations that programmers have built up over time is effectively nullified.

At this point in time I am approaching the 1800 line mark in my code.

It contains a series of different forms and one big function which checks, validates, and determines the next step in the process. I have a total of 12 functions and I'ld like to know the programming philosophies and thoughts on whether or not (or when?) to separate the functions into their own file and when to leave them all on the same page.

Any thoughts on both your style of programming and any links to established programming standards of a particular group or philosophy of programming?


According to Code Complete book, a function needs to contain -one- logical unit, if it contains more than one, then break it into two functions. Another hint is if function name is too cumbersome or long. That too is a hint about a function that can be refactored.

Incidentally, Code Complete book should be on reading list of any serious software developer.

Could anyone please explain this to me with an example? I am getting contradicted myself

  • High Fan in: A given class designed in such a way that it a high number of other classes can easily consume it.
  • High Fan out: A class should be using lot of other classes.

Both seems self contradictory. Can any one explain it with an example? possible in .NET framework.

Where did you read the High Fan Out principle? AFAIK, it is bad with High Fan Out.

High fan-out in object-oriented design is indicated when an object must deal directly with a large number of other objects. This is indicative of a high degree of class interdependency. In general, the higher the fan-out of an object, the poorer is the overall system design.

Also mentioned in Code Complete, High Fan In with Low Fan Out are good class designs.

Good morning everyone,

I hope I'm not posting this in the wrong place; I've been programming web projects, mostly in MVC and ASP.NET for consulting companies, but I always pick-up unfinished projects, so I gotta say, my experience in web development isn't as good as I'd like it to be. To improve my experience, I decided to accept building a project for a veterinarian clinic and I'm going to build the project in MVC. There are a few things I'd like to know to make my project well structured and to avoid feeling lost in the process because I don't have as much time to research as I'd like to. So the main questions I'd like to ask are:

  • When beginning a new project, where should I begin? Making the stylesheets? Should I go straight for the code? If I make some planning, how should I go about it then?

  • When building up the Media folder in my project, if I decide I'll use
    jQuery and the like, what files
    should I really get? What's the best way to implement jQuery in a MVC
    project without having to mention it in every page?

  • To make a sort of planning for
    myself, complete with deadlines I
    have to respect, what structure
    should I use?

  • Well, I'm not good at designing at
    all, and I often have to rely on
    other people's CSS to make things
    look decent, so how could I use this project to improve that and still
    make it look good?

I hope we can all share some experience in the matter at hand and make this topic help others who might be feeling the same weaknesses as I do.

When you start building your code I suggest you start with register, login and authentication. After that: Internationalization and localization (see:

Then create your CRUD's and so on..

EDIT: Some other resources you might wanna have a look at:

Good luck!!

I am currently working on a social networking application that needs to be highly scalable.

I have been reading about the publish/subscribe pattern(message bus) and am struggling with understanding proper use case scenarios - when this would be appropriate and when this would be overkill?

For example:

  • There are several areas of the site where users enter information on a form that needs to be saved to the database;
  • When the database saving occurs and an email notification(s) must be made to one or more users.

Also, for the save scenarios, I would like to give the user friendly messages letting them know their data is saved on the form after saving process completes, if I were to go pub/sub approach.
How would I return success/fail messages back to the UI after a specific task completed?

Which scenarios are ideal candidates for pub/sub pattern? It seems to be overkill for basic form database saving.

I suppose there is no definite answer to your question. IMHO, nobody can evaluate the performance of a design pattern. Perhaps, someone could consider comparing it with another design pattern but even then the comparison would be at least unsafe. The performance has to do with the actual implementation which could vary between different implementations of the same design pattern. If you want to evaluate the performance of a software module, you have to build it and then profile it. As Steve McConell in his legendary book suggests, make no decisions regarding performance without profiling.

Regarding the specific pattern and your scenarios, I would suggest to avoid using it. Publish-subscribe pattern is typically used when the subscribers do not want to receive all messages published, but rather some of them satisfying some specific criteria (eg belonging to a specific kind of messages). Therefore, I would not suggest using it for your scenarios.

I would also suggest looking at the Observer pattern. I suppose you could find many more references online.

Hope I helped!

I just found ... AGAIN ... a real time wastage bug as follows

for (int i = 0; i < length; i++)
{ //...Lots of code 
    for (int j = 0; i < length; j++)
        //...Lots of code 

Did you notice straight ahead the inner i which SHOULD BE j ? Neither did I. So from now on I am going to use:

for (int i = 0; i < length; i++)
    for (int i1 = 0; i1 < length; i1++)

What are your tips for inner and outer while and for loops ?

Edit: Thanks for the valuable responses. Herewith short summary of the proposed tips:

  • use meaningful variables names for index variables ( instead i use SomeObjCollectionLength )
  • place the contents of the inner loop into a separate method and call that method from the outer loop
  • not manageable amount of lines of code between the outer and inner loop is a strong signal for code smell
  • avoid copy pasting and rushing , write the index vars with care

You might want to check the summary by LBushkin for the following

  • use foreach and iterators whenever possible
  • initialize the variables just before entering the loops
  • Make each loop perform only one function. Avoid mixing responsibilities in a single loop
  • When possible, make your loops short enough to view all at once

My top advice (in no particular order) for writing better loop code (much of this is from the excellent book Code Complete):

  1. Avoid multiple exit points for loops.
  2. Use continue/break sparingly.
  3. Refactor nested loops into separate routines, when possible.
  4. Use meaningful variable names to make nested loops readable.
  5. Use foreach() loops when possible, rather than for(i=...) loops.
  6. Enter the loop from one location only. Don't jump into a loop with goto's. Ever.
  7. Put initialization code immediately before the loop.
  8. Keep loop initialization statements with the loop they are related to.
  9. Avoid reusing variables between non-nested loops. 10.Limit the scope of loop-index variables to the loop itself.
  10. Use while(true) for infinite loops, rather than for(;;)
  11. In languages that provide block constructs (e.g. '{' and '}') use them rather than indenting to enclose the statements of a loop. Yes, even for single line loops.
  12. Avoid empty loops.
  13. Avoid placing housekeeping chores in the middle of a loop, place them at the beginning and/or end instead.
  14. Make each loop perform only one function. Avoid mixing responsibilities in a single loop.
  15. Make loop termination conditions obvious.
  16. Don't monkey with the loop index variable of a for() loop to make it terminate.
  17. Avoid code that depends on the loop indexer's final value.
  18. Consider using safety counters in complex loops - they can be checked to make sure the loop doesn't execute too many, or too few times.
  19. Use break statements, when possible, to terminate while loops.
  20. When possible, make your loops short enough to view all at once.

I'm new to MVC and EF, so excuse me if my question is not clear or easy.

I have read some tutorials about EF Code-First approach and now I'm trying this tutorial Getting Started with EF using MVC.

Using Code-First approach I'm using the POCO classes to define my DB model, DB logic, DB validation, UI validation and some UI logic. So I can use these classes(or objects) in my presentation layer or use them as JSON objects when dealing with Web-Services(or in JavaScript code).

My question: Isn't that mixing some logic together? I mean shouldn't I use like special view-model classes for presentation and these classes should have the UI logic and validation ?!

Is it good practice to send the POCO object to the view(or to the client in general) ?

Finally i need some guidance on how to organize my project layers or folders? I see many ways and I'm confused about what to choose or what format I should base my own on?!!

You should not use your data entities in higher layers of your application. Transform/compose/copy data from DAL into view model or business classes using a library like Automapper. This keeps the layers of your application independent and hides your database schema from end users.

Keep in mind that MVC is only concerned with presentation, all real work should happen in other layers of your app. I try to keep layers independent and use a little bit of glue code to bundle them into an app. I may write data access and business logic layers when working on MVC project, but then re-use in a command line utility. There are a lot of books that talk about writing quality code. I personally found Clean Code, Code Complete 2, and Design Patterns extremely helpful.

I'm wondering which of the two options below is the most efficient in term of speed. There is probably just a really small difference between them (or no difference at all ?) but since I'm using that snippet of code 30 times a day, I would like to know what's the "best practice" here :)

Option 1 :

if (sender != null)
   LabelSave.Text = "Saved";
   LabelSave.Text = "Auto-Saved"; 

Option 2 :

if (sender == null)
    LabelSave.Text = "Auto-Saved";
    LabelSave.Text = "Saved";

Please don't take in consideration the value of the "sender" variable, the real question here is "what is the fastest operator between "!=" and "==" in c# ?"

I think the efficiency will change depending on the variable type, so like Eric said, you would really need to measure it to find out.

However, if you are asking which way around you should write it, I can suggest that Option 2 is (usually) better. The reason being that it's one less mental step to read without the negation. Since code is written once and read many times, we should almost always make readability our number one priority. We should only start working on efficiency once we've proved we need to.

There's a great book called Code Complete that talks about this kind of stuff that I really recommend.

Currently, I am using following code to convert an irregular multidimensional array into one dimensional array.

my $array = [0, 
        [3, 4, 5],
            [7, 8, 9 ],

my @mylist;

print Dumper (\@mylist);

sub getList


        my $array = shift;

        return if (!defined $array);
        if (ref $array eq "ARRAY")
               foreach my $i (@$array)
               print "pushing $array\n";
               push (@mylist, $array);

This is based on recursion where I am checking each element. If element is a reference to an array then calling it recursively with new array.

Is there a better way to solve this kind of problem?

First of all your function should never return data by modifying a global variable. Return a list instead.

As for efficiency, Perl has a surprisingly large function call overhead. Therefore for large data structures I would prefer a non-recursive approach. Like so:

use Data::Dumper;
my $array = [
  [3, 4, 5],
  [6, [7, 8, 9 ]],

my @mylist = get_list($array);

print Dumper (\@mylist);

sub get_list {
    my @work = @_;
    my @result;
    while (@work) {
        my $next = shift @work;
        if (ref($next) eq 'ARRAY') {
            unshift @work, @$next;
        else {
            push @result, $next;
    return @result;

Note that the formatting that I am using here matches the recommendations of perlstyle. We all know the futility of arguing the One True Brace Style. But at the least I'm going to suggest that you reduce your 8 space indent. There is research into this, and code comprehension has been shown to be improved with indents in the 2-4 space range. Read Code Complete for details. It doesn't matter where you are in that range for young people, but older programmers whose eyesight is going find 4 a better indent. Read Perl Best Practices for more on that.

Having been a hobbyist programmer for 3 years (mainly Python and C) and never having written an application longer than 500 lines of code, I find myself faced with two choices :

(1) Learn the essentials of data structures and algorithm design so I can become a l33t computer scientist.

(2) Learn Qt, which would help me build projects I have been itching to build for a long time.

For learning (1), everyone seems to recommend reading CLRS. Unfortunately, reading CLRS would take me at least an year of study (or more, I'm not Peter Krumins). I also understand that to accomplish any moderately complex task using (2), I will need to understand at least the fundamentals of (1), which brings me to my question : assuming I use C++ as the programming language of choice, which parts of CLRS would give me sufficient knowledge of algorithms and data structures to work on large projects using (2)?

In other words, I need a list of theoretical CompSci topics absolutely essential for everyday application programming tasks. Also, I want to use CLRS as a handy reference, so I don't want to skip any material critical to understanding the later sections of the book.

Don't get me wrong here. Discrete math and the theoretical underpinnings of CompSci have been on my "TODO: URGENT" list for about 6 months now, but I just don't have enough time owing to college work. After a long time, I have 15 days off to do whatever the hell I like, and I want to spend these 15 days building applications I really want to build rather than sitting at my desk, pen and paper in hand, trying to write down the solution to a textbook problem.

(BTW, a less-math-more-code resource on algorithms will be highly appreciated. I'm just out of high school and my math is not at the level it should be.)

Thanks :)

I would say the practical aspects of coding are more important. In particular, source control is vital if you don't use that already. I like bzr as an easy to set up and use system, though GUI support isn't as mature as it could be.

I'd then move on to one or both of the classics about the craft of coding, namely

You could also check out the list of recommended books on Stack Overflow.

For a less-math, more code resource on algorithms than CLRS, check out Algorithms in a Nutshell. If you're going to be writing desktop applications, I don't consider CLRS to be required reading. If you're using C++ I think Sedgewick is a more appropriate choice.

I am a begining programmer with some old AutoHotkey (scripting language) experience plus basic html.

I am reading Code Complete 2nd ed in the bathroom & Java How to Program in my class. - It is drab by comparison.

I don't want to spend too much time learning things that are, unofficially depreciated or otherwise silly. I don't have any real mentors (besides you guys), so I was just wondering if any experienced programmers would have solid any advice for me. I want the good stuff!

Read Effective Java (2nd Edition) . It is practically required reading for any Java programmer, both future and present. You won't find a better book for information on best-practices in Java. Some of the topics may be a bit advanced for a beginner, but you'll definitely want to get through most of hte book at some point.

I read the Core Java pair of books. They cover just about every aspect of Java itself, including advanced material like JNI and RMI. I found that the material was well-presented, thorough, up-to-date (make sure you have the latest editions which will cover everything in Java 1.6) and interesting to read. The problem was that I had done a few Java projects before really learning the language and libraries, and you can definitely see the difference in code quality. It's a lot of reading, but I found it to be well worth it. Everything is laid out well, so if for example Swing/GUI programming doesn't interest you, you can skip those chapters.

Other than that, I would say that it is important to truly know your development tools. I use Eclipse, and I have spent a lot of time over the years learning how it works. There are hundreds of hotkeys and they increase my productivity immensely.

And also I sit around all day reading other people's Stack Overflow answers if they look interesting :)

If I try to keep code and "other related stuff" together, how is that called? Or if I fail to do this, would that be an (anti-)pattern with a name? (I need the name to refer to this practice in a document that some people will call scientific.)

An example of what I mean would be to use annotations rather than XML configuration files in Java (for example JPA, Spring) to keep metadata close to the source code and thus reducing the risk that if code is updated it is not forgotten to update metadata.

This sounds like functional cohesion (from Wikipedia):

Functional cohesion is when parts of a module are grouped because they all contribute to a single well-defined task of the module (e.g. tokenizing a string of XML).

Other types of cohesion are:

  • Coincidental cohesion (worst)
  • Logical cohesion
  • Temporal cohesion
  • Procedural cohesion
  • Communicational cohesion
  • Sequential cohesion

For further reading, I suggest Steve McConnell's Code Complete, 2nd Edition.

I have lengthy functions in VB.NET (VS2008) and I'd like to shrink them down. I know about the #region directives, but they don't work inside functions.

I was wondering if anyone knew of any plugins for visual studio 2008 that would allow me to fold if statements, loops and try catch statements. I've found a couple of plugins for Visual Studio 2005 but none for Visual Studio 2008.

You may want to read Martin Fowler's book called Refactoring: Improving the Design of Existing Code, as well as Code Complete: 2nd Edition.

refactoring bookcode complete book

If you're having functions and subroutines that long, it means there are larger complexity forces at work that need refactoring.


I just finished the book Clean Code by "Uncle" Bob Martin, and I have to say that it belongs right next to the other two in the 'Must Read' category.

Clean Code

I keep having problems deciding how I should create my classes internally. Initially I would have an external class handle all the variable management:

String destination = car.setDestination("San Fransisco");
int totalGas = car.getAmountOfGas();
int requiredGas = car.gasRequiredForDestination(destination);
boolean enough = car.hasEnoughGas(totalGas, destination);
if (enough) 

But it seemed really strange to me that another class should be doing all the work for the car class's data, since the car should be able to do most of the work itself. So to fix that I thought... "hmmm let me just stick all this in the class where it seems like it should go". I figured by doing this I could avoid having to pass so much data back and forth between methods. So then I got this:

Class Car {
  String location = "home";
  String destination;
  int totalGas = 0;
  int requiredGas = 0;
  boolean enoughGas = false;

  public Car (String destination, int totalGas) {
    this.destination = destination;
    this.totalGas = totalGas;
  public boolean travelToDestination() {
     if (enoughGas == true)
        location = destination;

So the problem I encountered here is that now yes I don't have to pass the data around and things look real clean, but now I am dependent upon each function to return the value to the instance variable. Which in itself isn't terrible, but this just seems very wrong to me. At the same time I think to myself "well I doesn't make sense to pass all this data from one place to another when it seems like I should just manipulate the instance variables. On the other hand my programs end up having 6 lines in a row of:

myMethod {

I never really see things done this way in real life so I'm trying to figure basically A) if this is wrong B) if so when should we stick information in instance variables rather than pass it all around. Object Orientation allows us to do this but I don't know if it's a good thing. C) Are there any OO principles involved in doing or not doing things this way? Maybe something I'm violating that I'm not aware of?

I've been programming OO for a long time but I tend to have issues like this on and off so I was hoping to sort it out. If there are any book recommendations that deal with the trickier side of OO I'd be interested in that too.

EDIT: I should have said right off that this is a made up example so there are things in the real world I probably would not do this way necessarily. But I needed some sort of example as the code I had was too complicated.

Well this question can be explained in detail by wiser heads than me. But here is my take:

I tend to try to define classes as some data, and set of operations that need to be performed on them (following class inheritance hierarchy of course). So your approach of encapsulating operations on Car class is right because in this case you would just do


and it would be fine.

myMethod {

is not wrong in the sense that all your methods above are doing ONE logical operation each (I strongly recommend making methods do ONLY one logical operation) and they are being used correctly.

Regarding class data being passed around, it is generally considered a good design if you can encapsulate data and operations on it internally in one class, which seems to be the how you want to do in your example.

Regarding book recommendation, I found for myself Code Complete 2 has an excellent chapter on class design (chapter 6 called Working Classes, I think) that should address doubts like this. I find myself referring to it quite a bit. Either way I think that book should be required reading for all programmers.

I'm very new to flash and actionscript 3. I've been reading a lot about it and this is also my first aprouch on object oriented programming.

So far, I created an application with a login button, that's all. However, I would like to know what kind of things I am doing wrong or should be doing different (or better). I am using Adobe Flex Builder 3.

The main actionscript file is

    //import required libraries
    import flash.display.Sprite;

    //set project properties
    [SWF(width="800", height="600", frameRate="31", backgroundColor="#C0C0C0")]

    //launch main class
    public class Client2 extends Sprite
        public function Client2() { //the constructor
            trace("Client launched.");
            var loginGui:LoginInterface = new LoginInterface(); //load the login interface object
            loginGui.init(); //initialize the login interface (load it)
            addChild(loginGui); //add login gui to the display tree

It is loading the login interface object. Is that a good thing, and am I doing it the right way?

Then there's the class file:

    //import required libraries
    import flash.display.Sprite;

    //the LoginInterface class
    public class LoginInterface extends Sprite
        public function LoginInterface() //the constructor
            trace("LoginInterface object loaded.");

        public function init():void //initialize the login interface (load it)
            trace("LoginInterface init method was called.");
            var loginButton:CustomButton = new CustomButton(300, 300, 100, 30, 3, 18, "Login!"); //create a new custom button
            addChild(loginButton); //add the custom button to the display tree

What about that? Any comments? To make the creation of simple buttons a bit easier, I then created another class file called -->

    import flash.display.SimpleButton;
    import flash.display.Sprite;
    import flash.text.TextField;
    import flash.text.TextFormat;
    import flash.text.TextFormatAlign;

    public class CustomButton extends Sprite
        public function CustomButton(xLoc:int, yLoc:int, width:int, height:int, iLabelOffset:int, fontsize:uint, label:String)
            //create new simple button instance
            var myButton:SimpleButton = new SimpleButton();
            //create the look of the states
            var normal:Sprite = new Sprite();
  , 0x000000);
  , yLoc, width, height);
            //the mouseover sprite
            var over:Sprite = new Sprite();
  , 0x000000);
  , yLoc, width, height);
            // assign the sprites
            myButton.upState = normal;
            myButton.downState = normal;
            myButton.hitTestState = normal;
            myButton.overState = over;
            //add the button to the display tree

            //create button label
            var tText:TextField = new TextField();
            tText.mouseEnabled = false,
            tText.x = xLoc;
            tText.y = yLoc + iLabelOffset;
            tText.width = width;
            tText.selectable = false
            var Format:TextFormat = new TextFormat();
            Format.font = "Arial";
            Format.color = 0x000000;
            Format.size = fontsize;
            Format.bold = false;
            Format.align = TextFormatAlign.CENTER;
            tText.defaultTextFormat = Format;
            tText.text = label;

Is there anything to comment on this? I am sure that I'm doing a lot of things wrong, maybe I didn't really get the whole object oriented thing? Also, I have a bad feeling about the way I am using the "extends ..." after a class declaration, mainly because I'm just using Sprite all the time and don't really understand why or what it does (having trouble finding out on the internet aswell). Another thing I am unsure about is the naming of variables in AS3. Should I really be using names such as xLoc, or iLabelOffset? I think I am not being very consistent in my variable naming atleast?

I hope someone can give me a push to a better track than the one I am on now, as I am sure that I should improve my AS3 coding before I continue working on this beast.

Thanks a lot.

Spender hit the nail on the head. Those are definitely the issues that are raised when I looked over your code. The things he mentioned are not nessesarly Actionscript issues, (issue's not quite the right word, perhaps "areas to note"), these are issues general to all programing languages. Descriptive naming for example is extremely important.

There are few books that focus on this side of programming, and even fewer that do it well. I would highly recommend picking up the following two books if you want to grow more in this area, (I'll recommend it even if you don't want too :)

Code Complete

The pragmatic programmer

There both books that every programmer should read, so check them out. You're code from an Actionscript point of view is fine, but that's just syntax. It's important to note that these skill will never develop unless you actually write code, so by all means "continue working on this beast" and the rest will follow suit.

I'm working on a project with a contractor we hired to do some VB.Net programming. I used to program for this project, but I have more recently become primarily the project manager so I can work on other issues I have. His method of programming differs greatly from what I was taught in school. The only time he uses classes is basically in the form of a sort of dataset (not an actual dataset, as I learned how to use in school). As an example, there's the cEmployee class, where he creates a class for an Employee, and in that he defines all the fields an employee has.

All functions are put in Modules. So if he has several functions to populate a combo box, they all go in the combo box module. My understanding on this though is that when the program is run, the modules are treated as global functions and included regardless of whether or not they are actually used on a form. To me this sounds like it would increase the resource requirements to run such a program, since the large amount of module code has to be included for every form.

The way I learned was to put functions with specific purposes in namespaces and classes, then import the appropriate namespace and class when needed. Then only the necessary code to run a form is loaded, and the extra code for functions that are not needed isn't loaded until imported on a form. I never used modules myself, but I can see their utility, I just think he is severely mis-using modules in this case. Am I correct?

It's hard to answer without seeing the code. From the information in your question it seemed that your contractor was writing procedural structured code, which is old-fashioned, but not necessarily a huge problem, although many programmers who are used to OO won't enjoy looking at the code.

From your last comment on Dan's answer it sounds like he is writing a module for each functional area, with routines that can act on any object. The routines accept Object or Variant arguments, detect the type of object passed with case/if statements, then branch to the appropriate code for that object. Is that right? That is quite eccentric and hard to maintain. Usually, a better way is to declare interfaces for the functional areas, and have the classes implement the interfaces.

I've seen code like this from self-taught programmers. To be fair to them, they are bright people who were stumbling towards inventing OO programming from scratch! It can be a slow process though, and it's better to learn about other people's conclusions first! I might recommend a course on OO programming, or if he is a reader a book like Head First Design Patterns or even Code Complete 2, which has some good chapters on design. If you can get him hooked on Code Complete, that would be a good thing.

I noticed the last few weeks that programmers with higher experience do not discuss "coding" issues very often, rather they discuss design issues. I really love to learn about these patterns, I know the concepts of OO Programming and Design, I'm already applying a decent amount of these principles in my daily work, I try to keep my components modular, reusable, and each one has a specific, well defined responsibilities. Lately i'v been advised to start learning about the various design patterns (the advisor recommended code complete as a start), he mentioned some names of some popular patterns (like MVC, SingleTon, and others). So SO Gurus! how should I start learning about these patterns?

Because of the detailed explanation i think i'm goona give Saua the point

  1. learn about them (I personally like the C2 Wiki, others might suggest "Design Patterns" by the Gang of Four)
  2. apply them
  3. grossly mis-apply them (prime candidate: singelton)
  4. learn why number 3 was a mistake
  5. learn to apply them properly

That's more of a observation report than an instructional list ;-)

Though Code Complete is an excellent book (it is absolutely a must-have in my opinion), it is not the best of books to learn about patterns.

If you want to learn about patterns, a good book would be the Design Patterns book by the Gang of Four. It is a reference to some of the commonly used design patterns.

As for the patterns you mention: I see the MVC pattern more as an architectural pattern then a design pattern, since it effects the structure of the entire system (the architecture) rather than a more isolated piece of code. Singleton (not singletone) is an easy to grasp pattern, though it is overused and the downsides of it are often overlooked, so I wouldn't recommend to start with it (or at least, read it, learn the idea behind it, but don't just start applying it everywhere for the sake of using it - you usually don't really need it).

It's hard to recommend a pattern to start with, but I think Factory Method, Command, and Strategy are not too hard to learn, but I'm sure others could disagree.

I've an MVC application, which is divided into 3 layers: - Presentation - ASp.NET MVC - Busines Logic - Here we have entities and object services. We alo have mappers between DAL objects and BL objects - Data Access Layer - we use EF to query the database. Now, we've created a factory for object services, and the factory is injected into presentation later using Unity. Each time I want to do some logic, I call an appropriate service which uses DAL repositories to do some stuff. Now, silly question, let suppose that I want to check if I can add a user with a provided nickname. The nickname is unique in the database, so before I add the user, I check if a user with provided nickname exists. So, it's a simple query that returns true/false. Becuase I don't have any connections between presentation layer and business layer, I check it in the service. But the code of the service method simply looks like:

var exists = repository.NicknameExists(nickname);
return exists;

The code above is strange, because it does nothing, just calls a method and returns its value. On the other hand, I've Separation of Concerns, so my solution is well organised. Can someone give me some suggestions, how should I solve problems like that?

I don't see any problem here. It's perfect method for me:

public bool IsUserExists(string nickname)
    return repository.NicknameExists(nickname);

I would recomend to you to read this blog post about valid reasons to create a routine or the chapter 7.1 of the Code Complete 2ed

I've been given some code to go through and find problems and things that could be improved and changed (it's a homework task, but this question is unrelated to the task itself), part of the code is:

Function CheckIfSameCell(ByVal FirstCellPosition As CellReference, ByVal SecondCellPosition As CellReference) As Boolean
    Dim InSameCell As Boolean
    InSameCell = False
    If FirstCellPosition.NoOfCellsSouth = SecondCellPosition.NoOfCellsSouth And FirstCellPosition.NoOfCellsEast = SecondCellPosition.NoOfCellsEast Then
        InSameCell = True
    End If
    CheckIfSameCell = InSameCell
End Function

I can't understand why the InSameCell is variable is created, when it can just be assigned to the function name CheckIfSameCell?

Or just use return statements as in the following?

Function CheckIfSameCell(ByVal FirstCellPosition As CellReference, ByVal SecondCellPosition As CellReference) As Boolean
    If FirstCellPosition.NoOfCellsSouth = SecondCellPosition.NoOfCellsSouth And FirstCellPosition.NoOfCellsEast = SecondCellPosition.NoOfCellsEast Then
        Return True
    End If
    Return False
End Function

I can understand not returning the expression in the If statement directly, to increase readability.

I know that assigning a return value to the Function name doesn't exit the function, whereas Return does, but is it just a person's style, or is there any advantage to the first version (IMO, the second is more readable)?

The second method is more readable, I concur. It also happens to be my preference for returning out of methods. I really cannot think of a single downside to the latter in comparision, but can for the former. What happens if the method gets longer and someone forgets to set a Boolean flag? A subtle bug would be born. Additionally, it takes more code to write as well. In the latter approach, the code won't compile if it is missing a return, and it also will be shorter.

The only time you need local variables for the return type is when the routine needs to do some other work after the return value is first determined. In the example you post, this is not the case.

Code Complete, 2nd Edition agrees on page 391:

Use a return when it enhances readability In certain routines, once you know the answer, you want to return it to the calling routine immediately. If the routine is defined in such a way that it doesn’t require any further cleanup once it detects an error, not returning immediately means that you have to write more code.

NOTE: As other answers [1,2] have mentioned, you can reduce the method to a single code statement. Also using AndAlso should help speed up the evaluation by short-circuiting the logical expression early if the first part is false:

Return FirstCellPosition.NoOfCellsSouth = SecondCellPosition.NoOfCellsSouth 
    AndAlso FirstCellPosition.NoOfCellsEast = SecondCellPosition.NoOfCellsEast

I've been reading and learning about Object-Oriented programming (Head First Object-Oriented Analysis and Design and Code Complete: A Practical Handbook of Software Construction – thanks to suggestions found on StackOverflow). I've also been learning how to use a couple PHP MVC frameworks (specifically Codeigniter and Kohana).

Some of the principals of Object-Oriented that I've read about are handled differently by the MVC frameworks. I think I've managed to understand the differences and why the decisions were made (complete and easy to use solution), but I wanted to test my if you'll humour me...please comment or correct.

Assumption #1:

Thinking about the right abstraction for a web application, the directory containing the library of classes should be located outside of the directory containing the presentation files. This organization adheres to the DRY ("Don't Repeat Yourself") principal allowing for multiple presentation folders (,,, etc.) to work with the same objects.

Assumption #2:

If all your classes are located outside of your presentation folders, then the models in your MVC implementation just use instances of those classes. If that's true, then the MVC framework is just a presentation class (the controller) that helps to manage the input (GET & POST requests), the response (models or instances) and output (views or templates).

Assumption #3:

If the MVC framework is just a presentation class, then the database class that the controller instance initializes breaks the abstraction of the controller class. A model (of the controller instance) shouldn't have a ("has a") database, it should have a thing (user, product) from the library of classes and that thing should have a database.

Assumption #4:

Furthermore, if the MVC framework is just a presentation class, the database class that the controller instance initializes is too tightly coupled with the controller class. Changing from one method of storage to another requires re-factoring of all the models.

Assumption #5:

With a HMVC framework, the issues with the controller containing the database is worse, because your models are more module (more models, more re-factoring).


Sorry, I may have used the terms controller and model a little loosely...maybe that speaks to the tight coupling. The source of my confusion stems from the database class syntax.

With a model, shouldn't it be:

$user = new User($id);
$data['name'] = $user->getName();
$data['title'] = $user->getTitle();
return $data

Instead of:

$query = $this->db->get_where('user', array('id' => $id), 1, 0);
$row = $query->row_array(); 
$data['name'] = $row['name'];
$data['title'] = $row['title'];
return $data

The the PHP manual page about the exit construct states:

exit — Output a message and terminate the current script

Based on that, would it be correct to think that it violates the single responsibility principle?

Technically, yes. However the exact violation isn't that bad... The Single Responsibility Principle is actually an abstract principle that can be applied to any unit of code. In Steve McConnell's Code Complete 2, he actually talks about this as cohesion. In practice, this is a more specific form of the single-responsibility-principle used for routines.

The most desirable kind of cohesion according to him is functional cohesion where a routine performs one and only one operation (the examples he shows are sin(), getCustomerName(), eraseFile(), etc). So, exit() does 2 things. Therefore it shows Temporal cohesion (the operations are done in the same routine because they are done at the same time).

Now, the original arguments to exit($arg) was the return status of the applicaiton (See: linux exit status codes). In C, this is the integer value returned from main(). But since PHP doesn't have a native function, it was added in to the exit() parameter. Try it, add exit(2), and the return value of the program will be a status 2 (0 is usually success, see are there any standard linux exit status codes).

However, since PHP has a low barrier to entry, most developers likely won't know about status codes. So, it was made to accept a string. If the argument is a string, the status is echoed out on STDOUT, and then the application terminates. If it's an integer, it'll be returned from the program. So it's an artifact.

Now, is it a problem? Not really. Is it ideal? No. But it's also not horrible, since they are related. In the end, I wouldn't lose sleep over it...

What is the standard way to name a temp variable in the local function? let me give you an illustration of what I am doing. I get a pointer to a structure, so I want store one of its members locally to avoid a de-referenced, and then any modification assign back to the pointer.

To be more concrete:

struct  Foo
  double m_d;


void function (Foo* f)
   double tmp=f->m_d;

       /***Other stuff***/


I don't like tmp. If I have many of them in a function, they only add a confusion.


For your information: Code Complete has a chapter decicated to variable naming.

In your example one problem is that the member variable in Foo is not very descriptive to start with, which makes it hard to find a useful name for the placeholder local variable.

For example, I would do something like this:

struct Foo
  double mValue; // I don't use underscores here
                 // however, you can do as you please.
                 // Also 'mValue' is just for the sake of example,
                 // you should find a more descriptive name :D


void function (Foo* f)
   double oldValue = f->mValue;

       /***Other stuff***/

   f->mValue = oldValue;

Are the naming conventions similar in different languages? If not, what are the differences?

Of course there are some common guidelines but there are also differences due to difference in language syntax\design.

For .NET (C#, VB, etc) I would recommend following resource:


One of the best recommendations I can make is to read the relevant section(s) of Steve McConnell's Code Complete (Amazon Link). He has an excellent discussion on naming techniques.




Aside from language-specific constructs, for someone who is programming in PHP, javascript, java, and python, what are the top 10 clean coding must-do's? I would like to keep my code as clean as possible and as readable as possible. For example, what's the take on placing the function body opening bracket on the same line as the function declaration vs a new line? Also, what's the take on spacing between things for example (x==10) vs ( x == 10 )? Any helpful hints for clean coding will be appreciated!

The answer in my opinion is that there is no real answer. A good rule is to stick to a convention, which usually is a reasonable convention that the community is already familiar with. I would rather suggest you a couple of books to have an idea.

For Java/C++/C#/VisualBasic you should read

Code Complete: a practical handbook of software construction

On the Java-heavy side, you would get useful insight from

Clean Code: a handbook of agile software craftmanship

Consider that the concepts in those two books are of general validity and should not be bound to a specific language.

Can I Use Entity Framework 6 with .net 4.5 for a big project ?

In this project i have many solution in this project and some of them are in a fast communication with database like as Smtp with hug data communication


// edit for more details

i start a big project that it has five section

1- send sms with smtp that create huge request and overload on database for example in secound 1500 record insert and select

2- payment request

3- many other request ...

The default answer, is certainly yes. This is where all ORMs shine. In large systems where writing a data access layer is a big and error prone task.

As Steve McConnell suggests in his great book you should never make speculations about performance. Therefore, if you have specific performance concerns, you should try benchmarking.

If you want my opinion, between clear code and performance, I choose clear code. It will allow you to implement a more robust and maintainable system. Then, if you identify performance problems, you could make the necessary minor changes. This is my default rule.

Hope I helped!

I am a self taught "developer". I use the term loosely because I only know enough to make myself dangerous. I have no theory background, and I only pick up things to get this little tool to work or make that control do what I want.

That said, I am looking for some reading material that explains some of the theory behind application development especially from a business standpoint. Really I need to understand what all of these terms that float around really talk about. Business Logic Layer, UI abstraction level and all that. Anyone got a reading list that they feel helped them understand this stuff? I know how to code stuff up so that it works. It is not pretty mostly because I don't know the elegant way of doing it, and it is not planned out very well (I also don't know how to plan an application).

Any help would be appreciated. I have read a number of books on what I thought was the subject, but they all seem to rehash basic coding and what-not.

This doesn't have to be specific to VB.NET or WPF (or Entity Framework) but anything with those items would be quite helpful.

In addition to some of the others (and after Code Complete), try Domain-Driven Design: Tackling Complexity in the Heart of Software.

I think most people would recommend Code Complete by Steve McConnell as the first book to read on putting some good software together.

I am doing a report on Software Architecture and one of the sections which i need to write about is the issues that arise when using Software Architecture. I have tried to look online, read books etc but i can’t find anything to do with what the problems that will rise when using Software Architecture.

Any help on where i might be able to find anything would be great.

Thanks in advance

Have a read through Code Complete. It covers lots of relevant topics.

I am working on C# web application and i build various application with simple project structure like image is given below.

Solution Explorer

.cs page

.aspx page using master page

Basically i want to know about the best project structure of large web application on C#? or is above project structure(in the image) is right way to build an web application ?

Now i want to improve my project structure. So where should i start improve myself to build an lage web application.

I don't think there is really a correct way to structure a project. It depends on what the goals of the project our and your plans for implementation. Each project is different and have will different requirements.

I would go with the most logical approach for your particular project. Whether that's grouping similar components together or grouping whole sub-systems together.

You should carefully analyze your requirements up front before you even begin development and plan your structure accordingly. Having a good plan before you begin coding will save you a bunch of hassle down-stream if things aren't working out and you need to restructure.

I would suggest reading a book like Code Complete, it'll give you excellent tips on how to plan and structure your projects. You can find it here: Code Complete

I have a java ee app, there is a jax-ws client in the app, At the beginnig of each request to my app I use that jax-ws client, If I implement the jax-ws client as a singleton, can i improve the performance ?

As Steve McConell suggests you should not make architectural decisions based on slight performance improvement speculations. Instantiating an object once, comparing with instatiating it over and over again would of course lead to performance improvement.

Nevertheless, I suppose that if you don't instantiate thousands of jax-ws clients per second, your performance would not improve in a way that would justify the architectural change. Anyway, you should measure the performance overhead of the jax-ws instatiation and then decide if it's worth the trouble.

Hope I helped!

I have a sql server 2012 database which is the backend to an MVC application, storing customer and order information. This database is accessed under high load and high usage.

I know have a requirement to be able to generate ad hoc reports from the database accessing the same data as the MVC application works with. I am concerned what impact this would have on the database server and the database itself, around locking etc. As such their is a distinction between the data, for the app its operational, but for the reports its more data warehouse oriented.

Therefore I am looking at my options as to the best approach to avoid such.

I am considering creating another database on a different server and archive the data to it using a sql job at regular intervals during the day. Only concern around this is that it would require maintenance and also a dependency to ensure any necessary changes are made to the target database when the source database changes.

What other options opened to me in such a situation and what advice could be given regarding such? What is the best approach to such?

As Steve McConell suggests you should make no assumptions about performance. You should just measure it before making any decisions. It is not a wise choice to make design choices without knowing the actual performance overhead. So I would suggest to measure, or simulate the performance overhead before even consider using a complex architecture, because you would not know if it's worth the trouble.

Anyway, I think that your approach is right. I would create a windows service which periodically retrieves the data I need from my database and stores them in my warehouse (the new database). I don't think you would ever find a tool keeping consistency between the two schemas, unless you want one schema to be an exact copy of the other.

I don't know your exact needs and perhaps my suggestion is an overkill but I would encourage you to consider using an OLAP approach in the data warehouse where your reporting data will come from. I have to warn you that these systems are oriented in really big data and advanced reporting needs but perhaps you can take some ideas from them. Since you are familiar with the Microsoft ecosystem, I would suggest using Business Intelligence Studio. You could there build an OLAP cube using your normal database as data source and integrate advanced reporting.

Hope I helped.

I have been learning the various libraries of Java like IO, Collections and Swing. Now, if tomorrow, Java goes the way of Fortran and Cobol (which were in widespread use at one point of time) will all of my effort have been in vain? Does learning one API help in learning another language's API? Or is all the effort put into specializing in one technology akin to putting all of ones's eggs in one basket? I am having these doubts because I am halfway through learning the important Java libraries, and wonder if all the effort is worth it.

According to TIOBE community index, Java is still rather popular :) If you learn Java, you will get an understanding of many concepts of OOP, and will get familiar with modern approaches to business software creation.

Generally, language is just an instrument (you may take a look at McConnell's Code Complete - there's a lot of elaboration about that). There's nothing wrong with "putting efforts into specializing in one technology" - you, as professional programmer, just have to do it all the time :)

I have to develop an application which fulfills following demands:

  1. It has to be developed in .NET (C#/Visual Studio/Standalone windows application).

  2. The first important requirement is the visualization and manipulation of graph data. Here I will use "ZedGraph" as I can tell for the moment.

  3. The second requirement is a database connection. (SQL)

  4. The database has to be accessible via the companies intranet.

  5. Further some algorithmic functions have to be implemented in order to preprocess the given Graph-data before saving it to the database.

Certainly those are not all the requirements in detail but they briefly describe the main goals of the application.

Basicaly I could develop all the functions separately, drag & drop a GUI in Visual Studio's designer's, assign functionalities to the according GUI-elements etc. But this somehow wouldn't feel right. Especially for the following reasons:

  • I want to keep the application as extensible as possible.

  • It also should be maintainable

  • I could thing of many more reasons but that are the two which first come to my mind...

So my question is whether somebody can provide me with references for software architecture which would fit to this kind of project. Especially books are important, as the application is a part of my thesis, so every method and decision has to be justified with a reference.

Thanks in advance, BC++

I would definately go for Code Complete, it is not related to any programming language in particular, instead it discusses good and practizes, totally recommended

I'll try and ask my question so it doesn't end as a simple argumentative thread.

I've jumped into an application coded in C# recently and I'm discovering the exception mechanism. And I've had a few bad experiences with them such as the following

// _sValue is a string
    return float.Parse(_sValue);
    return 0;

I changed it into :

float l_fParsedValue = 0.0f;
if (float.TryParse(_sValue, out l_fParsedValue))
    return l_fParsedValue;
    return 0;

Result, my Output in Visual Studio is not anymore flooded with message like

First chance System.FormatException blabla

when string like '-' arrive in the snippet. I think it's cleaner to use the second snippet.

Going a step further, I've often seen that exceptions are too often used ilke: "I do whatever I want in this try-catch, if anything goes wrong, catch.".

Now in order to not get stuck with bad misconceptions, I would like you guys to help me clearly define how/when to use those exceptions and when to stick with old school "if...else".

Thanks in advance for your help!

Programs that use exceptions as part of their normal processing suffer from all the readability and maintainability problems of classic spaghetti code.

— Andy Hunt and Dave Thomas

I think there is no simple right answer about how/when to use exceptions. It depends on an architecture of the application you're working on and other factors.

I can suggest you to read the chapters 8.3. Error-Handling Techniques and 8.4. Exceptions of the Code Complete book.

I'm currently learning Javascript and trying out a some code. I have put together a Fiddle that makes a number animate from 0 to a 1000. It works fine, but I had to add some extra lines of code I sourced from other scripts I have seen to get it exactly how I want.

There are two fiddles I have setup:

With the variable - Runs nice and smooth an animates when the browser is scrolling or still.

Without the variable - The numbers animate only when the browser is still, and pauses when its scrolling.

The variables I add to get the desired result are:

if (!run && isScrolledIntoView('#visible')) {
var run = false;

Can someone explain to me why I have to have these to get it to run with the browser scrolling? I have tried to look around the web to find out what they do; but to no avail. I would rather understand exactly what it does than just code-and-hope, even if it does work.

This may seem obvious, but I'm trying to learn. Thanks in advanced.

It's simple. With the variable run in is place the code execution enters the if (!run && isScrolledIntoView('#visible')) block only once when you scroll first.

var run = false;

Evaluates to TRUE in the if block, so the code execution gets inside. Than it changes the variable to 'true', which in the if block evaluates to FALSE so that code execution doesn't get inside the block; Add alert('hello'); inside the if block and you'll see for yourself.

As a side note: One could've named the variable alreadyStarted instead of ambiguos run, which would save you from trouble figuring out what it does. Try to write obvious code yourself :). This is an excellent reading for the excellent practices Code complete

I'm holding database values in a variant. Which of the following typedefs makes most sense in your opinion:

typedef boost::variant<int, bool, std::string> Value;
typedef boost::variant<int, bool, std::string> Values;
typedef boost::variant<int, bool, std::string> Type;
typedef boost::variant<int, bool, std::string> Types;

None of them have meaning. What means Values or Types? Just abstract names. Better name them with more concrete for example as ObjectState or PacketField. Such names as Type means nothing, when you name your variable think that other people could read and understand what it means and when you forget what did you do you must remember what it is and dont read bunch of code to understand what this type for.

For more information about variables naming and more about software engineering you can buy wonderful book Code complete written by Steve McConnell.

I'm trying to learn how to make my application has the same "face" in all files, the same pattern, but I do not know if this is the right thing to do.

I want know if the more correct in software development, is to limit the application to only one type of pattern.

For example, that's how my application looks like (just a piece)


'use strict'
var Server = require('./server');
var Api = {
 init: function() {


'use strict'
var Server = {
 init: function(command) {
  this.command = command;

  if (command === 'start') {

I'm using the "initializer object pattern" in all my application. So i'm avoiding decorator pattern, facade, everything.

Should i avoid or i can use more than one? If the right answer is use several, depending on the needs, i have other question. This doesn't make the maintenance more difficult? Is not make the code like a spaghetti?



I'm going to try explain again, this time with a bounty.

i already try to ask but seems that no one really can give me a concise answer.

I would like to know what are the secrets from making a good app, a good web app, talking about code appearance. I want know if I should maintain the same standard code throughout my hole application, if this is GOOD, or really doesn't matter.

For example, i have two EXAMPLES files


var server = require('./server');

var app = {
 init: function() {

module.exports = app;


var server = {
 init: function(env) {
  if (env === 'DEVELOPMENT') {
    // CODE
  } else {
    // CODE

module.exports = server;

In this case, i'm using one object with method init, which is the pattern that i'm using in my hole app..

Or it doesn't matter, i should write anything:

first object:

var server = require('./server');

var app = {
 init: function() {

module.exports = app;

than server as a function:

var server =function(env) {
  if (env === 'DEVELOPMENT') {
    // CODE
  } else {
    // CODE

module.exports = server;

I can give 100 of my points. it's incredible how i can't find a good answer for this particular issue.


Should I using other patterns?

No, you should not insist on a single pattern.

No design pattern books will ever advise you to use a single pattern. Just like you cannot chop all ingredients in one single way (are you going to dice the spaghetti?), you cannot organise all logic in one single pattern.

Sure, you can make all your Objects use the initialiser pattern, and don't use constructors at all. This is ok. Been there, done that. I like it.

But these objects can be used with Builder or Abstract Factory (if it make things simpler). As long as the builders/factories themselves have initialiser, and that they properly initialise the created objects, then your use of the initialiser pattern will be consistent. Outside of creational patterns, it is usually good to organise objects with structural and behavioural patterns. They do not conflict with initialiser at all.

For example, look at DOM. All nodes are created by the Document object - elements, text nodes, comments, even events. This is the Factory pattern.

Yet the Document object is also a Facade! From it you access the whole system's status, objects, data, you can even write to it! Every DOM operation starts from the Document, it is the Facade of the DOM system.

DOM nodes also implements multiple patterns. They are organised in Composite, let you listen to events with Observer and Command, and handle events in a Chain of Responsibility. They are certainly parsed by an Interpreter, DocumentFragment is a Proxy, svg elements are implemented as Decorators, and createNodeIterator obviously gives you an Iterator.

The point is, good object-oriented design will yield multiple design patterns as a result, intentional or not.

What are the secrets for good code appearance

I think the best looking code is the one that is easiest to understand to you, and the way you read code changes as you gain more experience.

For example my style is too condensed for most programmers, but to me it strikes a good balance. So do develop your own style - you are not me, and you are not yesterday's you either.

Remember this as we go through the styles.

At the lowest level we have coding style - most importantly indent and bracket.

This one is simple, pick the one you like and stick with it. There are language specific styles, and they are often good starting points. Configure your IDE's formatter so that you can format all your code with hotkey.

Above the code syntax we have comment style and naming convention.

Setting rules on comment is fine, sometimes it is necessary for documenting tools. Avoid too much comment in practice. You may also want to decide your namespace and your stand on naming function expressions.

Above these structures, we have logic conventions.

The same code logic can often be done in many ways, some more 'beautiful' than the others in your eyes. Look at this example.

I picked the second style on first sight: no duplicate, logic is sectioned cleanly, format is not my style but reasonable. But many programmers would prefer the first style: logic is plain as day, a few duplications is worth it. While abstract, this level is quite deep - present your logic the wrong way actually increase the chance an experienced programmer read it wrong.

Finally, we arrives at the level of design pattern, about as far as code beauty goes.

The key to keep your code structure beautiful, is using the right patterns at right level to consistently accomplish loose coupling and code reuse, while avoiding pitfalls and over-design.

There are quite some books about beautiful code, and then there are even more books about designing and implementing beautiful software. (Decide for yourself which are beyond your level.) Knowledge is as important as experience, and you can gain them only by spending your time to study, to write, and to revise/refactor your apps.

Feel free to change your mind as you explore and experiment with your code. Changing your mind is a good sign of learning.

But first, familiarise yourself with design patterns. Just don't forget, they are the generic result of applying object-oriented principals to common tasks. It is still up to you to do the design.

Design Patterns Are Not Silver Bullets.

I often find my self writing these pieces of code, specifically when I have to do something in a 2D array.

The loops are the same, except the operations inside are different and, most importantly, the operation in the last group depends on the first.

My main concern is: is there a more efficient code for large values of n,m?

for ( int y = 0 ; y < m ; ++y ) {
  for ( int x = 0 ; x < n ; ++x ) {
    if ( v[x][y] == z ) a = true;

for ( int y = 0 ; y < m ; ++y ) {
  for ( int x = 0 ; x < n ; ++x ) {
    if ( a == true ) do_something( v[x][y] );

Thanks in advance

As per Code Complete: It depends on your programming language. What might give you a performance gain in programming language A might actually hurt performance in another language.

There are some techniques described in Code Complete such as Loop unrolling which could be a performance gain (for the first loop or if you could inline do_something).

Perhaps you can also escape/exit the loop(s) once a certain condition is true? For example once a=true, exit the first loop. (and as Findus already pointed out, only perform the second loop if a was set)

I want to know about JDBC Connection use with respect to performance.

E.g I want to fetch 5 Different Objects from Database having 5 Different Select Queries to fetch.

I have to options to achieve this.You can also tell another ways.

First One which is Currently I am Using

void fetchSeventObjectMethod(long id1,long id2,.....,long id5) {

    1. Open JDBC Connection

    2. PrepareStatment 1 
       2.1   Object 1 Fetch Query
       2.1   Bind Object 1 from ResultSet 1
       2.1   **Close** PrepareStatment 1 and ResultSet 1

    3. PrepareStatment 2 
       2.1   Object 2 Fetch Query
       2.1   Bind Object 2 from ResultSet 2
       2.1   Close PrepareStatment 2 and ResultSet 2

    4. PrepareStatment 3 
       2.1   Object 3 Fetch Query
       2.1   Bind Object 3 from ResultSet 3
       2.1   Close PrepareStatment 3 and ResultSet 3

    5. PrepareStatment 4 
       2.1   Object 4 Fetch Query
       2.1   Bind Object 4 from ResultSet 4
       2.1   Close PrepareStatment 4 and ResultSet 4

    6. PrepareStatment 5 
       2.1   Object 5 Fetch Query
       2.1   Bind Object 5 from ResultSet 5
       2.1   Close PrepareStatment 5 and ResultSet 5

    7. Close JDBC Connection in FINAL BLOCK


And Another is

 void fetchSeventObjectMethod(long id1,long id2,.....,long id5) {    
    call fetchObjectOneMethod(id1); 
    call fetchObjectTwoMethod(id2); 
    call fetchObjectTHreeMethod(id3); 
    call fetchObjectFourMethod(id4); 
    call fetchObjectFiveMethod(id5); 

        void fetchObjectOneMethod(long id1) {    
            1. Open JDBC Connection      
            2. PrepareStatment 1 
               2.1   Object 1 Fetch Query
               2.1   Bind Object 1 from ResultSet 1
               2.1   **Close** PrepareStatment 1 and ResultSet                          
            3. Close JDBC Connection in FINAL BLOCK           

       void fetchObjectTwoMethod(long id2) {              
            1. Open JDBC Connection            
            2. PrepareStatment 1 
               2.1   Object 1 Fetch Query
               2.1   Bind Object 1 from ResultSet 1
               2.1   **Close** PrepareStatment 1 and ResultSet 1                         
            3. Close JDBC Connection in FINAL BLOCK            

       void fetchObjectTHreeMethod(long id3) {             
            1. Open JDBC Connection           
            2. PrepareStatment 1 
               2.1   Object 1 Fetch Query
               2.1   Bind Object 1 from ResultSet 1
               2.1   **Close** PrepareStatment 1 and ResultSet 1                        
            3. Close JDBC Connection in FINAL BLOCK           

Here scenario is void fetchSeventObjectMethod(....) is called by several threads at time upto thousands or more.

in case of first one Just Single Connection is Used in case of Second Five Connections are Used

Which is better option in performance consideration or is there is alternative way. Here I am using c3p0 connection pooling also. Please help me.

Thank you

As Steve Mc Conell says, regarding performance, no assumptions are safe. You have to measure things before making a change, in order to decide if the code change, the code shuffling etc worth the trouble.

In your case though, I suppose that the choice is relatively easy. You should open the connection once and execute multiple queries for each connection. This way, you pay the connection overhead only once. This is the reason why JDBC offers you a way to handle the connections and many connection management schemas have been developed. Applying JDBC connection pooling gives you the option to use multiple connections for multiple queries, without the open/close overhead.

Hope I helped!

I work on embedded systems with limited memory and throughput. Making a system more robust requires more memory, and time on the processor. I like the idea of the Chaos Monkey to figure out if your system is fault tolerant, however with limited resources I'm not sure how feasible it is to just keep adding code to it. Are there certain design considerations whether in the architecture or otherwise that would improve the fault handling capabilities, without necessarily adding "more code" to a system?

One way I have seen to help in preventing writing an if then statement in c (or c++) that assigns rather then compares a static value, recommends writing that on the left hand side of the comparison, this way if you try to assign your variable to say the number 5, things will complain and you're likely to find any issues right away.

Are there Architectural or Design decisions that can be made early on that prevent possible redundancy/reliability issues in a similar way?

Yes many other techniques can be used. You'd do well to purchase and read "Code Complete".

Code Complete on Amazon

Our company, being rather small, doesn't have much in the way of policies and procedures for good development. When I first started, I developed some, but we now need to update these in a more formal manner. Since I don't have much experience in writing policies (I've written plenty of instruction sets, so I'm not as worried about the procedures, though still thoughtful), I want to ask the community for tips and advice on writing good policies and procedures for software development. Thanks.

Some of policies depend of your programming languages, but :

  1. Write naming rules to uniforme the code of your team
  2. Write general coding rules like KISS principle
  3. For customers and bugs management, take a look at ITIL
  4. For delay and tasks management, I recommand Scrum method
  5. Write a chapter about design patterns (design pattern head first)
  6. If you want more formal procedures, have a look at ISO 90003

Some other books can help you like Code complete

SO if I wanted to code the method Removelast(), would this work? this is for the linkedListDS Can I have a if without a else?

public E removeLast() {
    E result;          
        System.out.println("No data present");              
         return null;          //over here

     result =;          
      if(this.size() == 1)          
           head= null;               
            tail= null;          
            Node<E> current;               
            current = head;                               
            while( != tail)                   

          tail = current;               

         return result;      

Using a if without else is correct. Note that there are different styles of programming out there. You might want to check out this classic book: Code Complete: A Practical Handbook of Software Construction, Second Edition. I for myself use if only to partition the program flow that I dont have many top-level stand alone ifs (instead use 'else if').

You also have a problem in your code. If your list is empty (size() == 0) you try to walk the nodes, which should give you a null pointer exception.

Also if you have a linked list where each node stores a link to the previous node, you can remove tail by doing:

`Node<E> toRemove = tail;
tail = toRemove.previous;
if(tail == null) 
   head = null;
toRemove.previous = null;`

We're in the process of importing our documentation library to SharePoint, and I'm using a java program I wrote to build metadata for these documents. One of the things that I need to do is determine if a document has a cross referenced document. This condition is defined as having the phrase "see " in the document name. However, naming conventions are nonexistent, and all of the following variations exist:


I have created a variable which defaults as such: String xref = "no cross reference"; I would like to set this String to "see_other_document" in cases where there is a see <other document> substring in the filename.

My plan is to look for an instance of see_, use that as the start point of a substring, ending with the ., non-inclusive. But I want to ELIMINATE any special characters that may exist. In my cases above, I would like to return five instances of other_document, not other_document), etc.

My thought was to pull the substring into a variable, then use a regex [^a-zA-Z0-9] and replace non-alphanumeric characters in that second string variable, but is there a better, more elegant way to skin this cat?


if (filename.indexOf("see_">-1) {
    String tempFilename = fileName.substring(indexOf("see_")+4,indexOf("."-1));
    xref = tempFilename.replaceAll("[^a-zA-Z0-9]","");
    } else {

As Steve McConnell suggests writing something in one line is not more elegant. I believe that your way of doing things is the most elegant one.

Let's suppose that you find a magical way of using a complex regular expression doing all these things in one line.

Would the code be more readable then? Certainly not. Using complex regular expressions is far from easy reading. Nobody would understand what you want to do by reading a regular expression.

Would the code be more maintainable? Certainly not. Changing a regular expression to do a slightly different match could be a very tedious task. The same with debugging.

Would the code be faster? Maybe yes, maybe no. You would have to test it. Nevertheless, the performance difference is not your goal.

Therefore, I suppose your code is elegant enough and I would not suggerst to change it.

Hope I helped!

What are the points I must remember during the planning phase of the project to have a really firm foundation?


Edit: I mean more specifically related to coding. (I don't mean the budgets etc etc).

For example: Where can we use generics,reflection or concepts in C#

Check out Steve McConnell's book on Software Estimation. It will help you consider all area's before getting started. For if you have to estimate it then you should know what has to be done.

You should also consider reading Code Complete.

Software Estimations, Code Complete

I truly hope this question doesn't get deleted since I really do need help from the programming pros out there...

I've been programming for quite a while (my main platform being .Net), and am about 85% self-taught - Usually from the school of hard-knocks. For example, I had to learn the hard way why it was better to put your configuration parameters in an .ini file (that was easier for my users to change) rather than hard-code them into my main app and now to use the app.config rather than even a config file sometimes. Or what a unit-test was and why it proved to be so important. Or why smaller routines / bits of code are better 90% of the time than one, long worker process...

Basically, because I was thrown into the programming arena without even being shown .Net, I taught myself the things I needed to know, but believe I missed MANY of the fundamentals that any beginner would probably be taught in an intro class.

I still have A LOT more to learn and was hoping to be pointed in the right direction towards a book / web-resource / course / set of videos to really teach the best programming practices / how to set things up in the best way possible (in my case, specifically, for .Net). I know this may seem subjective and every programmer will do things differently, but if there are any good books / resources out there that have helped you learn the important fundamentals / tricks (for example, self-documenting code / design patterns / ) that most good programmers should know, please share!!!

My main platform is .Net.


I've found some code to convert an HTML to a JPG file, however with elaborated HTML I get the invalid Base64 error, while if I pass a very basic HTML line I get a strange error "invalid parameter" while creating the image FromStream. Here is the code that I have found:

public static void saveURLToImage(string url, string file_name)
    if (!string.IsNullOrEmpty(url))
        string content = "";

        System.Net.WebRequest webRequest = WebRequest.Create(url);
        System.Net.WebResponse webResponse = webRequest.GetResponse();
        System.IO.StreamReader sr = new StreamReader(webResponse.GetResponseStream(), System.Text.Encoding.GetEncoding("UTF-8"));
        content = sr.ReadToEnd();
        //following line will trigger "invalid Base64" error
        byte[] b = Encoding.Unicode.GetBytes(content);// Convert.FromBase64String(content);
        System.IO.MemoryStream ms = new System.IO.MemoryStream(b);
        //following line will trigger "invalid parameter" error
        System.Drawing.Image img = System.Drawing.Image.FromStream(ms);
        img.Save(file_name, System.Drawing.Imaging.ImageFormat.Jpeg);


In general...

Firstly a Base64 string and next a .NET Image, has to meet some criteria in order to be deemed as valid.

For Base64:

  • The base-64 digits in ascending order from zero are the uppercase characters "A" to "Z", lowercase characters "a" to "z", numerals "0" to "9", and the symbols "+" and "/".
  • The white-space characters, and their Unicode names and hexadecimal code points, are tab (CHARACTER TABULATION, U+0009), newline (LINE FEED, U+000A), carriage return (CARRIAGE RETURN, U+000D), and blank (SPACE, U+0020).
  • An arbitrary number of white-space characters can appear in s because all white-space characters are ignored.
  • The valueless character, "=", is used for trailing padding. The end of s can consist of zero, one, or two padding characters.

For .NET Image, all we have from the MSDN documentation is that an ArgumentException is thrown in the FromStream() constructor if

The stream does not have a valid image format

In your case...

For the least I can say that for your first error, if your string does not fit end-to-end and if there is no padding, the code will throw.

If written properly, which I am sure the GetBytes(string s) is, it will perform null and length checks before attempting a conversion. Your stream is probably not null, but the length of your stream is most likely an issue. There are two ways to err:

  1. The length of s, ignoring white-space characters, is not zero or a multiple of 4.
  2. The format of s is invalid. s contains a non-base-64 character, more than two padding characters, or a non-white space-character among the padding characters.

Have you ensured that the stream you are reading has a length (excluding white spaces) equal to a multiple of four AND, if indeed the stream length is a multiple of four, then is the padding proper? The way you are reading the stream and passing it for conversion, you cannot be sure that you are providing a valid string.

Just like the string issue explained above, there is a specific way to define a valid image. I'm not informed enough to elaborate about a valid Image. All I know currently is that an invalid image does not parse.

Therefore, you must try to pass a valid image only. The code you have above is the code for reading a web response and not necessarily a valid image. This is why in some cases when you are able to get through the string conversion at first 'error-point', you still fail at the next one while trying to initialize an invalid image.

From another perspective...

At first level, your faith, that a web response would be providing you a valid Base64 string, is naïve. As the most basic lesson of good programming, validate inputs from external sources.

On the next level, your faith, that the bytes created from a web response would convert into a valid image, no matter how random, is also misplaced.

On a side note, read through the Software Engineering gem, Steve McConnell's Code Complete 2.

I want to study some approaches on realizing a project, designing an application, and so on. I'm not referring to Design Patterns as much as i'm referring at some design styles. For example MVC. So, in that order, I need some links, book names or other suggestions to study on this topic. If you have some thoughts, please help me. Thanks.

I would start by reading upon Domain Driven Design. Eric Evans Tackling Complexity in the Heart of Software is a must-read on this topic. I can then recommend reading Jimmy Nilssons Applying Domain Driven Design and Patterns. This book has examples in .NET (C#) but you should be able to apply it to your language of choice.

Code Complete by Steve McConnell is also a good read if you want to learn how to write clean, maintainable code.

If you like Head Firsts books, i can also recommend reading Object-Oriented Analysis & Design.

For the record, MVC is a design pattern.

I would like to implement design for the real time software problems and need suggestions on my implementation ? IS there any website that can help me on this?

In software development, the steps are "SADCITM".

  • S for Specification
  • A for Analysis
  • D for Designing

Designing is the third step. If you have good knowledge of Algorithms and Data Structures, then from properly written Specifications and properly done Analysis a good Design should come out naturally.

Note that until you have designed the software, you write no code at all!

For completeness, the next steps are:

  • C for Coding
  • I for Integration
  • T for Testing, and
  • M for Maintenance.

A good way to arrive at a bad design is to not analyze what you want to make or worse, not having a specific target in mind in the first place.

Credits: Code Complete 2 by Steve McConnell

I am Just creating a 3 Tier WinForm Application with following pattern.


public class Domain

    public string CommandName = string.Empty;
    public List<Object> Parameters = new List<Object>();

    public void Save()   
        List<Object> Params = this.SaveEntity();
        this.ExecuteNonQuery(CommandName, Params.ToArray());

    public void Delete() 
        List<Object> Params = this.DeleteEntity();
        this.ExecuteNonQuery(CommandName, Params.ToArray());

    public void Update() 
        List<Object> Params = this.UpdateEntity();
        this.ExecuteNonQuery(CommandName, Params.ToArray());

    protected virtual List<Object> SaveEntity()
        return null;
    protected virtual List<Object> UpdateEntity()
        return null;
    protected virtual List<Object> DeleteEntity()
        return null;

    public int ExecuteNonQuery(string SqlText, params object[] Params)
         * Code block for executing Sql
        return 0;

My Business Layer Class which is going to inherit DLL Class

-- MY Children CLASS : BLL CLASS

public class Person : Domain
    public string name
    public string number

    protected override List<object> SaveEntity()
        return this.Parameters;

-- USE This is way to use my Base Class

void Main()
    Person p = new Person(); = "Vijay";
    p.number = "23";


  • Is this the right architecture I am following and Is there any chance to create the base class as Singleton?
  • Is there any other batter architecture?
  • Is there any pattern I can follow to extend my functionality?

Kindly suggest.

Is this the right architecture I am following

There is no architecture which is optimal for any problem without context. That said, there are things that you can do to make your life more difficult. Singleton is not your problem in your implementation.

Is there any other batter architecture?

Probably, yes. Just glimpsing at the code, I see quite a lot of stuff that is going to hurt you in the near and not so near future.

First, a piece of advice: get the basics right, don't run before you can walk. This may be the cause for the downvotes.

Some random issues:

  • You are talking about 3-Tier architecture, but there are technically no tiers there, not even layers. Person doesn't look like business logic to me: if I understood correctly, it also must supply the string for the commands to execute, so it has to know SQL.
  • Empty virtual methods should be abstract. If you want to be able to execute arbitrary SQL move this outside the class
  • As @Anand pointed out, there are no methods to query
  • CommandName and Parameters are exposed as fields instead of properties
  • CommandName is not a Name, Domain doesn't look like a fitting name for that class
  • It looks like an awkward solution to a well-known problem (ORM). You say that you want to be able to execute custom SQL but any decent ORM should be able to let you do that.

Suggested reads: Code Complete for the basic stuff and Architecting Applications for the Enterprise for some clarity on the architectural patterns you could need.

I am trying to find a book which contains information and best practices in software development and design. More specifically I need a book which talks about how each layer talks to other layers, for example how the logic layer of the server talks to the data access layer and vise versa, or what information should the client application send to the server and how to do this.

Mainly I would like it to be for Java development but it doesn't really matter.

Code Complete

written by Steve McConnell. I think it will be helpful.

Realated tags