Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you revise your code?

I find myself revisit areas of my project and refine them again. This usually happens when I begin something from scratch and as I understand it better my code and techniques become better, hence I go back over what I've done and make it stronger. Is this normal or I'm just inexperienced? How often do you revisit your code? do good programmers write once and don't need to change things?

like image 650
Roman Avatar asked Aug 11 '26 05:08

Roman


2 Answers

I agree with the others answers but it also depends on why you are writing code. If it's a specific project for a deadline you may not have much chance. If it's likely to be re-used by others make sure that the refactoring doesn't break anything they are using. If it's a long term project, especially a communal one ,then it's almost certain that it will need extensive refactoring.

I've been writing a communal library for about 13 years and been through 5 and a half revisions (the one I'm going through). In many cases this is because the technology has improved and things which I did for myself I can now do with standard libraries. And over the years I have learnt many better strategies.

In general good refactoring often means throwing out old code.

UPDATE Modern tools make it easy to do a lot of operations automatically (e.g. changing names, packages). The experts recommend that methods are short and so whenver you find your method stretching to two pages refactor it into shorter ones. But there are times where the tools wont help and you have to create code which is temporarily broken. In this case make sure that you (a) have unit tests running on the working version (b) commit it. It's easy to get into a complex refactoring operation and realise you've broken it badly enough that you need to backtrack. If you have users who depend on your code make sure you continue to provide the API they know. For example suppose you have a routine called

Date date = getLastUpdate();

and you decide (as I and many others have done) that java.util.Date is desperately broken. You decide to change to Joda DateTime it but during the process it will be tough. You probably need to set aside a time to complete it in a single pass. Do not change the API to

DateTime date = getLastUpdate();

Create a new interface such as

DateTime date = getLastDateTimeUpdate();

Then mark the original one as @Deprecated

You should not remove the earlier version until you decide to make a new release (changing APIs on a dynamic basis loses friends)

like image 141
peter.murray.rust Avatar answered Aug 13 '26 20:08

peter.murray.rust


I'd say you have nothing to worry about, going over you code is normal and helps improve your understanding. The only time you want to worry is if you are going over the same bit of code and changing the implementation again and again.

Even if you are inexperienced by getting your hands dirty, writing and testing code you'll get better.

like image 37
Robert Avatar answered Aug 13 '26 20:08

Robert



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!