Should My Method Do This? Should My Class?

Whose Job Is It? I wanted to share my experience that I had working on a recent project. If you've been programming for a while, you've definitely heard of the single responsibility principle. If you're new to programming, maybe this is news. The principle states: That every class should have responsibility over a single part of the functionality provided by the software, and that responsibility should be entirely encapsulated by the class You could extend this concept to apply to not only classes, but methods as well. Should you have that one method that is entirely responsible for creating a database connection, connecting to a web service, downloading data, updating the database, uploading some data, and then doing some user interface rendering? What would you even call that?! The idea is really this: break down your code into separate pieces of functionality.…

0 Comments

Refactoring For Interfaces: An Adventure From The Trenches

Refactoring: Some Background If you're a seasoned programmer you know all about refactoring. If you're relatively new to programming, you probably have heard of refactoring but don't have that much experience actually doing it. After all, it's easier to just rewrite things from scratch instead of trying to make a huge design change part way through, right? In any mature software project, it's often the case where you'll get to a point where your code base in its current state cannot properly sustain large changes going forward. It's not really anyone's fault--it's totally natural. It's impossible to plan absolutely everything that comes up, so it's probable that at some point at least part of your software project will face refactoring. In my real life example, I was tasked with refactoring a software project that has a single owner. I'm close…

1 Comment

Code Smells – Issue Number 3

Code Smells Welcome to the third edition of Code Smells! Periodically I’ll be posting about how to detect code smells and what they mean in terms of the big picture of your code. The previous installment can be found right here. What’s a code smell? Wikipedia says it perfectly: In computer programming, code smell is any symptom in the source code of a program that possibly indicates a deeper problem. Code smells are usually not bugs—they are not technically incorrect and don’t currently prevent the program from functioning. Instead, they indicate weaknesses in design that may be slowing down development or increasing the risk of bugs or failures in the future. These code smells are often based on my own opinion and experience with programming. If you disagree with what I'm saying in my post, please don't hesitate to post a comment.…

0 Comments

Lambdas: An Example in Refactoring Code

Background: Lambdas and Why This Example is Important Based on your experience in C# or other programming languages, you may or may not be familiar with what a lambda is. If the word "Lambda" is new and scary to you, don't worry. Hopefully after reading this you'll have a better idea of how you can use them. My definition of a lambda expression is a function that you can define in local scope to pass as an argument provided it meets the delegate signature. It's probably pretty obvious to you that you can pass in object references and value types into all kinds of functions... But what about passing in a whole function as an argument? And what if you just want to declare a simple anonymous method right when you want to provide it to a function? Lambdas. So…

1 Comment

How and Why to Avoid Excessive Nesting

Background This probably sounds really nit-picky or OCD, but I think it's an issue worth addressing. Excessive nesting of logic within code can make things nightmarish to read. Even a few of years ago I never thought anything of this. I mean, how much could it really affect someone reading it? He/she must be a complete newb to not be able to read my logic. Fast forward to a co-op placement where this was more closely moderated by my managers, and I began to pay more attention to it... Why? Alright, so all that you know so far about my opinion on this is that excessive nesting bothers me. So far, my mission is accomplished. Everything else is just extra. The first issue with excessive nesting is that it actually makes logic hard to follow. If you're doing code reviews…

0 Comments

End of content

No more pages to load