clean-code

Refactoring: Do you follow The Boy Scout Rule?

In the book Clean Code: A Handbook of Agile Software Craftsmanship by Robert C Martin he advises that you should follow The Boy Scout Rule when maintaining code. The idea is simply to leave the code a little cleaner than you found it. I.e. it is very similar to the tip about not leaving broken windows from the Pragmatic Programmer book. ...

Rails inline Javascript and Best Practices

I'm kinda new to using Rails, and an app I am working on is progressing well - however I'm looking though the generated HTML and noticed things like... <script type="text/javascript"> //<![CDATA[ Droppables.add(...); //]]> </script> Sprinkled around the HTML, which of course matches up with places where I use: <%= drop_receiving_elem...

what are the practical differences between upvar and global commands in tcl

I'm fairly new to TCL, and am providing QA on some code developed by others (no really!). There are lots and lots of global variables in this particular program, and I sometimes see upvar used, often in conjunction with global. I understand that upvar emulates pass-by-reference, but what would be the practical difference be between the...

Immutable Collections Actionscript 3

I've been trying lately to implement some clean coding practices in AS3. One of these has been to not give away references to Arrays from a containing object. The point being that I control addition and removal from one Class and all other users of the Array receive read only version. At the moment that read only version is a ArrayItera...

Writing clean, performant code for the iPhone

I have been developing on the iPhone with Objective-C for a few months now and I have been applying the best-practices learnt and refined while developing applications with Java. These include: designing classes that have a single responsibility, application of design patterns where appropriate, and writing short methods that do one thin...

Do One Thing - How far to take this rule?

Hi, So there's the "Do One Thing" rule in the book "Clean Code". But how far do we really have to take this. For example the following statements: Settings.Default.BaudRate = baudRate; Settings.Default.COMPort = port; Settings.Default.DataBits = dataBits; Settings.Default.Handshake = handshake; Settings.Default.Parity = parity; Settin...

C# Equivilent for C++ Macros and using Auto<> Properties

I have some auto-instantiation code which I would like to apply to about 15 properties in a fairly big class. The code is similar to the following but the type is different for each instance: protected ComplexType _propertyName; public ComplexType PropertyName { get { if (_propertyName == null) _propertyName ...

Cleanup Symfonys View Templates

I'm evaluating the PHP webframework: Symfony for a project. Everything is quite well organized so far, except for the view templates which suffer from an unreadable/error prone mix of html and php (I was trying to adapt the views to use clean urls and that just turned out to be an exercise in "spot the delimiters"). Is there any methods...

Is ASP.Net 2.0/Ajax toolkit the right technology for implementing a SEO friendly website

Hi, As I understand it, just URL re-writing is not the only thing one needs to do for making a website SEO friendly. You also needs to maximize the use of div (instead of tables), reduce javascripts, flashes and have a clean HTML. I need to know how can this be achieved when one used a ASP.Net control. ASP.Net send loads of stuff to th...

How to tidy up too many if statements for readability

I have an example of some code that I see often in websites that I'd like to improve and would appreciate some help. Often I see 5-10 nested if-statements in a page_load method which aim to eliminate invalid user input, but this looks ugly and is hard to read and maintain. How would you recommend cleaning up the following code example? ...

Properly handling platform specifics (unix/windows) in C?

This question is intentionally very generic and I'm not much of a C programmer although I dabble here and there. The following code is intentionally vague and probably won't compile, but I hope you get the point... Handling platform specifics seems dynamically in a compiled language like C seems unnecessary and even scary: int main(in...

Refactoring a method containing conditional with extremely different code blocks that are the same ;)

So I have this stinky method, the two conditional blocks do almost the exact same thing, but with radically different parameters (at least in my eyes). I want to clean it Uncle Bob style, but can't for the life of me figure out a tidy way of doing it. So I come to you, my fabulous nerd friends, to see how you might distill this down to s...

Help me optimize the if else in JavaScript (jQuery)

Is it possible to somehow shorten this code: var i=GetStringFromServer('/url'); if(i){ $('#Div1').hide(); $('#Div2').show(); } else{ $('#Div1).show(); $('#Div2).hide(); } In C# I'd simply do this: bool smth=GetBool(); _el1.Visible=smth; _el2.Visible=!smth; Is it possible to mimick the logic in JavaScript? UPDATE: Thank...

Should clean code always obey these rules?

Regarding the book "Clean Code - A Handbook of Agile Software Craftsmanship": it says that functions should do one thing. Should else be avoided? If so, should return true and return false that use to come together really be avoided too? it says that functions should have one level of abstraction. Is high-level like ruby or like less i...

Smaller Methods

Following the guidelines given in "Clean Code" by Uncle Bob Martin, I'm trying to make my methods smaller. One recommendation he gives is that methods that contain a trys should invoke other methods that don't involve the exceptional cases. My problem is one of naming. Usually by the time my method only contains the try expression, th...

How to consolidate validity checking and exception throwing in Java?

I am implementing an interface which defines a method that can throw an exception if the parameters are not valid. What constitutes valid parameters depends on the implementing class. The interface also defines an isValid() method which can be used to check the parameters but returns a boolean rather than throwing an exception. I have fo...

Simple refactoring example

Given the following code: public static bool UpdateUser(string userId, string jobTitle) { return GetProvider().UpdateUser (userId, jobTitle); } It needs to be changed and may not return a bool, for example: UserProfile userProfile = new UserProfile(); userProfile....

Merge multiple javascript functions into one

I have these two javascript functions and wish to "merge" them into one. I will be adding a few more of these converting functions later on and would like to keep it simple and cleaner than just duplicating the functions. <!--Convert kg in pnd--> <script type="text/javascript"> function init(){ document.getElementById('kg').onmouseup=fu...

Games: Who is responsible for display?

Should entities know how to draw themselves? I've used this approach: it's simple and it works, but after learning MVC-patterns I feel uneasy about this. It's hard to change the art style when all the display logic is buried in the model. One could introduce a view class, which takes the level as an argument and draws it, but this would...

Best approach dealing with joins in Zend Framework?

Well it is more like a design question. There are two ways I know to use joins in the Zend Framework Deal with it using instance of Zend table( its Select obj) Deal with it using the instance of Zend Db (its Select obj) in the first approach it seems really strange to me - the some table have to be dealing with other tables too, whic...