Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / New
Stats: 3,150,844 members, 7,810,255 topics. Date: Saturday, 27 April 2024 at 02:50 AM

Code Refactoring - Programming - Nairaland

Nairaland Forum / Science/Technology / Programming / Code Refactoring (823 Views)

(2) (3) (4)

(1) (Reply) (Go Down)

Code Refactoring by harryobas: 7:35pm On Jul 08, 2012
Code refactoring is a "disciplined technique for restructuring an existing body of code, altering its internal structure without changing its external behavior",undertaken in order to improve some of the nonfunctional attributes of the software. Advantages include improved code readability and reduced complexity to improve the maintainability of the source code. According Joshua Kerievsky, by continuously improving the design of code, we make it easier and easier to work with. This is in sharp contrast to what typically happens: little refactoring and a great deal of attention paid to expediently adding new features. If you get into the hygienic habit of refactoring continuously, you'll find that it is easier to extend and maintain code.
Re: Code Refactoring by ektbear: 6:37am On Jul 09, 2012
Refactoring is pretty key.

You have some long-azz function that does many things: break it into pieces.

You find yourself copying and pasting your code and thus have two similar functions: refactor.
Re: Code Refactoring by Nobody: 7:17pm On Jul 10, 2012
i'll share a little of my experience,

i have this logic working

get settings value from DB, check the display combo box if it's there
if it's there, set default selection to it, to do this in the language/toolkit am using, it requires i call a findText() method which will return the index as int, then i'll setCurrentIndex to the index.

if it's not there, add the settings to the combo box and set current index to it, it will be correct to search for the index so i can setCurrentIndex to it

This went well, Oh! i have 3 more of such to do.
at first i copied and repasted the whole code and started changing the widget names when it struck me, why not have a function hold that logic and i'll pass the names in as parameters. (since python actually pass object references around instead of the actual object)

so i ended up with this



def putItem(self,obj,txt):
loc=obj.findText(txt)
if loc==-1:
obj.addItem(txt)
obj.setCurrentIndex(obj.findText(txt))
else:
obj.setCurrentIndex(loc)


#so all i did was

putItem(self.ui_name.the_widget_name1, settings_text1)
putItem(self.ui_name.the_widget_name2, settings_text2)
putItem(self.ui_name.the_widget_name3, settings_text3)
putItem(self.ui_name.the_widget_name4, settings_text4)






Now compare the above to the disaster i would have ended up with below.



loc=self.ui_name.the_widget_name1.findText(settings_text1)
if loc==-1:
self.ui_name.the_widget_name1.addItem(settings_text1)
self.ui_name.the_widget_name1.setCurrentIndex(self.ui_name.the_widget_name1.findText(settings_text1))
else:
self.ui_name.the_widget_name1.setCurrentIndex(loc)


loc=self.ui_name.the_widget_name1.findText(settings_text1)
if loc==-1:
self.ui_name.the_widget_name1.addItem(settings_text1)
self.ui_name.the_widget_name1.setCurrentIndex(self.ui_name.the_widget_name1.findText(settings_text1))
else:
self.ui_name.the_widget_name1.setCurrentIndex(loc)



loc=self.ui_name.the_widget_name1.findText(settings_text1)
if loc==-1:
self.ui_name.the_widget_name1.addItem(settings_text1)
self.ui_name.the_widget_name1.setCurrentIndex(self.ui_name.the_widget_name1.findText(settings_text1))
else:
self.ui_name.the_widget_name1.setCurrentIndex(loc)



loc=self.ui_name.the_widget_name1.findText(settings_text1)
if loc==-1:
self.ui_name.the_widget_name1.addItem(settings_text1)
self.ui_name.the_widget_name1.setCurrentIndex(self.ui_name.the_widget_name1.findText(settings_text1))
else:
self.ui_name.the_widget_name1.setCurrentIndex(loc)



It becomes even more work when there is a need to modify the logic later on
trust me, there is joy in knowing you did it right and well
Re: Code Refactoring by ektbear: 12:42am On Jul 11, 2012
Or a simpler instance of where refactoring can be used:

1.
class A
def really_long_function_that_does_multiple_things
end
end

2.

class A
end

class B
def long_function_that_modifies B's instance of A that could be made a separate method of the class A
end
end


Things of this nature are what I've seen pretty frequently in my personal experiences.

(1) (Reply)

Normalization To "(3NF)" / Computer Automation Thread / A Good Python Programmer Needed

(Go Up)

Sections: politics (1) business autos (1) jobs (1) career education (1) romance computers phones travel sports fashion health
religion celebs tv-movies music-radio literature webmasters programming techmarket

Links: (1) (2) (3) (4) (5) (6) (7) (8) (9) (10)

Nairaland - Copyright © 2005 - 2024 Oluwaseun Osewa. All rights reserved. See How To Advertise. 28
Disclaimer: Every Nairaland member is solely responsible for anything that he/she posts or uploads on Nairaland.