Skip to main content
Tweeted twitter.com/StackCodeReview/status/728942723001143296
deleted 338 characters in body; edited title
Source Link
Jamal
  • 34.9k
  • 13
  • 133
  • 237

Can you critic my Download Function?function

I wrote a Download Functiondownload function and I wanted to know isif my code clean, readable and maintainable? Or could, or if I could make things easier ?. Please tell me if this Functionfunction could be useused in the real world?.

     public static void Download(Uri _downloadUri, string _path, string _passedSong, Song _currentObj) {

            if (_downloadUri != null) {

                if (!string.IsNullOrEmpty(_path) &&_downloadUri !string.IsNullOrEmpty(_passedSong)= null) {

                 if (!string.IsNullOrEmpty(_path) && try!string.IsNullOrEmpty(_passedSong)) {

                        var downloadedData = newtry byte[0];{

                        var webReqdownloadedData = WebRequest.Create(_downloadUri);new byte[0];

                        using (var webResponsewebReq = webReqWebRequest.GetResponseCreate()_downloadUri) {;

                            using (var dataStreamwebResponse = webResponsewebReq.GetResponseStreamGetResponse()) {

                                //Download chuncks
                               using byte[](var dataBufferdataStream = newwebResponse.GetResponseStream()) byte[1024];{

                                //GetDownload sizechuncks
                                intbyte[] dataLengthdataBuffer = (int)new webResponse.ContentLength;byte[1024];

                            //Get size
   ByteArgs byteArgs = new ByteArgs                     int dataLength = (int); webResponse.ContentLength;

                               ByteArgs byteArgs.downloaded = 0;
                                byteArgs.total =new dataLength;ByteArgs();

                                if (_bytesDownloadedbyteArgs.downloaded != null) {0;
                                    _bytesDownloaded(byteArgs, _currentObj);
                              .total = }dataLength;

                            if (_bytesDownloaded != null) //Download{
                                using _bytesDownloaded(varbyteArgs, memoryStream_currentObj);
 = new MemoryStream()) {                        }

                            //Download
        while                    using (truevar memoryStream = new MemoryStream()) {

                                        int bytesFromStream =while dataStream.Read(dataBuffer, 0, dataBuffer.Lengthtrue); {

                                      int bytesFromStream if= dataStream.Read(bytesFromStream ==dataBuffer, 0), {dataBuffer.Length);

                                            byteArgs.downloaded = dataLength;
                                          if (bytesFromStream byteArgs.total== =0) dataLength;{

                                            if (_bytesDownloadedbyteArgs.downloaded != null) {dataLength;
                                                _bytesDownloaded(byteArgs, _currentObj);
                                          .total = }dataLength;

                                         if (_bytesDownloaded != //Downloadnull) complete{
                                            break;_bytesDownloaded(byteArgs, _currentObj);
                                        }
                                        else {

                                        //Download complete
   //Write the downloaded data                                  break;
                                    }
        memoryStream.Write(dataBuffer, 0, bytesFromStream);                          else {

                                        //Write the downloaded data
   byteArgs                                     memoryStream.downloadedWrite(dataBuffer, =0, bytesFromStream;bytesFromStream);

                                        byteArgs.downloaded = bytesFromStream;
                                        byteArgs.total = dataLength;

// for download progressbar
                                            if (_bytesDownloaded != null) {
                                                _bytesDownloaded(byteArgs, _currentObj);
                                            }
                                        }
                                    }
                                }

                                    //Convert to byte array
                                    downloadedData = memoryStream.ToArray();

                                    //Write bytes to the specified file
                                    if (Directory.Exists(Settings.Default.downloadDirectory)) {
                                        using (var newFile = new FileStream(_path, FileMode.Create)) {
                                            newFile.Write(downloadedData, 0, downloadedData.Length);
                                        }
                                    }
                                }
                            }
                        }
                    }
                    catch(Exception ex) {
                        ExceptionLog.LogException(ex, "Download()", "in GeneralSettings.cs");
                    }
                }
                catch(Exception ex) {
                    ExceptionLog.LogException(ex, "Download()", "in GeneralSettings.cs");
                }
            }
        }
    }

Can you critic my Download Function?

I wrote a Download Function and I wanted to know is my code clean, readable and maintainable? Or could I make things easier ? Please tell me if this Function could be use in the real world?

     public static void Download(Uri _downloadUri, string _path, string _passedSong, Song _currentObj) {

            if (_downloadUri != null) {

                if (!string.IsNullOrEmpty(_path) && !string.IsNullOrEmpty(_passedSong)) {

                    try {

                        var downloadedData = new byte[0];

                        var webReq = WebRequest.Create(_downloadUri);

                        using (var webResponse = webReq.GetResponse()) {

                            using (var dataStream = webResponse.GetResponseStream()) {

                                //Download chuncks
                                byte[] dataBuffer = new byte[1024];

                                //Get size
                                int dataLength = (int) webResponse.ContentLength;

                                ByteArgs byteArgs = new ByteArgs();

                                byteArgs.downloaded = 0;
                                byteArgs.total = dataLength;

                                if (_bytesDownloaded != null) {
                                    _bytesDownloaded(byteArgs, _currentObj);
                                }

                                //Download
                                using (var memoryStream = new MemoryStream()) {

                                    while (true) {

                                        int bytesFromStream = dataStream.Read(dataBuffer, 0, dataBuffer.Length);

                                        if (bytesFromStream == 0) {

                                            byteArgs.downloaded = dataLength;
                                            byteArgs.total = dataLength;

                                            if (_bytesDownloaded != null) {
                                                _bytesDownloaded(byteArgs, _currentObj);
                                            }

                                            //Download complete
                                            break;
                                        }
                                        else {

                                            //Write the downloaded data
                                            memoryStream.Write(dataBuffer, 0, bytesFromStream);

                                            byteArgs.downloaded = bytesFromStream;
                                            byteArgs.total = dataLength;

// for download progressbar
                                            if (_bytesDownloaded != null) {
                                                _bytesDownloaded(byteArgs, _currentObj);
                                            }
                                        }
                                    }

                                    //Convert to byte array
                                    downloadedData = memoryStream.ToArray();

                                    //Write bytes to the specified file
                                    if (Directory.Exists(Settings.Default.downloadDirectory)) {
                                        using (var newFile = new FileStream(_path, FileMode.Create)) {
                                            newFile.Write(downloadedData, 0, downloadedData.Length);
                                        }
                                    }
                                }
                            }
                        }
                    }
                    catch(Exception ex) {
                        ExceptionLog.LogException(ex, "Download()", "in GeneralSettings.cs");
                    }
                }
            }
        }

Download function

I wrote a download function and I wanted to know if my code clean, readable and maintainable, or if I could make things easier. Please tell me if this function could be used in the real world.

public static void Download(Uri _downloadUri, string _path, string _passedSong, Song _currentObj) {

        if (_downloadUri != null) {

            if (!string.IsNullOrEmpty(_path) && !string.IsNullOrEmpty(_passedSong)) {

                try {

                    var downloadedData = new byte[0];

                    var webReq = WebRequest.Create(_downloadUri);

                    using (var webResponse = webReq.GetResponse()) {

                        using (var dataStream = webResponse.GetResponseStream()) {

                            //Download chuncks
                            byte[] dataBuffer = new byte[1024];

                            //Get size
                            int dataLength = (int) webResponse.ContentLength;

                            ByteArgs byteArgs = new ByteArgs();

                            byteArgs.downloaded = 0;
                            byteArgs.total = dataLength;

                            if (_bytesDownloaded != null) {
                                _bytesDownloaded(byteArgs, _currentObj);
                            }

                            //Download
                            using (var memoryStream = new MemoryStream()) {

                                while (true) {

                                    int bytesFromStream = dataStream.Read(dataBuffer, 0, dataBuffer.Length);

                                    if (bytesFromStream == 0) {

                                        byteArgs.downloaded = dataLength;
                                        byteArgs.total = dataLength;

                                        if (_bytesDownloaded != null) {
                                            _bytesDownloaded(byteArgs, _currentObj);
                                        }

                                        //Download complete
                                        break;
                                    }
                                    else {

                                        //Write the downloaded data
                                        memoryStream.Write(dataBuffer, 0, bytesFromStream);

                                        byteArgs.downloaded = bytesFromStream;
                                        byteArgs.total = dataLength;

// for download progressbar
                                        if (_bytesDownloaded != null) {
                                            _bytesDownloaded(byteArgs, _currentObj);
                                        }
                                    }
                                }

                                //Convert to byte array
                                downloadedData = memoryStream.ToArray();

                                //Write bytes to the specified file
                                if (Directory.Exists(Settings.Default.downloadDirectory)) {
                                    using (var newFile = new FileStream(_path, FileMode.Create)) {
                                        newFile.Write(downloadedData, 0, downloadedData.Length);
                                    }
                                }
                            }
                        }
                    }
                }
                catch(Exception ex) {
                    ExceptionLog.LogException(ex, "Download()", "in GeneralSettings.cs");
                }
            }
        }
    }
quickly modify the question.
Source Link
Marc-Andre
  • 6.7k
  • 5
  • 38
  • 65

Hej ,

iI wrote a Download Function and iI wanted to know is my code Clean, Readableclean, readable and maintainable? Or could iI make things easier ? Please tell me if this Function could be use in the Real World somewherereal world?

Sorry English is not my Native language.

Hej ,

i wrote a Download Function and i wanted to know is my code Clean, Readable, maintainable? Or could i make things easier ? Please tell me if this Function could be in the Real World somewhere?

Sorry English is not my Native language.

I wrote a Download Function and I wanted to know is my code clean, readable and maintainable? Or could I make things easier ? Please tell me if this Function could be use in the real world?

Source Link
iNCEPTiON_
  • 317
  • 2
  • 9

Can you critic my Download Function?

Hej ,

i wrote a Download Function and i wanted to know is my code Clean, Readable, maintainable? Or could i make things easier ? Please tell me if this Function could be in the Real World somewhere?

     public static void Download(Uri _downloadUri, string _path, string _passedSong, Song _currentObj) {

            if (_downloadUri != null) {

                if (!string.IsNullOrEmpty(_path) && !string.IsNullOrEmpty(_passedSong)) {

                    try {

                        var downloadedData = new byte[0];

                        var webReq = WebRequest.Create(_downloadUri);

                        using (var webResponse = webReq.GetResponse()) {

                            using (var dataStream = webResponse.GetResponseStream()) {

                                //Download chuncks
                                byte[] dataBuffer = new byte[1024];

                                //Get size
                                int dataLength = (int) webResponse.ContentLength;

                                ByteArgs byteArgs = new ByteArgs();

                                byteArgs.downloaded = 0;
                                byteArgs.total = dataLength;

                                if (_bytesDownloaded != null) {
                                    _bytesDownloaded(byteArgs, _currentObj);
                                }

                                //Download
                                using (var memoryStream = new MemoryStream()) {

                                    while (true) {

                                        int bytesFromStream = dataStream.Read(dataBuffer, 0, dataBuffer.Length);

                                        if (bytesFromStream == 0) {

                                            byteArgs.downloaded = dataLength;
                                            byteArgs.total = dataLength;

                                            if (_bytesDownloaded != null) {
                                                _bytesDownloaded(byteArgs, _currentObj);
                                            }

                                            //Download complete
                                            break;
                                        }
                                        else {

                                            //Write the downloaded data
                                            memoryStream.Write(dataBuffer, 0, bytesFromStream);

                                            byteArgs.downloaded = bytesFromStream;
                                            byteArgs.total = dataLength;

// for download progressbar
                                            if (_bytesDownloaded != null) {
                                                _bytesDownloaded(byteArgs, _currentObj);
                                            }
                                        }
                                    }

                                    //Convert to byte array
                                    downloadedData = memoryStream.ToArray();

                                    //Write bytes to the specified file
                                    if (Directory.Exists(Settings.Default.downloadDirectory)) {
                                        using (var newFile = new FileStream(_path, FileMode.Create)) {
                                            newFile.Write(downloadedData, 0, downloadedData.Length);
                                        }
                                    }
                                }
                            }
                        }
                    }
                    catch(Exception ex) {
                        ExceptionLog.LogException(ex, "Download()", "in GeneralSettings.cs");
                    }
                }
            }
        }

Sorry English is not my Native language.