What is the best practices for directory structures in my Visual Studio project? - visual-studio

I have this:
SolutionName: Foo.sln
Assembly:
Foo.Bar
Namespaces are:
Foo.Bar.Views
Foo.Bar.Model
Foo.Bar.BusinessObjects
Foo.Bar.Services
Should the directory structure be like this?
__Foo/Foo.Bar/Foo.Bar.View__ or __Foo/Bar/View__

If you keep the Visual Studio option of "Automatic Namespaces" you would need to have Foo/Bar/Views. Since this is the default behavior of Visual Studio people will be most used to this. Plus it keeps your folder names/paths from getting excessively long.

This is entirely personal preference. I would choose the latter.
Ask yourself the question, "Am I adding any useful information by repeating Foo and Bar in the sub-folders?" The answer here, in my opinion, is no, simply because the information is redundant. You've also created yourself a maintenance problem; if you need to rename Bar you now have to rename Foo.Bar, Foo.Bar.View, Foo.Bar.Model ...

Well, it can be anything you wish. Either are valid, but the former might get a little redundant and lead to directory hierarchies which are a little hard on the eyes/prohibitive.

Foo/Bar/View seems to be natural. Also MSVS tends to map solution folders to namespaces (i.e. if you add Abc folder to your solution each class you add to this folder will be in root_namespace.Abc namespace)

Foo/Bar/View seems a better choice, more if individual files are named as the individual types (e.g. Foo.Bar.View.IView => Foo/Bar/View/IView.cs).
That's a great pattern to follow, which makes it easier to find types, scripting over sources, do some metrics, etc.

Related

howto struct a code? e.g. at the top all variables, then all methods and last all event handlers

i'm currently working on a big projekt and i loose many time searching the right thing in the code. i need to get e.g. a method which makes someting special. so i scroll the whole code.
are there any common and effective methods to struct a file of code? e.g.
1. all global variables
2. constructor etc.
3. all methods
4. all event handlers
do you know common methods to do this??
It's more usual to break large projects into several source files, with logically related functionality. This helps with speeding up compilation and reducing coupling in your design as well as helping you navigate the code.
An example might be to have separate files for
UI functionality
helper classes (such as geometric/maths stuff)
file I/O
core functionality that connects the rest together
Design is a large topic, the book Code Complete by Steve McConnell might be a good starting point for you.
You shouldnt use global variables :)
Try spreading things out over different classes and files. Maks sure each class has only one purpose, instead of 1 class that manages a whole lot of different tasks.
That sounds like a sensible enough structure to me, what would really benefit you though is learning to use the tools you have available — whatever editor you're using it will have a search function, you can use that to quickly find what you're looking for.
Some editors will also include bookmarks too, and most offer a way to move back and forward through recent positions in the file.
Seen this sort of things started, never seen it kept on under the pressure to turn out code though.
Basically my rule of thumb is, if I feel the need to do this, break the code file up.

Is it a best practice to list down all the object files in a C++ makefile and are wildcards acceptable?

I'm a Java developer learning C++. I'm using eclipse as my IDE and MinGW as my toolset. Is it considered a best practice to list down every single object in a makefile? Also, is it just as acceptable to use wildcards to include all the files?
The use of wildcards is common, and accepted, but not really good practice.
If extra source files get into your source directories, they could wind up causing conflicts or -- worse -- riding silently in your libraries as useless baggage (introns?). Also, if a needed source file goes missing, your linker will complain about a missing {function|typename|whatever} and it might not be obvious what file has been lost (not really a problem if you have good source control, but still annoying). Finally, if your build system is expected to produce different targets using different subsets of the source files, wildcards will require you to either divide your source directories Venn-diagram-style, or resort to filename conventions that do the same thing (gah!).
Maintaining explicit lists of object files in a makefile really isn't that hard to do, and it keeps things simple.

Get rid of Hungarian notation in C# code in automated manner?

I have a large codebase that uses Systems Hungarian for most variable names, which basically means I have lots of objQueue's, objCommon's, dtDataSet's et cetera.
Is there any way to convert most of them? Especially, I want to get rid of irritating obj ones that have absolutely no sense, make variable names seem similar and the code completely unreadable.
There was a similar question but it was asking whether it's worth to do the replace.
Answers like “leave it as is” are not of any use to me because I'm definitely sure I need to do this.
The codebase is really large, and I don't want to rename variables one by one. Neither do I want to run Find & Replace because I'll get false hits and mess up code even further.
Is there any automated tool to make such replacements in Visual Studio? Some addin for Resharper, whatever?
Alas, but it seems like there is no bulk rename tool for C#.
Please correct me if I am wrong.
One solution would be to use something like Visual Assist's rename feature.
Other than that, a very careful search'n'replace (with a build between each modification followed by a check-in into source-control).

Visual Studio 2010: Easiest way to duplicate a class?

Is there an easy way to duplicate a class with a different name?
Not sure whether this can be qualified as the easiest way but if you have ReSharper, you can use its Copy Type refactoring to copy classes/interfaces/structs with control over the namespace the copy is landing in and naming within the copy - which means that if you're copying a class with 5 constructors, the copy will have all of them renamed to match the name of the new class.
However, depending on what you're trying to achieve, using Extract Interface or Extract Superclass might be a better option.
No, there's no refactoring tool for duplicating a class but with different name.
I would imagine that the reason why this feature is not present is because duplicating code is generally considered a bad idea. I'd suggest instead changing your class into a base class and then make two derived classes from it, overriding methods where you need to change the behaviour.
Refactor to give a new name, copy, undo, paste :-) Remember the undo! This will rename the constructors and finalizers!
Open file in Visual Studio.
Press Ctrl+A.
Press Ctrl+C.
Create a new file for the new class.
Press Ctrl+V in the new file.
Replace all old class names with the new one. (Ctrl+H)
First of all: now there is the option of "Copy Class" with the MS powertools for VS (marketplace)
Second: to all the critics that know better - there are legitimate cases where you want to copy a class as a starting point. I for example have a base class and from it I derive dozens of classes with different implementations, yet I do have a "reference class" derived from the base which already contains the overides, comments, etc. so I do not have to repeat the same over and over again.
This is just one of the many situation where you want to copy a class.
It's usually better to refactor your class to be reusable. Copy paste code leads to having to fix the same code in multiple places and much pain as you're breaking OO principles.

Codenamed Products and Visual Studio

Are there any practices regarding using codenames of products in Visual Studio projects and solutions? In that typically, namespaces, assembly names, binary outputs, et al need to be renamed once a product name is chosen: is there any way to deal with this?
In one place I worked we'd choose an unrelated and inoffensive, and typically systematic name for any given project.. Jupiter, Orca, Feynman whatever... and stick with that namespace forevermore.
Because as you've seen what the marketing/client/board chooses to call a product (hell, even what version number they choose) is so rarely going to be known at the start let alone the same by the end, that it's just so much easier for everyone to be able to have a fixed internal reference they don't have to refactor later.
I can't stress how important it is that the name is simple, one word and inoffensive though.
This will partly depend on the type of product. If it's a client library - or anything that will require users to see the names - then clearly the names matter. I don't think it really matters otherwise. Bear in mind that different versions of the product may have new codenames (e.g. Everett, Whidbey, Orcas for Visual Studio) - you don't want to have to rename the code each time you start a new version!

Resources