Post navigation

While working with (young) C# programmers I’ve noticed that some mistakes are being repeated by almost every one of them. These are mostly the mistakes, which once you point them, are quite easy to remember. However, if a developer is not aware of them, they can cause many problems with the efficiency and quality of the developed software. Therefore, I decided to gather the 8 most common mistakes made.

1. String concatenation instead of StringBuilder

String concatenation works in such a way that every time when you add something to a string, a new address in the memory is being allocated. The previous string is copied to a new location with the newly added part. This is inefficient. On the other hand we have StringBuilder which keeps the same position in the memory without performing the copy operation. Thanks to the strings’ appending by means of StringBuilder the process is much more efficient, especially in case of hundreds of append operations.

1

2

3

4

5

6

7

//INCORRECT

List values=newList(){“This “,“is “,“Sparta “,“!”};

stringoutputValue=string.Empty;

foreach(varvalue invalues)

{

outputValue+=value;

}

1

2

3

4

5

6

//CORRECT

StringBuilder outputValueBuilder=newStringBuilder();

foreach(varvalue invalues)

{

outputValueBuilder.Append(value);

}

2. LINQ – ‘Where’ with ‘First’ instead of FirstOrDefault

A lot of programmers find a certain set of elements by means of ‘Where’ and then return the first occurrence. This is inappropriate, because the ‘First’ method can be also applied with the ‘Where’ condition. What’s more, it shouldn’t be taken for granted that the value will always be found. If “First” is used when no value is found, an exception will be thrown. Thus, it’s better to use FirstOrDefault instead. When using FirstOrDefault, if no value has been found, the default value for this type will be returned and no exception will be thrown.

1

2

3

//INCORRECT

List numbers=newList(){1,4,5,9,11,15,20,21,25,34,55};

returnnumbers.Where(x=>Fibonacci.IsInFibonacciSequence(x)).First();

1

2

//PARTLY CORRECT

returnnumbers.First(x=>Fibonacci.IsInFibonacciSequence(x));

1

2

//CORRECT

returnnumbers.FirstOrDefault(x=>Fibonacci.IsInFibonacciSequence(x));

3. Casting by means of ‘(T)’ instead of ‘as (T)’ when possibly not castable

It’s common that software developers use simple ‘(T)’ casting, instead of ‘as (T)’. And usually it doesn’t have any negative influence because casted objects are always castable. Yet, if there is even a very slight probability that an object can be under some circumstances not castable, „as (T)” casting should be used. See Difference Between C# Cast Syntax and the AS Operators for more details.

1

2

//INCORRECT

varwoman=(Woman)person;

1

2

//CORRECT

varwoman=person asWoman;

4. Not using mapping for rewriting properties

There are a lot of ready and very powerful C# mappers (e.g. AutoMapper). If a few lines of code are simply connected with rewriting properties, it’s definitely a place for a mapper. Even if some properties aren’t directly copied but some additional logic is performed, using a mapper is still a good choice (mappers enable defining the rules of rewriting properties to a big extend).

5. Incorrect exceptions re-throwing

C# programmers usually forget that when they throw an exception using „throw ex” they loose the stack trace. It is then considerably harder to debug an application and to achieve appropriate log messages. When simply using „throw” no data is lost and the whole exception together with the stack trace can be easily retrieved.

1

2

3

4

5

6

7

8

9

10

//INCORRECT

try

{

//some code that can throw exception […]

}

catch(Exception ex)

{

//some exception logic […]

throwex;

}

1

2

3

4

5

6

7

8

9

10

//CORRECT

try

{

//some code that can throw exception […]

}

catch(Exception ex)

{

//some exception logic […]

throw;

}

6. Not using ‘using’ for objects disposal

Many C# software developers don’t even know that ‘using’ keyword is not only used as a directive for adding namespaces, but also for disposing objects. If you know that a certain object should be disposed after performing some operations, always use the ‘using’ statement to make sure that the object will actually be disposed.

7. Using ‘foreach’ instead of ‘for’ for anything else than collections

Remember that if you want to iterate through anything that is not a collection (so through e.g. an array), using the ‘for’ loop is much more efficient than using the ‘foreach’ loop. See Foreach vs For Performance for more details.

8. Retrieving or saving data to DB in more than 1 call

This is a very common mistake, especially among junior developers and especially when using ORMs like Entity Framework or NHibernate. Every DB call consumes some amount of time and therefore it’s crucial to decrease the amount of DB calls as much as possible. There are many ways to do so:

Using fetching (Eager Loading)

Enclosing DB operations in transactions

In case of a really complex logic, just moving it to the DB by building a stored procedure

The two main contenders as best multilanguage plugins for WordPress are WPML and QTranslate. If you want a quick answer to which one comes out on top, I would say go for WPML. If you have enough time to give them a test drive, by all means do so and judge for yourself which one works best for your needs.

WPML

If you want the most fully featured multilanguage plugin solution for WordPress, your best bet is WPML.

WPML makes it possible to turn WordPress blogs multilingual in a few minutes with no knowledge of PHP or WordPress. Its advanced features allow professional web developers to build full multilingual websites.

Turns a single WordPress site into a multilingual site.

Powerful translation management, allowing teams of translators to work on multilingual sites.

Built-in theme localization without .mo files.

Comments translation allows you to moderate and reply to comments in your own language.

Integrated professional translation (optional feature for folks who need help translating).

Includes CMS navigation elements for drop down menus, breadcrumbs trail and sidebar navigation.

Robust links to posts and pages that never break.

WPML is also offering full integration with Gravity Forms, so you can easily translate all the forms on your site as well.

ICanLocalize, the creators of WPML, offer reliable commercial support for WPML. This support provides timely and dependable help directly from the developers. You can also order translations to be done directly from the plugin interface, via the ICanLocalize service.

QTranslate

An alternative to WPML is QTranslate. It is also a valid multilanguage plugin that works a bit differently to WPML.

qTranslate makes creation of multilingual content as easy as working with a single language. Here are some features:

qTranslate Services – Professional human and automated machine translation with two clicks

One-Click-Switching between the languages – Change the language as easy as switching between Visual and HTML

Comparing WPML and QTranslate

qTranslate stores all languages alternatives for each post in the same post, whereas WPML manages multilingual posts in one post per language. Translations in WPML are then linked together, indicating that one page is the translation of another. Each of this plugin has advantages and disadvantages.

Most Project Managers find it hard to get all their tasks done each day, but you can get more out of your working hours by working smartly. Here’s how to do it…

How to make more time in your day

#1: Use templates
Templates are documents or files that you can use time and time again. Don’t create a new Project Charter for every project, for example. Use a template and simply fill in the relevant bits for this project. You’ll save time because you won’t have to set up titles, headers or footers or any other style elements.

You can also use templates for creating schedules, task lists, reports and anything else that you use on your project. Don’t just limit yourself to documents!

#2: Use time sheets
The key to getting more done is understanding what you currently do with your time. Time sheets will show you what you have actually been working on, so you can assess how your time is being spent. You might, for example, find that you spend too long in meetings each week, or working on a particular element of project management like budgeting. Using timesheet software will help you keep track of where your time is going.

Once you have completed time sheets for a couple of weeks you will be able to see where you are wasting time in the day. You can also decide if there is anything you could delegate to another team member as a learning opportunity.

#3: Take breaks
It might seem counter-intuitive, but it really will help you get more done if you schedule some breaks into your day. Breaks help refresh your brain and your body, which means you come back to your desk feeling more motivated and capable of completing your tasks. Go for a walk, talk to a colleague or simply get a cup of coffee. Anything that gives your eyes a rest from the screen and your mind a change of scene will do.

#4: Use dashboards
Dashboards are visual representations of project progress and they are a great way to see the big picture status of your project. You won’t have to review every element of the project to get a status update as you’ll be able to see it all at a glance.

Dashboards are also great to share with your stakeholders. They will save you time producing bespoke reports month after month as instead, your project sponsor and anyone else who is interested can simply log on and see the status for themselves in real-time.

#5: Store your files online
Project managers and their teams spend a great deal of time looking for important project files. You can save time if you set up an online filing system that will structure your files for you and keep them secure. Then you’ll always know where the latest copy of a document is kept, and you won’t waste time hunting for it when you need it. This will also help your project team save time!

Of course, you don’t need to spend your extra hours on project work! Why not use some of it to study for a professional qualification, or to catch up with a good book, or to just get home earlier and spend more time with your family?

Use the professional time sheet features in ProjectManager.com to track your time and work out where you could be spending it differently.

You can also store documents securely online, create tailored dashboards, store templates and produce reports with just a few clicks—all great ways to get a bit of time back during your working day. Give it a free try now.

Although many organizations that use SharePoint employ internal developers to create custom solutions, every customer organization I’ve encountered in my ten years of SharePoint consulting has used external vendors for at least some development. These vendors include individual contractors, Microsoft Certified Partner firms like non-linear creations, and independent software vendors (ISVs) like Bamboo Solutions or K2.

Whether you use internal employees or third-party vendors, and whether an application is a simple web part or a complex business-process automation, you must not deploy it until you know it can be trusted. A poorly-written application can slow (or bring down) your entire SharePoint farm. It can destroy or corrupt data in your databases, or critical files on your servers. It can also circumvent your security, opening you up to all manner of attacks.

Below are six critical questions that you must ask any SharePoint developer, internal or external.

1. Is this a Sandboxed Solution?

Prior to SharePoint 2010, the only option available to the customer was to deploy every solution as a Farm Solution, where files are deployed to the file system and the code runs in the same execution space as SharePoint itself. If the solution is well written and behaves itself (for example, by NOT hogging all the server memory and crashing the SharePoint site it lives in), this is not a problem. But since the quality of custom SharePoint solutions is fully dependent on the developer/vendor, deploying a Farm Solution calls for a lot of trust on the part of the server administrator/customer

Overcoming the customer’s understandable reluctance to deploy Farm Solutions was the driving force behind the introduction, with the 2010 product release, of Sandboxed Solutions. Sandboxed Solutions are web parts, Lists or other SharePoint features that run isolated from your SharePoint sites. By default, a Sandboxed Solution cannot access or affect anything outside of the local SharePoint site; external databases, web services, and the file system are all off limits. Your server administrators can apply limits and quotas to the Solution, so that if it misbehaves by (for example) hogging server memory, SharePoint 2010 will shut it down.

Your developers/vendor may complain that Sandboxed Solutions are additional work. They may say that they are too neutered and can’t do anything useful. However, for a solution that requires access only to the local sites, lists and libraries, the latter isn’t true. And Sandboxed Solutions can be given additional permissions safely by using something called Full Trust Proxies, which let your vendor’s code request specific rights, while remaining isolated from the SharePoint execution space.

Now that we have Sandboxed Solutions, a SharePoint developer’s first question (and yours) when designing a new feature should always be, “can I write this as a Sandboxed Solution?”.

2. If it’s not Sandboxed, does it run as fully or partially trusted?

Sandboxed Solutions are still quite new, and do require extra work, so they are not yet the industry standard (this will change very soon). And there will always be some solutions that can run only as Farm Solutions. You will still have occasions, then, when it will be acceptable to deploy a Farm Solution. But if your vendor or in-house developer delivers you a Farm Solution, you must know if it partially or fully trusted.With a fully-trusted solution, your developer’s code can access the file system on your SharePoint servers, it can access the Windows Registry — it is pretty much an administrator. From a developer’s viewpoint, this is the fastest and easiest way to develop. But with the availability of Sandboxed Solutions and partially-trusted solutions, there are very few reasons for you to accept a fully-trusted solution.

A partially-trusted solution has no innate rights, and must be explicitly granted permissions (in a special configuration file called a Code Access Security policy) to do anything. The developer must provide this file, which you can inspect.

3. Does it use Elevated Privileges?

If for some reason your vendor must provide you with a fully-trusted solution, you must not deploy it until you understand everything it is going to try to do.

One of the key questions in the vetting process: does it use RunWithElevatedPrivileges? This is a SharePoint feature that is exactly what it sounds like. By default, when your vendor’s code tries to do something like access a SharePoint document, it impersonates the user browsing the page; the code can’t do anything the user can’t do. However, a fully-trusted solution (or a partially-trusted one granted the permission) can use RunWithElevatedPrivileges to temporarily impersonate an account that has Full Control of your SharePoint application. RunWithElevatedPrivileges is a powerful tool that must be used sparingly, because it can circumvent your carefully-planned SharePoint security.

4. Does it use Feature Feature Receivers?

A Farm (non-Sandboxed) Solution can include small bits of code that run when your server administrator installs or uninstalls a custom solution. For example, the solution may depend on the existence of an external database; a Feature Event Receiver can confirm that the database exists, and create it if necessary.

Feature Event Receivers enjoy full administrative privileges on your server farm, so it is very important that you fully understand what they are programmed to do.

5. Where does it store configuration settings?

Many applications need a place to store configuration settings (e.g., database connection strings, or a email address for alerts). If your vendor’s solution uses the main configuration file (web.config) for this purpose, you increase your risk because this file may be shared by hundreds of SharePoint sites on your farm, and the slightest typo can bring them all down.

In general, an experienced SharePoint developer will not provide you a solution that requires manual entries to web.config. Instead, a Sandboxed Solution will keep its settings in a local SharePoint list, and a Farm Solution will do the same or include a Feature (or Feature Event Receiver) that adds settings to the web.config file automagically on installation (and removes them on uninstallation).

6. Where does it log messages?

Finally, an experienced vendor will develop a solution with extensive logging. Even if the code has been developed defensively and tested thoroughly (two other things you need to ask about), even the best software goes wrong occasionally. When that happens in a production environment, you need to know exactly what happened so it can be diagnosed and remedied.

An application can log messages to the Windows event log, a database table, the SharePoint Unified Logging Service, or a SharePoint List. Before you deploy the solution, you need to know which of these places it logs to, what kinds of messages it logs (is logging too verbose? Too sparse?), and whether the logging can be configured (is logging off by default? Are there different modes — Errors Only, Verbose, etc.?). If there is no way for you to get enough information when something goes wrong to at least understand what happened, without involving the vendor, this is a sign of a poorly-designed solution.

These questions are only a sampling of the kind of due diligence required of you as the owner of a SharePoint environment. As in any business relationship, the key ingredient is trust.