You may already be doing one or more of these tips, so this post might not be for everybody. But if you feel like you could improve your code even by a little bit, read on! Hopefully I can help make your code cleaner and easier to read. Also note, I didn't title this "Top 10 Ways..." because there might be different steps for different languages, as well as my own error. I'm still learning, and continue every day, these steps are some of what I have learned so far.
Also, this was meant to be a quick read... for a longer version of this article that goes further in depth and contains code examples, check out my other post on this subject, Writing Clean and Readable Code (C# Examples). (Still under development, posting soon!)
Step 1 : Learn the Language
"Think about cookbooks in the original sense. Although great cooks own cookbooks, no one who relies upon cookbooks can be a great cook. A great cook understands ingredients, preparation methods, and cooking methods and knows how they can be combined to make great meals. All a great cook needs to produce a tasty meal is a fully stocked kitchen. In the same way, a great programmer understands language syntax, application frameworks, algorithms, and software engineering principles and knows how they can be combined to make great programs. Give a great programmer a list of specifications , turn him loose with a fully stocked programming environment, and great things will happen." - Author,V. Anton Spraul (2012) Think Like a Programmer: An Introduction to Creative Problem Solving. Location: No Starch Press
That quote really sums it up. If you are looking to improve your code readability or performance, this is a great place to start. Knowing all the nooks and crannies of a language will allow you to use the language to its full potential. To take this a step further, learn about the different ways to perform a single task, and research the best for each situation. Again, let's think about this in cooking terms. A knife will still cut, but knowing which knife to use at what time can severely affect the end product.
Step 2 : Refactor Your Method At Least Once, Right Away
We've all been there when writing a complex method... You copy and paste code, create variables such as temp1, temp2, etc... and not a single comment is the end result. While the method and logic is still fresh in your mind, take a second before moving on to the unit test, next requirement, etc... and refactor right away. Remember back in highschool or college when you wrote multiple drafts before submitting an original? Yes, the general topic the paper is covering stays the same, but how you explain and structure that paper should increase with quality after each pass. That is how you should approach a complex method in which you just completed.
Step 3 : Look at Every Way To Solve a Problem
For simpler methods, this is not much of an issue. However when approaching larger, more complex methods, or even figuring out the architecture of a future project, one must go through almost every possibility to ensure that you are writing the method or class the best way you can. This step will not only reduce the amount of refactoring you will need to in the future since you have considered a plethora of possibilities, but also reduce the amount of technical debt you create by producing an incorrect or cumbersome solution.
Step 4 : Commenting Your Code
There are two main different comments you should include in your code. The first being in-line comments which serve as a code description for logic or business reason in a method. One way to include them for sure in your end product is to pseudocode your new methods with in-line comments. This will not only increase the readability of the code, but helps you logically set up the method you are about to write.
The second is XML documentation, usually at the top of a class, property, or method. These are important for three different reasons. The first is to describe what a class, property, or method does. This can be quite useful to somebody supporting your code so they don't have to logically walk through the method to find out what it is doing. The second is to create documentation in intellisense when you are referencing a class or property you made. When created using Microsoft's recommended tags, you can allow supporting engineers to quickly understand that method you wrote and what it was intended for vs having to navigate to its definition. The final reason is to be able to generate XML from your XML documentation for various uses.
Step 5 : Measure twice, cut once
"The carpenter’s saying, “Measure twice, cut once” is highly relevant to the construction part of software development, which can account for as much as 65 percent of the total project costs." - Author, Steve McConnell (2004). Code Complete, Second Edition. Location: Microsoft Press
This is in reference to the process before writing the code, the project planning stage. Project planning and gathering accurate business requirements is essential for medium to large projects for clean and easy to follow code. If you try and omit this in your project, you will end up with spaghetti code that will be near impossible to manage later on in the project. A common rule of thumb is specify at least 80% of the business rules up front. If you can stick to this, you can have less surprises, and less spaghetti code.
Step 6 : Generate Programming Standards, and Stick To It!
Another item you and your team can do right off the bat is to generate coding standards for your team to follow. In C#, Microsoft has already started this for you. This will make sure not only you, but your team as well will start outputting cleaner code. Make sure to implement code reviews to find issues before they even hit production environments.
Make sure to post in a general location and to keep updated with new versions of your language's framework.
Step 7 : Recognize Code Smell
For those of you not familiar with what Code Smell is, it's basically when code screams to be refactored. This can be when a method is too long, code is duplicated, excessively nested loops, etc... In the beginning of your programming career or learning a new programming language, you might have made a few of these mistakes and didn't recognize the code smell right away. However, after a few years and beyond these issues should become quite obvious to you. Fix these before you even move on to another method.
Step 8 : Make Methods and Classes as Small as Possible
One way to make your code easy to follow on a project level is the Top Down Design approach. Take your bigger issues and tasks and break down to smaller and smaller components until each method or property covers only a single operation. This will prevent you from creating "god classes" which contains way too much functionality for you or support staff to follow. Creating a very large class or method is common Anti-Pattern.
Step 9 : Using "White Space" To Your Advantage
A pretty obvious step, but one I wanted to make sure I mentioned. The way that we use white space when laying out code changes the way quite a bit of how we perceive a method. Items that are grouped together, for example instantiating local variables, shouldn't have a line space between the code. However, when you are working with local variables, then a foreach / if then else logic, then you want to make sure you have a blank line in between so your brain can quickly observe the different 'parts' of your method. This will help increase productivity of you and the support staff when scanning through a larger class or method.
Step 10 : Explain Your Logic to a Rubber Ducky
The final step is something called Rubber Ducking. Even though usually used when debugging code, I find you can use the same concept when writing new code. When you finish a method, and before you start your first refactoring pass (See Step 2) explain your logic to a rubber duck, or any inanimate object, on your desk. This will have you walk through your own code and catch logic errors you had accidentally scanned over when your first completed the method. This step in conjunction with stepping through executing code greatly decrease the amount of errors, and help you refactor your code to become organized logically.
Again, I hope I helped in some soft of way. If you have other suggestions or comments, please feel free to leave a comment below.
A good friend of mine just released his first game on iOS. Extreme Simon Says. Behind this game is iOS developer Scott Huller , out of California. It's a lot of fun and will have a lot of new updates & features coming soon!
Think that old Simon Says electronic game you used to play, with a twist.
Need a quick way to get the shorthand version (Mmm) of a month in a SSIS expression? Copy and paste the following into a variable and you have precisely just that.
MONTH(GETDATE()) == 1 ? "Jan" : MONTH(GETDATE()) == 2 ? "Feb" : MONTH(GETDATE()) == 3 ? "Mar" : MONTH(GETDATE()) == 4 ? "Apr" : MONTH(GETDATE()) == 5 ? "May" : MONTH(GETDATE()) == 6 ? "Jun" : MONTH(GETDATE()) == 7 ? "Jul" : MONTH(GETDATE()) == 8 ? "Aug" : MONTH(GETDATE()) == 9 ? "Sep" : MONTH(GETDATE()) == 10 ? "Oct" : MONTH(GETDATE()) == 11 ? "Nov" :"Dec"
A quick code snippet to hopefully save you a couple of minutes of typing.
After installing some recent updates to some visual studio extensions and features, I started getting the following error when I was trying to start up Visual Studio 2010 or Visual Studio 2012.
"exception was thrown by the target of an invocation"
After Googling around for a little bit, I found a Stack Overflow article which described the issue. Apparently there is a max length to the Path System variable (I believe is 2048 characters), which if exceeded, will prevent a few different apps from functioning correctly, including Visual Studio 2010 and 2012. However, if you attempt to go to System, then to Advanced System Settings you will get another error because the path is too long.
"windows cannot find ' %windir% system32 systempropertiesadvanced.exe' "
So, to correct this the first time, follow these steps :
- Go to your system32 folder in your Windows Directory (Most likely will be C:\Windows\System32)
- Find and run the executable System was trying to find - systempropertiesadvanced.exe
- You should be on the Advanced tab. If you are not, go there. Click Environment Variables
- Under the System Variables group, find the Path variable. Click Edit
- Copy to notepad, or text editor application. Delete unnecessary entries
- Make Sure the following stays (It's Windows 7 default) - %SystemRoot%\system32;%SystemRoot%;%SystemRoot%\System32\Wbem;%SYSTEMROOT%\System32\WindowsPowerShell\v1.0\;
- Copy back to the Variable Value, hit ok through all the dialogs, and you will be good to go!
* Please note you won't see icons back for folders, etc... until Explorer is restarted. You will be able to open up Visual Studio right away again however.
There have been a few times I've needed to access the event viewer, local users, etc... on a server and the options are not easy to find (Windows Server 2003), but this will work for almost all Windows Operating Systems. What's cool about MMC, you can set up on your local machine and point Snap-ins to other machines (servers, etc...)
Go to Run (or start) and type mmc
Go to File -> Add/Remove Snap-in...
Hit Add to bring up a list of snap-in components
Select the Snap-in, or Folder for organization, and click add
For select components, select either local computer (if you are adding on say, a server) or add the server name if you are adding from local machine to monitor a server
Hit File -> Save As and save to the desired location. There you have it, a custom MMC console!
Dawn is a morning assistant app which will display the meetings for the day, as well as display basic weather and traffic conditions in your area to know how that drive into work will be. You can also set up reminders to launch the application on a scheduled basis in the morning, which will also start a text to speech version of all the information. (Perfect for driving to work and playing over the radio)
Feature Summary :
- Displays the current high. low, and weather conditions in your area
- Displays the amount of time and distance to your work
- Displays all your meetings for the next 24 hours.
- Set up reminders for weekdays, Saturday, and Sunday
- Allows you to use the TTS function anytime
- Add option for male or female voice
- Add a news rss feed
- Minor bug fixes
- Low/High Weather Fix
- Stability improvements
- Lowered refresh time (down from 24hrs)
- Fixed blank meeting titles
GnuPG (GNU Privacy Guard) is a free implementation of the OpenPGP standard as defined by RFC4880, not to be confused with the proprietary PGP (Pretty Good Privacy) version originally created by Phil Zimmermann. When needing a quick, easy, and free solution to encrypt and decrypt OpenPGP standard files in a SSIS package, GnuPG was the way to go.
Note to Advanced Users : All I am pretty much doing is creating a Execute Process Task, pointing to the GnuPG executable and passing some dynamic cmd args. If you are unaware of this process, read on!
Really quick post regarding sp_help and tables in Schemas (not in dbo). Usually when doing a sp_help, you can do something resembling the following :
EXEC sp_help TableName
However, if you try to do the same with a table with a Schema (SomeSchema.TableName) you will get an error "Incorrect syntax near '.'". To execute the sp_help on a table with a schema, simply surround the desired table and schema with single quotes.
EXEC sp_help 'SomeSchema.TableName'
That's all there is to it.
I am not a huge fan of Google's new, new tab page. While it might be easier or quicker to search for people who are new Google's browser, it started becoming a pain for somebody who is used to using many chrome apps and used to searching straight from the address bar. Doing so is quite quick and painless, just involving tweaking a single setting in Chrome. See below for the full steps.
- Type, or copy and paste, into the address bar : chrome://flags/#enable-instant-extended-api
- Select Disabled under Enable Instant Extended API
- Hit the button that appears at the bottom of the page "Relaunch now"
- Hit ctrl + t and enjoy your old new tab functionality back.
**Looks the ability to disable the "Apps" functionality was removed 2/20/2014. Will let you guys know if I find a new way around!
I was in need of a tool to quickly format text for SQL and other various development applications, so I decided to write something light weight and to the point. MondoText, named for large text editing, can remove duplicates, change case, add output delimiters, and even add text qualifiers. Some of the features listed below :
- Remove Duplicates
- Remove Blank Lines, text
- Replace single quotes with double quotes
- Escapes quotes and tick marks
- Encode/decode html markup
- Lower / Upper case
- Add quotes or ticks to text
- Add delimiters to output
- Find & Replace
Also, for more downloads visit my Downloads Page!
When working with command prompt, to change directories you usually do something like the following :
I was using the above method to try and change directories to a different drive (ie c:\ to f:\), and it wasn't working. After googling and reading Microsoft's documentation about CD, I realized I needed to just include the drive. When using CD, you are just changing the directories within the drive which CMD is currently looking in. So, to change the drive, just write the following :
Update : 10/14/2013
Chris in the comments below included a good point I forgot to mention, if you want to change the directory all in one line, you can do so with the following command
cd /d o:\test\test
For example :
To change from c:\ to o:\ (Without the quotes... Syntax highlighter hides the drive path if I don't include them)
That's all you need!
After recently getting a new, larger, secondary drive, I thought it was time to move my TFS folder to the new drive to free up some space. After doing some research on how to get started, I saw articles that were either made it too complicated or articles didn't quite list all the steps. So, I attempted to aggregate the information and give you a quick, simple way to move your bindings to a new location.
Step 1 : Open Up Your Workspaces
Select File -> Source Control -> Workspaces... This will bring up all the local workspaces that you have access to. Select the name of the workspace for which you need to change the location for and hit Edit...
You will then need to double click the binding which you need to change. For example above, I'd double click the C:\TFS\Main under Local Folders, then click the ellipsis which appears. Then select the new location where you want your local repository and that's all there is to it!